Skip to content

Commit

Permalink
Version determiner
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Nov 23, 2023
1 parent c96cf8c commit b6797c1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
23 changes: 19 additions & 4 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,34 @@ jobs:
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- id: determine-version
run: deno run -A scripts/determine-version.ts
- if: github.ref_type == 'tag'
run: '[[ "$VERSION" = "$GITHUB_REF_NAME" ]]'
env:
VERSION: ${{ steps.determine-version.outputs.version }}
- run: deno install -Af --unstable https://x.nest.land/eggs@0.3.10/eggs.ts
- run: "eggs link '${{ secrets.NEST_API_KEY }}'"
- if: github.ref_type == 'tag'
uses: mikefarah/yq@master
with:
cmd: >-
yq -i
' strenv(GITHUB_REF_NAME) as $version
| .version = $version
| .unstable = false'
egg.yaml
- if: github.ref_type != 'tag'
uses: mikefarah/yq@master
with:
cmd: >-
yq -i
' strenv(GITHUB_RUN_NUMBER) as $run-number
| strenv(GITHUB_SHA) as $sha
| .version = .version + "-dev." + $run-number + "+" + $sha
| .version |= (match("^[^+]+\+[a-f0-9]{7}") | .string)
' strenv(VERSION) as $version
| .version = $version
| .unstable = true'
egg.yaml
env:
VERSION: ${{ steps.determine-version.outputs.version }}
- run: |
# Try up to 3 times as `eggs publish` frequently fails due to unknown
# reason (I guess it's a bug on the server side):
Expand Down
1 change: 0 additions & 1 deletion egg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ $schema: https://x.nest.land/eggs@0.3.10/src/schema.json
name: aitertools
description: Well-tested utility functions dealing with async iterables
homepage: https://github.com/dahlia/aitertools
version: 0.5.0
releaseType: null
unstable: false
unlisted: false
Expand Down
68 changes: 68 additions & 0 deletions scripts/determine-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
dirname,
fromFileUrl,
join,
} from "https://deno.land/std@0.207.0/path/mod.ts";
import * as commonmark from "npm:commonmark";

const projectDir = dirname(dirname(fromFileUrl(import.meta.url)));
const changelogPath = join(projectDir, "CHANGES.md");

function findTopmostVersion(node: typeof commonmark.Node): string {
const walker = node.walker();
while (true) {
const event = walker.next();
if (!event) break;
const node = event.node;
if (
node.type == "heading" && node.level == 2 &&
node.firstChild.type == "text" &&
node.firstChild.literal.startsWith("Version")
) {
return node.firstChild.literal.replace(/^Version\s+/, "").trim();
}
}

throw new Error("Version not found");
}

async function findLatestVersionFromChangelog(path: string): Promise<string> {
const parser = new commonmark.Parser();
const text = await Deno.readTextFile(path);
const tree = parser.parse(text);
return findTopmostVersion(tree);
}

function determineVersionSuffix(): string {
const runNo = Deno.env.get("GITHUB_RUN_NUMBER");
const commitSha = Deno.env.get("GITHUB_SHA");
return `dev.${runNo}+${commitSha?.substring(0, 7)}`;
}

async function output(name: string, value: string): Promise<void> {
const outputPath = Deno.env.get("GITHUB_OUTPUT");
if (Deno.env.get("GITHUB_ACTIONS") === "true" && outputPath != null) {
const escaped = value
.replace(/%/g, "%25")
.replace(/\r/g, "%0D")
.replace(/\n/g, "%0A");
await Deno.writeTextFile(outputPath, `${name}=${escaped}\n`);
} else {
console.log(value);
}
}

async function main() {
const baseVersion = await findLatestVersionFromChangelog(changelogPath);
if (
Deno.env.get("GITHUB_ACTIONS") === "true" &&
Deno.env.get("GITHUB_REF_TYPE") === "branch"
) {
const version = `${baseVersion}-${determineVersionSuffix()}`;
await output("version", version);
} else {
await output("version", baseVersion);
}
}

if (import.meta.main) await main();

0 comments on commit b6797c1

Please sign in to comment.