From ebad42cf99533fc8c3165da287a2e05771474c97 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 3 Jun 2022 10:46:26 +0200 Subject: [PATCH] Improved build script. --- .gitignore | 1 + build_system.d | 98 +++++++++++++++++-- .../rest/dto/component/in/SwitchPayload.java | 5 + .../service/ComponentService.java | 2 + 4 files changed, 98 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 0a528f5..114955f 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ build/ src/main/resources/app /build_system +/log diff --git a/build_system.d b/build_system.d index a64da60..9a626f7 100755 --- a/build_system.d +++ b/build_system.d @@ -1,6 +1,8 @@ #!/usr/bin/env dub /+ dub.sdl: dependency "dsh" version="~>1.6.1" + dependency "dxml" version="~>0.4.3" + dependency "requests" version="~>2.0.8" +/ /** @@ -11,27 +13,107 @@ module build_system; import dsh; +import dxml.dom; +import dxml.util; + import std.stdio; import std.string; +import std.algorithm; +import std.uni; const DIST = "./src/main/resources/app"; const DIST_ORIGIN = "./quasar-app/dist/spa"; +const APP_DIR = "./quasar-app"; const APP_BUILD = "quasar build -m spa"; const API_BUILD = "mvn clean package spring-boot:repackage -DskipTests=true"; -void main(string[] args) { - print("Building RailSignalAPI"); - chdir("quasar-app"); +const LOG_DIR = "./log"; +const API_LOG = LOG_DIR ~ "/api_build.txt"; +const APP_LOG = LOG_DIR ~ "/app_build.txt"; + +int main(string[] args) { + string ver = getVersion(); + if (ver is null) { + error("Could not determine version."); + return 1; + } + removeIfExists(LOG_DIR); + mkdir(LOG_DIR); + print("Building Rail Signal v%s", ver); + + if (args.length >= 2) { + string command = args[1].strip.toLower; + if (command == "app") { + buildApp(); + } else if (command == "api") { + buildApi(ver); + } else if (command == "all") { + buildApp(); + buildApi(ver); + if (args.length >= 3 && args[2].strip.toLower == "release") { + if (args.length >= 4) { + string token = args[3].strip(); + print("Are you sure you want to create a GitHub release for version %s?", ver); + string response = readln().strip.toLower; + if (response == "yes" || response == "y") createRelease(token, ver); + } else { + error("Missing required personal access token to create a GitHub release."); + return 1; + } + } + } + } else { + buildApp(); + buildApi(ver); + } + + return 0; +} + +/** + * Builds the production version of the frontend app and injects it into the + * API's resources to serve statically. + */ +void buildApp() { + chdir(APP_DIR); print("Building app..."); - runOrQuit(APP_BUILD); + runOrQuit(APP_BUILD, "." ~ APP_LOG); // Use an extra dot because we moved into app dir. print("Copying dist from %s to %s", DIST_ORIGIN, DIST); chdir(".."); removeIfExists(DIST); mkdir(DIST); copyDir(DIST_ORIGIN, DIST); - - print("Building API..."); - runOrQuit(API_BUILD); - print("Build complete!"); } +/** + * Builds the production version of the backend API. + */ +void buildApi(string ver) { + print("Building API..."); + runOrQuit(API_BUILD, API_LOG); + string[] jars = findFilesByExtension("target", ".jar", false); + string jarFile = jars[0]; + string finalJarFile = "./target/rail-signal-" ~ ver ~ ".jar"; + // Clean up the jar file name. + copy(jarFile, finalJarFile); + print("Build complete. Created %s", finalJarFile); +} + +/** + * Parses the version of the system from the pom file. + * Returns: The version string, or null if it couldn't be found. + */ +string getVersion() { + auto data = parseDOM!simpleXML(readText("pom.xml")); + auto root = data.children[0]; + foreach (child; root.children) { + if (child.name == "version") { + return child.children[0].text; + } + } + return null; +} + +void createRelease(string token, string ver) { + +} diff --git a/src/main/java/nl/andrewl/railsignalapi/rest/dto/component/in/SwitchPayload.java b/src/main/java/nl/andrewl/railsignalapi/rest/dto/component/in/SwitchPayload.java index 38d9f5c..fc3252d 100644 --- a/src/main/java/nl/andrewl/railsignalapi/rest/dto/component/in/SwitchPayload.java +++ b/src/main/java/nl/andrewl/railsignalapi/rest/dto/component/in/SwitchPayload.java @@ -4,6 +4,11 @@ import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; +/** + * A switch has a set of possible configurations, each of which links two + * different nodes. Most conventional switches have two configurations, but for + * universal compatibility, as many as 10 may be allowed. + */ public class SwitchPayload extends ComponentPayload { @NotNull @Size(max = 10) public SwitchConfigurationPayload[] possibleConfigurations; diff --git a/src/main/java/nl/andrewl/railsignalapi/service/ComponentService.java b/src/main/java/nl/andrewl/railsignalapi/service/ComponentService.java index d34536c..06a86ed 100644 --- a/src/main/java/nl/andrewl/railsignalapi/service/ComponentService.java +++ b/src/main/java/nl/andrewl/railsignalapi/service/ComponentService.java @@ -173,9 +173,11 @@ public class ComponentService { } private void updateConnectedNodes(PathNode owner, Set newNodes) { + // The set of all path nodes that will be disconnected from the owner. Set disconnected = new HashSet<>(owner.getConnectedNodes()); disconnected.removeAll(newNodes); + // The set of all path nodes that will be connected to the owner. Set connected = new HashSet<>(newNodes); connected.removeAll(owner.getConnectedNodes());