44import static io .quarkus .gradle .workspace .descriptors .ProjectDescriptor .TaskType .RESOURCES ;
55
66import java .io .File ;
7- import java .util .Collections ;
7+ import java .util .ArrayList ;
8+ import java .util .Collection ;
9+ import java .util .HashMap ;
810import java .util .HashSet ;
911import java .util .LinkedHashMap ;
12+ import java .util .List ;
1013import java .util .Map ;
1114import java .util .Set ;
1215import java .util .concurrent .atomic .AtomicReference ;
1619import org .gradle .api .tasks .SourceSet ;
1720import org .gradle .api .tasks .SourceSetContainer ;
1821import org .gradle .api .tasks .compile .AbstractCompile ;
19- import org .gradle .api .tasks .testing .Test ;
2022import org .gradle .language .jvm .tasks .ProcessResources ;
2123import org .jetbrains .kotlin .gradle .tasks .KotlinJvmCompile ;
2224
23- import com .google .common .collect .ImmutableSet ;
25+ import io .quarkus .bootstrap .workspace .ArtifactSources ;
26+ import io .quarkus .bootstrap .workspace .DefaultArtifactSources ;
27+ import io .quarkus .bootstrap .workspace .SourceDir ;
28+ import io .quarkus .bootstrap .workspace .WorkspaceModule ;
29+ import io .quarkus .bootstrap .workspace .WorkspaceModuleId ;
30+ import io .quarkus .maven .dependency .ArtifactCoords ;
2431
2532public class ProjectDescriptorBuilder {
2633
27- private static final Set <String > DEPENDENCY_SOURCE_SETS = ImmutableSet .of (SourceSet .MAIN_SOURCE_SET_NAME ,
28- SourceSet .TEST_SOURCE_SET_NAME , "test-fixtures" );
29-
3034 private final File projectDir ;
3135 private final File buildDir ;
3236 private final File buildFile ;
3337 private final Map <String , QuarkusTaskDescriptor > tasks ;
3438 private final Map <String , Set <String >> sourceSetTasks ;
3539 private final Map <String , Set <String >> sourceSetTasksRaw ;
36- private final Set <String > collectOnlySourceSets ;
3740
38- private ProjectDescriptorBuilder (Project project , Set <String > collectOnlySourceSets ) {
41+ private final WorkspaceModuleId moduleId ;
42+ private final Map <String , ClassifiedSources > classifiedSources = new HashMap <>();
43+
44+ private ProjectDescriptorBuilder (Project project ) {
3945 this .tasks = new LinkedHashMap <>();
4046 this .sourceSetTasks = new LinkedHashMap <>();
4147 this .sourceSetTasksRaw = new LinkedHashMap <>();
48+ this .moduleId = WorkspaceModuleId .of (String .valueOf (project .getGroup ()), project .getName (),
49+ String .valueOf (project .getVersion ()));
4250 this .buildFile = project .getBuildFile ();
4351 this .projectDir = project .getLayout ().getProjectDirectory ().getAsFile ();
4452 this .buildDir = project .getLayout ().getBuildDirectory ().get ().getAsFile ();
45- this .collectOnlySourceSets = collectOnlySourceSets ;
4653 }
4754
4855 public static Provider <DefaultProjectDescriptor > buildForApp (Project target ) {
49- ProjectDescriptorBuilder builder = new ProjectDescriptorBuilder (target , Collections . emptySet () );
56+ ProjectDescriptorBuilder builder = new ProjectDescriptorBuilder (target );
5057 target .afterEvaluate (project -> {
51- project .getTasks ().withType (AbstractCompile .class )
52- .configureEach (builder ::readConfigurationFor );
58+ project .getTasks ().withType (AbstractCompile .class ).configureEach (builder ::readConfigurationFor );
5359 builder .withKotlinJvmCompileType (project );
54- project .getTasks ().withType (ProcessResources .class )
55- .configureEach (builder ::readConfigurationFor );
56- project .getTasks ().withType (Test .class )
57- .configureEach (builder ::readConfigurationFor );
60+ project .getTasks ().withType (ProcessResources .class ).configureEach (builder ::readConfigurationFor );
5861 });
59- return target .getProviders ().provider (() -> new DefaultProjectDescriptor (
60- builder .projectDir ,
61- builder .buildDir ,
62- builder .buildFile ,
63- builder .tasks ,
64- builder .sourceSetTasks ,
65- builder .sourceSetTasksRaw ));
66- }
6762
68- public static Provider <DefaultProjectDescriptor > buildForDependency (Project target ) {
69- ProjectDescriptorBuilder builder = new ProjectDescriptorBuilder (target , DEPENDENCY_SOURCE_SETS );
70- target .afterEvaluate (project -> {
71- project .getTasks ().withType (AbstractCompile .class )
72- .configureEach (builder ::readConfigurationFor );
73- builder .withKotlinJvmCompileType (project );
74- project .getTasks ().withType (ProcessResources .class )
75- .configureEach (builder ::readConfigurationFor );
63+ return target .getProviders ().provider (() -> {
64+
65+ var moduleBuilder = WorkspaceModule .builder ()
66+ .setModuleId (builder .moduleId )
67+ .setBuildFile (builder .buildFile .toPath ())
68+ .setModuleDir (builder .projectDir .toPath ());
69+
70+ for (var classifiedSources : builder .classifiedSources .values ()) {
71+ moduleBuilder .addArtifactSources (classifiedSources .toArtifactSources ());
72+ }
73+
74+ return new DefaultProjectDescriptor (
75+ builder .projectDir ,
76+ builder .buildDir ,
77+ builder .buildFile ,
78+ builder .tasks ,
79+ builder .sourceSetTasks ,
80+ builder .sourceSetTasksRaw ,
81+ moduleBuilder );
7682 });
77- return target .getProviders ().provider (() -> new DefaultProjectDescriptor (
78- builder .projectDir ,
79- builder .buildDir ,
80- builder .buildFile ,
81- builder .tasks ,
82- builder .sourceSetTasks ,
83- builder .sourceSetTasksRaw ));
83+
8484 }
8585
8686 private void readConfigurationFor (AbstractCompile task ) {
@@ -95,18 +95,17 @@ private void readConfigurationFor(AbstractCompile task) {
9595 tasks .put (task .getName (), new QuarkusTaskDescriptor (task .getName (), COMPILE , srcDir , destDir ));
9696 SourceSetContainer sourceSets = task .getProject ().getExtensions ().getByType (SourceSetContainer .class );
9797 sourceSets .stream ().filter (sourceSet -> sourceSet .getOutput ().getClassesDirs ().contains (destDir ))
98- .forEach (sourceSet -> sourceSetTasks
99- .computeIfAbsent (sourceSet .getName (), s -> new HashSet <>())
100- .add (task .getName ()));
98+ .forEach (sourceSet -> {
99+ sourceSetTasks .computeIfAbsent (sourceSet .getName (), s -> new HashSet <>()).add (task .getName ());
100+ classifiedSources .computeIfAbsent (getClassifier (sourceSet ), ClassifiedSources ::new )
101+ .addSources (srcDir , destDir );
102+ });
101103 fileVisitDetails .stopVisiting ();
102104 }
103105 });
104106 }
105107 }
106108
107- private void readConfigurationFor (Test task ) {
108- }
109-
110109 private void readConfigurationFor (ProcessResources task ) {
111110 if (task .getEnabled () && !task .getSource ().isEmpty ()) {
112111 File destDir = task .getDestinationDir ();
@@ -116,9 +115,11 @@ private void readConfigurationFor(ProcessResources task) {
116115 tasks .put (task .getName (), new QuarkusTaskDescriptor (task .getName (), RESOURCES , srcDir , destDir ));
117116 SourceSetContainer sourceSets = task .getProject ().getExtensions ().getByType (SourceSetContainer .class );
118117 sourceSets .stream ().filter (sourceSet -> destDir .equals (sourceSet .getOutput ().getResourcesDir ()))
119- .forEach (sourceSet -> sourceSetTasks
120- .computeIfAbsent (sourceSet .getName (), s -> new HashSet <>())
121- .add (task .getName ()));
118+ .forEach (sourceSet -> {
119+ sourceSetTasks .computeIfAbsent (sourceSet .getName (), s -> new HashSet <>()).add (task .getName ());
120+ classifiedSources .computeIfAbsent (getClassifier (sourceSet ), ClassifiedSources ::new )
121+ .addResources (srcDir , destDir );
122+ });
122123 fileVisitDetails .stopVisiting ();
123124 }
124125 });
@@ -147,9 +148,77 @@ private void readConfigurationFor(KotlinJvmCompile task) {
147148 tasks .put (task .getName (), new QuarkusTaskDescriptor (task .getName (), COMPILE , srcDir .get (), destDir ));
148149 SourceSetContainer sourceSets = task .getProject ().getExtensions ().getByType (SourceSetContainer .class );
149150 sourceSets .stream ().filter (sourceSet -> sourceSet .getOutput ().getClassesDirs ().contains (destDir ))
150- .forEach (sourceSet -> sourceSetTasks
151- .computeIfAbsent (sourceSet .getName (), s -> new HashSet <>())
152- .add (task .getName ()));
151+ .forEach (sourceSet -> {
152+ sourceSetTasks .computeIfAbsent (sourceSet .getName (), s -> new HashSet <>()).add (task .getName ());
153+ classifiedSources .computeIfAbsent (getClassifier (sourceSet ), ClassifiedSources ::new )
154+ .addSources (srcDir .get (), destDir );
155+ });
156+ }
157+ }
158+
159+ private static String getClassifier (SourceSet sourceSet ) {
160+ switch (sourceSet .getName ()) {
161+ case SourceSet .MAIN_SOURCE_SET_NAME :
162+ return ArtifactCoords .DEFAULT_CLASSIFIER ;
163+ case SourceSet .TEST_SOURCE_SET_NAME :
164+ return "tests" ;
165+ default :
166+ return sourceSet .getName ();
167+ }
168+ }
169+
170+ private static class ClassifiedSources {
171+ private final String classifier ;
172+ private final List <SourceOutputDir > sources = new ArrayList <>(1 );
173+ private final List <SourceOutputDir > resources = new ArrayList <>(1 );
174+
175+ public ClassifiedSources (String classifier ) {
176+ this .classifier = classifier ;
177+ }
178+
179+ private void addSources (File src , File output ) {
180+ addIfNotPresent (sources , src , output );
181+ }
182+
183+ private void addResources (File src , File output ) {
184+ addIfNotPresent (resources , src , output );
185+ }
186+
187+ private static Collection <SourceDir > toSourceDir (List <SourceOutputDir > dirs ) {
188+ if (dirs .isEmpty ()) {
189+ return List .of ();
190+ }
191+ if (dirs .size () == 1 ) {
192+ return List .of (dirs .get (0 ).toSourceDir ());
193+ }
194+ final List <SourceDir > result = new ArrayList <>(dirs .size ());
195+ for (var dir : dirs ) {
196+ result .add (dir .toSourceDir ());
197+ }
198+ return result ;
199+ }
200+
201+ private ArtifactSources toArtifactSources () {
202+ return new DefaultArtifactSources (classifier , toSourceDir (sources ), toSourceDir (resources ));
203+ }
204+
205+ private static void addIfNotPresent (List <SourceOutputDir > resources , File src , File output ) {
206+ if (resources .isEmpty ()) {
207+ resources .add (new SourceOutputDir (src , output ));
208+ } else {
209+ for (var added : resources ) {
210+ if (added .src ().equals (src )) {
211+ return ;
212+ }
213+ }
214+ resources .add (new SourceOutputDir (src , output ));
215+ }
153216 }
154217 }
218+
219+ private record SourceOutputDir (File src , File output ) {
220+ private SourceDir toSourceDir () {
221+ return SourceDir .of (src .toPath (), output .toPath ());
222+ }
223+ };
155224}
0 commit comments