18
18
import com .intellij .ide .util .newProjectWizard .AddModuleWizard ;
19
19
import com .intellij .openapi .application .ReadAction ;
20
20
import com .intellij .openapi .externalSystem .model .execution .ExternalSystemTaskExecutionSettings ;
21
+ import com .intellij .openapi .externalSystem .service .execution .ProgressExecutionMode ;
21
22
import com .intellij .openapi .externalSystem .service .project .ProjectDataManager ;
22
23
import com .intellij .openapi .externalSystem .service .project .manage .ProjectDataImportListener ;
24
+ import com .intellij .openapi .externalSystem .task .TaskCallback ;
23
25
import com .intellij .openapi .externalSystem .util .ExternalSystemApiUtil ;
24
26
import com .intellij .openapi .externalSystem .util .ExternalSystemUtil ;
25
27
import com .intellij .openapi .module .ModifiableModuleModel ;
@@ -116,14 +118,34 @@ private void processDownload(Module module, Set<String> deploymentIds, List<Virt
116
118
Path outputPath = Files .createTempFile (null , ".txt" );
117
119
Path customBuildFile = generateCustomGradleBuild (getModuleDirPath (module ), outputPath , deploymentIds );
118
120
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
+ };
119
143
try {
120
- collectDependencies (module , customBuildFile , customSettingsFile , outputPath , result );
144
+ collectDependencies (module , customSettingsFile , outputPath , result , callback );
121
145
} catch (IOException e ) {
122
146
LOGGER .warn (e .getLocalizedMessage (), e );
123
147
} finally {
124
148
Files .delete (outputPath );
125
- Files .delete (customBuildFile );
126
- Files .delete (customSettingsFile );
127
149
}
128
150
}
129
151
@@ -149,37 +171,32 @@ private String getModuleDirPath(Module module) {
149
171
/**
150
172
* Collect all deployment JARs and dependencies through a Gradle specific task.
151
173
*
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
156
178
* @throws IOException if an error occurs running Gradle
157
179
*/
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 {
159
181
try {
160
182
ExternalSystemTaskExecutionSettings executionSettings = new ExternalSystemTaskExecutionSettings ();
161
183
executionSettings .setExternalSystemIdString (GradleConstants .SYSTEM_ID .toString ());
162
184
executionSettings .setTaskNames (Collections .singletonList ("listQuarkusDependencies" ));
163
185
executionSettings .setScriptParameters (String .format ("-c %s -q --console plain" , customSettingsFile .toString ()));
164
186
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 );
171
190
try (BufferedReader reader = Files .newBufferedReader (outputPath )) {
172
191
String id ;
173
192
174
193
while ((id = reader .readLine ()) != null ) {
175
194
String file = reader .readLine ();
176
195
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 );
183
200
}
184
201
}
185
202
}
@@ -233,6 +250,7 @@ private Path generateCustomGradleSettings(String basePath, Path customBuildFile)
233
250
try (Reader reader = Files .newBufferedReader (path , StandardCharsets .UTF_8 )) {
234
251
content = IOUtils .toString (reader );
235
252
} catch (IOException e ) {
253
+ LOGGER .warn (e .getLocalizedMessage (), e );
236
254
}
237
255
content += System .lineSeparator () + "rootProject.buildFileName =\" " + customBuildFile .getFileName ().toFile ().getName () + "\" " ;
238
256
Path customPath = Files .createTempFile (base , null , getScriptExtension ());
@@ -252,7 +270,7 @@ private Path generateCustomGradleSettings(String basePath, Path customBuildFile)
252
270
* @return the content of the custom build file
253
271
*/
254
272
private String appendQuarkusResolution (String content , Path outputPath , Set <String > deploymentIds ) {
255
- StringBuffer buffer = new StringBuffer (content );
273
+ StringBuilder buffer = new StringBuilder (content );
256
274
buffer .append (System .lineSeparator ());
257
275
buffer .append (createQuarkusConfiguration ()).append (System .lineSeparator ());
258
276
buffer .append ("dependencies {" ).append (System .lineSeparator ());
@@ -281,29 +299,27 @@ private void processLibrary(Library library, ModuleRootManager manager, Set<Stri
281
299
* @return the File object
282
300
*/
283
301
public static File getDeploymentFile (String artifactId ) {
284
- String path = toPath (artifactId , File . separatorChar );
302
+ String path = toPath (artifactId );
285
303
if (path != null ) {
286
304
return new File (M2_REPO , path );
287
305
}
288
306
return null ;
289
307
}
290
308
291
- private static String toPath (String artifactId , char separator ) {
309
+ private static String toPath (String artifactId ) {
292
310
String [] comps = artifactId .split (":" );
293
311
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" ;
297
313
}
298
314
return null ;
299
315
}
300
316
301
317
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 ()));
303
319
}
304
320
305
321
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 ));
307
323
}
308
324
309
325
@ Override
@@ -381,16 +397,16 @@ public void onImportFinished(@Nullable String projectPath) {
381
397
}
382
398
383
399
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 );
391
406
}
392
- listener .importFinished (modules );
393
- })
407
+ }
408
+ listener .importFinished (modules );
409
+ })
394
410
.inSmartMode (project )
395
411
.submit (NonUrgentExecutor .getInstance ());
396
412
}
0 commit comments