|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import { $ } from "bun"; |
| 4 | + |
| 5 | +const ERR_NO_TAGS = 1; |
| 6 | + |
| 7 | +const REPO_LINK = "https://github.com/foxssake/netfox"; |
| 8 | +const LATEST_REF = "HEAD"; |
| 9 | + |
| 10 | +interface Semver { |
| 11 | + prefix: string | undefined; |
| 12 | + major: number; |
| 13 | + minor: number; |
| 14 | + patch: number; |
| 15 | + suffix: string | undefined; |
| 16 | +} |
| 17 | + |
| 18 | +interface Commit { |
| 19 | + hash: string; |
| 20 | + description: string; |
| 21 | +} |
| 22 | + |
| 23 | +function parseSemver(version: string): Semver | undefined { |
| 24 | + const pattern = /(v?)(\d+)\.(\d+)\.(\d+)([\.-].*)?/; |
| 25 | + const matches = pattern.exec(version); |
| 26 | + if (matches === null) return undefined; |
| 27 | + |
| 28 | + const result = { |
| 29 | + prefix: matches[1], |
| 30 | + major: parseInt(matches[2]), |
| 31 | + minor: parseInt(matches[3]), |
| 32 | + patch: parseInt(matches[4]), |
| 33 | + suffix: matches[5], |
| 34 | + } as Semver; |
| 35 | + |
| 36 | + if (result.suffix === "") result.suffix = undefined; |
| 37 | + |
| 38 | + return result; |
| 39 | +} |
| 40 | + |
| 41 | +function compareSemver(a: Semver, b: Semver): number { |
| 42 | + if (a.major != b.major) return a.major - b.major; |
| 43 | + if (a.minor != b.minor) return a.minor - b.minor; |
| 44 | + if (a.patch != b.patch) return a.patch - b.patch; |
| 45 | + if (a.suffix != b.suffix) { |
| 46 | + if (a.suffix === undefined) return 1; // a has no suffix, therefore it's more mature |
| 47 | + if (b.suffix === undefined) return -1; // b has no suffix, therefore it's more mature |
| 48 | + if (a.suffix !== undefined && b.suffix !== undefined) |
| 49 | + return a.suffix.localeCompare(b.suffix); |
| 50 | + } |
| 51 | + return 0; |
| 52 | +} |
| 53 | + |
| 54 | +function stringifySemver(version: Semver): string { |
| 55 | + return `${version.prefix ?? ""}${version.major}.${version.minor}.${version.patch}${version.suffix ?? ""}`; |
| 56 | +} |
| 57 | + |
| 58 | +function parseOnelineCommit(line: string): Commit | undefined { |
| 59 | + const pattern = /([\w\d]+) (.+)/; |
| 60 | + const matches = pattern.exec(line); |
| 61 | + if (matches === null) return undefined; |
| 62 | + |
| 63 | + return { |
| 64 | + hash: matches[1], |
| 65 | + description: matches[2], |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +function renderCommitDescription(description: string): string { |
| 70 | + const pattern = /(.*)\(#([\d]+)\)$/; |
| 71 | + const matches = pattern.exec(description); |
| 72 | + if (matches === null) return description; |
| 73 | + |
| 74 | + const body = matches[1]; |
| 75 | + const pullId = matches[2]; |
| 76 | + const link = `${REPO_LINK}/pull/${pullId}`; |
| 77 | + return `${body}([#${pullId}](${link}))`; |
| 78 | +} |
| 79 | + |
| 80 | +function renderCommit(commit: Commit): string { |
| 81 | + const link = `${REPO_LINK}/commit/${commit.hash}`; |
| 82 | + const body = renderCommitDescription(commit.description); |
| 83 | + return `${body} [🔗](${link})`; |
| 84 | +} |
| 85 | + |
| 86 | +function renderRelease(tag: string, commits: Commit[]): string { |
| 87 | + const title = tag === LATEST_REF ? "Latest" : tag; |
| 88 | + const body = commits.map((it) => "* " + renderCommit(it)).join("\n"); |
| 89 | + return `## ${title}\n${body}`; |
| 90 | +} |
| 91 | + |
| 92 | +async function main(): Promise<number> { |
| 93 | + const versions = (await Array.fromAsync($`git tag`.quiet().lines())) |
| 94 | + .filter((it) => it.startsWith("v")) |
| 95 | + .map((it) => parseSemver(it)) |
| 96 | + .filter((it) => it !== undefined) |
| 97 | + .sort(compareSemver) |
| 98 | + .reverse() |
| 99 | + .map(stringifySemver); |
| 100 | + |
| 101 | + if (versions.length == 0) { |
| 102 | + console.error("No version tags found!"); |
| 103 | + return ERR_NO_TAGS; |
| 104 | + } |
| 105 | + |
| 106 | + versions.unshift(LATEST_REF); |
| 107 | + |
| 108 | + const logIntervals = versions.map((it, idx) => [ |
| 109 | + it, |
| 110 | + versions.at(idx + 1) ?? "", |
| 111 | + ]); |
| 112 | + |
| 113 | + const logs = await Promise.all( |
| 114 | + logIntervals |
| 115 | + .map(([from, to]) => |
| 116 | + to !== "" |
| 117 | + ? $`git log --format=oneline ${to}..${from}`.quiet().lines() |
| 118 | + : $`git log --format=oneline ${from}`.quiet().lines(), |
| 119 | + ) |
| 120 | + .map((it) => Array.fromAsync(it)) |
| 121 | + .map((it) => |
| 122 | + it.then((lines) => |
| 123 | + lines |
| 124 | + .map(parseOnelineCommit) |
| 125 | + .filter((commit) => commit !== undefined), |
| 126 | + ), |
| 127 | + ), |
| 128 | + ); |
| 129 | + |
| 130 | + const releaseNotes = versions.map((version, idx) => [ |
| 131 | + version, |
| 132 | + logs.at(idx) ?? [], |
| 133 | + ]) as [string, Commit[]][]; |
| 134 | + |
| 135 | + console.log("# Changelog\n"); |
| 136 | + console.log( |
| 137 | + releaseNotes |
| 138 | + .map(([version, commits]) => renderRelease(version, commits)) |
| 139 | + .join("\n\n"), |
| 140 | + ); |
| 141 | + |
| 142 | + return 0; |
| 143 | +} |
| 144 | + |
| 145 | +process.exit(await main()); |
0 commit comments