1
1
package org .graalvm .internal .tck ;
2
2
3
+ import com .fasterxml .jackson .annotation .JsonInclude ;
4
+ import com .fasterxml .jackson .core .type .TypeReference ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
6
+ import com .fasterxml .jackson .databind .SerializationFeature ;
7
+ import org .graalvm .internal .tck .model .MetadataIndexEntry ;
3
8
import org .graalvm .internal .tck .utils .ConfigurationStringBuilder ;
9
+ import org .graalvm .internal .tck .utils .FilesUtils ;
4
10
import org .graalvm .internal .tck .utils .InteractiveTaskUtils ;
5
11
import org .gradle .api .DefaultTask ;
6
12
import org .gradle .api .tasks .TaskAction ;
@@ -25,6 +31,8 @@ public abstract class ContributionTask extends DefaultTask {
25
31
private Path metadataDirectory ;
26
32
private Coordinates coordinates ;
27
33
34
+ private static final String METADATA_INDEX = "metadata/index.json" ;
35
+
28
36
private static final String BUILD_FILE = "build.gradle" ;
29
37
private static final String USER_CODE_FILTER_FILE = "user-code-filter.json" ;
30
38
private static final String REQUIRED_DOCKER_IMAGES_FILE = "required-docker-images.txt" ;
@@ -43,6 +51,9 @@ void run() throws IOException {
43
51
this .coordinates = getCoordinates ();
44
52
InteractiveTaskUtils .closeSection ();
45
53
54
+ Path coordinatesMetadataRoot = getProject ().file (CoordinateUtils .replace ("metadata/$group$/$artifact$" , coordinates )).toPath ();
55
+ boolean isExistingLibrary = Files .exists (coordinatesMetadataRoot );
56
+
46
57
Path testsLocation = getTestsLocation ();
47
58
InteractiveTaskUtils .closeSection ();
48
59
@@ -60,14 +71,14 @@ void run() throws IOException {
60
71
61
72
// initialize project
62
73
initializeWorkingDirectories ();
63
- createStubs ();
74
+ createStubs (isExistingLibrary );
75
+ updateAllowedPackages (packages );
64
76
65
77
// generate necessary infrastructure
66
78
addTests (testsLocation );
67
79
addResources (resourcesLocation );
68
80
addDockerImages (dockerImages );
69
81
addUserCodeFilterFile (packages );
70
- // TODO Update allowed-packages
71
82
addAdditionalDependencies (additionalTestImplementationDependencies );
72
83
addAgentConfigBlock ();
73
84
@@ -130,7 +141,7 @@ private Path getTestsLocation() {
130
141
131
142
private void checkPackages (Path testsPath ) {
132
143
List <Path > javaFiles = new ArrayList <>();
133
- findJavaFiles (testsPath , javaFiles );
144
+ FilesUtils . findJavaFiles (testsPath , javaFiles );
134
145
javaFiles .forEach (file -> {
135
146
try {
136
147
Optional <String > packageLine = Files .readAllLines (file ).stream ().filter (line -> line .contains ("package " )).findFirst ();
@@ -151,24 +162,6 @@ private void checkPackages(Path testsPath) {
151
162
});
152
163
}
153
164
154
- private void findJavaFiles (Path root , List <Path > result ) {
155
- if (Files .exists (root ) && Files .isRegularFile (root ) && root .toString ().endsWith (".java" )) {
156
- result .add (root );
157
- return ;
158
- }
159
-
160
- if (Files .isDirectory (root )) {
161
- File [] content = root .toFile ().listFiles ();
162
- if (content == null ) {
163
- return ;
164
- }
165
-
166
- for (var file : content ) {
167
- findJavaFiles (file .toPath (), result );
168
- }
169
- }
170
- }
171
-
172
165
private Path getResourcesLocation (){
173
166
String question = "Do your tests need any kind of resources? " +
174
167
"Absolute path to the resources directory required for your tests (type \" -\" if resources are not required): " ;
@@ -278,9 +271,41 @@ private List<Coordinates> getAdditionalDependencies() {
278
271
return dependencies ;
279
272
}
280
273
281
- private void createStubs (){
274
+ private void createStubs (boolean shouldUpdate ){
282
275
InteractiveTaskUtils .printUserInfo ("Generating stubs for: " + coordinates );
283
- invokeCommand ("gradle scaffold --coordinates " + coordinates , "Cannot generate stubs for: " + coordinates );
276
+ if (shouldUpdate ) {
277
+ invokeCommand ("gradle scaffold --coordinates " + coordinates + " --update" , "Cannot generate stubs for: " + coordinates );
278
+ } else {
279
+ invokeCommand ("gradle scaffold --coordinates " + coordinates , "Cannot generate stubs for: " + coordinates );
280
+ }
281
+ }
282
+
283
+ private void updateAllowedPackages (List <String > allowedPackages ) throws IOException {
284
+ InteractiveTaskUtils .printUserInfo ("Updating allowed packages in: " + METADATA_INDEX );
285
+ File metadataIndex = getProject ().file (METADATA_INDEX );
286
+ var objectMapper = new ObjectMapper ().enable (SerializationFeature .INDENT_OUTPUT ).setSerializationInclusion (JsonInclude .Include .NON_NULL );
287
+
288
+ List <MetadataIndexEntry > entries = objectMapper .readValue (metadataIndex , new TypeReference <>() {});
289
+ int replaceEntryIndex = -1 ;
290
+ for (int i = 0 ; i < entries .size (); i ++) {
291
+ if (entries .get (i ).module ().equals (coordinates .group () + ":" + coordinates .artifact ())) {
292
+ replaceEntryIndex = i ;
293
+ }
294
+ }
295
+
296
+ if (replaceEntryIndex != -1 ) {
297
+ MetadataIndexEntry replacedEntry = entries .remove (replaceEntryIndex );
298
+ Set <String > extendedAllowedPackages = new HashSet <>(replacedEntry .allowedPackages ());
299
+ extendedAllowedPackages .addAll (allowedPackages );
300
+
301
+ entries .add (new MetadataIndexEntry (replacedEntry .directory (), replacedEntry .module (), replacedEntry .requires (), new ArrayList <>(extendedAllowedPackages )));
302
+ }
303
+
304
+ List <MetadataIndexEntry > sortedEntries = entries .stream ()
305
+ .sorted (Comparator .comparing (MetadataIndexEntry ::module ))
306
+ .toList ();
307
+
308
+ objectMapper .writeValue (metadataIndex , sortedEntries );
284
309
}
285
310
286
311
private void addTests (Path originalTestsLocation ){
0 commit comments