1313import java .io .File ;
1414import java .io .IOException ;
1515import java .io .InputStreamReader ;
16- import java .nio .file .Files ;
17- import java .nio .file .Paths ;
1816import java .util .ArrayList ;
1917import java .util .List ;
2018
2422import org .eclipse .core .resources .IProject ;
2523import org .eclipse .core .resources .IResource ;
2624import org .eclipse .core .runtime .IPath ;
25+ import org .eclipse .core .runtime .IProgressMonitor ;
26+ import org .eclipse .core .runtime .IStatus ;
27+ import org .eclipse .core .runtime .Status ;
28+ import org .eclipse .core .runtime .jobs .Job ;
2729import org .eclipse .jdt .core .ICompilationUnit ;
2830import org .eclipse .jdt .core .IJavaProject ;
2931import org .eclipse .jface .dialogs .MessageDialog ;
3032import org .eclipse .jface .viewers .ISelection ;
3133import org .eclipse .jface .viewers .IStructuredSelection ;
3234import org .eclipse .swt .widgets .DirectoryDialog ;
35+ import org .eclipse .swt .widgets .Display ;
3336import org .eclipse .swt .widgets .Shell ;
37+ import org .eclipse .ui .IWorkbenchPage ;
38+ import org .eclipse .ui .IWorkbenchWindow ;
39+ import org .eclipse .ui .PartInitException ;
3440import org .eclipse .ui .PlatformUI ;
3541import org .eclipse .ui .console .ConsolePlugin ;
3642import org .eclipse .ui .console .IConsole ;
43+ import org .eclipse .ui .console .IConsoleConstants ;
44+ import org .eclipse .ui .console .IConsoleView ;
3745import org .eclipse .ui .console .MessageConsole ;
3846import org .eclipse .ui .console .MessageConsoleStream ;
3947import org .eclipse .ui .handlers .HandlerUtil ;
@@ -42,74 +50,96 @@ public class MigrateHandler extends AbstractHandler {
4250
4351 public static final String PAYARA_TRANSFORMER = "fish.payara.transformer" ;
4452 public static final String PAYARA_TRANSFORMER_MAVEN = "fish.payara.transformer.maven" ;
45- public static final String PAYARA_TRANSFORMER_VERSION = "0.2.10" ;
53+ public static final String PAYARA_TRANSFORMER_VERSION = "0.2.14" ;
54+
55+ private IPath resourcePath = null ;
56+ private IPath projectPath = null ;
57+ private String name = "" ;
4658
4759 @ Override
4860 public Object execute (ExecutionEvent event ) throws ExecutionException {
49- Shell shell = HandlerUtil .getActiveWorkbenchWindow (event ).getShell ();
61+ IWorkbenchWindow activeWorkbenchWindow = HandlerUtil .getActiveWorkbenchWindow (event );
62+ Shell shell = activeWorkbenchWindow .getShell ();
5063 IStructuredSelection selection = getSelection (event );
5164 if (selection != null && !selection .isEmpty ()) {
5265 Object firstElement = selection .getFirstElement ();
53- IPath resourcePath = null ;
54- IPath projectPath = null ;
55- String name = "" ;
56- boolean isFile = false ;
57- if (firstElement instanceof IResource ) {
58- IResource resource = (IResource ) firstElement ;
59- resourcePath = resource .getLocation ();
60- projectPath = resourcePath ;
61- name = resource .getName ();
62- } else if (firstElement instanceof IJavaProject ) {
63- IJavaProject javaProject = (IJavaProject ) firstElement ;
64- IProject project = javaProject .getProject ();
65- resourcePath = project .getLocation ();
66- projectPath = resourcePath ;
67- name = project .getName ();
68- } else if (firstElement instanceof ICompilationUnit ) {
69- ICompilationUnit file = (ICompilationUnit ) firstElement ;
70- resourcePath = file .getResource ().getLocation ();
71- projectPath = file .getJavaProject ().getProject ().getLocation ();
72- name = file .getElementName ();
73- isFile = true ;
74- }
66+
67+ final boolean isFile = checkIfFile (firstElement );
7568 if (resourcePath != null && projectPath != null && !"" .equals (name )) {
7669 String srcPath = resourcePath .toOSString ();
7770 String srcProjectPath = projectPath .toOSString ();
7871 String destinationPath = chooseDestinationPath (srcProjectPath , name , isFile );
7972 if ("" .equals (destinationPath )) return null ;
80- int exitCode = runMvnCommand (srcProjectPath , srcPath , destinationPath );
81- if (exitCode == 0 ) {
82- MessageDialog .openInformation (shell , "Success" , "Project " + destinationPath + " created successfully." );
83- } else {
84- MessageDialog .openError (shell , "Error" , "Maven command failed with exit code " + exitCode + "." );
85- }
73+
74+ Job job = new Job ("Running Maven Command" ) {
75+ @ Override
76+ protected IStatus run (IProgressMonitor monitor ) {
77+ Display .getDefault ().asyncExec (() -> {
78+ try {
79+ IWorkbenchPage page = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getActivePage ();
80+ IConsoleView consoleView = (IConsoleView ) page .showView (IConsoleConstants .ID_CONSOLE_VIEW );
81+ MessageConsole console = new MessageConsole ("Maven Command Console" , null );
82+ ConsolePlugin .getDefault ().getConsoleManager ().addConsoles (new IConsole [] { console });
83+ consoleView .display (console );
84+
85+ int exitCode = runMvnCommand (console , page , srcProjectPath , srcPath , destinationPath , shell );
86+ if (exitCode == 0 ) {
87+ Display .getDefault ().asyncExec (() -> {
88+ MessageDialog .openInformation (shell , "Success" , (isFile ? "File " : "Project " ) + destinationPath + " created successfully." );
89+ });
90+ } else {
91+ Display .getDefault ().asyncExec (() -> {
92+ MessageDialog .openError (shell , "Error" , "Maven command failed with exit code " + exitCode + "." );
93+ });
94+ }
95+ } catch (PartInitException e ) {
96+ e .printStackTrace ();
97+ }
98+ });
99+ return Status .OK_STATUS ;
100+ }
101+ };
102+ job .schedule ();
86103 }
87104 }
88105 return null ;
89106 }
90107
91- private String chooseDestinationPath (String srcPath , String name , boolean isFile ) {
108+ private boolean checkIfFile (Object firstElement ) {
109+ if (firstElement instanceof IResource ) {
110+ IResource resource = (IResource ) firstElement ;
111+ resourcePath = resource .getLocation ();
112+ projectPath = resourcePath ;
113+ name = resource .getName ();
114+ } else if (firstElement instanceof IJavaProject ) {
115+ IJavaProject javaProject = (IJavaProject ) firstElement ;
116+ IProject project = javaProject .getProject ();
117+ resourcePath = project .getLocation ();
118+ projectPath = resourcePath ;
119+ name = project .getName ();
120+ } else if (firstElement instanceof ICompilationUnit ) {
121+ ICompilationUnit file = (ICompilationUnit ) firstElement ;
122+ resourcePath = file .getResource ().getLocation ();
123+ projectPath = file .getJavaProject ().getProject ().getLocation ();
124+ name = file .getElementName ();
125+ return true ;
126+ }
127+ return false ;
128+ }
129+
130+ private String chooseDestinationPath (String srcPath , String name , boolean isFile ) {
92131 Shell shell = new Shell ();
93132 DirectoryDialog dialog = new DirectoryDialog (shell );
94133 dialog .setText ("Choose a " + (isFile ? "New File" : "" ) + " destination Folder" );
95134 dialog .setMessage ("Please select a Directory:" );
96135 dialog .setFilterPath (srcPath );
97136 String selectedDirectory = dialog .open ();
98137 if (selectedDirectory != null ) {
99- if (isFile ) {
100- String targetDir = selectedDirectory + "/jakartaee10/" ;
101- try {
102- Files .createDirectories (Paths .get (targetDir ));
103- return targetDir + name ;
104- } catch (IOException e ) {
105- throw new RuntimeException (e );
106- }
107- }
108- return selectedDirectory + "/" + name + "-JakartaEE10" ;
138+ return selectedDirectory + "/" + name ;
109139 }
110140 return "" ;
111141 }
112-
142+
113143 private IStructuredSelection getSelection (ExecutionEvent event ) {
114144 ISelection selection = HandlerUtil .getCurrentSelection (event );
115145 if (selection instanceof IStructuredSelection ) {
@@ -118,36 +148,31 @@ private IStructuredSelection getSelection(ExecutionEvent event) {
118148 return null ;
119149 }
120150
121- private int runMvnCommand (String srcProjectPath , String srcPath , String targetPath ) {
122- Shell shell = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getShell ();
123-
124- MessageConsole console = new MessageConsole ("Maven Command Console" , null );
125- ConsolePlugin .getDefault ().getConsoleManager ().addConsoles (new IConsole []{console });
126-
127- MessageConsoleStream consoleStream = console .newMessageStream ();
128-
151+ private int runMvnCommand (MessageConsole console , IWorkbenchPage page , String srcProjectPath , String srcPath , String targetPath , Shell shell ) {
129152 try {
130- List <String > command = new ArrayList <>();
131- command .add (getMvnCommand ());
132- command .add ("package" );
133- command .add (getTransformCommand ());
134- command .add ("-DselectedSource=" + srcPath );
135- command .add ("-DselectedTarget=" + targetPath );
136- ProcessBuilder builder = new ProcessBuilder (command );
137- builder .environment ().put ("PATH" , System .getenv ("PATH" ));
138- builder .directory (new File (srcProjectPath ));
139- builder .redirectErrorStream (true );
140- Process process = builder .start ();
141-
142- BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
143- String line = null ;
153+ MessageConsoleStream consoleStream = console .newMessageStream ();
154+
155+ List <String > command = new ArrayList <>();
156+ command .add (getMvnCommand ());
157+ command .add (getTransformCommand ());
158+ command .add ("-DselectedSource=" + srcPath );
159+ command .add ("-DselectedTarget=" + targetPath );
160+ ProcessBuilder builder = new ProcessBuilder (command );
161+ builder .environment ().put ("PATH" , System .getenv ("PATH" ));
162+ builder .directory (new File (srcProjectPath ));
163+ builder .redirectErrorStream (true );
164+ Process process = builder .start ();
165+
166+ BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
167+ String line = null ;
144168 while ((line = reader .readLine ()) != null ) {
145169 consoleStream .println (line );
146170 }
147-
171+
148172 return process .waitFor ();
149173 } catch (IOException | InterruptedException e ) {
150- MessageDialog .openError (shell , "Error" , "An error occurred while running the Maven command: " + e .getMessage ());
174+ MessageDialog .openError (shell , "Error" ,
175+ "An error occurred while running the Maven command: " + e .getMessage ());
151176 return -1 ;
152177 }
153178 }
0 commit comments