1818import com .intellij .ide .util .newProjectWizard .AddModuleWizard ;
1919import com .intellij .openapi .application .ReadAction ;
2020import com .intellij .openapi .externalSystem .model .execution .ExternalSystemTaskExecutionSettings ;
21+ import com .intellij .openapi .externalSystem .service .execution .ProgressExecutionMode ;
2122import com .intellij .openapi .externalSystem .service .project .ProjectDataManager ;
2223import com .intellij .openapi .externalSystem .service .project .manage .ProjectDataImportListener ;
24+ import com .intellij .openapi .externalSystem .task .TaskCallback ;
2325import com .intellij .openapi .externalSystem .util .ExternalSystemApiUtil ;
2426import com .intellij .openapi .externalSystem .util .ExternalSystemUtil ;
2527import com .intellij .openapi .module .ModifiableModuleModel ;
@@ -116,14 +118,34 @@ private void processDownload(Module module, Set<String> deploymentIds, List<Virt
116118 Path outputPath = Files .createTempFile (null , ".txt" );
117119 Path customBuildFile = generateCustomGradleBuild (getModuleDirPath (module ), outputPath , deploymentIds );
118120 Path customSettingsFile = generateCustomGradleSettings (getModuleDirPath (module ), customBuildFile );
121+ TaskCallback callback = new TaskCallback () {
122+ @ Override
123+ public void onSuccess () {
124+ cleanCustomFiles ();
125+ }
126+
127+ @ Override
128+ public void onFailure () {
129+ LOGGER .error ("Failed to run custom gradle build" );
130+ cleanCustomFiles ();
131+ }
132+
133+ private void cleanCustomFiles () {
134+ try {
135+ Files .delete (customBuildFile );
136+ Files .delete (customSettingsFile );
137+ } catch (IOException e ) {
138+ LOGGER .warn (e .getLocalizedMessage (), e );
139+ }
140+ }
141+
142+ };
119143 try {
120- collectDependencies (module , customBuildFile , customSettingsFile , outputPath , result );
144+ collectDependencies (module , customSettingsFile , outputPath , result , callback );
121145 } catch (IOException e ) {
122146 LOGGER .warn (e .getLocalizedMessage (), e );
123147 } finally {
124148 Files .delete (outputPath );
125- Files .delete (customBuildFile );
126- Files .delete (customSettingsFile );
127149 }
128150 }
129151
@@ -149,37 +171,32 @@ private String getModuleDirPath(Module module) {
149171 /**
150172 * Collect all deployment JARs and dependencies through a Gradle specific task.
151173 *
152- * @param module the module to analyze
153- * @param customBuildFile the custom Gradle build file with the specific task
154- * @param outputPath the file where the result of the specific task is stored
155- * @param result the list where to place results to
174+ * @param module the module to analyze
175+ * @param outputPath the file where the result of the specific task is stored
176+ * @param result the list where to place results to
177+ * @param callback the callback to call after running the task
156178 * @throws IOException if an error occurs running Gradle
157179 */
158- private void collectDependencies (Module module , Path customBuildFile , Path customSettingsFile , Path outputPath , List <VirtualFile >[] result ) throws IOException {
180+ private void collectDependencies (Module module , Path customSettingsFile , Path outputPath , List <VirtualFile >[] result , TaskCallback callback ) throws IOException {
159181 try {
160182 ExternalSystemTaskExecutionSettings executionSettings = new ExternalSystemTaskExecutionSettings ();
161183 executionSettings .setExternalSystemIdString (GradleConstants .SYSTEM_ID .toString ());
162184 executionSettings .setTaskNames (Collections .singletonList ("listQuarkusDependencies" ));
163185 executionSettings .setScriptParameters (String .format ("-c %s -q --console plain" , customSettingsFile .toString ()));
164186 executionSettings .setExternalProjectPath (getModuleDirPath (module ));
165- ExternalSystemUtil .runTask (
166- executionSettings ,
167- DefaultRunExecutor .EXECUTOR_ID ,
168- module .getProject (),
169- GradleConstants .SYSTEM_ID
170- );
187+ ExternalSystemUtil .runTask (executionSettings ,DefaultRunExecutor .EXECUTOR_ID ,
188+ module .getProject (),
189+ GradleConstants .SYSTEM_ID , callback , ProgressExecutionMode .IN_BACKGROUND_ASYNC , false );
171190 try (BufferedReader reader = Files .newBufferedReader (outputPath )) {
172191 String id ;
173192
174193 while ((id = reader .readLine ()) != null ) {
175194 String file = reader .readLine ();
176195 String [] ids = id .split (":" );
177- if (!isDependency (ModuleRootManager .getInstance (module ), ids [0 ], ids [1 ])) {
178- if (file != null ) {
179- VirtualFile jarFile = getJarFile (file );
180- if (jarFile != null ) {
181- result [file .endsWith ("sources.jar" ) ? SOURCES : BINARY ].add (jarFile );
182- }
196+ if (!isDependency (ModuleRootManager .getInstance (module ), ids [0 ], ids [1 ]) && file != null ) {
197+ VirtualFile jarFile = getJarFile (file );
198+ if (jarFile != null ) {
199+ result [file .endsWith ("sources.jar" ) ? SOURCES : BINARY ].add (jarFile );
183200 }
184201 }
185202 }
@@ -233,6 +250,7 @@ private Path generateCustomGradleSettings(String basePath, Path customBuildFile)
233250 try (Reader reader = Files .newBufferedReader (path , StandardCharsets .UTF_8 )) {
234251 content = IOUtils .toString (reader );
235252 } catch (IOException e ) {
253+ LOGGER .warn (e .getLocalizedMessage (), e );
236254 }
237255 content += System .lineSeparator () + "rootProject.buildFileName =\" " + customBuildFile .getFileName ().toFile ().getName () + "\" " ;
238256 Path customPath = Files .createTempFile (base , null , getScriptExtension ());
@@ -252,7 +270,7 @@ private Path generateCustomGradleSettings(String basePath, Path customBuildFile)
252270 * @return the content of the custom build file
253271 */
254272 private String appendQuarkusResolution (String content , Path outputPath , Set <String > deploymentIds ) {
255- StringBuffer buffer = new StringBuffer (content );
273+ StringBuilder buffer = new StringBuilder (content );
256274 buffer .append (System .lineSeparator ());
257275 buffer .append (createQuarkusConfiguration ()).append (System .lineSeparator ());
258276 buffer .append ("dependencies {" ).append (System .lineSeparator ());
@@ -281,29 +299,27 @@ private void processLibrary(Library library, ModuleRootManager manager, Set<Stri
281299 * @return the File object
282300 */
283301 public static File getDeploymentFile (String artifactId ) {
284- String path = toPath (artifactId , File . separatorChar );
302+ String path = toPath (artifactId );
285303 if (path != null ) {
286304 return new File (M2_REPO , path );
287305 }
288306 return null ;
289307 }
290308
291- private static String toPath (String artifactId , char separator ) {
309+ private static String toPath (String artifactId ) {
292310 String [] comps = artifactId .split (":" );
293311 if (comps .length == 3 ) {
294- StringBuffer buffer = new StringBuffer (comps [0 ].replace ('.' , separator ));
295- buffer .append (separator ).append (comps [1 ]).append (separator ).append (comps [2 ]).append (separator ).append (comps [1 ]).append ('-' ).append (comps [2 ]).append (".jar" );
296- return buffer .toString ();
312+ return comps [0 ].replace ('.' , File .separatorChar ) + File .separatorChar + comps [1 ] + File .separatorChar + comps [2 ] + File .separatorChar + comps [1 ] + '-' + comps [2 ] + ".jar" ;
297313 }
298314 return null ;
299315 }
300316
301317 private boolean isDependency (ModuleRootManager manager , String deploymentIdStr ) {
302- return Stream .of (manager .getOrderEntries ()).filter (entry -> entry instanceof LibraryOrderEntry && (GRADLE_LIBRARY_PREFIX + deploymentIdStr ).equals (((LibraryOrderEntry ) entry ).getLibraryName ())). findFirst (). isPresent ( );
318+ return Stream .of (manager .getOrderEntries ()).anyMatch (entry -> entry instanceof LibraryOrderEntry && (GRADLE_LIBRARY_PREFIX + deploymentIdStr ).equals (((LibraryOrderEntry ) entry ).getLibraryName ()));
303319 }
304320
305321 private boolean isDependency (ModuleRootManager manager , String groupId , String artifactId ) {
306- return Stream .of (manager .getOrderEntries ()).filter (entry -> entry instanceof LibraryOrderEntry && ((LibraryOrderEntry ) entry ).getLibraryName ().startsWith (GRADLE_LIBRARY_PREFIX + groupId + ':' + artifactId )). findFirst (). isPresent ( );
322+ return Stream .of (manager .getOrderEntries ()).anyMatch (entry -> entry instanceof LibraryOrderEntry && ((LibraryOrderEntry ) entry ).getLibraryName ().startsWith (GRADLE_LIBRARY_PREFIX + groupId + ':' + artifactId ));
307323 }
308324
309325 @ Override
@@ -381,16 +397,16 @@ public void onImportFinished(@Nullable String projectPath) {
381397 }
382398
383399 ReadAction .nonBlocking (() -> {
384- List <Module > modules = new ArrayList <>();
385- Module [] existingModules = ModuleManager .getInstance (project ).getModules ();
386- for (Module module : existingModules ) {
387- // Check if the module is a Gradle project
388- if (GradleRunnerUtil .isGradleModule (module ) && isValidGradleModule (module )) {
389- modules .add (module );
390- }
400+ List <Module > modules = new ArrayList <>();
401+ Module [] existingModules = ModuleManager .getInstance (project ).getModules ();
402+ for (Module module : existingModules ) {
403+ // Check if the module is a Gradle project
404+ if (GradleRunnerUtil .isGradleModule (module ) && isValidGradleModule (module )) {
405+ modules .add (module );
391406 }
392- listener .importFinished (modules );
393- })
407+ }
408+ listener .importFinished (modules );
409+ })
394410 .inSmartMode (project )
395411 .submit (NonUrgentExecutor .getInstance ());
396412 }
0 commit comments