From 3e991492be7690be8287115683fd515fcb46d40e Mon Sep 17 00:00:00 2001 From: Geolykt Date: Sun, 10 Jul 2022 00:20:05 +0200 Subject: [PATCH] [2/?] Improved task system: Death to the consumer Dealing with run configs will be a tough nut to crack --- brachyura/pom.xml | 2 +- .../brachyura/fabric/SimpleFabricProject.java | 24 ++++++--- ...AuthentificatedMavenPublishRepository.java | 2 +- .../brachyura/project/BrachyuraEntry.java | 40 +++++++------- .../brachyura/project/BuildscriptProject.java | 4 +- .../coolcrabs/brachyura/project/Project.java | 31 +++++++---- .../project/java/BaseJavaProject.java | 53 ++++++++++++------- .../project/java/SimpleJavaProject.java | 24 ++++++--- .../brachyura/fabric/FabricProjectTest.java | 10 ++-- .../brachyura/fabric/J8FabricProjectTest.java | 25 ++------- .../brachyura/fabric/MojmapProjectTest.java | 9 ++-- .../project/java/SimpleJavaProjectTest.java | 13 ++--- .../brachyura/quilt/QmQuiltProjectTest.java | 8 ++- .../brachyura/quilt/QuiltProjectTest.java | 8 ++- 14 files changed, 135 insertions(+), 118 deletions(-) diff --git a/brachyura/pom.xml b/brachyura/pom.xml index 91179acc..a34cc1fc 100644 --- a/brachyura/pom.xml +++ b/brachyura/pom.xml @@ -5,7 +5,7 @@ io.github.coolcrabs brachyura - 0.90.1 + 0.90.2 1.8 diff --git a/brachyura/src/main/java/io/github/coolcrabs/brachyura/fabric/SimpleFabricProject.java b/brachyura/src/main/java/io/github/coolcrabs/brachyura/fabric/SimpleFabricProject.java index 169f9a38..8f5429a5 100644 --- a/brachyura/src/main/java/io/github/coolcrabs/brachyura/fabric/SimpleFabricProject.java +++ b/brachyura/src/main/java/io/github/coolcrabs/brachyura/fabric/SimpleFabricProject.java @@ -224,15 +224,19 @@ public Path getModuleRoot() { } } + @NotNull @Override - public void getTasks(@NotNull Consumer<@NotNull Task> sink) { - super.getTasks(sink); - sink.accept(Task.of("build", (ThrowingRunnable) this::build)); - getPublishTasks(sink); + public List<@NotNull Task> getTasks() { + List<@NotNull Task> tasks = super.getTasks(); + tasks.add(Task.of("build", (ThrowingRunnable) this::build)); + tasks.addAll(getPublishTasks()); + return tasks; } - public void getPublishTasks(@NotNull Consumer<@NotNull Task> p) { - p.accept(Task.of("publishToMavenLocal", (ThrowingRunnable) () -> { + @NotNull + public List<@NotNull Task> getPublishTasks() { // Slbrachyura: Improved task system + List<@NotNull Task> tasks = new ArrayList<>(); + tasks.add(Task.of("publishToMavenLocal", (ThrowingRunnable) () -> { MavenPublisher publisher = new MavenPublisher().addRepository(new LocalMavenRepository(MavenResolver.MAVEN_LOCAL)); List mavendeps = new ArrayList<>(); ModDependencyCollector dependencies = new ModDependencyCollector(); @@ -244,7 +248,7 @@ public void getPublishTasks(@NotNull Consumer<@NotNull Task> p) { }); publisher.publishJar(build(), mavendeps); })); - p.accept(Task.of("publish", (ThrowingRunnable) () -> { + tasks.add(Task.of("publish", (ThrowingRunnable) () -> { MavenPublisher publisher = new MavenPublisher().addRepository(AuthentificatedMavenPublishRepository.fromEnvironmentVariables()); List mavendeps = new ArrayList<>(); ModDependencyCollector dependencies = new ModDependencyCollector(); @@ -256,6 +260,12 @@ public void getPublishTasks(@NotNull Consumer<@NotNull Task> p) { }); publisher.publishJar(build(), mavendeps); })); + return tasks; + } + + @Deprecated // Slbrachyura: Deprecate task handling with consumers + public final void getPublishTasks(@NotNull Consumer<@NotNull Task> p) { + getPublishTasks().forEach(p); } @Override diff --git a/brachyura/src/main/java/io/github/coolcrabs/brachyura/maven/publish/AuthentificatedMavenPublishRepository.java b/brachyura/src/main/java/io/github/coolcrabs/brachyura/maven/publish/AuthentificatedMavenPublishRepository.java index 788fccd2..d309940c 100644 --- a/brachyura/src/main/java/io/github/coolcrabs/brachyura/maven/publish/AuthentificatedMavenPublishRepository.java +++ b/brachyura/src/main/java/io/github/coolcrabs/brachyura/maven/publish/AuthentificatedMavenPublishRepository.java @@ -21,7 +21,7 @@ import io.github.coolcrabs.brachyura.util.PathUtil; /** - * Basically a backport of the old AuthentificatedMaven feature. + * Basically a port of the old AuthentificatedMaven feature. * No guarantees are made on whether this works in the first place, since I do not make * use of such maven repositories myself. */ diff --git a/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/BrachyuraEntry.java b/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/BrachyuraEntry.java index 25c37ab4..279bb626 100644 --- a/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/BrachyuraEntry.java +++ b/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/BrachyuraEntry.java @@ -11,7 +11,6 @@ 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; @@ -138,24 +137,27 @@ public static void main(String[] args, Path projectDir, List classpath) { BuildscriptProject buildscriptProject = new BuildscriptProject(); // Slbrachyura start: Improved task system if (args.length >= 1 && "buildscript".equalsIgnoreCase(args[0])) { - AtomicBoolean searchingTasks = new AtomicBoolean(true); + boolean searchingTasks = true; if (args.length >= 2) { - buildscriptProject.getTasks(task -> { + for (Task task : buildscriptProject.getTasks()) { if (task.name.equals(args[1])) { + if (!searchingTasks) { + throw new IllegalStateException("There are multiple tasks with the name \"" + task.name + "\"."); + } task.doTask(Arrays.copyOfRange(args, 2, args.length)); - searchingTasks.set(false); + searchingTasks = false; } - }); - if (searchingTasks.get()) { + } + if (searchingTasks) { Logger.error("Unable to find task with name: " + args[1]); } } - if (searchingTasks.get()) { + if (searchingTasks) { StringBuilder availableTasks = new StringBuilder(); - buildscriptProject.getTasks(task -> { + for (Task task : buildscriptProject.getTasks()) { availableTasks.append(' '); availableTasks.append(task.name); - }); + } Logger.info("Available buildscript tasks: " + availableTasks.toString()); } } else { @@ -163,25 +165,27 @@ public static void main(String[] args, Path projectDir, List classpath) { if (o.isPresent()) { Project project = o.get(); project.setIdeProject(buildscriptProject); - - AtomicBoolean searchingTasks = new AtomicBoolean(true); + boolean searchingTasks = true; if (args.length >= 1) { - project.getTasks(task -> { + for (Task task : project.getTasks()) { if (task.name.equals(args[0])) { + if (!searchingTasks) { + throw new IllegalStateException("There are multiple tasks with the name \"" + task.name + "\"."); + } task.doTask(Arrays.copyOfRange(args, 1, args.length)); - searchingTasks.set(false); + searchingTasks = false; } - }); - if (searchingTasks.get()) { + } + if (searchingTasks) { Logger.error("Unable to find task with name: " + args[0]); } } - if (searchingTasks.get()) { + if (searchingTasks) { StringBuilder availableTasks = new StringBuilder(); - project.getTasks(task -> { + for (Task task : project.getTasks()) { availableTasks.append(' '); availableTasks.append(task.name); - }); + } Logger.info("Available buildscript tasks: " + availableTasks.toString()); } // Slbrachyura end diff --git a/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/BuildscriptProject.java b/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/BuildscriptProject.java index 98a5c5ae..41a49466 100644 --- a/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/BuildscriptProject.java +++ b/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/BuildscriptProject.java @@ -67,7 +67,7 @@ public IdeModule[] getIdeModules() { PathUtil.createDirectories(runDirectory); Optional buildscriptInstance = project.get(); if (buildscriptInstance.isPresent()) { - buildscriptInstance.get().getTasks(task -> { + for (Task task : buildscriptInstance.get().getTasks()) { runConfigs.add(new RunConfigBuilder() .name(task.name) .cwd(runDirectory) @@ -76,7 +76,7 @@ public IdeModule[] getIdeModules() { .args(() -> { return Arrays.asList(super.getProjectDir().toString(), getCompileDependencies().stream().map(Path::toString).collect(Collectors.joining(File.pathSeparator)), task.name); })); - }); + } } // Slbrachyura end diff --git a/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/Project.java b/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/Project.java index 9ea401f4..0858c50d 100644 --- a/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/Project.java +++ b/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/Project.java @@ -1,8 +1,9 @@ package io.github.coolcrabs.brachyura.project; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; import java.util.NoSuchElementException; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; import org.jetbrains.annotations.NotNull; @@ -14,20 +15,32 @@ public class Project { BaseJavaProject buildscriptIdeProject; - public void getTasks(@NotNull Consumer<@NotNull Task> p) { - // no default tasks + @Deprecated // Slbrachyura: Deprecate task handling with consumers + public final void getTasks(@NotNull Consumer<@NotNull Task> p) { + getTasks().forEach(p); + } + + // Slbrachyura start: Improved task system + /** + * Obtains a list of all tasks. Must be mutable if the class is meant to be extended or + * used for libraries. + * + * @return A list of all tasks registered in this project + */ + @NotNull + public List<@NotNull Task> getTasks() { + return new ArrayList<>(); } public final void runTask(String name, String... args) { - // Slbrachyura start: Improved task system - AtomicBoolean foundTask = new AtomicBoolean(); - getTasks(task -> { + boolean foundTask = false; + for (Task task : getTasks()) { if (task.name.equals(args[2])) { task.doTask(new String[]{}); - foundTask.set(true); + foundTask = true; } - }); - if (!foundTask.get()) { + } + if (!foundTask) { throw new NoSuchElementException("Unable to get task with given name: " + name); } // Slbrachyura end diff --git a/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/java/BaseJavaProject.java b/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/java/BaseJavaProject.java index ee61bf46..289945d5 100644 --- a/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/java/BaseJavaProject.java +++ b/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/java/BaseJavaProject.java @@ -1,17 +1,24 @@ package io.github.coolcrabs.brachyura.project.java; -import io.github.coolcrabs.brachyura.compiler.java.JavaCompilation; -import io.github.coolcrabs.brachyura.compiler.java.JavaCompilationResult; -import io.github.coolcrabs.brachyura.dependency.JavaJarDependency; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayDeque; +import java.util.ArrayList; import java.util.Deque; import java.util.HashMap; import java.util.LinkedHashSet; +import java.util.List; +import java.util.Objects; import java.util.function.Consumer; import org.jetbrains.annotations.NotNull; import org.tinylog.Logger; +import io.github.coolcrabs.brachyura.compiler.java.JavaCompilation; +import io.github.coolcrabs.brachyura.compiler.java.JavaCompilationResult; +import io.github.coolcrabs.brachyura.dependency.JavaJarDependency; import io.github.coolcrabs.brachyura.ide.Ide; import io.github.coolcrabs.brachyura.ide.IdeModule; import io.github.coolcrabs.brachyura.processing.sinks.DirectoryProcessingSink; @@ -21,28 +28,17 @@ import io.github.coolcrabs.brachyura.util.JvmUtil; import io.github.coolcrabs.brachyura.util.PathUtil; import io.github.coolcrabs.brachyura.util.Util; -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Objects; public abstract class BaseJavaProject extends Project { @NotNull public abstract IdeModule[] getIdeModules(); - @Override - public void getTasks(@NotNull Consumer<@NotNull Task> p) { - super.getTasks(p); - getIdeTasks(p); - getRunConfigTasks(p); - } - - public void getIdeTasks(@NotNull Consumer<@NotNull Task> p) { + // Slbrachyura: Improved task handling + public List<@NotNull Task> getIdeTasks() { + List<@NotNull Task> tasks = new ArrayList<>(); for (Ide ide : Ide.getIdes()) { - p.accept(Task.of(ide.ideName(), (Runnable) () -> { + tasks.add(Task.of(ide.ideName(), (Runnable) () -> { BaseJavaProject buildscriptProject = getBuildscriptProject(); if (buildscriptProject != null) { ide.updateProject(getProjectDir(), ArrayUtil.join(IdeModule.class, getIdeModules(), buildscriptProject.getIdeModules())); @@ -51,8 +47,15 @@ public void getIdeTasks(@NotNull Consumer<@NotNull Task> p) { } })); } + return tasks; + } + + @Deprecated // Slbrachyura: Deprecate task handling with consumers + public final void getIdeTasks(@NotNull Consumer<@NotNull Task> p) { + getIdeTasks().forEach(p); } - + + @Deprecated // Slbrachyura: Deprecate task handling with consumers, prepare removal of runConfigs public void getRunConfigTasks(@NotNull Consumer<@NotNull Task> p) { IdeModule[] ms = getIdeModules(); for (IdeModule m : ms) { @@ -63,7 +66,7 @@ public void getRunConfigTasks(@NotNull Consumer<@NotNull Task> p) { } } } - + public void runRunConfig(IdeModule ideProject, IdeModule.RunConfig rc) { try { LinkedHashSet toCompile = new LinkedHashSet<>(); @@ -142,6 +145,16 @@ public Path getSrcDir() { return getProjectDir().resolve("src").resolve("main").resolve("java"); } + // Slbrachyura: Improved task handling + @Override + @NotNull + public List<@NotNull Task> getTasks() { + List<@NotNull Task> tasks = super.getTasks(); + tasks.addAll(getIdeTasks()); + getRunConfigTasks(tasks::add); + return tasks; + } + @NotNull public Path getResourcesDir() { Path resourceDir = getProjectDir().resolve("src").resolve("main").resolve("resources"); diff --git a/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaProject.java b/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaProject.java index 48de48ab..56320407 100644 --- a/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaProject.java +++ b/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaProject.java @@ -1,6 +1,7 @@ package io.github.coolcrabs.brachyura.project.java; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -105,22 +106,31 @@ public String getJarBaseName() { return getId().artifactId + "-" + getId().version; } + @NotNull @Override - public void getTasks(@NotNull Consumer<@NotNull Task> p) { - super.getTasks(p); - p.accept(Task.of("build", (ThrowingRunnable) this::build)); - getPublishTasks(p); + public List<@NotNull Task> getTasks() { + List<@NotNull Task> tasks = super.getTasks(); + tasks.add(Task.of("build", (ThrowingRunnable) this::build)); + tasks.addAll(getPublishTasks()); + return tasks; } - public void getPublishTasks(@NotNull Consumer<@NotNull Task> p) { - p.accept(Task.of("publishToMavenLocal", (ThrowingRunnable) () -> { + public List<@NotNull Task> getPublishTasks() { // Slbrachyura: Improved task handling + List<@NotNull Task> tasks = new ArrayList<>(); + tasks.add(Task.of("publishToMavenLocal", (ThrowingRunnable) () -> { MavenPublisher publisher = new MavenPublisher().addRepository(new LocalMavenRepository(MavenResolver.MAVEN_LOCAL)); publisher.publishJar(build(), projectModule.get().dependencies.get()); })); - p.accept(Task.of("publish", (ThrowingRunnable) () -> { + tasks.add(Task.of("publish", (ThrowingRunnable) () -> { MavenPublisher publisher = new MavenPublisher().addRepository(AuthentificatedMavenPublishRepository.fromEnvironmentVariables()); publisher.publishJar(build(), projectModule.get().dependencies.get()); })); + return tasks; + } + + @Deprecated // Slbrachyura: Deprecate task handling with consumers + public final void getPublishTasks(@NotNull Consumer<@NotNull Task> p) { + getPublishTasks().forEach(p); } @Override diff --git a/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/FabricProjectTest.java b/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/FabricProjectTest.java index b55f77d7..580e37a7 100644 --- a/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/FabricProjectTest.java +++ b/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/FabricProjectTest.java @@ -80,13 +80,9 @@ void testProject() { @Test void ide() { - //Todo better api for this? - fabricProject.getTasks(task -> { - // Slbrachyura: improved style - if (task.name.equals("netbeans") || task.name.equals("idea") || task.name.equals("jdt")) { - task.doTask(new String[] {}); - } - }); + fabricProject.runTask("netbeans"); // Slbrachyura: there is a better way to do this + fabricProject.runTask("idea"); + fabricProject.runTask("jdt"); } @Test diff --git a/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/J8FabricProjectTest.java b/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/J8FabricProjectTest.java index d9ec8765..f1deec44 100644 --- a/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/J8FabricProjectTest.java +++ b/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/J8FabricProjectTest.java @@ -83,31 +83,14 @@ void compile() { @Test void ide() { - //Todo better api for this? - fabricProject.getTasks(p -> { - try { - if (p.name.equals("netbeans")) p.doTask(new String[]{}); - if (p.name.equals("idea")) p.doTask(new String[]{}); - if (p.name.equals("jdt")) p.doTask(new String[]{}); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - }); + fabricProject.runTask("netbeans"); + fabricProject.runTask("idea"); + fabricProject.runTask("jdt"); } @Disabled @Test void bruh() { - fabricProject.getTasks(p -> { - if (p.name.equals("runMinecraftClient")) - try { - p.doTask(new String[]{}); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - - }); + fabricProject.runTask("runMinecraftClient"); } } diff --git a/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/MojmapProjectTest.java b/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/MojmapProjectTest.java index 1bcee1d4..108c81c3 100644 --- a/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/MojmapProjectTest.java +++ b/brachyura/src/test/java/io/github/coolcrabs/brachyura/fabric/MojmapProjectTest.java @@ -80,12 +80,9 @@ void testProject() { @Test void ide() { long a = System.currentTimeMillis(); - //Todo better api for this? - fabricProject.getTasks(p -> { - if (p.name.equals("netbeans")) p.doTask(new String[]{}); - if (p.name.equals("idea")) p.doTask(new String[]{}); - if (p.name.equals("jdt")) p.doTask(new String[]{}); - }); + fabricProject.runTask("netbeans"); // Slbrachyura: there is already a better way to do this + fabricProject.runTask("idea"); + fabricProject.runTask("jdt"); long b = System.currentTimeMillis(); System.out.println(b - a); } diff --git a/brachyura/src/test/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaProjectTest.java b/brachyura/src/test/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaProjectTest.java index 349ea7cc..32d9535d 100644 --- a/brachyura/src/test/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaProjectTest.java +++ b/brachyura/src/test/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaProjectTest.java @@ -78,17 +78,12 @@ public IdeModule ideModule() { }; } }; - //Todo better api for this? - project.getTasks(p -> { - if (p.name.equals("jdt")) p.doTask(new String[]{}); - if (p.name.equals("netbeans")) p.doTask(new String[]{}); - if (p.name.equals("idea")) p.doTask(new String[]{}); - }); + project.runTask("netbeans"); + project.runTask("idea"); + project.runTask("jdt"); assertDoesNotThrow(() -> { Assertions.assertNotNull(project.build()); }); - project.getTasks(p -> { - if (p.name.equals("publishToMavenLocal")) p.doTask(new String[]{}); - }); + project.runTask("publishToMavenLocal"); } } diff --git a/brachyura/src/test/java/io/github/coolcrabs/brachyura/quilt/QmQuiltProjectTest.java b/brachyura/src/test/java/io/github/coolcrabs/brachyura/quilt/QmQuiltProjectTest.java index 9038bf82..7621b1b0 100644 --- a/brachyura/src/test/java/io/github/coolcrabs/brachyura/quilt/QmQuiltProjectTest.java +++ b/brachyura/src/test/java/io/github/coolcrabs/brachyura/quilt/QmQuiltProjectTest.java @@ -65,11 +65,9 @@ public BrachyuraDecompiler decompiler() { @Test void ide() { long a = System.currentTimeMillis(); - proj.getTasks(p -> { - if (p.name.equals("netbeans")) p.doTask(new String[]{}); - if (p.name.equals("idea")) p.doTask(new String[]{}); - if (p.name.equals("jdt")) p.doTask(new String[]{}); - }); + proj.runTask("netbeans"); // Slbrachyura: there is a better way to do this + proj.runTask("idea"); + proj.runTask("jdt"); long b = System.currentTimeMillis(); System.out.println(b - a); } diff --git a/brachyura/src/test/java/io/github/coolcrabs/brachyura/quilt/QuiltProjectTest.java b/brachyura/src/test/java/io/github/coolcrabs/brachyura/quilt/QuiltProjectTest.java index 9c7812a7..4bef0059 100644 --- a/brachyura/src/test/java/io/github/coolcrabs/brachyura/quilt/QuiltProjectTest.java +++ b/brachyura/src/test/java/io/github/coolcrabs/brachyura/quilt/QuiltProjectTest.java @@ -63,11 +63,9 @@ public BrachyuraDecompiler decompiler() { @Test void ide() { long a = System.currentTimeMillis(); - proj.getTasks(p -> { - if (p.name.equals("netbeans")) p.doTask(new String[]{}); - if (p.name.equals("idea")) p.doTask(new String[]{}); - if (p.name.equals("jdt")) p.doTask(new String[]{}); - }); + proj.runTask("netbeans"); // Slbrachyura: there is a better way to do this + proj.runTask("idea"); + proj.runTask("jdt"); long b = System.currentTimeMillis(); System.out.println(b - a); }