From 820a5caccb9be6f1277df76ff3f67937c127ec7b Mon Sep 17 00:00:00 2001 From: DrakiaXYZ Date: Thu, 18 Apr 2024 07:54:16 +0000 Subject: [PATCH] Bake build data directly into the executable (!297) This is primarily to stop confusion when a user overwrites their `aki_data` folder with an old version, the data shown in logs/server console is now based on compile time data instead of runtime data. - New build.json file added to the `obj/ide/` folder that gets populated with the build data on build - Moved asset copying prior to packaging, so that `obj/ide/build.json` is available at package time - Updated all references of core.commit, and core.buildTime to use globalThis - Updated all references of core.akiVersion to use globalThis with a fallback if not found (When running in VSCode for example) Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/297 Co-authored-by: DrakiaXYZ Co-committed-by: DrakiaXYZ --- project/gulpfile.mjs | 33 +++++++++++++---------- project/src/controllers/GameController.ts | 4 ++- project/src/ide/BleedingEdgeEntry.ts | 5 ++++ project/src/ide/BleedingEdgeModsEntry.ts | 5 ++++ project/src/ide/DebugEntry.ts | 5 ++++ project/src/ide/ReleaseEntry.ts | 5 ++++ project/src/ide/TestEntry.ts | 7 ++++- project/src/ide/build.json | 5 ++++ project/src/loaders/PreAkiModLoader.ts | 2 +- project/src/servers/ConfigServer.ts | 4 +-- project/src/utils/App.ts | 10 +++---- project/src/utils/Watermark.ts | 10 ++++--- 12 files changed, 67 insertions(+), 28 deletions(-) create mode 100644 project/src/ide/build.json diff --git a/project/gulpfile.mjs b/project/gulpfile.mjs index 0b09ed47..32a27680 100644 --- a/project/gulpfile.mjs +++ b/project/gulpfile.mjs @@ -155,27 +155,32 @@ const downloadPnpm = async () => const copyLicense = () => gulp.src([licenseFile]).pipe(rename("LICENSE-Server.txt")).pipe(gulp.dest(buildDir)); /** - * Writes the latest Git commit hash to the core.json configuration file. + * Writes the latest build data to the core.json and build.json configuration files. */ -const writeCommitHashToCoreJSON = async () => +const writeBuildDataToJSON = async () => { try { - const coreJSONPath = path.resolve(dataDir, "configs", "core.json"); - const coreJSON = await fs.readFile(coreJSONPath, "utf8"); - const parsed = JSON.parse(coreJSON); - // Fetch the latest Git commit hash const gitResult = await exec("git rev-parse HEAD", { stdout: "pipe" }); - // Update the commit hash in the core.json object - parsed.commit = gitResult.stdout.trim() || ""; + // Update core.json + const coreJSONPath = path.resolve(dataDir, "configs", "core.json"); + const coreJSON = await fs.readFile(coreJSONPath, "utf8"); + const coreParsed = JSON.parse(coreJSON); - // Add build timestamp - parsed.buildTime = new Date().getTime(); + coreParsed.commit = gitResult.stdout.trim() || ""; + coreParsed.buildTime = new Date().getTime(); + await fs.writeFile(coreJSONPath, JSON.stringify(coreParsed, null, 4)); - // Write the updated object back to core.json - await fs.writeFile(coreJSONPath, JSON.stringify(parsed, null, 4)); + // Write build.json + const buildJsonPath = path.join("obj", "ide", "build.json"); + const buildInfo = {}; + + buildInfo.commit = coreParsed.commit; + buildInfo.buildTime = coreParsed.buildTime; + buildInfo.akiVersion = coreParsed.akiVersion; + await fs.writeFile(buildJsonPath, JSON.stringify(buildInfo, null, 4)); } catch (error) { @@ -195,7 +200,7 @@ const createHashFile = async () => }; // Combine all tasks into addAssets -const addAssets = gulp.series(copyAssets, downloadPnpm, copyLicense, writeCommitHashToCoreJSON, createHashFile); +const addAssets = gulp.series(copyAssets, downloadPnpm, copyLicense, writeBuildDataToJSON, createHashFile); /** * Cleans the build directory. @@ -313,9 +318,9 @@ const build = (packagingType) => cleanBuild, validateJSONs, compile, + addAssets, fetchPackageImage, anonPackaging, - addAssets, updateBuildProperties, cleanCompiled, ]; diff --git a/project/src/controllers/GameController.ts b/project/src/controllers/GameController.ts index ac7e9dbe..cafdf078 100644 --- a/project/src/controllers/GameController.ts +++ b/project/src/controllers/GameController.ts @@ -964,7 +964,9 @@ export class GameController protected logProfileDetails(fullProfile: IAkiProfile): void { this.logger.debug(`Profile made with: ${fullProfile.aki.version}`); - this.logger.debug(`Server version: ${this.coreConfig.akiVersion} ${this.coreConfig.commit}`); + this.logger.debug( + `Server version: ${globalThis.G_AKIVERSION || this.coreConfig.akiVersion} ${globalThis.G_COMMIT}`, + ); this.logger.debug(`Debug enabled: ${globalThis.G_DEBUG_CONFIGURATION}`); this.logger.debug(`Mods enabled: ${globalThis.G_MODS_ENABLED}`); } diff --git a/project/src/ide/BleedingEdgeEntry.ts b/project/src/ide/BleedingEdgeEntry.ts index c177c186..94cdafdd 100644 --- a/project/src/ide/BleedingEdgeEntry.ts +++ b/project/src/ide/BleedingEdgeEntry.ts @@ -2,6 +2,7 @@ import "reflect-metadata"; import "source-map-support/register"; import { Program } from "@spt-aki/Program"; +import * as buildInfo from "./build.json"; globalThis.G_DEBUG_CONFIGURATION = true; globalThis.G_RELEASE_CONFIGURATION = true; @@ -10,5 +11,9 @@ globalThis.G_MODS_TRANSPILE_TS = true; globalThis.G_LOG_REQUESTS = true; globalThis.G_WATERMARK_ENABLED = true; +globalThis.G_AKIVERSION = buildInfo.akiVersion; +globalThis.G_COMMIT = buildInfo.commit; +globalThis.G_BUILDTIME = buildInfo.buildTime; + const program = new Program(); program.start(); diff --git a/project/src/ide/BleedingEdgeModsEntry.ts b/project/src/ide/BleedingEdgeModsEntry.ts index b90cf77f..badeb416 100644 --- a/project/src/ide/BleedingEdgeModsEntry.ts +++ b/project/src/ide/BleedingEdgeModsEntry.ts @@ -2,6 +2,7 @@ import "reflect-metadata"; import "source-map-support/register"; import { Program } from "@spt-aki/Program"; +import * as buildInfo from "./build.json"; globalThis.G_DEBUG_CONFIGURATION = true; globalThis.G_RELEASE_CONFIGURATION = true; @@ -10,5 +11,9 @@ globalThis.G_MODS_TRANSPILE_TS = true; globalThis.G_LOG_REQUESTS = true; globalThis.G_WATERMARK_ENABLED = true; +globalThis.G_AKIVERSION = buildInfo.akiVersion; +globalThis.G_COMMIT = buildInfo.commit; +globalThis.G_BUILDTIME = buildInfo.buildTime; + const program = new Program(); program.start(); diff --git a/project/src/ide/DebugEntry.ts b/project/src/ide/DebugEntry.ts index 33a37e75..5c4377ea 100644 --- a/project/src/ide/DebugEntry.ts +++ b/project/src/ide/DebugEntry.ts @@ -2,6 +2,7 @@ import "reflect-metadata"; import "source-map-support/register"; import { Program } from "@spt-aki/Program"; +import * as buildInfo from "./build.json"; globalThis.G_DEBUG_CONFIGURATION = true; globalThis.G_RELEASE_CONFIGURATION = true; @@ -10,5 +11,9 @@ globalThis.G_MODS_TRANSPILE_TS = true; globalThis.G_LOG_REQUESTS = true; globalThis.G_WATERMARK_ENABLED = false; +globalThis.G_AKIVERSION = buildInfo.akiVersion; +globalThis.G_COMMIT = buildInfo.commit; +globalThis.G_BUILDTIME = buildInfo.buildTime; + const program = new Program(); program.start(); diff --git a/project/src/ide/ReleaseEntry.ts b/project/src/ide/ReleaseEntry.ts index e45141dc..230e7d32 100644 --- a/project/src/ide/ReleaseEntry.ts +++ b/project/src/ide/ReleaseEntry.ts @@ -2,6 +2,7 @@ import "reflect-metadata"; import "source-map-support/register"; import { Program } from "@spt-aki/Program"; +import * as buildInfo from "./build.json"; globalThis.G_DEBUG_CONFIGURATION = false; globalThis.G_RELEASE_CONFIGURATION = true; @@ -10,5 +11,9 @@ globalThis.G_MODS_TRANSPILE_TS = true; globalThis.G_LOG_REQUESTS = false; globalThis.G_WATERMARK_ENABLED = false; +globalThis.G_AKIVERSION = buildInfo.akiVersion; +globalThis.G_COMMIT = buildInfo.commit; +globalThis.G_BUILDTIME = buildInfo.buildTime; + const program = new Program(); program.start(); diff --git a/project/src/ide/TestEntry.ts b/project/src/ide/TestEntry.ts index 9a1c53f4..e1ff4e4a 100644 --- a/project/src/ide/TestEntry.ts +++ b/project/src/ide/TestEntry.ts @@ -2,13 +2,18 @@ import "reflect-metadata"; import "source-map-support/register"; import { Program } from "@spt-aki/Program"; +import * as buildInfo from "./build.json"; globalThis.G_DEBUG_CONFIGURATION = true; globalThis.G_RELEASE_CONFIGURATION = false; globalThis.G_MODS_ENABLED = true; globalThis.G_MODS_TRANSPILE_TS = false; globalThis.G_LOG_REQUESTS = true; -globalThis.G_WATERMARK_ENABLED = true; +globalThis.G_WATERMARK_ENABLED = false; + +globalThis.G_AKIVERSION = buildInfo.akiVersion; +globalThis.G_COMMIT = buildInfo.commit; +globalThis.G_BUILDTIME = buildInfo.buildTime; const program = new Program(); program.start(); diff --git a/project/src/ide/build.json b/project/src/ide/build.json new file mode 100644 index 00000000..999634cd --- /dev/null +++ b/project/src/ide/build.json @@ -0,0 +1,5 @@ +{ + "akiVersion": "", + "commit": "", + "buildTime": 0 +} diff --git a/project/src/loaders/PreAkiModLoader.ts b/project/src/loaders/PreAkiModLoader.ts index f566a235..df9ffc74 100644 --- a/project/src/loaders/PreAkiModLoader.ts +++ b/project/src/loaders/PreAkiModLoader.ts @@ -329,7 +329,7 @@ export class PreAkiModLoader implements IModLoader */ protected isModCombatibleWithAki(mod: IPackageJsonData): boolean { - const akiVersion = this.akiConfig.akiVersion; + const akiVersion = globalThis.G_AKIVERSION || this.akiConfig.akiVersion; const modName = `${mod.author}-${mod.name}`; // Error and prevent loading If no akiVersion property exists diff --git a/project/src/servers/ConfigServer.ts b/project/src/servers/ConfigServer.ts index 85c4cb6f..ccd67d62 100644 --- a/project/src/servers/ConfigServer.ts +++ b/project/src/servers/ConfigServer.ts @@ -53,7 +53,7 @@ export class ConfigServer } } - this.logger.info(`Commit hash: ${(this.configs[ConfigTypes.CORE] as ICoreConfig).commit || "DEBUG"}`); - this.logger.info(`Build date: ${(this.configs[ConfigTypes.CORE] as ICoreConfig).buildTime || "DEBUG"}`); + this.logger.info(`Commit hash: ${globalThis.G_COMMIT || "DEBUG"}`); + this.logger.info(`Build date: ${globalThis.G_BUILDTIME || "DEBUG"}`); } } diff --git a/project/src/utils/App.ts b/project/src/utils/App.ts index 235926b4..e3152ebb 100644 --- a/project/src/utils/App.ts +++ b/project/src/utils/App.ts @@ -42,15 +42,15 @@ export class App this.logger.debug(`RAM: ${(os.totalmem() / 1024 / 1024 / 1024).toFixed(2)}GB`); this.logger.debug(`PATH: ${this.encodingUtil.toBase64(process.argv[0])}`); this.logger.debug(`PATH: ${this.encodingUtil.toBase64(process.execPath)}`); - this.logger.debug(`Server: ${this.coreConfig.akiVersion}`); - if (this.coreConfig.buildTime) + this.logger.debug(`Server: ${globalThis.G_AKIVERSION || this.coreConfig.akiVersion}`); + if (globalThis.G_BUILDTIME) { - this.logger.debug(`Date: ${this.coreConfig.buildTime}`); + this.logger.debug(`Date: ${globalThis.G_BUILDTIME}`); } - if (this.coreConfig.commit) + if (globalThis.G_COMMIT) { - this.logger.debug(`Commit: ${this.coreConfig.commit}`); + this.logger.debug(`Commit: ${globalThis.G_COMMIT}`); } for (const onLoad of this.onLoadComponents) diff --git a/project/src/utils/Watermark.ts b/project/src/utils/Watermark.ts index 91edd475..b3a9f77e 100644 --- a/project/src/utils/Watermark.ts +++ b/project/src/utils/Watermark.ts @@ -108,9 +108,10 @@ export class Watermark */ public getVersionTag(withEftVersion = false): string { + const akiVersion = globalThis.G_AKIVERSION || this.akiConfig.akiVersion; const versionTag = (globalThis.G_DEBUG_CONFIGURATION) - ? `${this.akiConfig.akiVersion} - ${this.localisationService.getText("bleeding_edge_build")}` - : this.akiConfig.akiVersion; + ? `${akiVersion} - ${this.localisationService.getText("bleeding_edge_build")}` + : akiVersion; if (withEftVersion) { @@ -128,9 +129,10 @@ export class Watermark */ public getInGameVersionLabel(): string { + const akiVersion = globalThis.G_AKIVERSION || this.akiConfig.akiVersion; const versionTag = (globalThis.G_DEBUG_CONFIGURATION) - ? `${this.akiConfig.akiVersion} - BLEEDINGEDGE ${this.akiConfig.commit?.slice(0, 6) ?? ""}` - : `${this.akiConfig.akiVersion} - ${this.akiConfig.commit?.slice(0, 6) ?? ""}`; + ? `${akiVersion} - BLEEDINGEDGE ${globalThis.G_COMMIT?.slice(0, 6) ?? ""}` + : `${akiVersion} - ${globalThis.G_COMMIT?.slice(0, 6) ?? ""}`; return `${this.akiConfig.projectName} ${versionTag}`; }