3333import java .io .File ;
3434import java .io .FileWriter ;
3535import java .io .IOException ;
36+ import java .nio .file .Paths ;
3637
38+ import org .eclipse .core .resources .IProject ;
39+ import org .eclipse .core .resources .IResource ;
3740import org .eclipse .core .runtime .CoreException ;
41+ import org .eclipse .core .runtime .IPath ;
42+ import org .eclipse .ui .IEditorInput ;
43+ import org .eclipse .ui .IEditorPart ;
44+ import org .eclipse .ui .IWorkbenchPage ;
45+ import org .eclipse .ui .IWorkbenchWindow ;
46+ import org .eclipse .ui .PlatformUI ;
3847
3948import com .fasterxml .jackson .databind .ObjectMapper ;
4049
4150public class JsonUtils {
4251
4352 public void serializeJson (JsonProperty jsonProperty , String pipelineString ) throws CoreException , IOException {
4453 try {
45- File file = new File (jsonProperty .getName () + ".json" );
54+ String projectPath = getProjectPath ();
55+ File file ;
56+ if (projectPath != null ) {
57+ String path = Paths .get (projectPath , jsonProperty .getName (), jsonProperty .getVersion ()).toString ();
58+ new File (path ).mkdirs ();
59+ file = new File (Paths .get (path , "pipeline.json" ).toString ());
60+ } else {
61+ file = new File (jsonProperty .getName () + ".json" );
62+ }
4663 jsonProperty .setTemplate (pipelineString );
4764 BufferedWriter writer = new BufferedWriter (new FileWriter (file ));
4865 ObjectMapper obj = new ObjectMapper ();
@@ -54,4 +71,27 @@ public void serializeJson(JsonProperty jsonProperty, String pipelineString) thro
5471 }
5572 }
5673
74+ public String getProjectPath () {
75+ IWorkbenchWindow window = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ();
76+ IWorkbenchPage activePage = window .getActivePage ();
77+
78+ IEditorPart activeEditor = activePage .getActiveEditor ();
79+
80+ if (activeEditor != null ) {
81+ IEditorInput input = activeEditor .getEditorInput ();
82+
83+ IProject project = input .getAdapter (IProject .class );
84+ if (project == null ) {
85+ IResource resource = input .getAdapter (IResource .class );
86+ if (resource != null ) {
87+ project = resource .getProject ();
88+ IPath path = project .getLocation ();
89+ return path .toString ();
90+ }
91+ }
92+ }
93+
94+ return null ;
95+ }
96+
5797}
0 commit comments