|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Bumps the Rust crate version based on conventional commits since the last |
| 4 | + * Beachball-generated release tag, then packages the crate into publish_artifacts/cargo/. |
| 5 | + * |
| 6 | + * Beachball tags use the format: {package-name}_v{version} |
| 7 | + * e.g. microsoft-fast-build_v0.1.0 |
| 8 | + * |
| 9 | + * Version bump rules (conventional commits): |
| 10 | + * BREAKING CHANGE / feat!: / fix!: / refactor!: / chore!: → major |
| 11 | + * feat: → minor |
| 12 | + * anything else → patch |
| 13 | + * |
| 14 | + * Only commits that touch crates/microsoft-fast-build/ are considered. |
| 15 | + * If no such commits exist since the last Beachball tag, the script exits without change. |
| 16 | + */ |
| 17 | +import { execSync } from "node:child_process"; |
| 18 | +import { copyFileSync, globSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 19 | +import { dirname, join } from "node:path"; |
| 20 | +import { fileURLToPath } from "node:url"; |
| 21 | + |
| 22 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 23 | +const repoRoot = join(__dirname, ".."); |
| 24 | +const crateDir = join(repoRoot, "crates", "microsoft-fast-build"); |
| 25 | +const cargoTomlPath = join(crateDir, "Cargo.toml"); |
| 26 | +const outputDir = join(repoRoot, "publish_artifacts", "cargo"); |
| 27 | + |
| 28 | +function getCurrentVersion() { |
| 29 | + const content = readFileSync(cargoTomlPath, "utf-8"); |
| 30 | + const match = content.match(/^version\s*=\s*"([^"]+)"/m); |
| 31 | + if (!match) throw new Error("Could not find version in Cargo.toml"); |
| 32 | + return match[1]; |
| 33 | +} |
| 34 | + |
| 35 | +function parseVersion(version) { |
| 36 | + const [major, minor, patch] = version.split(".").map(Number); |
| 37 | + return { major, minor, patch }; |
| 38 | +} |
| 39 | + |
| 40 | +function getCommitsSinceLastTag(crateName) { |
| 41 | + // Beachball generates tags in the format {package-name}_v{version}. |
| 42 | + // Find the latest matching tag rather than constructing one from the |
| 43 | + // current Cargo.toml version, which may diverge from the last release. |
| 44 | + const latestTag = |
| 45 | + execSync( |
| 46 | + `git -C "${repoRoot}" tag --list "${crateName}_v*" --sort=-version:refname`, |
| 47 | + { encoding: "utf-8" }, |
| 48 | + ) |
| 49 | + .trim() |
| 50 | + .split("\n")[0] || null; |
| 51 | + |
| 52 | + const range = latestTag ? `${latestTag}..HEAD` : "HEAD"; |
| 53 | + const log = execSync( |
| 54 | + `git -C "${repoRoot}" log ${range} --pretty=format:"%s%n%b" -- crates/microsoft-fast-build/`, |
| 55 | + { encoding: "utf-8" }, |
| 56 | + ); |
| 57 | + return log.trim(); |
| 58 | +} |
| 59 | + |
| 60 | +function determineBump(commits) { |
| 61 | + if (!commits) return null; |
| 62 | + |
| 63 | + if ( |
| 64 | + /BREAKING CHANGE/m.test(commits) || |
| 65 | + /^(feat|fix|refactor|chore)!(\([^)]*\))?:/m.test(commits) |
| 66 | + ) { |
| 67 | + return "major"; |
| 68 | + } |
| 69 | + if (/^feat(\([^)]*\))?:/m.test(commits)) { |
| 70 | + return "minor"; |
| 71 | + } |
| 72 | + return "patch"; |
| 73 | +} |
| 74 | + |
| 75 | +function bumpVersion(version, bump) { |
| 76 | + const { major, minor, patch } = parseVersion(version); |
| 77 | + switch (bump) { |
| 78 | + case "major": |
| 79 | + return `${major + 1}.0.0`; |
| 80 | + case "minor": |
| 81 | + return `${major}.${minor + 1}.0`; |
| 82 | + case "patch": |
| 83 | + return `${major}.${minor}.${patch + 1}`; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +function updateCargoToml(newVersion) { |
| 88 | + let content = readFileSync(cargoTomlPath, "utf-8"); |
| 89 | + content = content.replace(/^(version\s*=\s*)"[^"]+"/m, `$1"${newVersion}"`); |
| 90 | + writeFileSync(cargoTomlPath, content); |
| 91 | +} |
| 92 | + |
| 93 | +function packageCrate(version) { |
| 94 | + mkdirSync(outputDir, { recursive: true }); |
| 95 | + execSync( |
| 96 | + `cargo package --manifest-path "${cargoTomlPath}" --no-verify --allow-dirty`, |
| 97 | + { stdio: "inherit" }, |
| 98 | + ); |
| 99 | + const targetPackageDir = join(crateDir, "target", "package"); |
| 100 | + const crateFiles = globSync(`microsoft-fast-build-${version}.crate`, { |
| 101 | + cwd: targetPackageDir, |
| 102 | + }); |
| 103 | + if (crateFiles.length === 0) { |
| 104 | + throw new Error(`Could not find packaged .crate file in ${targetPackageDir}`); |
| 105 | + } |
| 106 | + for (const file of crateFiles) { |
| 107 | + copyFileSync(join(targetPackageDir, file), join(outputDir, file)); |
| 108 | + console.log(`Packaged ${file} → ${outputDir}`); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +const currentVersion = getCurrentVersion(); |
| 113 | +const commits = getCommitsSinceLastTag("microsoft-fast-build"); |
| 114 | +const bump = determineBump(commits); |
| 115 | + |
| 116 | +if (!bump) { |
| 117 | + console.log("No changes to Rust crate since last release, skipping."); |
| 118 | + process.exit(0); |
| 119 | +} |
| 120 | + |
| 121 | +const newVersion = bumpVersion(currentVersion, bump); |
| 122 | +console.log(`Bumping microsoft-fast-build: ${currentVersion} → ${newVersion} (${bump})`); |
| 123 | + |
| 124 | +updateCargoToml(newVersion); |
| 125 | +packageCrate(newVersion); |
| 126 | +console.log(`Rust crate microsoft-fast-build@${newVersion} packaged to ${outputDir}/`); |
0 commit comments