|
| 1 | +package org.minimallycorrect.mixinplugin; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import lombok.AllArgsConstructor; |
| 8 | +import lombok.NonNull; |
| 9 | +import lombok.val; |
| 10 | + |
| 11 | +import org.gradle.api.artifacts.Configuration; |
| 12 | +import org.gradle.api.artifacts.Dependency; |
| 13 | +import org.gradle.api.artifacts.DependencySet; |
| 14 | +import org.gradle.api.artifacts.component.ModuleComponentIdentifier; |
| 15 | +import org.gradle.api.artifacts.dsl.DependencyHandler; |
| 16 | +import org.gradle.api.artifacts.query.ArtifactResolutionQuery; |
| 17 | +import org.gradle.api.artifacts.result.ResolvedArtifactResult; |
| 18 | +import org.gradle.api.tasks.Internal; |
| 19 | +import org.gradle.api.tasks.Nested; |
| 20 | +import org.gradle.api.tasks.OutputDirectory; |
| 21 | +import org.gradle.maven.MavenModule; |
| 22 | +import org.gradle.maven.MavenPomArtifact; |
| 23 | + |
| 24 | +import org.minimallycorrect.mixinplugin.deps.MModuleComponentIdentifier; |
| 25 | + |
| 26 | +@AllArgsConstructor |
| 27 | +public class ApplyMixinsRepo { |
| 28 | + private ApplyMixins applyMixins; |
| 29 | + |
| 30 | + @NonNull |
| 31 | + public File repo; |
| 32 | + |
| 33 | + @NonNull |
| 34 | + private transient Configuration mixinConfiguration; |
| 35 | + |
| 36 | + @Nested |
| 37 | + public ApplyMixins getApplyMixins() { |
| 38 | + return this.applyMixins; |
| 39 | + } |
| 40 | + |
| 41 | + @NonNull |
| 42 | + @OutputDirectory |
| 43 | + public File getRepo() { |
| 44 | + return this.repo; |
| 45 | + } |
| 46 | + |
| 47 | + @Internal |
| 48 | + @NonNull |
| 49 | + public final Map<Dependency, File> getOutputDependencyFiles() { |
| 50 | + val result = new HashMap<Dependency, File>(); |
| 51 | + for (Dependency dependency : mixinConfiguration.getDependencies()) { |
| 52 | + result.put(dependency, new File(repo, getMavenPath(dependency.getGroup(), dependency.getName(), dependency.getVersion() + '-' + getStage()) + ".jar")); |
| 53 | + } |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + @Internal |
| 58 | + @NonNull |
| 59 | + public String getStage() { |
| 60 | + val type = applyMixins.getApplicationType().get(); |
| 61 | + switch (type) { |
| 62 | + case PRE_PATCH: |
| 63 | + return "pre"; |
| 64 | + case FINAL_PATCH: |
| 65 | + return "final"; |
| 66 | + } |
| 67 | + throw new IllegalStateException("Unexpected value: " + type); |
| 68 | + } |
| 69 | + |
| 70 | + @SuppressWarnings("unchecked") |
| 71 | + public void remapMixinArtifacts(@NonNull DependencyHandler dependencyHandler) { |
| 72 | + val applicator = applyMixins.makeApplicator(); |
| 73 | + val config = mixinConfiguration; |
| 74 | + config.resolve(); |
| 75 | + val resolved = config.getResolvedConfiguration(); |
| 76 | + val stage = getStage(); |
| 77 | + resolved.rethrowFailure(); |
| 78 | + |
| 79 | + for (val artifact : resolved.getResolvedArtifacts()) { |
| 80 | + val id = artifact.getId().getComponentIdentifier(); |
| 81 | + if (id instanceof ModuleComponentIdentifier) { |
| 82 | + val mcid = (ModuleComponentIdentifier) id; |
| 83 | + val output = new File(repo, getMavenPath(mcid.getGroup(), mcid.getModule(), mcid.getVersion() + '-' + stage) + ".jar"); |
| 84 | + output.getParentFile().mkdirs(); |
| 85 | + applicator.getMixinTransformer().transform(artifact.getFile().toPath(), output.toPath()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + ArtifactResolutionQuery query = artifactResolution(dependencyHandler, mixinConfiguration.getDependencies()); |
| 90 | + query = query.withArtifacts(MavenModule.class, MavenPomArtifact.class); |
| 91 | + val result = query.execute(); |
| 92 | + for (val resolvedComponent : result.getResolvedComponents()) { |
| 93 | + val id = resolvedComponent.getId(); |
| 94 | + if (id instanceof ModuleComponentIdentifier) { |
| 95 | + val mcid = (ModuleComponentIdentifier) id; |
| 96 | + for (val artifact : resolvedComponent.getArtifacts(MavenPomArtifact.class)) { |
| 97 | + if (artifact instanceof ResolvedArtifactResult) { |
| 98 | + Utils.setPomRootVal(((ResolvedArtifactResult) artifact).getFile(), new File(repo, getMavenPath(mcid.getGroup(), mcid.getModule(), mcid.getVersion() + '-' + stage) + ".pom"), "version", ((ModuleComponentIdentifier) id).getVersion() + '-' + stage); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private static ArtifactResolutionQuery artifactResolution(DependencyHandler dependencyHandler, DependencySet dependencies) { |
| 106 | + ArtifactResolutionQuery query = dependencyHandler.createArtifactResolutionQuery(); |
| 107 | + for (val target : dependencies) { |
| 108 | + query = query.forComponents(new MModuleComponentIdentifier(target.getGroup(), target.getName(), target.getVersion())); |
| 109 | + } |
| 110 | + return query; |
| 111 | + } |
| 112 | + |
| 113 | + private static String getMavenPath(String group, String name, String version) { |
| 114 | + return group.replace('.', '/') + '/' + name + '/' + version + '/' + name + '-' + version; |
| 115 | + } |
| 116 | +} |
0 commit comments