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

Commit

Permalink
[2/?] Improved task system: Death to the consumer
Browse files Browse the repository at this point in the history
Dealing with run configs will be a tough nut to crack
  • Loading branch information
Geolykt committed Jul 9, 2022
1 parent 1eb5140 commit 3e99149
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 118 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.1</version>
<version>0.90.2</version>

<properties>
<java.version>1.8</java.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MavenDependency> mavendeps = new ArrayList<>();
ModDependencyCollector dependencies = new ModDependencyCollector();
Expand All @@ -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<MavenDependency> mavendeps = new ArrayList<>();
ModDependencyCollector dependencies = new ModDependencyCollector();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -138,50 +137,55 @@ public static void main(String[] args, Path projectDir, List<Path> 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 {
Optional<Project> o = buildscriptProject.project.get();
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public IdeModule[] getIdeModules() {
PathUtil.createDirectories(runDirectory);
Optional<Project> 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)
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()));
Expand All @@ -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) {
Expand All @@ -63,7 +66,7 @@ public void getRunConfigTasks(@NotNull Consumer<@NotNull Task> p) {
}
}
}

public void runRunConfig(IdeModule ideProject, IdeModule.RunConfig rc) {
try {
LinkedHashSet<IdeModule> toCompile = new LinkedHashSet<>();
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 3e99149

Please sign in to comment.