13
13
import org .graalvm .internal .tck .utils .FilesUtils ;
14
14
import org .graalvm .internal .tck .utils .InteractiveTaskUtils ;
15
15
import org .gradle .api .DefaultTask ;
16
+ import org .gradle .api .file .FileSystemOperations ;
16
17
import org .gradle .api .tasks .TaskAction ;
17
18
import org .gradle .process .ExecOperations ;
18
19
@@ -35,6 +36,9 @@ public abstract class ContributionTask extends DefaultTask {
35
36
@ Inject
36
37
protected abstract ExecOperations getExecOperations ();
37
38
39
+ @ Inject
40
+ protected abstract FileSystemOperations getFileSystemOperations ();
41
+
38
42
private Path gradlew ;
39
43
40
44
private final ObjectMapper objectMapper = new ObjectMapper ().enable (SerializationFeature .INDENT_OUTPUT ).setSerializationInclusion (JsonInclude .Include .NON_NULL );
@@ -156,7 +160,12 @@ private Path getTestsLocation() {
156
160
157
161
private void checkPackages (Path testsPath ) {
158
162
List <Path > javaFiles = new ArrayList <>();
159
- FilesUtils .findJavaFiles (testsPath , javaFiles );
163
+ try {
164
+ FilesUtils .findJavaFiles (testsPath , javaFiles );
165
+ } catch (IOException e ) {
166
+ throw new RuntimeException ("Cannot find java files. Reason: " + e );
167
+ }
168
+
160
169
javaFiles .forEach (file -> {
161
170
try {
162
171
Optional <String > packageLine = Files .readAllLines (file ).stream ().filter (line -> line .contains ("package " )).findFirst ();
@@ -325,21 +334,26 @@ private void addTests(Path originalTestsLocation){
325
334
}
326
335
327
336
InteractiveTaskUtils .printUserInfo ("Removing dummy test stubs" );
328
- invokeCommand ( "rm -r " + destination , "Cannot delete files from: " + destination );
337
+ getFileSystemOperations (). delete ( deleteSpec -> deleteSpec . delete ( destination ) );
329
338
330
339
InteractiveTaskUtils .printUserInfo ("Copying tests from: " + originalTestsLocation + " to " + destination );
331
- invokeCommand ("cp -a " + allTests + " " + destination , "Cannot copy files to: " + destination );
340
+ getFileSystemOperations ().copy (copySpec -> {
341
+ copySpec .from (allTests );
342
+ copySpec .into (destination );
343
+ });
332
344
}
333
345
334
346
private void addResources (Path originalResourcesDirectory ){
335
347
if (originalResourcesDirectory == null ) {
336
348
return ;
337
349
}
338
350
339
- Path destination = testsDirectory .resolve ("src" ).resolve ("test" );
351
+ Path destination = testsDirectory .resolve ("src" ).resolve ("test" ). resolve ( "resources" ) ;
340
352
InteractiveTaskUtils .printUserInfo ("Copying resources from: " + originalResourcesDirectory + " to " + destination );
341
-
342
- invokeCommand ("cp -r " + originalResourcesDirectory + " " + destination , "Cannot copy files to: " + destination );
353
+ getFileSystemOperations ().copy (copySpec -> {
354
+ copySpec .from (originalResourcesDirectory );
355
+ copySpec .into (destination );
356
+ });
343
357
}
344
358
345
359
private void addDockerImages (List <String > images ) throws IOException {
@@ -354,7 +368,7 @@ private void addDockerImages(List<String> images) throws IOException {
354
368
}
355
369
356
370
for (var image : images ) {
357
- writeToFile (destination , image .concat (" \n " ), StandardOpenOption .APPEND );
371
+ writeToFile (destination , image .concat (System . lineSeparator () ), StandardOpenOption .APPEND );
358
372
}
359
373
}
360
374
@@ -431,7 +445,7 @@ private void addAgentConfigBlock() {
431
445
throw new RuntimeException ("Cannot find template for the graalvm configuration block" );
432
446
}
433
447
434
- String content = " \n " + (new String (stream .readAllBytes (), StandardCharsets .UTF_8 ));
448
+ String content = System . lineSeparator () + (new String (stream .readAllBytes (), StandardCharsets .UTF_8 ));
435
449
writeToFile (buildFilePath , content , StandardOpenOption .APPEND );
436
450
} catch (IOException e ) {
437
451
throw new RuntimeException ("Cannot add agent block into: " + buildFilePath );
@@ -510,10 +524,12 @@ private void removeEmptyConfigFiles() throws IOException {
510
524
if (Files .exists (agentExtractedPredefinedClasses )) {
511
525
File [] extractedPredefinedClasses = new File (agentExtractedPredefinedClasses .toUri ()).listFiles ();
512
526
if (extractedPredefinedClasses == null || extractedPredefinedClasses .length == 0 ) {
527
+ ensureFileBelongsToProject (agentExtractedPredefinedClasses );
528
+
513
529
boolean canDelete = InteractiveTaskUtils .askForDeletePermission (agentExtractedPredefinedClasses );
514
530
if (canDelete ) {
515
531
InteractiveTaskUtils .printUserInfo ("Removing empty: agent-extracted-predefined-classes" );
516
- invokeCommand ( "rm -r " + agentExtractedPredefinedClasses , "Cannot delete empty config file: " + agentExtractedPredefinedClasses );
532
+ getFileSystemOperations (). delete ( deleteSpec -> deleteSpec . delete ( agentExtractedPredefinedClasses ) );
517
533
}
518
534
}
519
535
}
@@ -526,7 +542,7 @@ private void removeConfigFile(Path path, CONFIG_FILES file, List<CONFIG_FILES> r
526
542
boolean canDelete = InteractiveTaskUtils .askForDeletePermission (path );
527
543
if (canDelete ) {
528
544
InteractiveTaskUtils .printUserInfo ("Removing empty: " + file .get ());
529
- invokeCommand ( "rm " + path , "Cannot delete empty config file: " + path );
545
+ getFileSystemOperations (). delete ( deleteSpec -> deleteSpec . delete ( path ) );
530
546
remainingFiles .remove (file );
531
547
}
532
548
}
0 commit comments