|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import { execSync } from "child_process"; |
| 4 | +import { mkdtempSync, rmSync } from "fs"; |
| 5 | +import { tmpdir } from "os"; |
| 6 | +import { dirname, join, resolve } from "path"; |
| 7 | + |
| 8 | +import { collectUpgradeVersionResults } from "./upgrade-version-check-lib"; |
| 9 | +import { CONTRACT_HINTS, PACKAGE_CONSTRAINTS } from "./upgrade-report-hints"; |
| 10 | + |
| 11 | +type PackageName = "host-contracts" | "gateway-contracts"; |
| 12 | + |
| 13 | +const PACKAGE_CONFIG: Record<PackageName, { extraDeps?: string }> = { |
| 14 | + "host-contracts": { extraDeps: "forge soldeer install" }, |
| 15 | + "gateway-contracts": {}, |
| 16 | +}; |
| 17 | + |
| 18 | +function usage(): never { |
| 19 | + console.error("Usage: bun ci/list-upgrades.ts --from <tag/ref> [--to <tag/ref>] [--package host-contracts|gateway-contracts]"); |
| 20 | + process.exit(1); |
| 21 | +} |
| 22 | + |
| 23 | +function parseArgs() { |
| 24 | + const args = process.argv.slice(2); |
| 25 | + let fromRef: string | undefined; |
| 26 | + let toRef: string | undefined; |
| 27 | + const packages: PackageName[] = []; |
| 28 | + |
| 29 | + for (let idx = 0; idx < args.length; idx++) { |
| 30 | + const arg = args[idx]; |
| 31 | + if (arg === "--from") { |
| 32 | + fromRef = args[++idx]; |
| 33 | + } else if (arg === "--to") { |
| 34 | + toRef = args[++idx]; |
| 35 | + } else if (arg === "--package") { |
| 36 | + const value = args[++idx] as PackageName; |
| 37 | + if (value !== "host-contracts" && value !== "gateway-contracts") usage(); |
| 38 | + packages.push(value); |
| 39 | + } else { |
| 40 | + usage(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (!fromRef) usage(); |
| 45 | + |
| 46 | + return { |
| 47 | + fromRef, |
| 48 | + toRef, |
| 49 | + packages: packages.length > 0 ? packages : (["host-contracts", "gateway-contracts"] as PackageName[]), |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +function run(cmd: string, cwd: string) { |
| 54 | + execSync(cmd, { cwd, stdio: "inherit", env: { ...process.env, NO_COLOR: "1" } }); |
| 55 | +} |
| 56 | + |
| 57 | +function addWorktree(repoRoot: string, path: string, ref: string) { |
| 58 | + run(`git worktree add --detach "${path}" "${ref}"`, repoRoot); |
| 59 | +} |
| 60 | + |
| 61 | +function preparePackage(currentRepoRoot: string, targetRoot: string, baselineRoot: string, pkg: PackageName) { |
| 62 | + const targetDir = join(targetRoot, pkg); |
| 63 | + const baselineDir = join(baselineRoot, pkg); |
| 64 | + const extraDeps = PACKAGE_CONFIG[pkg].extraDeps; |
| 65 | + |
| 66 | + run("npm ci", targetDir); |
| 67 | + run("npm ci", baselineDir); |
| 68 | + if (extraDeps) { |
| 69 | + run(extraDeps, targetDir); |
| 70 | + run(extraDeps, baselineDir); |
| 71 | + } |
| 72 | + run("make ensure-addresses", targetDir); |
| 73 | + run("make ensure-addresses", baselineDir); |
| 74 | + run(`bun ci/merge-address-constants.ts "${join(baselineDir, "addresses")}" "${join(targetDir, "addresses")}"`, currentRepoRoot); |
| 75 | + run(`cp "${join(targetDir, "foundry.toml")}" "${join(baselineDir, "foundry.toml")}"`, currentRepoRoot); |
| 76 | +} |
| 77 | + |
| 78 | +function printPackageReport(pkg: PackageName, repoRoot: string, baselineRoot: string) { |
| 79 | + const results = collectUpgradeVersionResults(join(baselineRoot, pkg), join(repoRoot, pkg)); |
| 80 | + const changed = results.filter((result) => result.baselineExists && result.bytecodeChanged); |
| 81 | + const unchanged = results.filter((result) => result.baselineExists && !result.bytecodeChanged); |
| 82 | + const errors = results.flatMap((result) => result.errors.map((error) => `${result.name}: ${error}`)); |
| 83 | + |
| 84 | + console.log(`\n## ${pkg}`); |
| 85 | + |
| 86 | + if (errors.length > 0) { |
| 87 | + console.log("\nErrors:"); |
| 88 | + for (const error of errors) { |
| 89 | + console.log(`- ${error}`); |
| 90 | + } |
| 91 | + process.exitCode = 1; |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + console.log("\nNeed upgrade:"); |
| 96 | + for (const result of changed) { |
| 97 | + console.log(`- ${result.name}`); |
| 98 | + if (result.reinitializer) { |
| 99 | + console.log(` reinitializer: ${result.reinitializer.signature}`); |
| 100 | + console.log(` upgrade args: ${result.reinitializer.inputs.length > 0 ? "yes" : "no"}`); |
| 101 | + const defaults = CONTRACT_HINTS[pkg][result.name]?.defaults; |
| 102 | + if (defaults) { |
| 103 | + console.log(" task defaults:"); |
| 104 | + for (const [name, value] of Object.entries(defaults)) { |
| 105 | + console.log(` - ${name} = ${value}`); |
| 106 | + } |
| 107 | + } else if (result.reinitializer.inputs.length > 0) { |
| 108 | + console.log(" note: check arg values with a repo owner"); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + console.log("\nNo upgrade needed:"); |
| 114 | + for (const result of unchanged) { |
| 115 | + console.log(`- ${result.name}`); |
| 116 | + } |
| 117 | + |
| 118 | + const changedNames = new Set(changed.map((result) => result.name)); |
| 119 | + const activeConstraints = PACKAGE_CONSTRAINTS[pkg].filter((constraint) => |
| 120 | + constraint.contracts.every((contract) => changedNames.has(contract)), |
| 121 | + ); |
| 122 | + if (activeConstraints.length > 0) { |
| 123 | + console.log("\nAttention points:"); |
| 124 | + for (const constraint of activeConstraints) { |
| 125 | + console.log(`- ${constraint.message}`); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +const { fromRef, toRef, packages } = parseArgs(); |
| 131 | +const repoRoot = resolve(dirname(import.meta.dir)); |
| 132 | +const tempRoot = mkdtempSync(join(tmpdir(), "fhevm-upgrade-report-")); |
| 133 | +const baselineRoot = join(tempRoot, "baseline"); |
| 134 | +const targetRoot = toRef ? join(tempRoot, "target") : repoRoot; |
| 135 | + |
| 136 | +try { |
| 137 | + addWorktree(repoRoot, baselineRoot, fromRef); |
| 138 | + if (toRef) { |
| 139 | + addWorktree(repoRoot, targetRoot, toRef); |
| 140 | + } |
| 141 | + |
| 142 | + for (const pkg of packages) { |
| 143 | + preparePackage(repoRoot, targetRoot, baselineRoot, pkg); |
| 144 | + printPackageReport(pkg, targetRoot, baselineRoot); |
| 145 | + } |
| 146 | +} finally { |
| 147 | + try { |
| 148 | + run("git worktree prune", repoRoot); |
| 149 | + } catch {} |
| 150 | + rmSync(tempRoot, { recursive: true, force: true }); |
| 151 | +} |
0 commit comments