diff options
author | LogicFan <38597904+LogicFan@users.noreply.github.com> | 2021-07-18 09:13:47 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-18 14:13:47 +0100 |
commit | 81fa551382d9c7016f769014ed56d119a3d676ed (patch) | |
tree | bd87e8c82a2d7779f6cae7ed093860255af1836f /src/main/java/net/fabricmc/loom/build | |
parent | b4ac68825f4120dd689d6032c6381780cf8181b0 (diff) | |
download | architectury-loom-81fa551382d9c7016f769014ed56d119a3d676ed.tar.gz architectury-loom-81fa551382d9c7016f769014ed56d119a3d676ed.tar.bz2 architectury-loom-81fa551382d9c7016f769014ed56d119a3d676ed.zip |
Better Mixin AP configuration (#423)
* [SPON-15] Apply Mixin AP config for all projects.
* Revert "[SPON-15] Apply Mixin AP config for all projects."
This reverts commit 93576e83b1221949d551b6307938f7dd6dc8fbbe.
* use setter & getter
* fix broken test introduced in 54d6ef7896f22f89d6594703d09e3195c814cf10
* initial commit for mixin extension
* refactor getConfiguration
* apply mixin extension
* [SPON-15] allow across project AP config
* [SPON-15] revert some changes
* [SPON-15] refactor codes
* [SPON-15] fix bugs
* [SPON-15] bring back cross-project apconfig
* [SPON-15] bug fix: move add default sourceSet earlier
* [SPON-15] fix style
* [SPON-15] refactor MixinAPExtension
* add test
* update test
* [SPON-15] fix test
* Update MixinAnnotationProcessorExtension.java
* [SPON-15] fix test
* fix deprecated gradle API
* [SPON-15] refactor ApInvoker
* [SPON-15] refactor ApInvoker
* allow change refmap name in sourceSet bases
* add new condition on test
* [SPON-15] fix wrong suffix
* Revert "[SPON-15] fix wrong suffix"
This reverts commit 98910392d91c26cd0454cca8cfc03c4e3d417fd6.
* fix mixinjson suffix
* use stream instead of collection for mixin json name
* change name for function
* use correct auto-refmap
* fix file name
* add with action
* add test
* refactor some codes
* refactor code
* update test
* fix checkstyle
* better error message
* fix checkstyle
* remove corss project option
* allow mixin inside loom
* remove project0
I should remove all project0. If I forget one please tell me.
* move `mixin` inside `loom`
* fix spotless
* merge attempt
* fix checkstyle
* seperate api & impl
* add experimental annotation for API
* use API
* Fix indentation
Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>
* fix typo
Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>
* fix typo
Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>
* better javadoc
Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>
Diffstat (limited to 'src/main/java/net/fabricmc/loom/build')
5 files changed, 104 insertions, 103 deletions
diff --git a/src/main/java/net/fabricmc/loom/build/MixinRefmapHelper.java b/src/main/java/net/fabricmc/loom/build/MixinRefmapHelper.java index bc775746..458a5ffb 100644 --- a/src/main/java/net/fabricmc/loom/build/MixinRefmapHelper.java +++ b/src/main/java/net/fabricmc/loom/build/MixinRefmapHelper.java @@ -25,71 +25,47 @@ package net.fabricmc.loom.build; import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; import java.nio.file.Path; -import java.util.HashSet; -import java.util.Set; +import java.util.Objects; +import java.util.stream.Stream; import java.util.zip.ZipEntry; import com.google.gson.JsonObject; +import org.gradle.api.Project; import org.zeroturnaround.zip.ZipUtil; import org.zeroturnaround.zip.transform.StringZipEntryTransformer; import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry; +import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.LoomGradlePlugin; +import net.fabricmc.loom.extension.MixinApExtension; public final class MixinRefmapHelper { private MixinRefmapHelper() { } - public static boolean addRefmapName(String filename, Path outputPath) { + public static boolean addRefmapName(Project project, Path outputPath) { + MixinApExtension mixin = LoomGradleExtension.get(project).getMixinApExtension(); File output = outputPath.toFile(); - Set<String> mixinFilenames = findMixins(output, true); - if (mixinFilenames.size() > 0) { - return ZipUtil.transformEntries(output, mixinFilenames.stream().map((f) -> new ZipEntryTransformerEntry(f, new StringZipEntryTransformer("UTF-8") { + return mixin.getMixinSourceSetsStream().map(sourceSet -> { + MixinApExtension.MixinInformationContainer container = Objects.requireNonNull( + MixinApExtension.getMixinInformationContainer(sourceSet) + ); + Stream<String> mixinJsonNames = container.getMixinJsonNames(); + String refmapName = container.getRefmapName(); + + return ZipUtil.transformEntries(output, mixinJsonNames.map(f -> new ZipEntryTransformerEntry(f, new StringZipEntryTransformer("UTF-8") { @Override - protected String transform(ZipEntry zipEntry, String input) throws IOException { + protected String transform(ZipEntry zipEntry, String input) { JsonObject json = LoomGradlePlugin.GSON.fromJson(input, JsonObject.class); if (!json.has("refmap")) { - json.addProperty("refmap", filename); + json.addProperty("refmap", refmapName); } return LoomGradlePlugin.GSON.toJson(json); } })).toArray(ZipEntryTransformerEntry[]::new)); - } else { - return false; - } - } - - private static Set<String> findMixins(File output, boolean onlyWithoutRefmap) { - // first, identify all of the mixin files - Set<String> mixinFilename = new HashSet<>(); - // TODO: this is a lovely hack - ZipUtil.iterate(output, (stream, entry) -> { - if (!entry.isDirectory() && entry.getName().endsWith(".json") && !entry.getName().contains("/") && !entry.getName().contains("\\")) { - // JSON file in root directory - try (InputStreamReader inputStreamReader = new InputStreamReader(stream)) { - JsonObject json = LoomGradlePlugin.GSON.fromJson(inputStreamReader, JsonObject.class); - - if (json != null) { - boolean hasMixins = json.has("mixins") && json.get("mixins").isJsonArray(); - boolean hasClient = json.has("client") && json.get("client").isJsonArray(); - boolean hasServer = json.has("server") && json.get("server").isJsonArray(); - - if (json.has("package") && (hasMixins || hasClient || hasServer)) { - if (!onlyWithoutRefmap || !json.has("refmap") || !json.has("minVersion")) { - mixinFilename.add(entry.getName()); - } - } - } - } catch (Exception ignored) { - // ... - } - } - }); - return mixinFilename; + }).reduce(false, Boolean::logicalOr); } } diff --git a/src/main/java/net/fabricmc/loom/build/mixin/AnnotationProcessorInvoker.java b/src/main/java/net/fabricmc/loom/build/mixin/AnnotationProcessorInvoker.java index 6d0910c1..04c47c9d 100644 --- a/src/main/java/net/fabricmc/loom/build/mixin/AnnotationProcessorInvoker.java +++ b/src/main/java/net/fabricmc/loom/build/mixin/AnnotationProcessorInvoker.java @@ -29,16 +29,17 @@ import java.io.IOException; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.stream.Stream; +import java.util.Objects; +import java.util.function.Function; +import java.util.stream.Collectors; import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.ConfigurationContainer; -import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSet; -import org.gradle.api.tasks.TaskCollection; +import net.fabricmc.loom.extension.MixinApExtension; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.util.Constants; @@ -48,37 +49,46 @@ import net.fabricmc.loom.util.Constants; * See Java and Kapt implementations for a more deep understanding of the things passed by the children. */ public abstract class AnnotationProcessorInvoker<T extends Task> { + public static final String JAVA = "java"; + public static final String SCALA = "scala"; + protected final Project project; - private final Collection<Configuration> annotationProcessorConfigurations; - protected final TaskCollection<T> invokerTasks; + protected final Map<SourceSet, T> invokerTasks; + private final Collection<Configuration> apConfigurations; protected AnnotationProcessorInvoker(Project project, - Collection<Configuration> annotationProcessorConfigurations, - TaskCollection<T> invokerTasks) { + Collection<Configuration> apConfigurations, + Map<SourceSet, T> invokerTasks) { this.project = project; - this.annotationProcessorConfigurations = annotationProcessorConfigurations; + this.apConfigurations = apConfigurations; this.invokerTasks = invokerTasks; } + protected static Collection<Configuration> getApConfigurations(Project project, Function<String, String> getApConfigNameFunc) { + MixinApExtension mixin = LoomGradleExtension.get(project).getMixinApExtension(); + return mixin.getApConfigurationsStream(getApConfigNameFunc).collect(Collectors.toList()); + } + protected abstract void passArgument(T compileTask, String key, String value); - protected abstract File getDestinationDir(T task); + protected abstract File getRefmapDestinationDir(T task); - protected final String getRefmapDestination(T task, LoomGradleExtension extension) throws IOException { - return new File(getDestinationDir(task), extension.getRefmapName()).getCanonicalPath(); + protected final String getRefmapDestination(T task, String refmapName) throws IOException { + return new File(getRefmapDestinationDir(task), refmapName).getCanonicalPath(); } - private void passMixinArguments(T task) { + private void passMixinArguments(T task, SourceSet sourceSet) { try { - LoomGradleExtension extension = LoomGradleExtension.get(project); + LoomGradleExtension loom = LoomGradleExtension.get(project); + String refmapName = Objects.requireNonNull(MixinApExtension.getMixinInformationContainer(sourceSet)).getRefmapName(); Map<String, String> args = new HashMap<>() {{ - put(Constants.MixinArguments.IN_MAP_FILE_NAMED_INTERMEDIARY, extension.getMappingsProvider().tinyMappings.getCanonicalPath()); - put(Constants.MixinArguments.OUT_MAP_FILE_NAMED_INTERMEDIARY, extension.getNextMixinMappings().getCanonicalPath()); - put(Constants.MixinArguments.OUT_REFMAP_FILE, getRefmapDestination(task, extension)); + put(Constants.MixinArguments.IN_MAP_FILE_NAMED_INTERMEDIARY, loom.getMappingsProvider().tinyMappings.getCanonicalPath()); + put(Constants.MixinArguments.OUT_MAP_FILE_NAMED_INTERMEDIARY, loom.getNextMixinMappings().getCanonicalPath()); + put(Constants.MixinArguments.OUT_REFMAP_FILE, getRefmapDestination(task, refmapName)); put(Constants.MixinArguments.DEFAULT_OBFUSCATION_ENV, "named:intermediary"); }}; - project.getLogger().debug("Outputting refmap to dir: " + getDestinationDir(task) + " for compile task: " + task); + project.getLogger().debug("Outputting refmap to dir: " + getRefmapDestinationDir(task) + " for compile task: " + task); args.forEach((k, v) -> passArgument(task, k, v)); } catch (IOException e) { project.getLogger().error("Could not configure mixin annotation processors", e); @@ -90,7 +100,7 @@ public abstract class AnnotationProcessorInvoker<T extends Task> { LoomGradleExtension extension = LoomGradleExtension.get(project); if (!extension.ideSync()) { - for (Configuration processorConfig : annotationProcessorConfigurations) { + for (Configuration processorConfig : apConfigurations) { project.getLogger().info("Adding mixin to classpath of AP config: " + processorConfig.getName()); // Pass named MC classpath to mixin AP classpath processorConfig.extendsFrom( @@ -105,14 +115,8 @@ public abstract class AnnotationProcessorInvoker<T extends Task> { } } - for (T task : invokerTasks) { - passMixinArguments(task); + for (Map.Entry<SourceSet, T> entry : invokerTasks.entrySet()) { + passMixinArguments(entry.getValue(), entry.getKey()); } } - - static Stream<SourceSet> getNonTestSourceSets(Project project) { - return project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets() - .stream() - .filter(sourceSet -> !sourceSet.getName().equals("test")); - } } diff --git a/src/main/java/net/fabricmc/loom/build/mixin/JavaApInvoker.java b/src/main/java/net/fabricmc/loom/build/mixin/JavaApInvoker.java index aa7d46a7..a449600a 100644 --- a/src/main/java/net/fabricmc/loom/build/mixin/JavaApInvoker.java +++ b/src/main/java/net/fabricmc/loom/build/mixin/JavaApInvoker.java @@ -25,17 +25,30 @@ package net.fabricmc.loom.build.mixin; import java.io.File; -import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; import org.gradle.api.Project; -import org.gradle.api.artifacts.Configuration; import org.gradle.api.plugins.JavaPlugin; +import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.compile.JavaCompile; +import net.fabricmc.loom.LoomGradleExtension; +import net.fabricmc.loom.extension.MixinApExtension; + public class JavaApInvoker extends AnnotationProcessorInvoker<JavaCompile> { public JavaApInvoker(Project project) { - super(project, getConfigurations(project), project.getTasks().withType(JavaCompile.class)); + super( + project, + AnnotationProcessorInvoker.getApConfigurations(project, JavaApInvoker::getAptConfigurationName), + getInvokerTasks(project)); + } + + private static Map<SourceSet, JavaCompile> getInvokerTasks(Project project) { + MixinApExtension mixin = LoomGradleExtension.get(project).getMixinApExtension(); + return mixin.getInvokerTasksStream(AnnotationProcessorInvoker.JAVA) + .collect(Collectors.toMap(Map.Entry::getKey, entry -> Objects.requireNonNull((JavaCompile) entry.getValue()))); } @Override @@ -44,18 +57,10 @@ public class JavaApInvoker extends AnnotationProcessorInvoker<JavaCompile> { } @Override - protected File getDestinationDir(JavaCompile task) { + protected File getRefmapDestinationDir(JavaCompile task) { return task.getDestinationDir(); } - private static List<Configuration> getConfigurations(Project project) { - // java plugin generates an AP configuration for every source set based off of the getAptConfigurationName method. - return AnnotationProcessorInvoker.getNonTestSourceSets(project) - .map(sourceSet -> project.getConfigurations() - .getByName(getAptConfigurationName(sourceSet.getName())) - ).collect(Collectors.toList()); - } - private static String getAptConfigurationName(String sourceSet) { // This is documented by the gradle 4.6 release notes https://docs.gradle.org/4.6/release-notes.html#potential-breaking-changes return sourceSet.equals("main") ? JavaPlugin.ANNOTATION_PROCESSOR_CONFIGURATION_NAME : sourceSet + "AnnotationProcessor"; diff --git a/src/main/java/net/fabricmc/loom/build/mixin/KaptApInvoker.java b/src/main/java/net/fabricmc/loom/build/mixin/KaptApInvoker.java index 56324235..3fea3c6e 100644 --- a/src/main/java/net/fabricmc/loom/build/mixin/KaptApInvoker.java +++ b/src/main/java/net/fabricmc/loom/build/mixin/KaptApInvoker.java @@ -29,18 +29,18 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; import kotlin.Unit; import org.gradle.api.Project; -import org.gradle.api.artifacts.Configuration; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.compile.JavaCompile; -import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.gradle.plugin.KaptExtension; import net.fabricmc.loom.LoomGradleExtension; +import net.fabricmc.loom.extension.MixinApExtension; public class KaptApInvoker extends AnnotationProcessorInvoker<JavaCompile> { private final KaptExtension kaptExtension = project.getExtensions().getByType(KaptExtension.class); @@ -48,7 +48,10 @@ public class KaptApInvoker extends AnnotationProcessorInvoker<JavaCompile> { private final File dummyRefmapDirectory; public KaptApInvoker(Project project) { - super(project, getConfigurations(project), project.getTasks().withType(JavaCompile.class)); + super( + project, + AnnotationProcessorInvoker.getApConfigurations(project, KaptApInvoker::getKaptConfigurationName), + getInvokerTasks(project)); try { dummyRefmapDirectory = Files.createTempDirectory("temp_refmap").toFile(); @@ -62,18 +65,26 @@ public class KaptApInvoker extends AnnotationProcessorInvoker<JavaCompile> { kaptExtension.setIncludeCompileClasspath(false); } + private static Map<SourceSet, JavaCompile> getInvokerTasks(Project project) { + MixinApExtension mixin = LoomGradleExtension.get(project).getMixinApExtension(); + return mixin.getInvokerTasksStream(AnnotationProcessorInvoker.JAVA) + .collect(Collectors.toMap(Map.Entry::getKey, entry -> Objects.requireNonNull((JavaCompile) entry.getValue()))); + } + @Override public void configureMixin() { super.configureMixin(); - for (JavaCompile task : invokerTasks) { + for (Map.Entry<SourceSet, JavaCompile> entry : invokerTasks.entrySet()) { // Kapt only allows specifying javac args to all annotation processors at once. So we need to specify some dummy // target location for the refmap and then move it to the correct place for each sourceset + JavaCompile task = entry.getValue(); + SourceSet sourceSet = entry.getKey(); task.doLast(t -> { try { - LoomGradleExtension extension = LoomGradleExtension.get(project); - Path src = Paths.get(getRefmapDestination(task, extension)); - Path dest = Paths.get(task.getDestinationDir().toString(), extension.getRefmapName()); + String refmapName = Objects.requireNonNull(MixinApExtension.getMixinInformationContainer(sourceSet)).getRefmapName(); + Path src = Paths.get(getRefmapDestination(task, refmapName)); + Path dest = Paths.get(task.getDestinationDir().toString(), refmapName); // Possible that no mixin annotations exist if (Files.exists(src)) { @@ -87,15 +98,6 @@ public class KaptApInvoker extends AnnotationProcessorInvoker<JavaCompile> { } } - @NotNull - private static List<Configuration> getConfigurations(Project project) { - // Kapt generates an AP configuration for every source set based off of the getKaptConfigurationName method. - return AnnotationProcessorInvoker.getNonTestSourceSets(project) - .map(sourceSet -> project.getConfigurations() - .getByName(getKaptConfigurationName(sourceSet.getName())) - ).collect(Collectors.toList()); - } - // Pulled out from the internal class: https://github.com/JetBrains/kotlin/blob/33a0ec9b4f40f3d6f1f96b2db504ade4c2fafe03/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/kapt/Kapt3KotlinGradleSubplugin.kt#L92 private static String getKaptConfigurationName(String sourceSetName) { if (!sourceSetName.equals(SourceSet.MAIN_SOURCE_SET_NAME)) { @@ -116,7 +118,7 @@ public class KaptApInvoker extends AnnotationProcessorInvoker<JavaCompile> { } @Override - protected File getDestinationDir(JavaCompile task) { + protected File getRefmapDestinationDir(JavaCompile task) { return dummyRefmapDirectory; } } diff --git a/src/main/java/net/fabricmc/loom/build/mixin/ScalaApInvoker.java b/src/main/java/net/fabricmc/loom/build/mixin/ScalaApInvoker.java index bc77e152..7a3238b7 100644 --- a/src/main/java/net/fabricmc/loom/build/mixin/ScalaApInvoker.java +++ b/src/main/java/net/fabricmc/loom/build/mixin/ScalaApInvoker.java @@ -25,17 +25,31 @@ package net.fabricmc.loom.build.mixin; import java.io.File; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; import com.google.common.collect.ImmutableList; import org.gradle.api.Project; +import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.scala.ScalaCompile; +import net.fabricmc.loom.LoomGradleExtension; +import net.fabricmc.loom.extension.MixinApExtension; + public class ScalaApInvoker extends AnnotationProcessorInvoker<ScalaCompile> { public ScalaApInvoker(Project project) { - super(project, - // Scala just uses the java AP configuration afaik. This of course assumes the java AP also gets configured. - ImmutableList.of(), - project.getTasks().withType(ScalaCompile.class)); + super( + project, + // Scala just uses the java AP configuration afaik. This of course assumes the java AP also gets configured. + ImmutableList.of(), + getInvokerTasks(project)); + } + + private static Map<SourceSet, ScalaCompile> getInvokerTasks(Project project) { + MixinApExtension mixin = LoomGradleExtension.get(project).getMixinApExtension(); + return mixin.getInvokerTasksStream(AnnotationProcessorInvoker.SCALA) + .collect(Collectors.toMap(Map.Entry::getKey, entry -> Objects.requireNonNull((ScalaCompile) entry.getValue()))); } @Override @@ -44,7 +58,7 @@ public class ScalaApInvoker extends AnnotationProcessorInvoker<ScalaCompile> { } @Override - protected File getDestinationDir(ScalaCompile task) { + protected File getRefmapDestinationDir(ScalaCompile task) { return task.getDestinationDir(); } } |