-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.ts
More file actions
executable file
Β·393 lines (339 loc) Β· 11.6 KB
/
release.ts
File metadata and controls
executable file
Β·393 lines (339 loc) Β· 11.6 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env bun
import { $ } from "bun";
import clipboard from "clipboardy";
/**
* Release script for ohmyfs
* Bumps versions in package.json, Cargo.toml, and tauri.conf.json
* Then builds the application for specified platforms (or all platforms if none specified)
*
* Usage:
* bun run release.ts [options]
*
* Options:
* --bump [major|minor|patch] Bump version (defaults to patch if no value)
* --target <extension> Build for specific file extension(s) (comma-separated or multiple flags)
* Valid: exe, AppImage, deb, rpm, dmg
* --dry-run Show what would be done without making changes
*/
// Regex for matching version lines in Cargo.toml
const VERSION_REGEX = /^version = "[^"]*"/m;
// Build targets configuration
const BUILD_TARGETS: BuildTarget[] = [
{
name: "windows",
target: "x86_64-pc-windows-msvc",
runner: "cargo-xwin",
extension: "exe",
getInstallerName: (version: string) => `ohmyfs_${version}_x64-setup.exe`,
displayName: "Windows 10/11 64-bit",
},
{
name: "linux",
target: "x86_64-unknown-linux-gnu",
extension: "AppImage",
getInstallerName: (version: string) => `ohmyfs_${version}_amd64.AppImage`,
displayName: "Linux 64-bit",
},
{
name: "linux",
target: "x86_64-unknown-linux-gnu",
extension: "deb",
getInstallerName: (version: string) => `ohmyfs_${version}_amd64.deb`,
displayName: "Linux 64-bit",
},
{
name: "linux",
target: "x86_64-unknown-linux-gnu",
extension: "rpm",
getInstallerName: (version: string) => `ohmyfs-${version}-1.x86_64.rpm`,
displayName: "Linux 64-bit",
},
{
name: "macos-intel",
target: "x86_64-apple-darwin",
extension: "dmg",
getInstallerName: (version: string) => `ohmyfs_${version}_x64.dmg`,
displayName: "macOS Intel 64-bit",
},
{
name: "macos-arm",
target: "aarch64-apple-darwin",
extension: "dmg",
getInstallerName: (version: string) => `ohmyfs_${version}_aarch64.dmg`,
displayName: "macOS Apple Silicon",
},
];
type BumpType = "major" | "minor" | "patch";
interface Version {
major: number;
minor: number;
patch: number;
}
interface BuildTarget {
name: string;
target: string;
extension: string;
runner?: string;
getInstallerName: (version: string) => string;
displayName: string;
}
function parseVersion(version: string): Version {
const parts = version.split(".").map(Number);
if (parts.length !== 3 || parts.some(Number.isNaN)) {
throw new Error(`Invalid version format: ${version}`);
}
return {
major: parts[0],
minor: parts[1],
patch: parts[2],
};
}
function bumpVersion(version: Version, bumpType: BumpType): Version {
switch (bumpType) {
case "major":
return { major: version.major + 1, minor: 0, patch: 0 };
case "minor":
return { major: version.major, minor: version.minor + 1, patch: 0 };
case "patch":
return {
major: version.major,
minor: version.minor,
patch: version.patch + 1,
};
default:
throw new Error(`Unknown bump type: ${bumpType}`);
}
}
function formatVersion(version: Version): string {
return `${version.major}.${version.minor}.${version.patch}`;
}
async function updatePackageJson(newVersion: string): Promise<void> {
const packageJsonPath = "package.json";
// biome-ignore lint/correctness/noUndeclaredVariables: Bun is a global in Bun runtime
const packageJson = JSON.parse(await Bun.file(packageJsonPath).text());
packageJson.version = newVersion;
// biome-ignore lint/correctness/noUndeclaredVariables: Bun is a global in Bun runtime
await Bun.write(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`);
console.log(`β Updated ${packageJsonPath} to version ${newVersion}`);
}
async function updateCargoToml(newVersion: string): Promise<void> {
const cargoTomlPath = "src-tauri/Cargo.toml";
// biome-ignore lint/correctness/noUndeclaredVariables: Bun is a global in Bun runtime
let content = await Bun.file(cargoTomlPath).text();
// Update the version line
content = content.replace(VERSION_REGEX, `version = "${newVersion}"`);
// biome-ignore lint/correctness/noUndeclaredVariables: Bun is a global in Bun runtime
await Bun.write(cargoTomlPath, content);
console.log(`β Updated ${cargoTomlPath} to version ${newVersion}`);
}
async function updateTauriConfJson(newVersion: string): Promise<void> {
const tauriConfPath = "src-tauri/tauri.conf.json";
// biome-ignore lint/correctness/noUndeclaredVariables: Bun is a global in Bun runtime
const tauriConf = JSON.parse(await Bun.file(tauriConfPath).text());
tauriConf.version = newVersion;
// biome-ignore lint/correctness/noUndeclaredVariables: Bun is a global in Bun runtime
await Bun.write(tauriConfPath, `${JSON.stringify(tauriConf, null, 2)}\n`);
console.log(`β Updated ${tauriConfPath} to version ${newVersion}`);
}
async function getCurrentVersion(): Promise<string> {
// biome-ignore lint/correctness/noUndeclaredVariables: Bun is a global in Bun runtime
const packageJson = JSON.parse(await Bun.file("package.json").text());
return packageJson.version;
}
async function buildForTarget(
target: BuildTarget,
dryRun: boolean
): Promise<void> {
console.log(`π¨ Building for ${target.displayName}...`);
const args = target.runner
? [
"run",
"tauri",
"build",
"--runner",
target.runner,
"--target",
target.target,
]
: ["run", "tauri", "build", "--target", target.target];
const command = `bun ${args.join(" ")}`;
if (dryRun) {
console.log(`π [DRY RUN] Would run: ${command}`);
} else {
try {
await $`bun ${args}`;
console.log(`β
Successfully built for ${target.displayName}`);
} catch (error) {
console.error(`β Failed to build for ${target.displayName}:`, error);
throw error;
}
}
}
function parseTargets(args: string[]): string[] | undefined {
const targetIndices: number[] = [];
for (let index = 0; index < args.length; index++) {
if (args[index] === "--target") {
targetIndices.push(index);
}
}
if (targetIndices.length === 0) {
return undefined;
}
const extensions: string[] = [];
for (const index of targetIndices) {
const targetValue = args[index + 1];
if (targetValue && !targetValue.startsWith("--")) {
// Support comma-separated extensions
extensions.push(...targetValue.split(",").map((t) => t.trim()));
}
}
// Validate extensions
const validExtensions = [...new Set(BUILD_TARGETS.map((t) => t.extension))];
const invalidExtensions = extensions.filter(
(ext) => !validExtensions.includes(ext)
);
if (invalidExtensions.length > 0) {
throw new Error(
`Invalid extension(s): ${invalidExtensions.join(", ")}. Valid extensions: ${validExtensions.join(", ")}`
);
}
// Map extensions to platform names
const platforms: string[] = [];
for (const ext of extensions) {
const matchingPlatforms = BUILD_TARGETS.filter((t) => t.extension === ext);
platforms.push(...matchingPlatforms.map((t) => t.name));
}
// Remove duplicates
return [...new Set(platforms)];
}
function parseBumpType(args: string[]): BumpType | undefined {
const bumpIndex = args.indexOf("--bump");
if (bumpIndex === -1) {
return undefined;
}
const bumpValue = args[bumpIndex + 1];
if (!bumpValue || bumpValue.startsWith("--")) {
// --bump without value or followed by another flag defaults to patch
return "patch";
}
const validBumpTypes: BumpType[] = ["major", "minor", "patch"];
if (!validBumpTypes.includes(bumpValue as BumpType)) {
throw new Error(
`Invalid bump type: ${bumpValue}. Must be one of: ${validBumpTypes.join(", ")}`
);
}
return bumpValue as BumpType;
}
function parseArgs(): {
bumpType?: BumpType;
dryRun: boolean;
targets?: string[];
extensions?: string[];
} {
const args = process.argv.slice(2);
const dryRun = args.includes("--dry-run");
const bumpType = parseBumpType(args);
const targets = parseTargets(args);
const extensions = parseExtensions(args);
return { bumpType, dryRun, targets, extensions };
}
function parseExtensions(args: string[]): string[] | undefined {
const targetIndices: number[] = [];
for (let index = 0; index < args.length; index++) {
if (args[index] === "--target") {
targetIndices.push(index);
}
}
if (targetIndices.length === 0) {
return undefined;
}
const extensions: string[] = [];
for (const index of targetIndices) {
const targetValue = args[index + 1];
if (targetValue && !targetValue.startsWith("--")) {
// Support comma-separated extensions
extensions.push(...targetValue.split(",").map((t) => t.trim()));
}
}
// Remove duplicates
return [...new Set(extensions)];
}
async function main() {
try {
console.log("π Starting ohmyfs release process...\n");
const { bumpType, dryRun, targets, extensions } = parseArgs();
let finalVersion: string;
// Filter targets to build
let targetsToBuild = targets
? BUILD_TARGETS.filter((target) => targets.includes(target.name))
: BUILD_TARGETS;
// Deduplicate platforms by name since multiple extensions can map to the same platform
if (targets) {
const seen = new Set<string>();
targetsToBuild = targetsToBuild.filter((target) => {
if (seen.has(target.name)) {
return false;
}
seen.add(target.name);
return true;
});
}
if (dryRun) {
console.log(
"π Running in dry-run mode - no actual changes will be made\n"
);
}
if (bumpType) {
console.log(`π¦ Bumping version (${bumpType})...`);
const currentVersion = await getCurrentVersion();
const version = parseVersion(currentVersion);
const newVersion = bumpVersion(version, bumpType);
const newVersionString = formatVersion(newVersion);
finalVersion = newVersionString;
console.log(`Current version: ${currentVersion}`);
console.log(`New version: ${newVersionString}\n`);
if (dryRun) {
console.log("π [DRY RUN] Would update version files\n");
} else {
// Update all version files
await updatePackageJson(newVersionString);
await updateCargoToml(newVersionString);
await updateTauriConfJson(newVersionString);
}
} else {
console.log("π¦ Skipping version bump (--bump not provided)\n");
finalVersion = await getCurrentVersion();
}
console.log(
`π¨ Building application for ${targetsToBuild.length} platform(s)...\n`
);
// Build for each target
for (const target of targetsToBuild) {
await buildForTarget(target, dryRun);
}
console.log("\nβ
Release completed successfully!");
// Generate release content with downloads for requested extensions
const targetsForDownload = extensions
? BUILD_TARGETS.filter((target) => extensions.includes(target.extension))
: BUILD_TARGETS;
const downloadLinks = targetsForDownload
.map(
(target) =>
`- [${target.displayName} (${target.extension})](https://github.com/reliverse/ohmyfs/releases/download/${finalVersion}/${target.getInstallerName(finalVersion)})`
)
.join("\n");
const releaseContent = `## Downloads
${downloadLinks}`;
if (dryRun) {
console.log("π [DRY RUN] Would copy to clipboard:");
console.log(releaseContent);
} else {
await clipboard.write(releaseContent);
console.log("π Release content copied to clipboard!");
}
} catch (error) {
console.error("\nβ Release failed:", error);
process.exit(1);
}
}
main();