forked from supabase/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-homebrew.ts
More file actions
153 lines (133 loc) · 4.98 KB
/
Copy pathupdate-homebrew.ts
File metadata and controls
153 lines (133 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { $ } from "bun";
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import process from "node:process";
import { parseArgs } from "node:util";
const { values } = parseArgs({
options: {
version: { type: "string" },
repo: { type: "string", default: "supabase/cli" },
tap: { type: "string", default: "supabase/homebrew-tap" },
name: { type: "string", default: "supabase" },
local: { type: "boolean", default: false },
"dry-run": { type: "boolean", default: false },
},
});
const version = values.version;
if (!version) {
console.error(
"Usage: bun run scripts/update-homebrew.ts --version <version> [--repo <owner/repo>] [--tap <owner/repo>] [--name <formula-name>] [--local] [--dry-run]",
);
process.exit(1);
}
const repo = values.repo!;
const tap = values.tap!;
const name = values.name!;
const local = values.local!;
const dryRun = values["dry-run"]!;
const root = path.resolve(import.meta.dir, "../../..");
const distDir = path.join(root, "dist");
// Convert name (e.g. "supabase-beta") to the Ruby class name Homebrew
// expects (e.g. "SupabaseBeta"). The class + filename differ by channel so
// `supabase` and `supabase-beta` can coexist as separate formulas in the
// same tap, but the installed binary is always `supabase` (matching the
// Go CLI's historical behaviour).
const className = name
.split(/[-_]/)
.filter(Boolean)
.map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
.join("");
// `supabase-go` is the Go sidecar the legacy shell spawns via
// apps/cli/src/shared/legacy/go-proxy.layer.ts. It is looked up by exact
// filename colocated with process.execPath, so we MUST install it with its
// original name right next to the SFE. The `if File.exist?` guard makes the
// formula work for both the `legacy` shell (ships both binaries) and the
// future `next` shell (SFE only).
const installBlock = [
` bin.install "supabase"`,
` bin.install "supabase-go" if File.exist?("supabase-go")`,
].join("\n");
const testInvocation = `#{bin}/supabase`;
// Parse checksums
const checksums = new Map<string, string>();
const checksumsText = await readFile(path.join(distDir, "checksums.txt"), "utf-8");
for (const line of checksumsText.trim().split("\n")) {
const [hash, file] = line.split(/\s+/) as [string, string];
checksums.set(file, hash);
}
function sha(file: string): string {
const hash = checksums.get(file);
if (!hash) throw new Error(`Checksum not found for ${file}`);
return hash;
}
const baseUrl = local
? `file://${distDir}`
: `https://github.com/${repo}/releases/download/v${version}`;
const formula = `class ${className} < Formula
desc "Supabase CLI"
homepage "https://supabase.com"
version "${version}"
license "MIT"
on_macos do
if Hardware::CPU.arm?
url "${baseUrl}/supabase_${version}_darwin_arm64.tar.gz"
sha256 "${sha(`supabase_${version}_darwin_arm64.tar.gz`)}"
else
url "${baseUrl}/supabase_${version}_darwin_amd64.tar.gz"
sha256 "${sha(`supabase_${version}_darwin_amd64.tar.gz`)}"
end
end
on_linux do
if Hardware::CPU.arm?
url "${baseUrl}/supabase_${version}_linux_arm64.tar.gz"
sha256 "${sha(`supabase_${version}_linux_arm64.tar.gz`)}"
else
url "${baseUrl}/supabase_${version}_linux_amd64.tar.gz"
sha256 "${sha(`supabase_${version}_linux_amd64.tar.gz`)}"
end
end
def install
${installBlock}
end
test do
assert_match version.to_s, shell_output("${testInvocation} --version")
end
end
`;
const formulaFileName = `${name}.rb`;
const formulaOut = path.join(distDir, formulaFileName);
await writeFile(formulaOut, formula);
console.log(`Formula written to ${formulaOut}`);
if (local || dryRun) {
console.log(formula);
process.exit(0);
}
async function hasStagedChanges(repoDir: string, repoPath: string): Promise<boolean> {
const diff =
await $`git -C ${repoDir} diff --cached --quiet --exit-code -- ${repoPath}`.nothrow();
if (diff.exitCode === 0) return false;
if (diff.exitCode === 1) return true;
throw new Error(`Failed to inspect staged changes for ${repoPath}`);
}
// Clone tap repo, update formula, commit, push
const tmpDir = await mkdtemp(path.join(tmpdir(), "homebrew-tap-"));
try {
const tapUrl = `https://github.com/${tap}.git`;
await $`git clone ${tapUrl} ${tmpDir}`;
const formulaDir = path.join(tmpDir, "Formula");
await $`mkdir -p ${formulaDir}`;
const tapFormulaPath = path.join(formulaDir, formulaFileName);
const tapFormulaRepoPath = `Formula/${formulaFileName}`;
await writeFile(tapFormulaPath, formula);
await $`git -C ${tmpDir} add ${tapFormulaRepoPath}`;
if (await hasStagedChanges(tmpDir, tapFormulaRepoPath)) {
await $`git -C ${tmpDir} commit -m ${name + " " + version}`;
await $`git -C ${tmpDir} push`;
console.log(`Pushed formula update to ${tap}`);
} else {
console.log(`Formula ${formulaFileName} is already up to date in ${tap}`);
}
} finally {
await rm(tmpDir, { recursive: true });
}