Open History to see the last commit
1#!/usr/bin/env node
2
3import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import * as Config from "effect/Config";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Schema from "effect/Schema";
9import * as Stream from "effect/Stream";
10import * as String from "effect/String";
11import { Command, Flag } from "effect/cli";
12import { ChildProcess, ChildProcessSpawner } from "effect/process";
13
14const ReleaseChannel = Schema.Literals(["stable", "nightly", "preview"]);
15type ReleaseChannel = typeof ReleaseChannel.Type;
16
17export class InvalidReleaseTagError extends Schema.TaggedError<InvalidReleaseTagError>()(
18 "InvalidReleaseTagError",
19 {
20 channel: ReleaseChannel,
21 currentTag: Schema.String,
22 },
23) {
24 override get message(): string {
25 return `Invalid ${this.channel} release tag '${this.currentTag}'.`;
26 }
27}
28
29const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
30
31const releaseTagListProcessContext = {
32 executable: Schema.Literal("git"),
33 argumentCount: NonNegativeInt,
34 cwd: Schema.String,
35};
36
37export class ReleaseTagListProcessError extends Schema.TaggedError<ReleaseTagListProcessError>()(
38 "ReleaseTagListProcessError",
39 {
40 ...releaseTagListProcessContext,
41 operation: Schema.Literals(["spawn", "read-stdout", "read-stderr", "wait-for-exit"]),
42 cause: Schema.Defect(),
43 },
44) {
45 override get message(): string {
46 return `Failed to list release tags during process operation "${this.operation}".`;
47 }
48}
49
50export class ReleaseTagListProcessExitError extends Schema.TaggedError<ReleaseTagListProcessExitError>()(
51 "ReleaseTagListProcessExitError",
52 {
53 ...releaseTagListProcessContext,
54 exitCode: Schema.Number,
55 stdoutLength: NonNegativeInt,
56 stderrLength: NonNegativeInt,
57 },
58) {
59 override get message(): string {
60 return `Release tag listing exited with code ${this.exitCode}.`;
61 }
62}
63
64export class PreviousReleaseTagGitHubOutputConfigError extends Schema.TaggedError<PreviousReleaseTagGitHubOutputConfigError>()(
65 "PreviousReleaseTagGitHubOutputConfigError",
66 {
67 cause: Schema.Defect(),
68 },
69) {
70 override get message(): string {
71 return "Failed to resolve the GITHUB_OUTPUT path for the previous release tag.";
72 }
73}
74
75export class PreviousReleaseTagGitHubOutputAppendError extends Schema.TaggedError<PreviousReleaseTagGitHubOutputAppendError>()(
76 "PreviousReleaseTagGitHubOutputAppendError",
77 {
78 outputPath: Schema.String,
79 cause: Schema.Defect(),
80 },
81) {
82 override get message(): string {
83 return `Failed to append the previous release tag to ${this.outputPath}.`;
84 }
85}
86
87interface StableVersion {
88 readonly major: number;
89 readonly minor: number;
90 readonly patch: number;
91 readonly prerelease: ReadonlyArray<string>;
92}
93
94interface NightlyVersion {
95 readonly major: number;
96 readonly minor: number;
97 readonly patch: number;
98 readonly date: number;
99 readonly runNumber: number;
100}
101
102const parseNumericIdentifier = (identifier: string): number | undefined =>
103 /^\d+$/.test(identifier) ? Number(identifier) : undefined;
104
105const comparePrereleaseIdentifiers = (left: string, right: string): number => {
106 const leftNumeric = parseNumericIdentifier(left);
107 const rightNumeric = parseNumericIdentifier(right);
108
109 if (leftNumeric !== undefined && rightNumeric !== undefined) {
110 return leftNumeric - rightNumeric;
111 }
112 if (leftNumeric !== undefined) {
113 return -1;
114 }
115 if (rightNumeric !== undefined) {
116 return 1;
117 }
118 return left.localeCompare(right);
119};
120
121const compareStableVersions = (left: StableVersion, right: StableVersion): number => {
122 if (left.major !== right.major) return left.major - right.major;
123 if (left.minor !== right.minor) return left.minor - right.minor;
124 if (left.patch !== right.patch) return left.patch - right.patch;
125
126 const leftHasPrerelease = left.prerelease.length > 0;
127 const rightHasPrerelease = right.prerelease.length > 0;
128 if (!leftHasPrerelease && !rightHasPrerelease) return 0;
129 if (!leftHasPrerelease) return 1;
130 if (!rightHasPrerelease) return -1;
131
132 const maxLength = Math.max(left.prerelease.length, right.prerelease.length);
133 for (let index = 0; index < maxLength; index += 1) {
134 const leftIdentifier = left.prerelease[index];
135 const rightIdentifier = right.prerelease[index];
136 if (leftIdentifier === undefined) return -1;
137 if (rightIdentifier === undefined) return 1;
138
139 const comparison = comparePrereleaseIdentifiers(leftIdentifier, rightIdentifier);
140 if (comparison !== 0) return comparison;
141 }
142
143 return 0;
144};
145
146const parseStableTag = (tag: string): StableVersion | undefined => {
147 const match = /^v(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/.exec(tag);
148 if (!match) return undefined;
149
150 const [, major, minor, patch, prerelease] = match;
151 if (!major || !minor || !patch) return undefined;
152
153 const prereleaseIdentifiers = prerelease ? prerelease.split(".") : [];
154 if (prereleaseIdentifiers[0] === "nightly" || prereleaseIdentifiers[0] === "preview") {
155 return undefined;
156 }
157
158 return {
159 major: Number(major),
160 minor: Number(minor),
161 patch: Number(patch),
162 prerelease: prereleaseIdentifiers,
163 };
164};
165
166const compareNightlyVersions = (left: NightlyVersion, right: NightlyVersion): number => {
167 if (left.major !== right.major) return left.major - right.major;
168 if (left.minor !== right.minor) return left.minor - right.minor;
169 if (left.patch !== right.patch) return left.patch - right.patch;
170 if (left.date !== right.date) return left.date - right.date;
171 return left.runNumber - right.runNumber;
172};
173
174const parseNightlyTag = (
175 tag: string,
176 channel: "nightly" | "preview" = "nightly",
177): NightlyVersion | undefined => {
178 const match = new RegExp(
179 `^(?:nightly-)?v(\\d+)\\.(\\d+)\\.(\\d+)-${channel}\\.(\\d{8})\\.(\\d+)$`,
180 ).exec(tag);
181 if (!match) return undefined;
182
183 const [, major, minor, patch, date, runNumber] = match;
184 if (!major || !minor || !patch || !date || !runNumber) return undefined;
185
186 return {
187 major: Number(major),
188 minor: Number(minor),
189 patch: Number(patch),
190 date: Number(date),
191 runNumber: Number(runNumber),
192 };
193};
194
195export const resolvePreviousReleaseTag = (
196 channel: ReleaseChannel,
197 currentTag: string,
198 tags: ReadonlyArray<string>,
199) =>
200 Effect.gen(function* () {
201 if (channel === "stable") {
202 const current = parseStableTag(currentTag);
203 if (!current) {
204 return yield* new InvalidReleaseTagError({ channel, currentTag });
205 }
206
207 const candidates = tags
208 .map((tag) => ({ tag, parsed: parseStableTag(tag) }))
209 .filter(
210 (entry): entry is { tag: string; parsed: StableVersion } => entry.parsed !== undefined,
211 )
212 .filter((entry) => compareStableVersions(entry.parsed, current) < 0)
213 .toSorted((left, right) => compareStableVersions(right.parsed, left.parsed));
214
215 return candidates[0]?.tag;
216 }
217
218 const current = parseNightlyTag(currentTag, channel);
219 if (!current) {
220 return yield* new InvalidReleaseTagError({ channel, currentTag });
221 }
222
223 const candidates = tags
224 .map((tag) => ({ tag, parsed: parseNightlyTag(tag, channel) }))
225 .filter(
226 (entry): entry is { tag: string; parsed: NightlyVersion } => entry.parsed !== undefined,
227 )
228 .filter((entry) => compareNightlyVersions(entry.parsed, current) < 0)
229 .toSorted((left, right) => compareNightlyVersions(right.parsed, left.parsed));
230
231 return candidates[0]?.tag;
232 });
233
234const collectStreamAsString = <E>(stream: Stream.Stream<Uint8Array, E>): Effect.Effect<string, E> =>
235 stream.pipe(
236 Stream.decodeText(),
237 Stream.runFold(
238 () => "",
239 (acc, chunk) => acc + chunk,
240 ),
241 );
242
243export const listGitTags = Effect.fn("listGitTags")(function* (cwd = process.cwd()) {
244 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
245 const args = ["tag", "--list"] as const;
246 const context = {
247 executable: "git",
248 argumentCount: args.length,
249 cwd,
250 } as const;
251 const child = yield* spawner.spawn(ChildProcess.make("git", args, { cwd })).pipe(
252 Effect.mapError(
253 (cause) =>
254 new ReleaseTagListProcessError({
255 ...context,
256 operation: "spawn",
257 cause,
258 }),
259 ),
260 );
261 const [stdout, stderr, exitCode] = yield* Effect.all(
262 [
263 collectStreamAsString(child.stdout).pipe(
264 Effect.mapError(
265 (cause) =>
266 new ReleaseTagListProcessError({
267 ...context,
268 operation: "read-stdout",
269 cause,
270 }),
271 ),
272 ),
273 collectStreamAsString(child.stderr).pipe(
274 Effect.mapError(
275 (cause) =>
276 new ReleaseTagListProcessError({
277 ...context,
278 operation: "read-stderr",
279 cause,
280 }),
281 ),
282 ),
283 child.exitCode.pipe(
284 Effect.map(Number),
285 Effect.mapError(
286 (cause) =>
287 new ReleaseTagListProcessError({
288 ...context,
289 operation: "wait-for-exit",
290 cause,
291 }),
292 ),
293 ),
294 ],
295 { concurrency: "unbounded" },
296 );
297
298 if (exitCode !== 0) {
299 return yield* new ReleaseTagListProcessExitError({
300 ...context,
301 exitCode,
302 stdoutLength: stdout.length,
303 stderrLength: stderr.length,
304 });
305 }
306
307 return stdout.split(/\r?\n/).map(String.trim).filter(String.isNonEmpty);
308});
309
310export const writePreviousReleaseTagOutput = Effect.fn("writePreviousReleaseTagOutput")(function* (
311 previousTag: string | undefined,
312 writeGithubOutput: boolean,
313) {
314 const entry = `previous_tag=${previousTag ?? ""}\n`;
315
316 if (writeGithubOutput) {
317 const fs = yield* FileSystem.FileSystem;
318 const githubOutputPath = yield* Config.NonEmptyString("GITHUB_OUTPUT").pipe(
319 Effect.mapError(
320 (cause) =>
321 new PreviousReleaseTagGitHubOutputConfigError({
322 cause,
323 }),
324 ),
325 );
326 yield* fs.writeFileString(githubOutputPath, entry, { flag: "a" }).pipe(
327 Effect.mapError(
328 (cause) =>
329 new PreviousReleaseTagGitHubOutputAppendError({
330 outputPath: githubOutputPath,
331 cause,
332 }),
333 ),
334 );
335 return;
336 }
337
338 process.stdout.write(entry);
339});
340
341const command = Command.make(
342 "resolve-previous-release-tag",
343 {
344 channel: Flag.Literals("channel", ReleaseChannel.literals).pipe(
345 Flag.withDescription("Release channel whose previous tag should be resolved."),
346 ),
347 currentTag: Flag.String("current-tag").pipe(
348 Flag.withDescription("Current release tag to compare against."),
349 ),
350 githubOutput: Flag.Boolean("github-output").pipe(
351 Flag.withDescription("Write values to GITHUB_OUTPUT instead of stdout."),
352 Flag.withDefault(false),
353 ),
354 },
355 ({ channel, currentTag, githubOutput }) =>
356 listGitTags().pipe(
357 Effect.flatMap((tags) => resolvePreviousReleaseTag(channel, currentTag, tags)),
358 Effect.flatMap((previousTag) => writePreviousReleaseTagOutput(previousTag, githubOutput)),
359 ),
360).pipe(Command.withDescription("Resolve the previous release tag for a stable or nightly series."));
361
362if (import.meta.main) {
363 Command.run(command, { version: "0.0.0" }).pipe(
364 Effect.scoped,
365 Effect.provide(NodeServices.layer),
366 NodeRuntime.runMain,
367 );
368}