diff --git a/buildSrc/src/main/groovy/mmc/MindustryModCoreExtension.java b/buildSrc/src/main/groovy/mmc/MindustryModCoreExtension.java index deee65a..f99b03d 100644 --- a/buildSrc/src/main/groovy/mmc/MindustryModCoreExtension.java +++ b/buildSrc/src/main/groovy/mmc/MindustryModCoreExtension.java @@ -7,34 +7,38 @@ import org.gradle.api.tasks.*; public class MindustryModCoreExtension implements - AbstractExtension, - AddKaptAnnotationsExtensions, - AddArcLibraryExtension, - SetupAnnotationProjectExtension, - AddMindustryCoreExtension, - SetupSpriteGenerationTask, - JarMindustryTaskExtension{ + AbstractExtension, + AddKaptAnnotationsExtensions, + AddArcLibraryExtension, + SetupAnnotationProjectExtension, + AddMindustryCoreExtension, + SetupSpriteGenerationTask, + JarMindustryTaskExtension, + SetupDefaultJarTasks { public final Project project; @Getter(onMethod_ = {@Input}) private final ProjectInfo projectInfo; - public MindustryModCoreExtension(Project project){ + public MindustryModCoreExtension(Project project) { this.project = project; ObjectFactory objects = project.getObjects(); projectInfo = new ProjectInfo(objects); } + public ProjectInfo getProjectInto() { + return projectInfo; + } @SuppressWarnings("rawtypes") @Input - public void projectInfo(Action closure){ + public void projectInfo(Action closure) { closure.execute(projectInfo); // project.configure(projectInfo,closure); } @Override - public Project getProject(){ + public Project getProject() { return project; } } diff --git a/buildSrc/src/main/groovy/mmc/MindustryModGradle.java b/buildSrc/src/main/groovy/mmc/MindustryModGradle.java index 3c46073..7948cd7 100644 --- a/buildSrc/src/main/groovy/mmc/MindustryModGradle.java +++ b/buildSrc/src/main/groovy/mmc/MindustryModGradle.java @@ -11,10 +11,7 @@ import static mmc.utils.MainUtils.findVersion; public class MindustryModGradle implements Plugin { - public static MavenArtifactRepository githubRepo(String user, String name) { - String it = "method for documentation"; - return githubRepo(null, user, name); - } + public static MavenArtifactRepository githubRepo(RepositoryHandler handler, String user, String repo) { return handler.maven(it1 -> { diff --git a/buildSrc/src/main/groovy/mmc/extentions/SetupDefaultJarTasks.java b/buildSrc/src/main/groovy/mmc/extentions/SetupDefaultJarTasks.java new file mode 100644 index 0000000..cc95e62 --- /dev/null +++ b/buildSrc/src/main/groovy/mmc/extentions/SetupDefaultJarTasks.java @@ -0,0 +1,36 @@ +package mmc.extentions; + +import mmc.MindustryModCoreExtension; +import mmc.extentions.defaultjar.DeployTask; +import mmc.extentions.defaultjar.JarAndroidTask; +import org.gradle.api.Project; +import org.gradle.api.tasks.AbstractCopyTask; +import org.gradle.api.tasks.TaskContainer; + +public interface SetupDefaultJarTasks extends AbstractExtension { + default void setupDefaultEnvironment(Project target) { + TaskContainer tasks = getProject().getTasks(); + + + AbstractCopyTask jar = (AbstractCopyTask) tasks.getByName("jar"); + MindustryModCoreExtension extension = target.getExtensions().getByType(MindustryModCoreExtension.class); + jar.exclude(extension.getProjectInto().rootPackage.get()+"/entities/comp/**"); + + + tasks.register("androidJar", JarAndroidTask.class,it->{ + it.setGroup("build"); + it.dependsOn("jar"); + }); + tasks.register("deploy", DeployTask.class, it->{ + it.setGroup("build"); + it.dependsOn("jar"); + it.dependsOn("androidJar"); + }); + + + } + + default void setupDefaultEnvironment() { + setupDefaultEnvironment(getProject()); + } +} diff --git a/buildSrc/src/main/groovy/mmc/extentions/defaultjar/DeployTask.java b/buildSrc/src/main/groovy/mmc/extentions/defaultjar/DeployTask.java new file mode 100644 index 0000000..0f52948 --- /dev/null +++ b/buildSrc/src/main/groovy/mmc/extentions/defaultjar/DeployTask.java @@ -0,0 +1,30 @@ +package mmc.extentions.defaultjar; + + +import org.gradle.api.Project; +import org.gradle.api.Task; +import org.gradle.api.tasks.AbstractCopyTask; +import org.gradle.api.tasks.TaskAction; +import org.gradle.api.tasks.TaskContainer; +import org.gradle.api.tasks.bundling.Zip; +import org.gradle.jvm.tasks.Jar; + +public class DeployTask extends Zip { + @TaskAction + public void run(){ + Project project = getProject(); + TaskContainer tasks = project.getTasks(); + Zip jar = (Zip) tasks.getByPath("jar"); + Zip androidJar = (Zip) tasks.getByPath("androidJar"); + + + from(project.zipTree(jar.getArchiveFile().get())); + from(project.zipTree(androidJar.getArchiveFile().get())); + doLast(it->{ + project.delete(del->{ + del.delete(jar.getArchiveFile(),androidJar.getArchiveFile()); + }); + }); + } + +} diff --git a/buildSrc/src/main/groovy/mmc/extentions/defaultjar/JarAndroidTask.java b/buildSrc/src/main/groovy/mmc/extentions/defaultjar/JarAndroidTask.java new file mode 100644 index 0000000..c92cf43 --- /dev/null +++ b/buildSrc/src/main/groovy/mmc/extentions/defaultjar/JarAndroidTask.java @@ -0,0 +1,62 @@ +package mmc.extentions.defaultjar; + +import arc.struct.Seq; +import arc.struct.Sort; +import arc.util.Strings; +import arc.util.Structs; +import org.codehaus.groovy.runtime.ProcessGroovyMethods; +import org.gradle.api.DefaultTask; +import org.gradle.api.GradleException; +import org.gradle.api.tasks.TaskAction; +import org.gradle.api.tasks.TaskContainer; +import org.gradle.api.tasks.bundling.Zip; +import org.gradle.jvm.tasks.Jar; + +import java.io.File; +import java.io.FileDescriptor; +import java.io.IOException; +import java.io.OutputStream; +import java.sql.Struct; +import java.util.ArrayList; +import java.util.Comparator; + +public class JarAndroidTask extends Zip { + + @TaskAction + public void run() throws IOException, InterruptedException { + + String sdkRoot = System.getenv("ANDROID_HOME"); + if (sdkRoot == null) { + sdkRoot = System.getenv("ANDROID_SDK_ROOT"); + } + if (sdkRoot == null || !new File(sdkRoot).exists()) + throw new GradleException("No valid Android SDK found. Ensure that ANDROID_HOME is set to your Android SDK directory."); + + File[] files = new File("$sdkRoot/platforms/").listFiles(); + Sort.instance().sort(files, Comparator.reverseOrder()); + assert files != null; + File platformRoot = Structs.find(files, f -> new File(f, "android.jar").exists()); + + if (platformRoot == null) + throw new GradleException("No android.jar found. Ensure that you have an Android platform installed."); + + Seq files1 = new Seq<>(); + files1.add(new File(platformRoot, "android.jar")); + getProject().getConfigurations().getByName("compileClasspath").forEach(files1::add); + getProject().getConfigurations().getByName("runtimeClasspath").forEach(files1::add); + //collect dependencies needed for desugaring + String dependencies = files1.toString(" ", it -> "--classpath " + it.getPath()); + + String androidName = getArchiveBaseName().getOrElse(getProject().getName() + "Android.jar"); + TaskContainer tasks = getProject().getTasks(); + + String desktopName = ((Zip) tasks.getByName("jar")).getArchiveBaseName().getOrElse(getProject().getName() + "Desktop.jar"); + ProcessBuilder builder = new ProcessBuilder(); + //dex and desugar files - this requires d8 in your PATH + builder.command("d8 " + dependencies + " --min-api 14 --output " + androidName + " " + desktopName); + builder.directory(new File(getProject().getBuildDir(), "libs")); + ProcessGroovyMethods.waitForProcessOutput(builder.start(),(OutputStream) System.out, System.err); + + + } +}