Skip to content

Commit

Permalink
Update run package
Browse files Browse the repository at this point in the history
  • Loading branch information
sormuras committed Apr 12, 2024
1 parent 4457bc7 commit 3bd6183
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 85 deletions.
60 changes: 60 additions & 0 deletions .bach/src/run.bach/run/Ant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package run;

import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import java.util.spi.ToolProvider;
import run.bach.ToolCall;
import run.bach.ToolFinder;
import run.bach.ToolInstaller;
import run.bach.ToolProgram;
import run.bach.ToolSpace;

/**
* Apache Ant.
*
* @see <a href="https://ant.apache.org">https://ant.apache.org</a>
*/
public record Ant(String version) implements ToolInstaller {
public static final String DEFAULT_VERSION = "1.10.14";

public static void main(String... args) {
var version = System.getProperty("version", DEFAULT_VERSION);
new Ant(version).install().run(args.length == 0 ? new String[] {"-version"} : args);
}

public Ant() {
this(DEFAULT_VERSION);
}

@Override
public ToolProvider installInto(Path folder) {
var title = "apache-ant-" + version;
var archive = title + "-bin.zip";
var target = folder.resolve(archive);
if (!Files.exists(target)) {
var source = "https://dlcdn.apache.org/ant/binaries/" + archive;
download(target, URI.create(source));
}
var antHome = folder.resolve(title);
if (!Files.isDirectory(antHome)) {
var jar =
ToolProgram.findJavaDevelopmentKitTool("jar")
.orElseThrow()
.withProcessBuilderTweaker(builder -> builder.directory(folder.toFile()))
.withProcessWaiter(process -> process.waitFor(1, TimeUnit.MINUTES) ? 0 : 1)
.tool();
var silent =
new ToolSpace(ToolFinder.compose()) {
@Override
protected void announce(ToolCall call) {}
};
silent.run(jar, "--extract", "--file", archive);
}
return ToolProgram.java(
"--class-path",
antHome.resolve("lib/ant-launcher.jar").toString(),
"org.apache.tools.ant.launch.Launcher");
}
}
2 changes: 1 addition & 1 deletion .bach/src/run.bach/run/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Format {
public static void main(String... args) {
var tool = new GoogleJavaFormat().tool("1.22.0");
var tool = new GoogleJavaFormat("1.22.0").install();
if (args.length == 0) {
tool.run(call -> call.add("--replace").addFiles("**.java"));
} else {
Expand Down
29 changes: 16 additions & 13 deletions .bach/src/run.bach/run/GoogleJavaFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,37 @@
/**
* @see <a href="https://github.com/google/google-java-format">Google Java Format</a>
*/
public final class GoogleJavaFormat implements ToolInstaller {
public record GoogleJavaFormat(String version) implements ToolInstaller {
/**
* @see <a href="https://github.com/google/google-java-format/releases/latest">Latest release</a>
*/
public static final String DEFAULT_VERSION = "1.22.0";

public static void main(String... args) {
var installer = new GoogleJavaFormat();
var version = System.getProperty("version", installer.version());
installer.tool(version).run(args.length == 0 ? new String[] {"--version"} : args);
var version = System.getProperty("version", DEFAULT_VERSION);
new GoogleJavaFormat(version)
.install()
.run(args.length == 0 ? new String[] {"--version"} : args);
}

public GoogleJavaFormat() {
this(DEFAULT_VERSION);
}

@Override
public String name() {
return "google-java-format";
}

/**
* @see <a href="https://github.com/google/google-java-format/releases/latest">Latest release</a>
*/
public String version() {
return "1.22.0";
}

@Override
public ToolProvider install(String version, Path into) {
public ToolProvider installInto(Path into) {
var filename = "google-java-format-" + version + "-all-deps.jar";
var target = into.resolve(filename);
if (!Files.exists(target)) {
var releases = "https://github.com/google/google-java-format/releases/download/";
var source = releases + "v" + version + "/" + filename;
download(target, URI.create(source));
}
return ToolProgram.findJavaDevelopmentKitTool("java", "-jar", target.toString()).orElseThrow();
return ToolProgram.java("-jar", target.toString());
}
}
24 changes: 14 additions & 10 deletions .bach/src/run.bach/run/Greeter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,34 @@
public record Greeter() implements ToolInstaller {
public static void main(String... args) {
var greeter = new Greeter();
greeter.tool("0").run(); // "external/greeter@0"
greeter.install().run(); // "external/greeter"

var folders = Bach.Folders.ofCurrentWorkingDirectory();
Tool.of("greeter/eager@1", greeter.install("1", folders.tools("greeter/eager@1"))).run();
Tool.of("greeter/inert@2", () -> greeter.install("2", folders.tools("greeter/inert@2"))).run();
Tool.of("my/c@99", ToolProvider.findFirst("javac").orElseThrow()).run();
Tool.of("greeter/eager", greeter.installInto(folders.tools("greeter/eager"))).run();
Tool.of("greeter/inert", () -> greeter.installInto(folders.tools("greeter/inert"))).run();
}

@Override
public ToolProvider install(String version, Path into) {
var file = into.resolve("Prog.java");
public String version() {
return null;
}

@Override
public ToolProvider installInto(Path folder) {
var file = folder.resolve("Prog.java");
if (!Files.exists(file)) {
try {
Files.createDirectories(into);
Files.createDirectories(folder);
var text = // language=java
"""
class Prog {
public static void main(String... args) {
System.out.println("Greetings! [%s]");
System.out.println("Greetings!");
}
}
"""
.formatted(version);
""";
Files.writeString(file, text);
System.out.println("Wrote " + file.toUri());
} catch (Exception exception) {
throw new RuntimeException();
}
Expand Down
53 changes: 53 additions & 0 deletions .bach/src/run.bach/run/Maven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package run;

import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.spi.ToolProvider;
import run.bach.ToolInstaller;
import run.bach.ToolProgram;

/**
* Apache Maven is a software project management and comprehension tool.
*
* @see <a href="https://maven.apache.org">https://maven.apache.org</a>
*/
public record Maven(String version) implements ToolInstaller {
public static final String DEFAULT_VERSION = "3.9.6";

public static void main(String... args) {
var version = System.getProperty("version", DEFAULT_VERSION);
new Maven(version).install().run(args.length == 0 ? new String[] {"--version"} : args);
}

public Maven() {
this(DEFAULT_VERSION);
}

@Override
public ToolProvider installInto(Path into) {
var base = "https://repo.maven.apache.org/maven2/org/apache/maven";
var mavenWrapperProperties = into.resolve("maven-wrapper.properties");
if (!Files.exists(mavenWrapperProperties))
try {
Files.writeString(
mavenWrapperProperties,
// language=properties
"""
distributionUrl=%s/apache-maven/%s/apache-maven-%s-bin.zip
"""
.formatted(base, version, version));
} catch (Exception exception) {
throw new RuntimeException(exception);
}
var uri = URI.create(base + "/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar#SIZE=62547");
var mavenWrapperJar = into.resolve("maven-wrapper.jar");
download(mavenWrapperJar, uri);
return ToolProgram.findJavaDevelopmentKitTool(
"java",
"-D" + "maven.multiModuleProjectDirectory=.",
"--class-path=" + mavenWrapperJar,
"org.apache.maven.wrapper.MavenWrapperMain")
.orElseThrow();
}
}
30 changes: 0 additions & 30 deletions .bach/src/run.bach/run/Run.java

This file was deleted.

22 changes: 9 additions & 13 deletions .bach/src/run.bach/run/ToolFinderDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void main(String... args) {
ToolFinder.compose(
// ToolProvider SPI, i.e. ToolProvider.findFirst(NAME)
ToolFinder.of(Tool.of("jar"), Tool.of("javac")),
// Native executable in ${JAVA_HOME}/bin/NAME[.exe]
// Native executable program in ${JAVA_HOME}/bin/NAME[.exe]
ToolFinder.of(Tool.of("java"), Tool.of("jfr")),
// ToolProvider instance
ToolFinder.of(
Expand All @@ -24,23 +24,19 @@ public static void main(String... args) {
Tool.of(new ConsumerTool("consume", ConsumerTool::example))),
// Tool installation support
ToolFinder.ofInstaller(ToolInstaller.Mode.INSTALL_IMMEDIATE)
// canonical
.with(new GoogleJavaFormat().installation("1.20.0"))
.with("demo/google-java-format@1.21.0", new GoogleJavaFormat())
.with("demo/google-java-format@1.22.0", GoogleJavaFormat::new)
// convenient
// dedicated installer
.with(new Ant("1.10.14"))
.with(new GoogleJavaFormat("1.22.0"))
.with(new Maven("3.9.6"))
// convenient installer
.withJavaApplication(
"rife2/bld@1.9.0",
"https://github.com/rife2/bld/releases/download/1.9.0/bld-1.9.0.jar")
.withJavaApplication(
"kordamp/jarviz@0.3.0",
"https://github.com/kordamp/jarviz/releases/download/v0.3.0/jarviz-tool-provider-0.3.0.jar"));

// JReleaser.tool(cache, "1.11.0")
// Ant.tool("1.10.14", cache)
// Maven.tool(cache, "3.9.6")

System.out.println(toString(finder));
finder.findToolOrElseThrow("google-java-format").run("--version");
finder.findToolOrElseThrow("flush").run();
finder.findToolOrElseThrow("jarviz").run("--version");
}

record Noop(String name, int code) implements ToolProvider {
Expand Down
32 changes: 32 additions & 0 deletions .bach/src/run.bach/run/ToolSpaceDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package run;

import run.bach.ToolCall;
import run.bach.ToolFinder;
import run.bach.ToolRun;
import run.bach.ToolSpace;

class ToolSpaceDemo extends ToolSpace {
public static void main(String... args) {
var finder = ToolFinder.ofInstaller().with(new Maven("3.9.6"));
var space = new ToolSpaceDemo(finder);

var run = space.run("maven", "--version");

run.out().lines().filter(line -> line.startsWith("Apache Maven")).forEach(System.out::println);
if (run.code() != 0) throw new Error("Non-zero error code: " + run.code());
}

ToolSpaceDemo(ToolFinder finder) {
super(finder);
}

@Override
protected void announce(ToolCall call) {
System.out.println("BEGIN -> " + call.tool().name());
}

@Override
protected void verify(ToolRun run) {
System.out.println("<-- END. " + run.call().tool().name());
}
}
35 changes: 18 additions & 17 deletions .bach/src/run.bach/run/Versions.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,27 @@ public static void main(String... args) {
Tool.of("java").run("--version");

// 1-shot, tool installer
new GoogleJavaFormat().tool("1.22.0").run("--version");
Tool.of(new Ant()).run("-version");
Tool.of(new GoogleJavaFormat()).run("--version");
Tool.of(new Maven()).run("--version");
Tool.of("https://github.com/rife2/bld/releases/download/1.9.0/bld-1.9.0.jar").run("version");

// JReleaser.tool(cache, "1.11.0").run("--version");
// Ant.tool("1.10.14", tools.cache()).run("-version");
// Maven.tool(cache, "3.9.6").run("--version");

// canonical
// multi-shot, tool finder
var finder =
ToolFinder.ofInstaller()
.with(new GoogleJavaFormat().installation("1.21.0"))
.with(new GoogleJavaFormat().installation("1.22.0"))
// JReleaser.tool(cache, "1.11.0"),
// Ant.tool("1.10.14", tools.cache())
// Maven.tool(cache, "3.9.6")
;
.with(new Ant())
.withJavaApplication(
"rife2/bld@1.9.0",
"https://github.com/rife2/bld/releases/download/1.9.0/bld-1.9.0.jar")
.with("run/google-java-format@1.22", new GoogleJavaFormat("1.22.0"))
.with("run/google-java-format@1.19", new GoogleJavaFormat("1.19.2"))
.with(new Maven());

var runner = ToolRunner.of(finder);
runner.run("google-java-format@1.21.0", "--version");
runner.run("google-java-format@1.22.0", "--version");
// runner.run("jreleaser", "--version");
// runner.run("ant", "-version");
// runner.run("maven", "-version");
runner.run("ant", "-version");
runner.run("bld", "version");
runner.run("google-java-format", "--version");
runner.run("google-java-format@1.19", "--version");
runner.run("maven", "--version");
}
}
2 changes: 1 addition & 1 deletion .bach/src/run.bach/run/bach

0 comments on commit 3bd6183

Please sign in to comment.