From faae6ccf975e840a9870bcd895564d43db38ebf4 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sun, 6 Dec 2020 17:04:25 +0100 Subject: [PATCH] Rework mod build system to be multi-version capable Add a variants array which can specify overriden properties of the info.json for building multiple versions of the mod at the same time and fix the additonal_files to allow specifying under what path the file is put. Together these two changes allow setting up the build to do all supported versions at the same time and add specialized code for each version of Factorio supported. --- packages/lib/build_mod.js | 34 +++++++++++++--------- packages/slave/lua/clusterio_lib/info.json | 18 ++++++++---- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/packages/lib/build_mod.js b/packages/lib/build_mod.js index 6fa8822f..4ea89c51 100644 --- a/packages/lib/build_mod.js +++ b/packages/lib/build_mod.js @@ -13,11 +13,10 @@ async function main() { .scriptName("build") .options({ "clean": { describe: "Remove previous builds", type: "boolean", default: false }, - "build": { describe: "Build mod", type: "boolean", default: true }, + "build": { describe: "Build mod(s)", type: "boolean", default: true }, "pack": { describe: "Pack into zip file", type: "boolean", default: true }, "source-dir": { describe: "Path to mod source files", nargs: 1, type: "string", demandOption: true }, - "output-dir": { describe: "Path to output built mod", nargs: 1, type: "string", default: "dist" }, - "bump-patch": { describe: "Increment patch number of build", type: "boolean", default: false }, + "output-dir": { describe: "Path to output built mod(s)", nargs: 1, type: "string", default: "dist" }, "factorio-version": { describe: "Override factorio_version", type: "string" }, }) .strict() @@ -30,12 +29,6 @@ async function main() { async function build(args) { let info = JSON.parse(await fs.readFile(path.join(args.sourceDir, "info.json"))); - if (args.bumpPatch) { - let [major, minor, patch] = info.version.split("."); - patch = String(Number.parseInt(patch, 10) + 1); - info.version = [major, minor, patch].join("."); - } - if (args.factorioVersion) { info.factorio_version = args.factorioVersion; } @@ -59,6 +52,21 @@ async function build(args) { } } + if (info.variants) { + for (let [variant, variantOverrides] of Object.entries(info.variants)) { + let variantInfo = { + ...info, + ...variantOverrides, + }; + delete variantInfo.variants; + await buildMod(args, variantInfo); + } + } else { + await buildMod(args, info); + } +} + +async function buildMod(args, info) { if (args.build) { await fs.ensureDir(args.outputDir); let modName = `${info.name}_${info.version}`; @@ -78,12 +86,12 @@ async function build(args) { ; await events.once(walker, "end"); - for (let pathParts of info.additional_files || []) { + for (let [fileName, pathParts] of Object.entries(info.additional_files || {})) { let filePath = path.join(args.sourceDir, ...pathParts); if (!await fs.pathExists(filePath)) { throw new Error(`Additional file ${filePath} does not exist`); } - zip.file(path.posix.join(modName, path.basename(filePath)), fs.createReadStream(filePath)); + zip.file(path.posix.join(modName, fileName), fs.createReadStream(filePath)); } delete info.additional_files; @@ -102,9 +110,9 @@ async function build(args) { } console.log(`Building ${modDir}`); await fs.copy(args.sourceDir, modDir); - for (let pathParts of info.additional_files || []) { + for (let [fileName, pathParts] of Object.entries(info.additional_files) || []) { let filePath = path.join(...pathParts); - await fs.copy(filePath, path.join(modDir, path.basename(filePath))); + await fs.copy(filePath, path.join(modDir, fileName)); } delete info.additional_files; diff --git a/packages/slave/lua/clusterio_lib/info.json b/packages/slave/lua/clusterio_lib/info.json index b66e2a2e..64b04a33 100644 --- a/packages/slave/lua/clusterio_lib/info.json +++ b/packages/slave/lua/clusterio_lib/info.json @@ -1,12 +1,20 @@ { "name": "clusterio_lib", - "version": "0.1.1", + "variants": [ + { + "version": "0.1.0", + "factorio_version": "0.17" + }, + { + "version": "0.1.1", + "factorio_version": "0.18" + } + ], "title": "Clusterio Library (Alpha)", "author": "Clusterio Team", "homepage": "https://github.com/clusterio/factorioClusterio", "description": "Library for interfacing with Clusterio. Warning: work in progress alpha.", - "factorio_version": "0.17", - "additional_files": [ - ["..", "..", "modules", "clusterio", "serialize.lua"] - ] + "additional_files": { + "serialize.lua": ["..", "..", "modules", "clusterio", "serialize.lua"] + } }