Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

Commit

Permalink
[3/?] Improved task system: Merged run configs with Tasks
Browse files Browse the repository at this point in the history
There are a few features that got nullified with this refractor, but I guess
it is time to see whether we really needed those features or not
  • Loading branch information
Geolykt committed Jul 10, 2022
1 parent 3e99149 commit 107a550
Show file tree
Hide file tree
Showing 17 changed files with 482 additions and 231 deletions.
2 changes: 1 addition & 1 deletion brachyura/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.coolcrabs</groupId>
<artifactId>brachyura</artifactId>
<version>0.90.2</version>
<version>0.90.3</version>

<properties>
<java.version>1.8</java.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -23,6 +24,7 @@
import io.github.coolcrabs.brachyura.processing.ProcessingEntry;
import io.github.coolcrabs.brachyura.processing.ProcessingSource;
import io.github.coolcrabs.brachyura.processing.sources.ProcessingSponge;
import io.github.coolcrabs.brachyura.project.TaskBuilder;
import io.github.coolcrabs.brachyura.project.java.BuildModule;
import io.github.coolcrabs.brachyura.util.AtomicFile;
import io.github.coolcrabs.brachyura.util.JvmUtil;
Expand Down Expand Up @@ -132,25 +134,23 @@ public IdeModule ideModule() {
.sourcePaths(getSrcDirs())
.resourcePaths(getResourceDirs())
.dependencyModules(getModuleDependencies().stream().map(BuildModule::ideModule).collect(Collectors.toList()))
.runConfigs(
new IdeModule.RunConfigBuilder()
.name("Minecraft Client")
.cwd(cwd)
.mainClass("net.fabricmc.loader.launch.knot.KnotClient")
.classpath(classpath)
.resourcePaths(getResourceDirs())
.vmArgs(() -> this.ideVmArgs(true))
.args(() -> this.ideArgs(true)),
new IdeModule.RunConfigBuilder()
.name("Minecraft Server")
.cwd(cwd)
.mainClass("net.fabricmc.loader.launch.knot.KnotServer")
.classpath(classpath)
.resourcePaths(getResourceDirs())
.vmArgs(() -> this.ideVmArgs(false))
.args(() -> this.ideArgs(false))
)
.build();
.addTask(new TaskBuilder("Minecraft Client")
.withWorkingDirectory(cwd)
.withMainClass("net.fabricmc.loader.launch.knot.KnotClient")
.withClasspath(classpath.get())
.withResourcePath(Arrays.asList(getResourceDirs()))
.withVMArgs(this.ideArgs(true))
.withVMArgs(this.ideArgs(true))
.buildUnconditionallyThrowing())
.addTask(new TaskBuilder("Minecraft Server")
.withWorkingDirectory(cwd)
.withMainClass("net.fabricmc.loader.launch.knot.KnotServer")
.withClasspath(classpath.get())
.withResourcePath(Arrays.asList(getResourceDirs()))
.withVMArgs(this.ideArgs(false))
.withVMArgs(this.ideArgs(false))
.buildUnconditionallyThrowing())
.build();
}

public Path writeMappings4FabricStuff() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public String getModuleName() {
}

