3131import java .util .Map ;
3232import java .util .Objects ;
3333import java .util .stream .Collectors ;
34- import kotlin . Unit ;
34+
3535import dev .aoqia .leaf .loom .LoomGradleExtension ;
3636import dev .aoqia .leaf .loom .extension .MixinExtension ;
37+
38+ import kotlin .Unit ;
3739import org .gradle .api .Project ;
3840import org .gradle .api .tasks .SourceSet ;
41+ import org .gradle .api .tasks .TaskProvider ;
3942import org .gradle .api .tasks .compile .JavaCompile ;
4043import org .jetbrains .kotlin .gradle .plugin .KaptExtension ;
4144
4245public class KaptApInvoker extends AnnotationProcessorInvoker <JavaCompile > {
43- private final KaptExtension kaptExtension = project .getExtensions ().getByType (KaptExtension .class );
46+ private final KaptExtension kaptExtension = project .getExtensions ()
47+ .getByType (KaptExtension .class );
4448 // Refmap will be written to here with mixin, then moved after JavaCompile to the correct place
4549 private final File dummyRefmapDirectory ;
4650
4751 public KaptApInvoker (Project project ) {
48- super (
49- project ,
50- AnnotationProcessorInvoker .getApConfigurations (project , KaptApInvoker ::getKaptConfigurationName ),
51- getInvokerTasks (project ),
52- "Kotlin" );
52+ super (project , AnnotationProcessorInvoker .getApConfigurations (project ,
53+ KaptApInvoker ::getKaptConfigurationName ), getInvokerTasks (project ), "Kotlin" );
5354
5455 try {
5556 dummyRefmapDirectory = Files .createTempDirectory ("temp_refmap" ).toFile ();
@@ -63,69 +64,77 @@ public KaptApInvoker(Project project) {
6364 kaptExtension .setIncludeCompileClasspath (false );
6465 }
6566
66- private static Map <SourceSet , JavaCompile > getInvokerTasks (Project project ) {
67+ private static Map <SourceSet , TaskProvider < JavaCompile > > getInvokerTasks (Project project ) {
6768 MixinExtension mixin = LoomGradleExtension .get (project ).getMixin ();
68- return mixin .getInvokerTasksStream (AnnotationProcessorInvoker .JAVA )
69- .collect (Collectors .toMap (
70- Map .Entry ::getKey , entry -> Objects .requireNonNull ((JavaCompile ) entry .getValue ())));
69+ return mixin .getInvokerTasksStream (AnnotationProcessorInvoker .JAVA , JavaCompile .class )
70+ .collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
71+ }
72+
73+ // Pulled out from the internal class:
74+ // https://github.com/JetBrains/kotlin/blob/33a0ec9b4f40f3d6f1f96b2db504ade4c2fafe03/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/kapt/Kapt3KotlinGradleSubplugin.kt#L92
75+ private static String getKaptConfigurationName (SourceSet sourceSet ) {
76+ String sourceSetName = sourceSet .getName ();
77+
78+ if (!sourceSetName .equals (SourceSet .MAIN_SOURCE_SET_NAME )) {
79+ return "kapt" +
80+ (sourceSetName .substring (0 , 1 ).toUpperCase () + sourceSetName .substring (1 ));
81+ }
82+
83+ return "kapt" ;
7184 }
7285
7386 @ Override
7487 public void configureMixin () {
7588 super .configureMixin ();
7689
77- for (Map .Entry <SourceSet , JavaCompile > entry : invokerTasks .entrySet ()) {
78- // Kapt only allows specifying javac args to all annotation processors at once. So we need to specify some
90+ for (Map .Entry <SourceSet , TaskProvider <JavaCompile >> entry : invokerTasks .entrySet ()) {
91+ // Kapt only allows specifying javac args to all annotation processors at once. So we
92+ // need to specify some
7993 // dummy
80- // target location for the refmap and then move it to the correct place for each sourceset
81- JavaCompile task = entry .getValue ();
82- SourceSet sourceSet = entry .getKey ();
83- task .doLast (t -> {
84- try {
85- String refmapName = Objects .requireNonNull (MixinExtension .getMixinInformationContainer (sourceSet ))
94+ // target location for the refmap and then move it to the correct place for each
95+ // sourceset
96+ entry .getValue ().configure (task -> {
97+ SourceSet sourceSet = entry .getKey ();
98+ task .doLast (t -> {
99+ try {
100+ String refmapName = Objects .requireNonNull (
101+ MixinExtension .getMixinInformationContainer (sourceSet ))
86102 .refmapNameProvider ()
87103 .get ();
88- Path src = Paths .get (getRefmapDestination (task , refmapName ));
89- Path dest = Paths .get (
90- task .getDestinationDirectory ().get ().getAsFile ().toString (), refmapName );
91-
92- // Possible that no mixin annotations exist
93- if (Files .exists (src )) {
94- project .getLogger ().info ("Copying refmap from " + src + " to " + dest );
95- Files .move (src , dest );
104+ Path src = Paths .get (getRefmapDestination (task , refmapName ));
105+ Path dest = Paths .get (
106+ task .getDestinationDirectory ().get ().getAsFile ().toString (),
107+ refmapName );
108+
109+ // Possible that no mixin annotations exist
110+ if (Files .exists (src )) {
111+ project .getLogger ().info ("Copying refmap from {} to {}" , src , dest );
112+ Files .move (src , dest );
113+ }
114+ } catch (IOException e ) {
115+ project .getLogger ()
116+ .warn ("Could not move refmap generated by kapt for task {}" , task , e );
96117 }
97- } catch (IOException e ) {
98- project .getLogger ().warn ("Could not move refmap generated by kapt for task " + task , e );
99- }
118+ });
100119 });
101120 }
102121 }
103122
104- // Pulled out from the internal class:
105- // https://github.com/JetBrains/kotlin/blob/33a0ec9b4f40f3d6f1f96b2db504ade4c2fafe03/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/kapt/Kapt3KotlinGradleSubplugin.kt#L92
106- private static String getKaptConfigurationName (SourceSet sourceSet ) {
107- String sourceSetName = sourceSet .getName ();
108-
109- if (!sourceSetName .equals (SourceSet .MAIN_SOURCE_SET_NAME )) {
110- return "kapt" + (sourceSetName .substring (0 , 1 ).toUpperCase () + sourceSetName .substring (1 ));
111- }
112-
113- return "kapt" ;
123+ @ Override
124+ protected File getRefmapDestinationDir (JavaCompile task ) {
125+ return dummyRefmapDirectory ;
114126 }
115127
116128 @ Override
117129 protected void passArgument (JavaCompile compileTask , String key , String value ) {
118- // Note: this MUST be run early on, before kapt uses this data, and there is only a point to setting the value
130+ // Note: this MUST be run early on, before kapt uses this data, and there is only a
131+ // point
132+ // to setting the value
119133 // once since
120134 // kapt shares the options with all java compilers
121135 kaptExtension .arguments (args -> {
122136 args .arg (key , value );
123137 return Unit .INSTANCE ;
124138 });
125139 }
126-
127- @ Override
128- protected File getRefmapDestinationDir (JavaCompile task ) {
129- return dummyRefmapDirectory ;
130- }
131140}
0 commit comments