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

Commit

Permalink
[1/?] Improved task system: Purge Tasks.java
Browse files Browse the repository at this point in the history
Next step would be to get rid of consumer-based task calling entirely,
then merge the run configs into the task system if possible
  • Loading branch information
Geolykt committed Jul 9, 2022
1 parent b63a3b2 commit 1eb5140
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 96 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.0</version>
<version>0.90.1</version>

<properties>
<java.version>1.8</java.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ 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 Path> sourcePaths;
public final List<@NotNull Path> resourcePaths;
public final List<@NotNull Path> testSourcePaths;
Expand Down Expand Up @@ -49,6 +52,7 @@ public static class IdeModuleBuilder {
@SuppressWarnings("null")
private Supplier<@NotNull List<JavaJarDependency>> dependencies = Collections::emptyList;
private List<IdeModule> dependencyModules = Collections.emptyList();
@Deprecated // Slbrachyura: Improved tasks system
private List<RunConfigBuilder> runConfigs = Collections.emptyList();
private List<@NotNull Path> sourcePaths = Collections.emptyList();
private List<@NotNull Path> resourcePaths = Collections.emptyList();
Expand Down Expand Up @@ -92,11 +96,13 @@ public IdeModuleBuilder dependencyModules(IdeModule... dependencyModules) {
return this;
}

@Deprecated // Slbrachyura: Improved tasks system
public IdeModuleBuilder runConfigs(List<RunConfigBuilder> runConfigs) {
this.runConfigs = runConfigs;
return this;
}

@Deprecated // Slbrachyura: Improved tasks system
public IdeModuleBuilder runConfigs(RunConfigBuilder... runConfigs) {
this.runConfigs = Arrays.asList(runConfigs);
return this;
Expand Down Expand Up @@ -169,6 +175,7 @@ public IdeModule build() {
}
}

@Deprecated // Slbrachyura: Improved tasks system
public class RunConfig {
public final String name;
public final String mainClass;
Expand All @@ -191,6 +198,7 @@ public class RunConfig {
}
}

@Deprecated // Slbrachyura: Improved tasks system
public static class RunConfigBuilder {
private String name;
private String mainClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

import org.jetbrains.annotations.NotNull;
import org.tinylog.Logger;
Expand Down Expand Up @@ -135,28 +136,55 @@ public static void main(String[] args, Path projectDir, List<Path> classpath) {
EntryGlobals.projectDir = projectDir;
EntryGlobals.buildscriptClasspath = classpath;
BuildscriptProject buildscriptProject = new BuildscriptProject();
// Slbrachyura start: Improved task system
if (args.length >= 1 && "buildscript".equalsIgnoreCase(args[0])) {
Tasks t = new Tasks();
buildscriptProject.getTasks(t);
AtomicBoolean searchingTasks = new AtomicBoolean(true);
if (args.length >= 2) {
Task task = t.get(args[1]);
task.doTask(args.length > 3 ? Arrays.copyOfRange(args, 2, args.length) : new String[]{});
} else {
Logger.info("Avalible buildscript tasks: " + t.toString());
buildscriptProject.getTasks(task -> {
if (task.name.equals(args[1])) {
task.doTask(Arrays.copyOfRange(args, 2, args.length));
searchingTasks.set(false);
}
});
if (searchingTasks.get()) {
Logger.error("Unable to find task with name: " + args[1]);
}
}
if (searchingTasks.get()) {
StringBuilder availableTasks = new StringBuilder();
buildscriptProject.getTasks(task -> {
availableTasks.append(' ');
availableTasks.append(task.name);
});
Logger.info("Available buildscript tasks: " + availableTasks.toString());
}
} else {
Optional<Project> o = buildscriptProject.project.get();
if (o.isPresent()) {
Project project = o.get();
project.setIdeProject(buildscriptProject);
Tasks t = new Tasks();
project.getTasks(t);

AtomicBoolean searchingTasks = new AtomicBoolean(true);
if (args.length >= 1) {
Task task = t.get(args[0]);
task.doTask(args.length > 2 ? Arrays.copyOfRange(args, 1, args.length) : new String[]{});
} else {
Logger.info("Avalible tasks: " + t.toString());
project.getTasks(task -> {
if (task.name.equals(args[0])) {
task.doTask(Arrays.copyOfRange(args, 1, args.length));
searchingTasks.set(false);
}
});
if (searchingTasks.get()) {
Logger.error("Unable to find task with name: " + args[0]);
}
}
if (searchingTasks.get()) {
StringBuilder availableTasks = new StringBuilder();
project.getTasks(task -> {
availableTasks.append(' ');
availableTasks.append(task.name);
});
Logger.info("Available buildscript tasks: " + availableTasks.toString());
}
// Slbrachyura end
} else {
Logger.warn("Invalid build script :(");
Logger.info("Tip: If you invoke the bootstrap with \"createTemplate\" a template project will be created.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public java.util.Optional<Project> createProject() {
};
};
buildscript.setIdeProject(buildscriptProject);
Tasks t = new Tasks();
buildscript.getTasks(t);
Task task = t.get(args[2]);
task.doTask(new String[]{});
buildscript.runTask(args[2]); // Slbrachyura: Cleaner task calling
} finally {
for (Plugin plugin : plugins) {
plugin.onExit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -47,43 +46,43 @@ public void getRunConfigTasks(@NotNull Consumer<@NotNull Task> p) {
//noop
}

@Override
@NotNull
public IdeModule[] getIdeModules() {
Tasks builtProjectTasks = new Tasks();
// Slbrachyura start: Allow to rename the name of the IDE project generated by the buildscript project
protected String getProjectName() {
String ideBuildscriptName = "Buildscript";
Optional<Project> buildscriptInstance = project.get();
if (buildscriptInstance.isPresent()) {
Project concreteBuildscriptInstance = buildscriptInstance.get();
concreteBuildscriptInstance.getTasks(builtProjectTasks);
if (concreteBuildscriptInstance instanceof DescriptiveBuildscriptName) {
ideBuildscriptName = ((DescriptiveBuildscriptName) concreteBuildscriptInstance).getBuildscriptName();
}
}
ArrayList<RunConfigBuilder> runConfigs = new ArrayList<>(builtProjectTasks.getAllTasks().size());
Path cwd = getProjectDir().resolve("run");
PathUtil.createDirectories(cwd);
for (Map.Entry<String, Task> e : builtProjectTasks.getAllTasks().entrySet()) {
String projectDir = super.getProjectDir().toString(); // eclipe's null evaluation can sometimes be a bit strange when generics are at play
runConfigs.add(
new RunConfigBuilder()
.name(e.getKey())
.cwd(cwd)
.mainClass("io.github.coolcrabs.brachyura.project.BuildscriptDevEntry")
.classpath(getCompileDependencies())
.args(
() -> Arrays.asList(
projectDir,
getCompileDependencies().stream().map(Path::toString).collect(Collectors.joining(File.pathSeparator)),
e.getKey()
)
)
);
return ideBuildscriptName;
}

@Override
@NotNull
public IdeModule[] getIdeModules() {
ArrayList<RunConfigBuilder> runConfigs = new ArrayList<>();
Path runDirectory = getProjectDir().resolve("run");
PathUtil.createDirectories(runDirectory);
Optional<Project> buildscriptInstance = project.get();
if (buildscriptInstance.isPresent()) {
buildscriptInstance.get().getTasks(task -> {
runConfigs.add(new RunConfigBuilder()
.name(task.name)
.cwd(runDirectory)
.mainClass("io.github.coolcrabs.brachyura.project.BuildscriptDevEntry")
.classpath(getCompileDependencies())
.args(() -> {
return Arrays.asList(super.getProjectDir().toString(), getCompileDependencies().stream().map(Path::toString).collect(Collectors.joining(File.pathSeparator)), task.name);
}));
});
}
// Slbrachyura end

return new @NotNull IdeModule[] {
new IdeModule.IdeModuleBuilder()
.name(ideBuildscriptName)
.name(getProjectName())
.root(getProjectDir())
.sourcePath(getSrcDir())
.dependencies(this::getIdeDependencies)
Expand Down Expand Up @@ -120,7 +119,7 @@ public ClassLoader getBuildscriptClassLoader() {
JavaCompilationResult compilation = new JavaCompilation()
.addSourceDir(getSrcDir())
.addClasspath(getCompileDependencies())
.addOption(JvmUtil.compileArgs(JvmUtil.CURRENT_JAVA_VERSION, 8))
.addOption(JvmUtil.compileArgs(JvmUtil.CURRENT_JAVA_VERSION, 8)) // TODO: Make configurable - somehow
.compile();
BuildscriptClassloader r = new BuildscriptClassloader(BuildscriptProject.class.getClassLoader());
compilation.getInputs(r);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.coolcrabs.brachyura.project;

import java.nio.file.Path;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

import org.jetbrains.annotations.NotNull;
Expand All @@ -17,9 +19,18 @@ public void getTasks(@NotNull Consumer<@NotNull Task> p) {
}

public final void runTask(String name, String... args) {
Tasks t = new Tasks();
getTasks(t);
t.get(name).doTask(args);
// Slbrachyura start: Improved task system
AtomicBoolean foundTask = new AtomicBoolean();
getTasks(task -> {
if (task.name.equals(args[2])) {
task.doTask(new String[]{});
foundTask.set(true);
}
});
if (!foundTask.get()) {
throw new NoSuchElementException("Unable to get task with given name: " + name);
}
// Slbrachyura end
}

@SuppressWarnings("null") // There are circumstances that this is null, but we are going to ignore these
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.function.Consumer;

import org.jetbrains.annotations.NotNull;
import org.tinylog.Logger;

import io.github.coolcrabs.brachyura.ide.Ide;
import io.github.coolcrabs.brachyura.ide.IdeModule;
Expand Down Expand Up @@ -55,7 +56,8 @@ public void getIdeTasks(@NotNull Consumer<@NotNull Task> p) {
public void getRunConfigTasks(@NotNull Consumer<@NotNull Task> p) {
IdeModule[] ms = getIdeModules();
for (IdeModule m : ms) {
for (IdeModule.RunConfig rc : m.runConfigs) {
for (IdeModule.RunConfig rc : m.runConfigs) { // Slbrachyura: TODO Document what the hell this does
Logger.info("Debug run config name: " + rc.name);
String tname = ms.length == 1 ? "run" + rc.name.replace(" ", "") : m.name.replace(" ", "") + ":run" + rc.name.replace(" ", "");
p.accept(Task.of(tname, (Runnable) () -> runRunConfig(m, rc)));
}
Expand Down

0 comments on commit 1eb5140

Please sign in to comment.