Skip to content

Commit

Permalink
Added setupDefaultEnvironment()
Browse files Browse the repository at this point in the history
  • Loading branch information
Zelaux committed Jun 16, 2024
1 parent e42050d commit 2f90fe9
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 14 deletions.
24 changes: 14 additions & 10 deletions buildSrc/src/main/groovy/mmc/MindustryModCoreExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProjectInfo> closure){
public void projectInfo(Action<ProjectInfo> closure) {
closure.execute(projectInfo);
// project.configure(projectInfo,closure);
}


@Override
public Project getProject(){
public Project getProject() {
return project;
}
}
5 changes: 1 addition & 4 deletions buildSrc/src/main/groovy/mmc/MindustryModGradle.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import static mmc.utils.MainUtils.findVersion;

public class MindustryModGradle implements Plugin<Project> {
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 -> {
Expand Down
36 changes: 36 additions & 0 deletions buildSrc/src/main/groovy/mmc/extentions/SetupDefaultJarTasks.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
30 changes: 30 additions & 0 deletions buildSrc/src/main/groovy/mmc/extentions/defaultjar/DeployTask.java
Original file line number Diff line number Diff line change
@@ -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());
});
});
}

}
Original file line number Diff line number Diff line change
@@ -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<File> 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);


}
}

0 comments on commit 2f90fe9

Please sign in to comment.