|
| 1 | +/** |
| 2 | + * Copyright (c) 2023-2025, AprilNEA LLC. |
| 3 | + * |
| 4 | + * Dual licensed under: |
| 5 | + * - GPL-3.0 (open source) |
| 6 | + * - Commercial license (contact us) |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * See LICENSE file for details or contact admin@aprilnea.com |
| 14 | + */ |
| 15 | + |
| 16 | +import { execSync } from "node:child_process"; |
| 17 | +import { appendFileSync, readFileSync, writeFileSync } from "node:fs"; |
| 18 | +import { platform } from "node:os"; |
| 19 | +import { join } from "node:path"; |
| 20 | + |
| 21 | +/** |
| 22 | + * Get the current version from package.json |
| 23 | + */ |
| 24 | +function getCurrentVersion(): string { |
| 25 | + const packageJsonPath = join(process.cwd(), "package.json"); |
| 26 | + const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")); |
| 27 | + return packageJson.version; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Get the short git commit hash |
| 32 | + */ |
| 33 | +function getShortCommitHash(): string { |
| 34 | + try { |
| 35 | + return execSync("git rev-parse --short HEAD", { encoding: "utf-8" }).trim(); |
| 36 | + } catch (error) { |
| 37 | + console.error("Failed to get git commit hash:", error); |
| 38 | + return "unknown"; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Set environment variable in GitHub Actions |
| 44 | + * Handles both Windows and Unix-like systems |
| 45 | + */ |
| 46 | +function setGitHubEnv(name: string, value: string): void { |
| 47 | + const githubEnvPath = process.env.GITHUB_ENV; |
| 48 | + if (githubEnvPath) { |
| 49 | + // GitHub Actions environment |
| 50 | + const envLine = `${name}=${value}`; |
| 51 | + appendFileSync(githubEnvPath, `${envLine}\n`, "utf-8"); |
| 52 | + console.log(`Set GitHub Actions environment variable: ${name}=${value}`); |
| 53 | + } else { |
| 54 | + // Local environment - just print for now |
| 55 | + console.log(`Export ${name}=${value}`); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Update version in various configuration files |
| 61 | + */ |
| 62 | +function updateVersionInFiles( |
| 63 | + version: string, |
| 64 | + updateConfigs: boolean = false, |
| 65 | +): void { |
| 66 | + if (!updateConfigs) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Update package.json |
| 71 | + const packageJsonPath = join(process.cwd(), "package.json"); |
| 72 | + const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")); |
| 73 | + const originalVersion = packageJson.version; |
| 74 | + packageJson.version = version; |
| 75 | + writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n"); |
| 76 | + console.log(`Updated package.json: ${originalVersion} → ${version}`); |
| 77 | + |
| 78 | + // Update Cargo.toml files |
| 79 | + const cargoFiles = [ |
| 80 | + "dev-utility/Cargo.toml", |
| 81 | + "dev-utility-tauri/Cargo.toml", |
| 82 | + "dev-utility-workers/Cargo.toml", |
| 83 | + ]; |
| 84 | + |
| 85 | + for (const cargoFile of cargoFiles) { |
| 86 | + const cargoPath = join(process.cwd(), cargoFile); |
| 87 | + try { |
| 88 | + let cargoContent = readFileSync(cargoPath, "utf-8"); |
| 89 | + const originalContent = cargoContent; |
| 90 | + |
| 91 | + // Update version in [package] section |
| 92 | + cargoContent = cargoContent.replace( |
| 93 | + /^version = ".*"$/m, |
| 94 | + `version = "${version}"`, |
| 95 | + ); |
| 96 | + |
| 97 | + if (originalContent !== cargoContent) { |
| 98 | + writeFileSync(cargoPath, cargoContent); |
| 99 | + console.log(`Updated ${cargoFile}`); |
| 100 | + } |
| 101 | + } catch (error) { |
| 102 | + console.warn(`Could not update ${cargoFile}:`, error); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Check if tauri.conf.json has a version field to update |
| 107 | + const tauriConfPath = join( |
| 108 | + process.cwd(), |
| 109 | + "dev-utility-tauri/tauri.conf.json", |
| 110 | + ); |
| 111 | + try { |
| 112 | + const tauriConf = JSON.parse(readFileSync(tauriConfPath, "utf-8")); |
| 113 | + |
| 114 | + // Only update if version is not pointing to package.json |
| 115 | + if (tauriConf.version && tauriConf.version !== "../package.json") { |
| 116 | + tauriConf.version = version; |
| 117 | + writeFileSync(tauriConfPath, JSON.stringify(tauriConf, null, 2) + "\n"); |
| 118 | + console.log(`Updated tauri.conf.json version to ${version}`); |
| 119 | + } |
| 120 | + } catch (error) { |
| 121 | + console.warn("Could not update tauri.conf.json:", error); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Set the build version based on: |
| 127 | + * 1. Custom version from command line argument |
| 128 | + * 2. Custom version from GitHub Actions input |
| 129 | + * 3. Custom version from environment variable |
| 130 | + * 4. Current version from package.json + git commit hash |
| 131 | + */ |
| 132 | +function setBuildVersion(): string { |
| 133 | + // Check for command line arguments |
| 134 | + const args = process.argv.slice(2); |
| 135 | + const customVersionFromArgs = args.find((arg) => !arg.startsWith("--")); |
| 136 | + const shouldUpdateFiles = args.includes("--update-files"); |
| 137 | + |
| 138 | + // Check for custom version from GitHub Actions input |
| 139 | + // This is automatically set by GitHub Actions as an environment variable |
| 140 | + const customVersionFromInput = process.env.INPUT_VERSION; |
| 141 | + |
| 142 | + // Check for custom version from environment variable (for CI/CD) |
| 143 | + const customVersionFromEnv = process.env.CUSTOM_VERSION; |
| 144 | + |
| 145 | + let version: string; |
| 146 | + let isCustomVersion = true; |
| 147 | + |
| 148 | + if (customVersionFromArgs) { |
| 149 | + version = customVersionFromArgs; |
| 150 | + console.log(`Using custom version from command line: ${version}`); |
| 151 | + } else if (customVersionFromInput && customVersionFromInput.trim()) { |
| 152 | + version = customVersionFromInput.trim(); |
| 153 | + console.log(`Using custom version from GitHub Actions input: ${version}`); |
| 154 | + } else if (customVersionFromEnv) { |
| 155 | + version = customVersionFromEnv; |
| 156 | + console.log(`Using custom version from environment: ${version}`); |
| 157 | + } else { |
| 158 | + // Get current version and add commit hash |
| 159 | + const currentVersion = getCurrentVersion(); |
| 160 | + const commitHash = getShortCommitHash(); |
| 161 | + version = `${currentVersion}-${commitHash}`; |
| 162 | + console.log(`Generated version: ${version}`); |
| 163 | + isCustomVersion = false; |
| 164 | + } |
| 165 | + |
| 166 | + // Update files if requested |
| 167 | + if (shouldUpdateFiles && isCustomVersion) { |
| 168 | + updateVersionInFiles(version, true); |
| 169 | + } |
| 170 | + |
| 171 | + // Export to environment variable for other scripts to use |
| 172 | + setGitHubEnv("CUSTOM_VERSION", version); |
| 173 | + |
| 174 | + return version; |
| 175 | +} |
| 176 | + |
| 177 | +// Show usage if --help is passed |
| 178 | +if (process.argv.includes("--help")) { |
| 179 | + console.log(` |
| 180 | +Usage: tsx scripts/build-version.ts [version] [options] |
| 181 | +
|
| 182 | +Arguments: |
| 183 | + version Custom version to set (optional) |
| 184 | +
|
| 185 | +Options: |
| 186 | + --update-files Update version in package.json and Cargo.toml files |
| 187 | + --help Show this help message |
| 188 | +
|
| 189 | +Examples: |
| 190 | + tsx scripts/build-version.ts # Generate version with git hash |
| 191 | + tsx scripts/build-version.ts 1.2.3 # Set custom version |
| 192 | + tsx scripts/build-version.ts 1.2.3 --update-files # Set and update all files |
| 193 | +
|
| 194 | +Environment variables: |
| 195 | + CUSTOM_VERSION Custom version (alternative to command line) |
| 196 | + INPUT_VERSION GitHub Actions input version |
| 197 | + GITHUB_ENV GitHub Actions environment file path |
| 198 | +`); |
| 199 | + process.exit(0); |
| 200 | +} |
| 201 | + |
| 202 | +// Main execution |
| 203 | +const version = setBuildVersion(); |
| 204 | +console.log(`Custom version set to: ${version}`); |
| 205 | + |
| 206 | +// Exit with version for shell scripts to capture |
| 207 | +process.stdout.write(version); |
0 commit comments