RailSignalAPI/build_system.d

174 lines
5.2 KiB
D
Raw Normal View History

#!/usr/bin/env dub
/+ dub.sdl:
dependency "dsh" version="~>1.6.1"
2022-06-03 08:46:26 +00:00
dependency "dxml" version="~>0.4.3"
dependency "requests" version="~>2.0.8"
2022-06-03 10:00:23 +00:00
dependency "d-properties" version="~>1.0.4"
+/
/**
* This script will build the Rail Signal Vue app, then bundle it into this
* Spring project's files under src/main/resources/app/, and will then build
* this project into a jar file.
*/
module build_system;
import dsh;
2022-06-03 08:46:26 +00:00
import dxml.dom;
import dxml.util;
import std.stdio;
import std.string;
2022-06-03 08:46:26 +00:00
import std.algorithm;
import std.uni;
const DIST = "./src/main/resources/app";
const DIST_ORIGIN = "./quasar-app/dist/spa";
2022-06-03 08:46:26 +00:00
const APP_DIR = "./quasar-app";
const APP_BUILD = "quasar build -m spa";
const API_BUILD = "mvn clean package spring-boot:repackage -DskipTests=true";
2022-06-03 08:46:26 +00:00
const LOG_DIR = "./log";
const API_LOG = LOG_DIR ~ "/api_build.txt";
const APP_LOG = LOG_DIR ~ "/app_build.txt";
const GITHUB_PROPS_FILE = "github_token.properties";
2022-06-03 08:46:26 +00:00
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") {
2022-06-03 10:00:23 +00:00
print("Are you sure you want to create a GitHub release for version %s?", ver);
string response = readln().strip.toLower;
2022-06-03 11:12:07 +00:00
if (response == "yes" || response == "y") {
print("Please enter a short description for this release.");
string description = readln().strip;
createRelease(ver, description);
}
2022-06-03 08:46:26 +00:00
}
}
} 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...");
2022-06-03 08:46:26 +00:00
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);
2022-06-03 08:46:26 +00:00
}
2022-06-03 08:46:26 +00:00
/**
* Builds the production version of the backend API.
*/
void buildApi(string ver) {
print("Building API...");
2022-06-03 08:46:26 +00:00
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;
}
2022-06-03 11:12:07 +00:00
/**
* Creates a new GitHub release using the specified version, and uploads the
* JAR file to the release.
* Params:
* ver = The version.
*/
void createRelease(string ver, string description) {
2022-06-03 10:00:23 +00:00
import d_properties;
import requests;
import std.json;
print("Creating release...");
JSONValue data = [
"tag_name": "v" ~ ver,
"name": "Rail Signal v" ~ ver,
2022-06-03 11:12:07 +00:00
"body": description
2022-06-03 10:00:23 +00:00
];
data.object["prerelease"] = JSONValue(false);
2022-06-03 10:00:23 +00:00
data.object["generate_release_notes"] = JSONValue(false);
auto rq = Request();
auto props = Properties(GITHUB_PROPS_FILE);
2022-06-03 10:00:23 +00:00
string username = props["username"];
string token = props["token"];
rq.authenticator = new BasicAuthentication(username, token);
auto response = rq.post(
"https://api.github.com/repos/andrewlalis/RailSignalAPI/releases",
data.toString,
"application/json"
);
if (response.code == 201) {
string responseBody = cast(string) response.responseBody;
JSONValue responseData = parseJSON(responseBody);
print("Created release %s", responseData["url"].str);
long releaseId = responseData["id"].integer;
string uploadUrl = format!"https://uploads.github.com/repos/andrewlalis/RailSignalAPI/releases/%d/assets?name=%s"(
releaseId,
"rail-signal-" ~ ver ~ ".jar"
2022-06-03 10:00:23 +00:00
);
print("Uploading JAR file to %s", uploadUrl);
auto f = File("./target/rail-signal-" ~ ver ~ ".jar", "rb");
ulong assetSize = f.size();
rq.addHeaders(["Content-Length": format!"%d"(assetSize)]);
auto assetResponse = rq.post(uploadUrl, f.byChunk(4096));
if (assetResponse.code == 201) {
print("JAR file uploaded successfully.");
} else {
error("An error occurred while uploading the JAR file.");
}
2022-06-03 10:00:23 +00:00
} else {
error("An error occurred while creating the release.");
2022-06-03 10:00:23 +00:00
writeln(response.responseBody);
}
2022-06-03 08:46:26 +00:00
}
2022-06-03 10:00:23 +00:00