@Override
@NotNull
public Path getModuleRoot() {
return getProjectDir();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.google.gson.stream.JsonWriter;

import io.github.coolcrabs.brachyura.dependency.JavaJarDependency;
import io.github.coolcrabs.brachyura.ide.IdeModule.RunConfig;
import io.github.coolcrabs.brachyura.project.Task;
import io.github.coolcrabs.brachyura.util.AtomicFile;
import io.github.coolcrabs.brachyura.util.JvmUtil;
import io.github.coolcrabs.brachyura.util.PathUtil;
Expand Down Expand Up @@ -183,14 +183,17 @@ void writeClasspath(IdeModule project) throws IOException, XMLStreamException {

void writeLaunchConfigs(Path projectDir, IdeModule ideProject) throws IOException, XMLStreamException {
try {
for (RunConfig rc : ideProject.runConfigs) {
if (rc.name.equals("idea")
|| rc.name.equals("netbeans")
|| rc.name.equals("publish")
|| rc.name.equals("publishToMavenLocal")) {
for (Task task : ideProject.tasks) {

// TODO use a proper system for that
if (task.name.equals("idea")
|| task.name.equals("netbeans")
|| task.name.equals("publish")
|| task.name.equals("publishToMavenLocal")) {
continue;
}
String rcname = ideProject.name + " - " + rc.name;

String rcname = ideProject.name + " - " + task.getName();
try (FormattedXMLStreamWriter w = XmlUtil.newStreamWriter(Files.newBufferedWriter(projectDir.resolve(rcname + ".launch")))) {
w.writeStartDocument("UTF-8", "1.0");
w.writeStartElement("launchConfiguration");
Expand Down Expand Up @@ -228,16 +231,17 @@ void writeLaunchConfigs(Path projectDir, IdeModule ideProject) throws IOExceptio
w.writeStartElement("listAttribute");
w.writeAttribute("key", "org.eclipse.jdt.launching.CLASSPATH");
w.indent();
List<Path> cp = new ArrayList<>(rc.classpath.get());
cp.addAll(rc.resourcePaths);
List<Path> cp = new ArrayList<>(task.getIdeRunConfigClasspath());
cp.addAll(task.getIdeRunConfigResourcepath());
for (Path p : cp) {
w.newline();
w.writeEmptyElement("listEntry");
w.writeAttribute("value", libraryValue(p));
}
List<IdeModule> modules = new ArrayList<>();
modules.add(ideProject);
modules.addAll(rc.additionalModulesClasspath);
// TODO Replace - if needed
// modules.addAll(rc.additionalModulesClasspath);
for (IdeModule mod : modules) {
w.newline();
w.writeEmptyElement("listEntry");
Expand All @@ -249,10 +253,10 @@ void writeLaunchConfigs(Path projectDir, IdeModule ideProject) throws IOExceptio
w.newline();
booleanAttribute(w, "org.eclipse.jdt.launching.DEFAULT_CLASSPATH", false);
w.newline();
stringAttribute(w, "org.eclipse.jdt.launching.MAIN_TYPE", rc.mainClass);
stringAttribute(w, "org.eclipse.jdt.launching.MAIN_TYPE", task.getIdeRunConfigMainClass());
w.newline();
StringBuilder args = new StringBuilder();
for (String arg : rc.args.get()) {
for (String arg : task.getIdeRunConfigArgs()) {
args.append(quote(arg));
args.append(' ');
}
Expand All @@ -261,13 +265,13 @@ void writeLaunchConfigs(Path projectDir, IdeModule ideProject) throws IOExceptio
stringAttribute(w, "org.eclipse.jdt.launching.PROJECT_ATTR", ideProject.name);
w.newline();
StringBuilder vmargs = new StringBuilder();
for (String vmarg : rc.vmArgs.get()) {
for (String vmarg : task.getIdeRunConfigVMArgs()) {
vmargs.append(quote(vmarg));
vmargs.append(' ');
}
stringAttribute(w, "org.eclipse.jdt.launching.VM_ARGUMENTS", vmargs.toString());
w.newline();
stringAttribute(w, "org.eclipse.jdt.launching.WORKING_DIRECTORY", rc.cwd.toString());
stringAttribute(w, "org.eclipse.jdt.launching.WORKING_DIRECTORY", task.getIdeRunConfigWorkingDir().toString());
w.unindent();
w.newline();
w.writeEndElement();
Expand Down Expand Up @@ -375,23 +379,23 @@ void vscodeLaunchJson(Path rootDir, IdeModule... basemodules) throws IOException
jsonWriter.name("configurations");
jsonWriter.beginArray();
for (IdeModule mod : basemodules) {
for (RunConfig runConfig : mod.runConfigs) {
for (Task task : mod.tasks) {
jsonWriter.beginObject();
jsonWriter.name("type").value("java");
jsonWriter.name("name").value(mod.name + " - " + runConfig.name);
jsonWriter.name("name").value(mod.name + " - " + task.getName());
jsonWriter.name("request").value("launch");
jsonWriter.name("cwd").value(runConfig.cwd.toString());
jsonWriter.name("cwd").value(task.getIdeRunConfigWorkingDir().toString());
jsonWriter.name("console").value("internalConsole");
jsonWriter.name("mainClass").value(runConfig.mainClass);
jsonWriter.name("mainClass").value(task.getIdeRunConfigMainClass());
jsonWriter.name("vmArgs");
jsonWriter.beginArray();
for (String vmArg : runConfig.vmArgs.get()) {
for (String vmArg : task.getIdeRunConfigVMArgs()) {
jsonWriter.value(vmArg);
}
jsonWriter.endArray();
jsonWriter.name("args");
jsonWriter.beginArray();
for (String arg : runConfig.args.get()) {
for (String arg : task.getIdeRunConfigArgs()) {
jsonWriter.value(arg);
}
jsonWriter.endArray();
Expand All @@ -403,10 +407,10 @@ void vscodeLaunchJson(Path rootDir, IdeModule... basemodules) throws IOException
for (IdeModule m : mod.dependencyModules) {
jsonWriter.value(m.root.resolve(".brachyura").resolve("eclipseout").toString());
}
for (Path path : runConfig.resourcePaths) {
for (Path path : task.getIdeRunConfigResourcepath()) {
jsonWriter.value(path.toString());
}
for (Path path : runConfig.classpath.get()) {
for (Path path : task.getIdeRunConfigClasspath()) {
jsonWriter.value(path.toString());
}
jsonWriter.endArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import org.jetbrains.annotations.NotNull;

import io.github.coolcrabs.brachyura.ide.IdeModule.RunConfig;

public interface Ide {
public static Ide[] getIdes() {
return new Ide[] {
Expand All @@ -25,18 +23,15 @@ static void validate(IdeModule... ideModules) {
for (IdeModule m1 : m0.dependencyModules) {
if (!modules.contains(m1)) throw new IllegalArgumentException("Module " + m0.name + " references module " + m1.name + " not in ide project as a dependency");
}
/* TODO Slbrachyura: Replace if needed
for (RunConfig rc : m0.runConfigs) {
for (IdeModule m1 : rc.additionalModulesClasspath) {
if (!modules.contains(m1)) throw new IllegalArgumentException("Module " + m0.name + " references module " + m1.name + " not in ide project in a run config");
}
}
// Fail early for lazies
*/
// Fail early for lazy
m0.dependencies.get();
for (RunConfig rc : m0.runConfigs) {
rc.vmArgs.get();
rc.args.get();
rc.classpath.get();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import java.util.Objects;
import java.util.function.Supplier;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import io.github.coolcrabs.brachyura.dependency.JavaJarDependency;
import io.github.coolcrabs.brachyura.project.Task;
import io.github.coolcrabs.brachyura.project.TaskBuilder;
import io.github.coolcrabs.brachyura.util.Lazy;

public class IdeModule {
Expand All @@ -20,30 +23,24 @@ public class IdeModule {
public final Path root;
public final Lazy<@NotNull List<JavaJarDependency>> dependencies;
public final List<IdeModule> dependencyModules;

@Deprecated // Slbrachyura: Improved task system
public final List<RunConfig> runConfigs;

public final List<@NotNull Task> tasks; // Slbrachyura: Improved task system
public final List<@NotNull Path> sourcePaths;
public final List<@NotNull Path> resourcePaths;
public final List<@NotNull Path> testSourcePaths;
public final List<@NotNull Path> testResourcePaths;
public final int javaVersion;

IdeModule(@NotNull String name, @NotNull Path root, Supplier<@NotNull List<JavaJarDependency>> dependencies, List<IdeModule> dependencyModules, List<RunConfigBuilder> runConfigs, List<@NotNull Path> sourcePaths, List<@NotNull Path> resourcePaths, List<@NotNull Path> testSourcePaths, List<@NotNull Path> testResourcePaths, int javaVersion) {
IdeModule(@NotNull String name, @NotNull Path root, Supplier<@NotNull List<JavaJarDependency>> dependencies, List<IdeModule> dependencyModules, List<@NotNull Path> sourcePaths, List<@NotNull Path> resourcePaths, List<@NotNull Path> testSourcePaths, List<@NotNull Path> testResourcePaths, int javaVersion, List<@NotNull Task> tasks) {
this.name = name;
this.root = root;
this.dependencies = new Lazy<>(dependencies);
this.dependencyModules = dependencyModules;
this.runConfigs = new ArrayList<>(runConfigs.size());
for (RunConfigBuilder b : runConfigs) {
this.runConfigs.add(b.build(this));
}
this.sourcePaths = sourcePaths;
this.resourcePaths = resourcePaths;
this.testSourcePaths = testSourcePaths;
this.testResourcePaths = testResourcePaths;
this.javaVersion = javaVersion;
this.tasks = tasks;
}

public static class IdeModuleBuilder {
Expand All @@ -54,12 +51,23 @@ public static class IdeModuleBuilder {
private List<IdeModule> dependencyModules = Collections.emptyList();
@Deprecated // Slbrachyura: Improved tasks system
private List<RunConfigBuilder> runConfigs = Collections.emptyList();
private List<@NotNull Task> tasks = Collections.emptyList();
private List<@NotNull Path> sourcePaths = Collections.emptyList();
private List<@NotNull Path> resourcePaths = Collections.emptyList();
private List<@NotNull Path> testSourcePaths = Collections.emptyList();
private List<@NotNull Path> testResourcePaths = Collections.emptyList();
private int javaVersion = 8;


@Contract(pure = false, mutates = "this", value = "_ -> this")
@NotNull
public IdeModuleBuilder addTask(@NotNull Task task) {
if (tasks.getClass() != ArrayList.class) {
tasks = new ArrayList<>(tasks); // Ensure that it is mutable
}
tasks.add(task);
return this;
}

public IdeModuleBuilder name(String name) {
this.name = name;
return this;
Expand Down Expand Up @@ -108,6 +116,13 @@ public IdeModuleBuilder runConfigs(RunConfigBuilder... runConfigs) {
return this;
}

@Contract(pure = false, mutates = "this", value = "_ -> this")
@NotNull
public IdeModuleBuilder withTasks(List<@NotNull Task> tasks) {
this.tasks = tasks;
return this;
}

public IdeModuleBuilder sourcePaths(List<@NotNull Path> sourcePaths) {
this.sourcePaths = sourcePaths;
return this;
Expand Down Expand Up @@ -171,30 +186,11 @@ public IdeModuleBuilder javaVersion(int javaVersion) {
public IdeModule build() {
Objects.requireNonNull(name, "IdeModule missing name");
Objects.requireNonNull(root, "IdeModule missing root");
return new IdeModule(name, root, dependencies, dependencyModules, runConfigs, sourcePaths, resourcePaths, testSourcePaths, testResourcePaths, javaVersion);
}
}

@Deprecated // Slbrachyura: Improved tasks system
public class RunConfig {
public final String name;
public final String mainClass;
public final Path cwd; // Make sure this exists
public final Lazy<List<String>> vmArgs;
public final Lazy<List<String>> args;
public final Lazy<List<Path>> classpath;
public final List<IdeModule> additionalModulesClasspath;
public final List<Path> resourcePaths;

RunConfig(String name, String mainClass, Path cwd, Supplier<List<String>> vmArgs, Supplier<List<String>> args, Supplier<List<Path>> classpath, List<IdeModule> additionalModulesClasspath, List<Path> resourcePaths) {
this.name = name;
this.mainClass = mainClass;
this.cwd = cwd;
this.vmArgs = new Lazy<>(vmArgs);
this.args = new Lazy<>(args);
this.classpath = new Lazy<>(classpath);
this.additionalModulesClasspath = additionalModulesClasspath;
this.resourcePaths = resourcePaths;
List<@NotNull Task> tasks = new ArrayList<>(this.tasks);
this.runConfigs.forEach(rcBuilder -> {
tasks.add(rcBuilder.build());
});
return new IdeModule(name, root, dependencies, dependencyModules, sourcePaths, resourcePaths, testSourcePaths, testResourcePaths, javaVersion, tasks);
}
}

Expand All @@ -206,6 +202,7 @@ public static class RunConfigBuilder {
private Supplier<List<String>> vmArgs = Collections::emptyList;
private Supplier<List<String>> args = Collections::emptyList;
private Supplier<List<Path>> classpath = Collections::emptyList;
@Deprecated @SuppressWarnings("unused") // Slbrachyura: Does anyone know what this does? I do not
private List<IdeModule> additionalModulesClasspath = Collections.emptyList();
private List<Path> resourcePaths = Collections.emptyList();

Expand Down Expand Up @@ -289,11 +286,19 @@ public RunConfigBuilder resourcePaths(Path... resourcePaths) {
return this;
}

RunConfig build(IdeModule project) {
@SuppressWarnings("null")
Task build() {
Objects.requireNonNull(name, "Null name");
Objects.requireNonNull(mainClass, "Null mainClass");
Objects.requireNonNull(cwd, "Null cwd");
return project.new RunConfig(name, mainClass, cwd, vmArgs, args, classpath, additionalModulesClasspath, resourcePaths);
return new TaskBuilder(name)
.withMainClass(mainClass)
.withWorkingDirectory(cwd)
.withVMArgs(vmArgs.get())
.withArgs(args.get())
.withClasspath(classpath.get())
.withResourcePath(resourcePaths)
.buildUnconditionallyThrowing();
}
}
}
Loading

0 comments on commit 107a550

Please sign in to comment.