|
| 1 | +package dev.skidfuscator.gradle; |
| 2 | + |
| 3 | +import dev.skidfuscator.gradle.util.MiscUtil; |
| 4 | +import org.apache.commons.io.FilenameUtils; |
| 5 | +import org.gradle.api.Plugin; |
| 6 | +import org.gradle.api.Project; |
| 7 | +import org.gradle.api.plugins.GroovyPlugin; |
| 8 | +import org.gradle.api.plugins.JavaPlugin; |
| 9 | +import org.gradle.api.plugins.JavaPluginExtension; |
| 10 | +import org.gradle.api.plugins.scala.ScalaPlugin; |
| 11 | +import org.gradle.api.tasks.SourceSetContainer; |
| 12 | +import org.gradle.api.tasks.compile.AbstractCompile; |
| 13 | +import org.gradle.jvm.tasks.Jar; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class SkidfuscatorGradlePlugin implements Plugin<Project> { |
| 20 | + |
| 21 | + private List<File> classpath; |
| 22 | + private SourceSetContainer sourceSets; |
| 23 | + private int jvm; |
| 24 | + |
| 25 | + @Override |
| 26 | + public void apply(Project project) { |
| 27 | + SkidfuscatorExtension extension = project.getExtensions().create("skidfuscator", SkidfuscatorExtension.class); |
| 28 | + |
| 29 | + this.sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets(); |
| 30 | + this.classpath = new ArrayList<>(); |
| 31 | + this.jvm = 8; |
| 32 | + project.afterEvaluate(p -> this.handle(p, extension)); |
| 33 | + } |
| 34 | + |
| 35 | + private void handle(Project project, SkidfuscatorExtension extension) { |
| 36 | + // read jvm and classpath information directly from language's compile task |
| 37 | + project.getPlugins().withType(JavaPlugin.class, plugin -> this.handlePlugin(project, "java")); |
| 38 | + project.getPlugins().withType(GroovyPlugin.class, plugin -> this.handlePlugin(project, "groovy")); |
| 39 | + project.getPlugins().withType(ScalaPlugin.class, plugin -> this.handlePlugin(project, "scala")); |
| 40 | + project.getPlugins().withId("org.jetbrains.kotlin.jvm", plugin -> this.handlePlugin(project, "kotlin")); |
| 41 | + // compute session and obfuscate right after jar task |
| 42 | + project.getTasks().withType(Jar.class, jar -> { |
| 43 | + final String home = System.getProperty("java.home"); |
| 44 | + final File javaRuntime; |
| 45 | + final File exemption; |
| 46 | + |
| 47 | + if (extension.getRuntime().isPresent()) { |
| 48 | + javaRuntime = extension.getRuntime().get().getAsFile(); |
| 49 | + } else { |
| 50 | + javaRuntime = new File( |
| 51 | + home, |
| 52 | + MiscUtil.getJavaVersion() > 8 |
| 53 | + ? "jmods" |
| 54 | + : "lib/rt.jar" |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + if (extension.getExemptionFile().isPresent()) { |
| 59 | + exemption = extension.getExemptionFile().get().getAsFile(); |
| 60 | + } else { |
| 61 | + exemption = null; |
| 62 | + } |
| 63 | + |
| 64 | + File input = jar.getArchiveFile().get().getAsFile(); |
| 65 | + File directory = input.getParentFile(); |
| 66 | + |
| 67 | + File output = new File(directory, FilenameUtils.getName(input.getName()) + extension.getClassifier().get() + FilenameUtils.getExtension(input.getName())); |
| 68 | + SkidfuscatorSpec spec = SkidfuscatorSpec.builder() |
| 69 | + .input(input) |
| 70 | + .output(output) |
| 71 | + .fuckit(extension.getFuckit().get()) |
| 72 | + .phantom(extension.getPhantom().get()) |
| 73 | + .analytics(!extension.getNotrack().get()) |
| 74 | + .runtime(javaRuntime) |
| 75 | + .exempt(exemption) |
| 76 | + .jmod(this.jvm > 8) |
| 77 | + .libs(this.classpath.toArray(new File[0])) |
| 78 | + .excludes(extension.getExcludes().getOrNull()) |
| 79 | + .build(); |
| 80 | + SkidfuscatorRuntime runtime = new SkidfuscatorRuntime(project, extension.getVersion().get()); |
| 81 | + SkidfuscatorCompileAction action = project.getObjects().newInstance( |
| 82 | + SkidfuscatorCompileAction.class, |
| 83 | + spec, |
| 84 | + runtime |
| 85 | + ); |
| 86 | + |
| 87 | + jar.doLast("skidfuscator", action); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + private void handlePlugin(Project project, String language) { |
| 92 | + sourceSets.all(sourceSet -> { |
| 93 | + project.getTasks().named(sourceSet.getCompileTaskName(language), AbstractCompile.class, compile -> { |
| 94 | + this.jvm = MiscUtil.decodeJvmVersion(compile.getTargetCompatibility()); |
| 95 | + this.classpath.addAll(compile.getClasspath().getFiles()); |
| 96 | + }); |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments