-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: migrate changelog generation to deno (#234)
* build: use import map for dax * chore: indent size * feat: parse commits * feat: feature parity * chore: flatten `scripts/` * feat: cli * test: update render result checks
- Loading branch information
Showing
22 changed files
with
311 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { assertEquals } from "$std/assert/assert_equals.ts" | ||
import { getSections, parseCommits } from "./mod.ts" | ||
|
||
export const exampleCommits = parseCommits([ | ||
"feat: asdf (#1234)", | ||
"feat!: test (#123)", | ||
"fix(L10n): `language` stuff (#415)", | ||
]) | ||
export const exampleSections = getSections(exampleCommits) | ||
|
||
Deno.test("parseCommits() correctly parse commits", () => { | ||
assertEquals(exampleCommits, [ | ||
{ | ||
type: "feat", | ||
scopes: undefined, | ||
breaking: undefined, | ||
subject: "asdf", | ||
pr: "1234", | ||
}, | ||
{ | ||
type: "feat", | ||
scopes: undefined, | ||
breaking: "!", | ||
subject: "test", | ||
pr: "123", | ||
}, | ||
{ | ||
type: "fix", | ||
scopes: "L10n", | ||
breaking: undefined, | ||
subject: "`language` stuff", | ||
pr: "415", | ||
}, | ||
]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import $ from "$dax/mod.ts" | ||
import { typedRegEx } from "https://deno.land/x/typed_regex@0.2.0/mod.ts" | ||
import type { RegExCaptureResult } from "https://deno.land/x/typed_regex@0.2.0/type_parser.ts" | ||
|
||
import { renderBBCode } from "./render_bbcode.ts" | ||
import { getNextVersion } from "./semver.ts" | ||
import { renderMarkdown } from "./render_markdown.ts" | ||
import { renderStS } from "./render_sts.ts" | ||
|
||
export type Commit = RegExCaptureResult<typeof re> | ||
export type Sections = Record<string, Commit[]> | ||
|
||
export const getTags = () => $`git tag --sort=-v:refname`.lines() | ||
|
||
type CommitRange = { from: string; to: string } | ||
export const getCommits = ({ from, to }: CommitRange) => | ||
$`git log ${from}..${to} --pretty=format:"%s"`.lines() | ||
|
||
const re = | ||
"^(?<type>\\w+)(\\((?<scopes>.*)?\\))?(?<breaking>!)?:\\s*(?<subject>.*?)\\s*\\(#(?<pr>\\d+)\\)$" | ||
|
||
const commitParser = typedRegEx(re) | ||
|
||
export const parseCommits = (xs: string[]) => | ||
xs | ||
.map(commitParser.captures) | ||
.filter((x): x is Commit => x !== undefined) | ||
.filter((x) => x.breaking || ["fix", "feat"].includes(x.type)) | ||
|
||
export const getSections = (commits: Commit[]): Sections => ({ | ||
"Breaking Changes": commits.filter((x) => x.breaking), | ||
"New Features": commits.filter((x) => x.type === "feat"), | ||
Fixes: commits.filter((x) => x.type === "fix"), | ||
}) | ||
|
||
if (import.meta.main) { | ||
const [latestTag] = await getTags() | ||
const commits = await getCommits({ from: latestTag, to: "main" }).then(parseCommits) | ||
if (commits.length === 0) { | ||
console.log("No new commits") | ||
Deno.exit(0) | ||
} | ||
|
||
const option = { | ||
version: getNextVersion(latestTag, commits), | ||
date: new Date().toISOString().split("T")[0], | ||
sections: getSections(commits), | ||
} | ||
|
||
const files = { | ||
bbcode: renderBBCode, | ||
md: renderMarkdown, | ||
"sts.txt": renderStS, | ||
} | ||
|
||
const changelogPath = `${import.meta.dirname}/../docs/changelog` | ||
|
||
const writes = Object.entries(files) | ||
.map(([ext, render]) => [ext, render(option)]) | ||
.map(([ext, text]) => { | ||
if (ext === "md") console.log(text) | ||
return Deno.writeTextFile(`${changelogPath}/changelog.${ext}`, text) | ||
}) | ||
.concat(Deno.writeTextFile(`${changelogPath}/version.txt`, option.version.replace("v", ""))) | ||
|
||
await Promise.all(writes) | ||
|
||
switch (Deno.args[0]) { | ||
case "nextversion": { | ||
await $`git tag ${option.version}` | ||
console.log(await getTags()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Commit, Sections } from "./mod.ts" | ||
|
||
export type ChangelogRenderer = (option: { | ||
version: string | ||
date: string | ||
sections: Sections | ||
}) => string | ||
|
||
export type SectionFormatter = (tup: [section: string, commits: Commit[]]) => string | ||
export type SectionsFormatter = { fmtSection: SectionFormatter; sections: Sections } | ||
|
||
export const renderSections = ( | ||
{ fmtSection, sections }: SectionsFormatter, | ||
) => | ||
Object.entries(sections) | ||
.filter(([, commits]) => commits.length > 0) | ||
.map(fmtSection) | ||
.join("\n\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { outdent } from "$dax/src/deps.ts" | ||
import { Commit } from "./mod.ts" | ||
import { ChangelogRenderer, renderSections, SectionFormatter } from "./render.ts" | ||
|
||
const fmtCommit = (x: Commit) => | ||
` [*] ${x.subject} ([url=https://github.com/scarf005/marisa/pull/${x.pr}]#${x.pr}[/url])` | ||
.replace(/`([^`]+)`/g, "[i]$1[/i]") | ||
|
||
const fmtSection: SectionFormatter = ([section, commits]) => | ||
`[h2]${section}[/h2]\n` + "[list]\n" + commits.map(fmtCommit).join("\n") + "\n[/list]" | ||
|
||
export const renderBBCode: ChangelogRenderer = ({ version, date, sections }) => | ||
outdent` | ||
[h1]${version} (${date})[/h1] | ||
${renderSections({ fmtSection, sections })} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { assertEquals } from "$std/assert/assert_equals.ts" | ||
import { outdent } from "$dax/src/deps.ts" | ||
import { renderBBCode } from "./render_bbcode.ts" | ||
import { exampleSections } from "./changelog_test.ts" | ||
|
||
Deno.test("renderBBCode() outputs identical text", () => | ||
assertEquals( | ||
renderBBCode({ version: "v1.0.0", date: "2024-03-12", sections: exampleSections }), | ||
outdent` | ||
[h1]v1.0.0 (2024-03-12)[/h1] | ||
[h2]Breaking Changes[/h2] | ||
[list] | ||
[*] test ([url=https://github.com/scarf005/marisa/pull/123]#123[/url]) | ||
[/list] | ||
[h2]New Features[/h2] | ||
[list] | ||
[*] asdf ([url=https://github.com/scarf005/marisa/pull/1234]#1234[/url]) | ||
[*] test ([url=https://github.com/scarf005/marisa/pull/123]#123[/url]) | ||
[/list] | ||
[h2]Fixes[/h2] | ||
[list] | ||
[*] [i]language[/i] stuff ([url=https://github.com/scarf005/marisa/pull/415]#415[/url]) | ||
[/list] | ||
`, | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { outdent } from "$dax/src/deps.ts" | ||
import { Commit } from "./mod.ts" | ||
import { ChangelogRenderer, renderSections, SectionFormatter } from "./render.ts" | ||
|
||
const fmtCommit = (x: Commit) => | ||
`- ${x.subject} (#${x.pr})` | ||
|
||
const fmtSection: SectionFormatter = ([section, commits]) => | ||
`## ${section}\n\n` + commits.map(fmtCommit).join("\n") | ||
|
||
export const renderMarkdown: ChangelogRenderer = ({ version, date, sections }) => | ||
outdent` | ||
# ${version} (${date}) | ||
${renderSections({ fmtSection, sections })} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { outdent } from "$dax/src/deps.ts" | ||
import { Commit } from "./mod.ts" | ||
import { ChangelogRenderer, renderSections, SectionFormatter } from "./render.ts" | ||
|
||
export const fmtCommit = (x: Commit) => | ||
`- ${x.subject}` | ||
.replace(/`([^`]+)`/g, "[$1]") | ||
.replace(/\(#(\d+)\)/g, "") | ||
.trim() | ||
|
||
const fmtSection: SectionFormatter = ([section, commits]) => | ||
`* ${section}\n\n` + commits.map(fmtCommit).join("\n") | ||
|
||
export const renderStS: ChangelogRenderer = ({ version, date, sections }) => | ||
outdent` | ||
What's new in ${version} (${date}) | ||
${renderSections({ fmtSection, sections })} | ||
please visit https://github.com/scarf005/Marisa/releases/latest for more info | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { assertEquals } from "$std/assert/assert_equals.ts" | ||
import { Commit } from "./mod.ts" | ||
import { fmtCommit } from "./render_sts.ts" | ||
|
||
Deno.test( | ||
"fmtCommit() strips invalid characters", | ||
() => assertEquals(fmtCommit({ subject: "`foo` (#123)" } as unknown as Commit), "- [foo]"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { SemVer } from "$std/semver/types.ts" | ||
import type { Commit } from "./mod.ts" | ||
|
||
import { format, increment, parse } from "$std/semver/mod.ts" | ||
|
||
const increaseBy = (commit: Commit): "major" | "minor" | "patch" => | ||
commit.breaking ? "major" : commit.type === "feat" ? "minor" : "patch" | ||
|
||
export const increaseVersion = (begin: SemVer, commits: Commit[]): SemVer => | ||
commits.reduce(({ ver, count }, commit) => { | ||
const by = increaseBy(commit) | ||
count[by]++ | ||
return (count[by] > 1) ? { ver, count } : { ver: increment(ver, increaseBy(commit)), count } | ||
}, { ver: begin, count: { major: 0, minor: 0, patch: 0 } }).ver | ||
|
||
export const getNextVersion = (latest: string, commits: Commit[]): string => | ||
"v" + format(increaseVersion(parse(latest), commits)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { assertObjectMatch } from "$std/assert/assert_object_match.ts" | ||
import { assertEquals } from "$std/assert/assert_equals.ts" | ||
import { getNextVersion, increaseVersion } from "./semver.ts" | ||
|
||
const major = { type: "fix", breaking: "!", subject: "big stuff", pr: "123", scopes: undefined } | ||
const minor = { type: "feat", breaking: undefined, subject: "feat", pr: "124", scopes: undefined } | ||
const patch = { type: "fix", breaking: undefined, subject: "fix", pr: "125", scopes: undefined } | ||
|
||
Deno.test("increaseVersion() only increases biggest change", async (t) => { | ||
await t.step("major", () => { | ||
assertObjectMatch( | ||
increaseVersion({ major: 1, minor: 0, patch: 0 }, [ | ||
minor, | ||
patch, | ||
major, | ||
]), | ||
{ major: 2, minor: 0, patch: 0 }, | ||
) | ||
}) | ||
|
||
await t.step("minor", () => { | ||
assertObjectMatch( | ||
increaseVersion({ major: 1, minor: 0, patch: 0 }, [patch, minor]), | ||
{ major: 1, minor: 1, patch: 0 }, | ||
) | ||
}) | ||
}) | ||
|
||
Deno.test("nextVersion() returns correct version", () => { | ||
assertEquals(getNextVersion("v1.0.0", [minor, patch]), "v1.1.1") | ||
}) | ||
|
||
Deno.test("nextVersion() handles multiple patch in single release", () => { | ||
assertEquals(getNextVersion("v1.0.0", [patch, patch, patch]), "v1.0.1") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters