From 6ae284b9b61558078e9003defc50966c3e41e45c Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 5 Feb 2021 09:47:22 +0100 Subject: Better ninja rules. --- example/engine.ninja | 24 ++++------- .../io/trygvis/rules/engine/cli/EngineFile.java | 36 ++++++++++++++++ .../io/trygvis/rules/engine/cli/NinjaCommand.java | 49 +++++++++++++++------- 3 files changed, 80 insertions(+), 29 deletions(-) diff --git a/example/engine.ninja b/example/engine.ninja index 3c53218..046b779 100644 --- a/example/engine.ninja +++ b/example/engine.ninja @@ -19,35 +19,29 @@ build engine.dot: ninja-to-dot build.ninja engine.ninja build engine.png: dot-to-png engine.dot -# Jobs - -rule acme +rule engine-run command=engine run $name $inputs $output_state $output_includes $generated_output $agenda_groups $modules -build db/acme.yaml: acme +### Jobs + +build db/acme.yaml: engine-run name=--name acme - output_state=--output-state db/acme.yaml + output_state=--output-state acme modules=$ --module=foo $ --module=bar -rule acme-apps - command=engine run $name $inputs $output_state $output_includes $generated_output $agenda_groups $modules - -build db/acme-apps.yaml: acme-apps db/acme.yaml +build db/acme-apps.yaml: engine-run db/acme.yaml name=--name acme-apps inputs=$ --input=db/acme.yaml - output_state=--output-state db/acme-apps.yaml - -rule acme-wireguard - command=engine run $name $inputs $output_state $output_includes $generated_output $agenda_groups $modules + output_state=--output-state acme-apps -build db/acme-wireguard.yaml: acme-wireguard db/acme.yaml +build db/acme-wireguard.yaml: engine-run db/acme.yaml name=--name acme-wireguard inputs=$ --input=db/acme.yaml - output_state=--output-state db/acme-wireguard.yaml + output_state=--output-state acme-wireguard modules=$ --module=foo $ --module=$$MODULE_HOME/bar diff --git a/src/ri-engine/src/main/java/io/trygvis/rules/engine/cli/EngineFile.java b/src/ri-engine/src/main/java/io/trygvis/rules/engine/cli/EngineFile.java index 8da0e39..bae14f7 100644 --- a/src/ri-engine/src/main/java/io/trygvis/rules/engine/cli/EngineFile.java +++ b/src/ri-engine/src/main/java/io/trygvis/rules/engine/cli/EngineFile.java @@ -2,6 +2,7 @@ package io.trygvis.rules.engine.cli; import java.util.ArrayList; import java.util.List; +import java.util.StringJoiner; public class EngineFile { public String dbDir; @@ -10,9 +11,44 @@ public class EngineFile { public static class Job { public String name; public List inputs = new ArrayList<>(); + public List outputs = new ArrayList<>(); public List outputIncludes = new ArrayList<>(); public String generatedOutput; public List agendaGroups = new ArrayList<>(); public List modules = new ArrayList<>(); + + @Override + public String toString() { + return new StringJoiner(",\n", Job.class.getSimpleName() + "[", "]") + .add("name='" + name + "'") + .add("inputs=" + inputs) + .add("outputs=" + outputs) + .add("outputIncludes=" + outputIncludes) + .add("generatedOutput='" + generatedOutput + "'") + .add("agendaGroups=" + agendaGroups) + .add("modules=" + modules) + .toString(); + } + } + + public static class Output { + public String name; + public List includes = new ArrayList<>(); + + @Override + public String toString() { + return new StringJoiner(",\n", Output.class.getSimpleName() + "[", "]") + .add("name='" + name + "'") + .add("includes=" + includes) + .toString(); + } + } + + @Override + public String toString() { + return new StringJoiner(",\n", EngineFile.class.getSimpleName() + "[", "]") + .add("dbDir='" + dbDir + "'") + .add("jobs=" + jobs) + .toString(); } } diff --git a/src/ri-engine/src/main/java/io/trygvis/rules/engine/cli/NinjaCommand.java b/src/ri-engine/src/main/java/io/trygvis/rules/engine/cli/NinjaCommand.java index ef5ed1d..9e9d5f9 100644 --- a/src/ri-engine/src/main/java/io/trygvis/rules/engine/cli/NinjaCommand.java +++ b/src/ri-engine/src/main/java/io/trygvis/rules/engine/cli/NinjaCommand.java @@ -11,11 +11,11 @@ import java.io.FileWriter; import java.io.PrintWriter; import java.io.StringWriter; import java.nio.file.Path; -import java.util.List; import java.util.Objects; import java.util.concurrent.Callable; import java.util.stream.Collectors; +import static io.trygvis.rules.engine.cli.EngineFile.*; import static picocli.CommandLine.Command; @Command(name = "ninja") @@ -45,6 +45,23 @@ public class NinjaCommand implements Callable { dbDir = Path.of(f.dbDir); } + for (var job : f.jobs) { + if (job.outputs.isEmpty()) { + var output = new Output(); + output.name = job.name; + output.includes = job.outputIncludes; + job.outputIncludes = null; + + job.outputs.add(output); + } + } + + if (false) { + System.out.println("----------------------------------------------------"); + System.out.println(f); + System.out.println("----------------------------------------------------"); + } + var buf = new StringWriter(); var out = new PrintWriter(buf); @@ -69,25 +86,22 @@ public class NinjaCommand implements Callable { out.println(""); out.println("build engine.png: dot-to-png engine.dot"); out.println(""); - out.println("# Jobs"); + out.println("rule engine-run"); + out.println(" command=engine run $name $inputs $output_state $output_includes $generated_output $agenda_groups $modules"); + out.println(); + out.println("### Jobs"); for (var job : f.jobs) { out.println(""); - - out.println("rule %s".formatted(job.name)); - out.println(" command=engine run $name $inputs $output_state $output_includes $generated_output $agenda_groups $modules"); - out.println(); var dependencies = job.inputs.stream() .map(s -> dbDir.resolve(s + ".yaml").toString()) .collect(Collectors.joining(" ")); - var outputState = dbDir.resolve(job.name + ".yaml"); - - var generated = List.of(outputState).stream() - .map(Path::toString) + var generated = job.outputs.stream() + .map(output -> dbDir.resolve(output.name + ".yaml").toString()) .collect(Collectors.joining(" ")); - out.println("build %s: %s %s".formatted(generated, job.name, dependencies)); + out.println("build %s: engine-run %s".formatted(generated, dependencies)); out.println(" name=--name %s".formatted(job.name)); if (!job.inputs.isEmpty()) { @@ -98,9 +112,16 @@ public class NinjaCommand implements Callable { out.println(" inputs=$%s".formatted(is)); } - out.println(" output_state=--output-state %s".formatted(outputState)); - if (!job.outputIncludes.isEmpty()) { - var str = job.outputIncludes.stream() + if (job.outputs.size() != 1) { + System.err.println("Only one output supported for now."); + return 1; + } + + var output = job.outputs.get(0); + + out.println(" output_state=--output-state %s".formatted(dbDir.resolve(output.name + ".yaml"))); + if (!output.includes.isEmpty()) { + var str = output.includes.stream() .map(s -> "--output-include=" + s) .collect(Collectors.joining(" $\n ", "\n ", "")); -- cgit v1.2.3