|
| 1 | +// Copyright © 2025-2026 OpenVCS Contributors |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | + |
| 4 | +/** |
| 5 | + * Bumps the Tauri package version for CI prerelease builds. |
| 6 | + * |
| 7 | + * Uses cargo metadata to read the current version from Backend/Cargo.toml, |
| 8 | + * increments the patch component, appends a prerelease suffix with the GitHub |
| 9 | + * run number, and writes back to the manifest. |
| 10 | + * |
| 11 | + * Usage: node scripts/bump-tauri-version.js <channel> |
| 12 | + * channel: 'alpha' for nightly, 'beta' for beta releases |
| 13 | + * |
| 14 | + * Environment: |
| 15 | + * GITHUB_RUN_NUMBER - used for monotonic versioning (set by GitHub Actions) |
| 16 | + */ |
| 17 | + |
| 18 | +const fs = require('fs'); |
| 19 | +const path = require('path'); |
| 20 | +const { execFileSync } = require('child_process'); |
| 21 | + |
| 22 | +const CARGO_PATH = path.resolve(__dirname, '../Backend/Cargo.toml'); |
| 23 | + |
| 24 | +/** |
| 25 | + * Read the package version using cargo metadata (TOML-aware). |
| 26 | + * @returns {string} Current version string (e.g., "0.3.0") |
| 27 | + */ |
| 28 | +function readVersion() { |
| 29 | + const metadata = JSON.parse( |
| 30 | + execFileSync('cargo', [ |
| 31 | + 'metadata', |
| 32 | + '--no-deps', |
| 33 | + '--format-version', |
| 34 | + '1', |
| 35 | + '--manifest-path', |
| 36 | + CARGO_PATH, |
| 37 | + ], { encoding: 'utf8' }) |
| 38 | + ); |
| 39 | + |
| 40 | + const version = metadata.packages?.find((pkg) => { |
| 41 | + return path.resolve(pkg.manifest_path) === CARGO_PATH; |
| 42 | + })?.version; |
| 43 | + if (!version) { |
| 44 | + throw new Error('Could not read package version from cargo metadata'); |
| 45 | + } |
| 46 | + return version; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Compute the new prerelease version. |
| 51 | + * @param {string} currentVersion - Current semver (e.g., "0.3.0") |
| 52 | + * @param {string} channel - Prerelease channel ('alpha' or 'beta') |
| 53 | + * @returns {string} New version (e.g., "0.3.1-alpha.1234") |
| 54 | + */ |
| 55 | +function computeNewVersion(currentVersion, channel) { |
| 56 | + // Strip any existing prerelease suffix (e.g., "0.3.0-alpha.1" -> "0.3.0") |
| 57 | + const baseVersion = currentVersion.split('-')[0]; |
| 58 | + const parts = baseVersion.split('.'); |
| 59 | + |
| 60 | + if (parts.length !== 3) { |
| 61 | + throw new Error(`Invalid semver format: ${currentVersion}`); |
| 62 | + } |
| 63 | + |
| 64 | + const major = parseInt(parts[0], 10); |
| 65 | + const minor = parseInt(parts[1], 10); |
| 66 | + const patch = parseInt(parts[2], 10) + 1; // bump patch for next release |
| 67 | + |
| 68 | + // Use GITHUB_RUN_NUMBER for monotonic versioning |
| 69 | + // Reruns of the same workflow keep the same version to avoid mixed artifacts |
| 70 | + const runNumber = process.env.GITHUB_RUN_NUMBER || '0'; |
| 71 | + |
| 72 | + return `${major}.${minor}.${patch}-${channel}.${runNumber}`; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Write the new version back to the `[package]` section in Cargo.toml. |
| 77 | + * @param {string} newVersion - New version string to write |
| 78 | + */ |
| 79 | +function writeVersion(newVersion) { |
| 80 | + const content = fs.readFileSync(CARGO_PATH, 'utf8'); |
| 81 | + const lines = content.split(/\r?\n/); |
| 82 | + let inPackageSection = false; |
| 83 | + let replaced = false; |
| 84 | + |
| 85 | + const newContent = lines.map((line) => { |
| 86 | + const sectionMatch = line.match(/^\s*\[([^\]]+)\]\s*$/); |
| 87 | + if (sectionMatch) { |
| 88 | + inPackageSection = sectionMatch[1].trim() === 'package'; |
| 89 | + return line; |
| 90 | + } |
| 91 | + |
| 92 | + if (inPackageSection && /^\s*version\s*=\s*".*"\s*$/.test(line)) { |
| 93 | + if (replaced) { |
| 94 | + throw new Error('Found multiple package version entries in Cargo.toml'); |
| 95 | + } |
| 96 | + replaced = true; |
| 97 | + return `version = "${newVersion}"`; |
| 98 | + } |
| 99 | + |
| 100 | + return line; |
| 101 | + }).join('\n'); |
| 102 | + |
| 103 | + if (!replaced) { |
| 104 | + throw new Error('Could not find package version in Cargo.toml'); |
| 105 | + } |
| 106 | + |
| 107 | + fs.writeFileSync(CARGO_PATH, newContent); |
| 108 | +} |
| 109 | + |
| 110 | +function main() { |
| 111 | + const channel = process.argv[2]; |
| 112 | + |
| 113 | + if (!channel || !['alpha', 'beta'].includes(channel)) { |
| 114 | + console.error('Usage: node bump-tauri-version.js <alpha|beta>'); |
| 115 | + process.exit(1); |
| 116 | + } |
| 117 | + |
| 118 | + const currentVersion = readVersion(); |
| 119 | + console.log('Current version:', currentVersion); |
| 120 | + |
| 121 | + const newVersion = computeNewVersion(currentVersion, channel); |
| 122 | + console.log('New version:', newVersion); |
| 123 | + |
| 124 | + writeVersion(newVersion); |
| 125 | + console.log('Updated Backend/Cargo.toml'); |
| 126 | +} |
| 127 | + |
| 128 | +main(); |
0 commit comments