|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 4 | +import { createHash } from "node:crypto"; |
| 5 | +import path from "node:path"; |
| 6 | + |
| 7 | +const FORMULA_RELATIVE_PATH = path.join("Formula", "hunk.rb"); |
| 8 | +const RELEASE_ASSET_NAMES = { |
| 9 | + darwinArm64: "hunkdiff-darwin-arm64.tar.gz", |
| 10 | + darwinX64: "hunkdiff-darwin-x64.tar.gz", |
| 11 | + linuxArm64: "hunkdiff-linux-arm64.tar.gz", |
| 12 | + linuxX64: "hunkdiff-linux-x64.tar.gz", |
| 13 | +} as const; |
| 14 | + |
| 15 | +interface Options { |
| 16 | + assetRoot: string; |
| 17 | + outputRoot: string; |
| 18 | + repo: string; |
| 19 | + tag: string; |
| 20 | +} |
| 21 | + |
| 22 | +function parseArgs(argv: string[]): Options { |
| 23 | + const repoRoot = path.resolve(import.meta.dir, ".."); |
| 24 | + const options: Options = { |
| 25 | + assetRoot: path.join(repoRoot, "dist", "release", "github"), |
| 26 | + outputRoot: repoRoot, |
| 27 | + repo: "modem-dev/hunk", |
| 28 | + tag: "", |
| 29 | + }; |
| 30 | + |
| 31 | + for (let index = 0; index < argv.length; index += 1) { |
| 32 | + const argument = argv[index]; |
| 33 | + const value = argv[index + 1]; |
| 34 | + |
| 35 | + if (argument === "--asset-root") { |
| 36 | + if (!value) { |
| 37 | + throw new Error("Missing value for --asset-root."); |
| 38 | + } |
| 39 | + options.assetRoot = path.resolve(value); |
| 40 | + index += 1; |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + if (argument === "--output-root") { |
| 45 | + if (!value) { |
| 46 | + throw new Error("Missing value for --output-root."); |
| 47 | + } |
| 48 | + options.outputRoot = path.resolve(value); |
| 49 | + index += 1; |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + if (argument === "--repo") { |
| 54 | + if (!value) { |
| 55 | + throw new Error("Missing value for --repo."); |
| 56 | + } |
| 57 | + options.repo = value; |
| 58 | + index += 1; |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + if (argument === "--tag") { |
| 63 | + if (!value) { |
| 64 | + throw new Error("Missing value for --tag."); |
| 65 | + } |
| 66 | + options.tag = value; |
| 67 | + index += 1; |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + throw new Error(`Unknown argument: ${argument}`); |
| 72 | + } |
| 73 | + |
| 74 | + if (!options.tag) { |
| 75 | + throw new Error("Missing required --tag vX.Y.Z argument."); |
| 76 | + } |
| 77 | + |
| 78 | + return options; |
| 79 | +} |
| 80 | + |
| 81 | +function versionFromTag(tag: string) { |
| 82 | + const version = tag.startsWith("v") ? tag.slice(1) : tag; |
| 83 | + if (!/^\d+\.\d+\.\d+$/.test(version)) { |
| 84 | + throw new Error(`Homebrew formula updates only support stable semver tags, got ${tag}.`); |
| 85 | + } |
| 86 | + |
| 87 | + return version; |
| 88 | +} |
| 89 | + |
| 90 | +function assertSafeRepoSlug(repo: string) { |
| 91 | + if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repo)) { |
| 92 | + throw new Error(`Invalid GitHub repository slug: ${repo}`); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +function sha256(file: string) { |
| 97 | + return createHash("sha256").update(readFileSync(file)).digest("hex"); |
| 98 | +} |
| 99 | + |
| 100 | +function assetUrl(repo: string, tag: string, assetName: string) { |
| 101 | + return `https://github.com/${repo}/releases/download/${tag}/${assetName}`; |
| 102 | +} |
| 103 | + |
| 104 | +function formulaContent(options: Options) { |
| 105 | + const version = versionFromTag(options.tag); |
| 106 | + assertSafeRepoSlug(options.repo); |
| 107 | + const checksums = Object.fromEntries( |
| 108 | + Object.entries(RELEASE_ASSET_NAMES).map(([key, assetName]) => { |
| 109 | + const assetPath = path.join(options.assetRoot, assetName); |
| 110 | + if (!existsSync(assetPath)) { |
| 111 | + const found = existsSync(options.assetRoot) |
| 112 | + ? readdirSync(options.assetRoot).join(", ") |
| 113 | + : ""; |
| 114 | + throw new Error(`Missing release asset ${assetPath}. Found: ${found}`); |
| 115 | + } |
| 116 | + |
| 117 | + return [key, sha256(assetPath)]; |
| 118 | + }), |
| 119 | + ) as Record<keyof typeof RELEASE_ASSET_NAMES, string>; |
| 120 | + |
| 121 | + return `class Hunk < Formula |
| 122 | + desc "Desktop-inspired terminal diff viewer for agent-authored changesets" |
| 123 | + homepage "https://github.com/modem-dev/hunk" |
| 124 | + version "${version}" |
| 125 | + license "MIT" |
| 126 | +
|
| 127 | + on_macos do |
| 128 | + if Hardware::CPU.arm? |
| 129 | + url "${assetUrl(options.repo, options.tag, RELEASE_ASSET_NAMES.darwinArm64)}" |
| 130 | + sha256 "${checksums.darwinArm64}" |
| 131 | + else |
| 132 | + url "${assetUrl(options.repo, options.tag, RELEASE_ASSET_NAMES.darwinX64)}" |
| 133 | + sha256 "${checksums.darwinX64}" |
| 134 | + end |
| 135 | + end |
| 136 | +
|
| 137 | + on_linux do |
| 138 | + if Hardware::CPU.arm? |
| 139 | + url "${assetUrl(options.repo, options.tag, RELEASE_ASSET_NAMES.linuxArm64)}" |
| 140 | + sha256 "${checksums.linuxArm64}" |
| 141 | + else |
| 142 | + url "${assetUrl(options.repo, options.tag, RELEASE_ASSET_NAMES.linuxX64)}" |
| 143 | + sha256 "${checksums.linuxX64}" |
| 144 | + end |
| 145 | + end |
| 146 | +
|
| 147 | + def install |
| 148 | + chmod 0755, "hunk" |
| 149 | + libexec.install "hunk" |
| 150 | + (bin/"hunk").write_env_script libexec/"hunk", HUNK_INSTALL_SOURCE: "homebrew" |
| 151 | + end |
| 152 | +
|
| 153 | + test do |
| 154 | + assert_match version.to_s, shell_output("#{bin}/hunk --version") |
| 155 | + end |
| 156 | +end |
| 157 | +`; |
| 158 | +} |
| 159 | + |
| 160 | +const options = parseArgs(process.argv.slice(2)); |
| 161 | +const formulaPath = path.join(options.outputRoot, FORMULA_RELATIVE_PATH); |
| 162 | +mkdirSync(path.dirname(formulaPath), { recursive: true }); |
| 163 | +writeFileSync(formulaPath, formulaContent(options)); |
| 164 | +console.log(`Updated ${formulaPath}`); |
0 commit comments