|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; |
| 4 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 5 | +import { Array, Config, Effect, FileSystem, Schema, Stream, String } from "effect"; |
| 6 | +import { Command, Flag } from "effect/unstable/cli"; |
| 7 | +import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; |
| 8 | + |
| 9 | +const ReleaseChannel = Schema.Literals(["stable", "nightly"]); |
| 10 | +type ReleaseChannel = typeof ReleaseChannel.Type; |
| 11 | + |
| 12 | +interface StableVersion { |
| 13 | + readonly major: number; |
| 14 | + readonly minor: number; |
| 15 | + readonly patch: number; |
| 16 | + readonly prerelease: ReadonlyArray<string>; |
| 17 | +} |
| 18 | + |
| 19 | +interface NightlyVersion { |
| 20 | + readonly major: number; |
| 21 | + readonly minor: number; |
| 22 | + readonly patch: number; |
| 23 | + readonly date: number; |
| 24 | + readonly runNumber: number; |
| 25 | +} |
| 26 | + |
| 27 | +const parseNumericIdentifier = (identifier: string): number | undefined => |
| 28 | + /^\d+$/.test(identifier) ? Number(identifier) : undefined; |
| 29 | + |
| 30 | +const comparePrereleaseIdentifiers = (left: string, right: string): number => { |
| 31 | + const leftNumeric = parseNumericIdentifier(left); |
| 32 | + const rightNumeric = parseNumericIdentifier(right); |
| 33 | + |
| 34 | + if (leftNumeric !== undefined && rightNumeric !== undefined) { |
| 35 | + return leftNumeric - rightNumeric; |
| 36 | + } |
| 37 | + if (leftNumeric !== undefined) { |
| 38 | + return -1; |
| 39 | + } |
| 40 | + if (rightNumeric !== undefined) { |
| 41 | + return 1; |
| 42 | + } |
| 43 | + return left.localeCompare(right); |
| 44 | +}; |
| 45 | + |
| 46 | +const compareStableVersions = (left: StableVersion, right: StableVersion): number => { |
| 47 | + if (left.major !== right.major) return left.major - right.major; |
| 48 | + if (left.minor !== right.minor) return left.minor - right.minor; |
| 49 | + if (left.patch !== right.patch) return left.patch - right.patch; |
| 50 | + |
| 51 | + const leftHasPrerelease = left.prerelease.length > 0; |
| 52 | + const rightHasPrerelease = right.prerelease.length > 0; |
| 53 | + if (!leftHasPrerelease && !rightHasPrerelease) return 0; |
| 54 | + if (!leftHasPrerelease) return 1; |
| 55 | + if (!rightHasPrerelease) return -1; |
| 56 | + |
| 57 | + const maxLength = Math.max(left.prerelease.length, right.prerelease.length); |
| 58 | + for (let index = 0; index < maxLength; index += 1) { |
| 59 | + const leftIdentifier = left.prerelease[index]; |
| 60 | + const rightIdentifier = right.prerelease[index]; |
| 61 | + if (leftIdentifier === undefined) return -1; |
| 62 | + if (rightIdentifier === undefined) return 1; |
| 63 | + |
| 64 | + const comparison = comparePrereleaseIdentifiers(leftIdentifier, rightIdentifier); |
| 65 | + if (comparison !== 0) return comparison; |
| 66 | + } |
| 67 | + |
| 68 | + return 0; |
| 69 | +}; |
| 70 | + |
| 71 | +const parseStableTag = (tag: string): StableVersion | undefined => { |
| 72 | + const match = /^v(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/.exec(tag); |
| 73 | + if (!match) return undefined; |
| 74 | + |
| 75 | + const [, major, minor, patch, prerelease] = match; |
| 76 | + if (!major || !minor || !patch) return undefined; |
| 77 | + |
| 78 | + return { |
| 79 | + major: Number(major), |
| 80 | + minor: Number(minor), |
| 81 | + patch: Number(patch), |
| 82 | + prerelease: prerelease ? prerelease.split(".") : [], |
| 83 | + }; |
| 84 | +}; |
| 85 | + |
| 86 | +const compareNightlyVersions = (left: NightlyVersion, right: NightlyVersion): number => { |
| 87 | + if (left.major !== right.major) return left.major - right.major; |
| 88 | + if (left.minor !== right.minor) return left.minor - right.minor; |
| 89 | + if (left.patch !== right.patch) return left.patch - right.patch; |
| 90 | + if (left.date !== right.date) return left.date - right.date; |
| 91 | + return left.runNumber - right.runNumber; |
| 92 | +}; |
| 93 | + |
| 94 | +const parseNightlyTag = (tag: string): NightlyVersion | undefined => { |
| 95 | + const match = /^nightly-v(\d+)\.(\d+)\.(\d+)-nightly\.(\d{8})\.(\d+)$/.exec(tag); |
| 96 | + if (!match) return undefined; |
| 97 | + |
| 98 | + const [, major, minor, patch, date, runNumber] = match; |
| 99 | + if (!major || !minor || !patch || !date || !runNumber) return undefined; |
| 100 | + |
| 101 | + return { |
| 102 | + major: Number(major), |
| 103 | + minor: Number(minor), |
| 104 | + patch: Number(patch), |
| 105 | + date: Number(date), |
| 106 | + runNumber: Number(runNumber), |
| 107 | + }; |
| 108 | +}; |
| 109 | + |
| 110 | +const resolvePreviousReleaseTag = ( |
| 111 | + channel: ReleaseChannel, |
| 112 | + currentTag: string, |
| 113 | + tags: ReadonlyArray<string>, |
| 114 | +): string | undefined => { |
| 115 | + if (channel === "stable") { |
| 116 | + const current = parseStableTag(currentTag); |
| 117 | + if (!current) { |
| 118 | + throw new Error(`Invalid stable release tag '${currentTag}'.`); |
| 119 | + } |
| 120 | + |
| 121 | + const candidates = tags |
| 122 | + .map((tag) => ({ tag, parsed: parseStableTag(tag) })) |
| 123 | + .filter( |
| 124 | + (entry): entry is { tag: string; parsed: StableVersion } => entry.parsed !== undefined, |
| 125 | + ) |
| 126 | + .filter((entry) => compareStableVersions(entry.parsed, current) < 0) |
| 127 | + .toSorted((left, right) => compareStableVersions(right.parsed, left.parsed)); |
| 128 | + |
| 129 | + return candidates[0]?.tag; |
| 130 | + } |
| 131 | + |
| 132 | + const current = parseNightlyTag(currentTag); |
| 133 | + if (!current) { |
| 134 | + throw new Error(`Invalid nightly release tag '${currentTag}'.`); |
| 135 | + } |
| 136 | + |
| 137 | + const candidates = tags |
| 138 | + .map((tag) => ({ tag, parsed: parseNightlyTag(tag) })) |
| 139 | + .filter((entry): entry is { tag: string; parsed: NightlyVersion } => entry.parsed !== undefined) |
| 140 | + .filter((entry) => compareNightlyVersions(entry.parsed, current) < 0) |
| 141 | + .toSorted((left, right) => compareNightlyVersions(right.parsed, left.parsed)); |
| 142 | + |
| 143 | + return candidates[0]?.tag; |
| 144 | +}; |
| 145 | + |
| 146 | +const listGitTags = Effect.fn("listGitTags")(function* () { |
| 147 | + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 148 | + const child = yield* spawner.spawn(ChildProcess.make("git", ["tag", "--list"])); |
| 149 | + const tags = yield* child.stdout.pipe( |
| 150 | + Stream.decodeText(), |
| 151 | + Stream.runFold( |
| 152 | + () => "", |
| 153 | + (acc, chunk) => acc + chunk, |
| 154 | + ), |
| 155 | + Effect.map(String.split(/\r?\n/)), |
| 156 | + Effect.map(Array.map(String.trim)), |
| 157 | + Effect.map(Array.filter(String.isNonEmpty)), |
| 158 | + ); |
| 159 | + return tags; |
| 160 | +}); |
| 161 | + |
| 162 | +const writeOutput = Effect.fn("writeOutput")(function* ( |
| 163 | + previousTag: string | undefined, |
| 164 | + writeGithubOutput: boolean, |
| 165 | +) { |
| 166 | + const entry = `previous_tag=${previousTag ?? ""}\n`; |
| 167 | + |
| 168 | + if (writeGithubOutput) { |
| 169 | + const fs = yield* FileSystem.FileSystem; |
| 170 | + const githubOutputPath = yield* Config.nonEmptyString("GITHUB_OUTPUT"); |
| 171 | + yield* fs.writeFileString(githubOutputPath, entry, { flag: "a" }); |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + process.stdout.write(entry); |
| 176 | +}); |
| 177 | + |
| 178 | +const command = Command.make( |
| 179 | + "resolve-previous-release-tag", |
| 180 | + { |
| 181 | + channel: Flag.choice("channel", ReleaseChannel.literals).pipe( |
| 182 | + Flag.withDescription("Release channel whose previous tag should be resolved."), |
| 183 | + ), |
| 184 | + currentTag: Flag.string("current-tag").pipe( |
| 185 | + Flag.withDescription("Current release tag to compare against."), |
| 186 | + ), |
| 187 | + githubOutput: Flag.boolean("github-output").pipe( |
| 188 | + Flag.withDescription("Write values to GITHUB_OUTPUT instead of stdout."), |
| 189 | + Flag.withDefault(false), |
| 190 | + ), |
| 191 | + }, |
| 192 | + ({ channel, currentTag, githubOutput }) => |
| 193 | + listGitTags().pipe( |
| 194 | + Effect.map((tags) => resolvePreviousReleaseTag(channel, currentTag, tags)), |
| 195 | + Effect.flatMap((previousTag) => writeOutput(previousTag, githubOutput)), |
| 196 | + ), |
| 197 | +).pipe(Command.withDescription("Resolve the previous release tag for a stable or nightly series.")); |
| 198 | + |
| 199 | +Command.run(command, { version: "0.0.0" }).pipe( |
| 200 | + Effect.scoped, |
| 201 | + Effect.provide(NodeServices.layer), |
| 202 | + NodeRuntime.runMain, |
| 203 | +); |
0 commit comments