Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
- git-clone.com
- pc-cmake.com
- fix-machos.com
- version-transformer.com

runs-on: ${{ matrix.platform.os }}
container: ${{ matrix.platform.img }}
Expand Down
66 changes: 59 additions & 7 deletions lib/hooks/usePantry.getVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function handleGitHubVersions(versions: PlainObject): Promise<SemVer[]> {
const type = types?.join("/").chuzzle() ?? "releases/tags";

const ignore = parseIgnore(versions.ignore);

const transform = (x => isString(x) ? x : undefined)(versions.transform);
const strip = parseStrip(versions.strip);

switch (type) {
Expand All @@ -81,7 +81,7 @@ function handleGitHubVersions(versions: PlainObject): Promise<SemVer[]> {

const fetch = useGitHubAPI().getVersions({ user, repo, type });

return handleAPIResponse({ fetch, ignore, strip });
return handleAPIResponse({ fetch, ignore, strip, transform });
}

function handleGitLabVersions(versions: PlainObject): Promise<SemVer[]> {
Expand Down Expand Up @@ -169,18 +169,27 @@ interface APIResponseParams {
fetch: AsyncGenerator<{ version: string; tag?: string }, any, unknown>;
ignore: RegExp[];
strip: (x: string) => string;
transform?: string | undefined;
}

async function handleAPIResponse(
{ fetch, ignore, strip }: APIResponseParams,
): Promise<SemVer[]> {
async function handleAPIResponse({ fetch, ignore, strip, transform }: APIResponseParams): Promise<SemVer[]>
{
const rv: SemVer[] = [];

// if (transform) {
// handleTransformer({ transform }, fetch).then(x => rv.push(...x)
// }

const verstrs: string[] = []
for await (const { version: pre_strip_name, tag } of fetch) {
let name = strip(pre_strip_name);

if (ignore.some((x) => x.test(name))) {
console.debug({ ignoring: pre_strip_name, reason: "explicit" });
} else {
continue;
}

if (!transform) {
// An unfortunate number of tags/releases/other
// replace the dots in the version with underscores.
// This is parser-unfriendly, but we can make a
Expand All @@ -196,7 +205,6 @@ async function handleAPIResponse(
if (name.includes("-") && !name.includes(".")) {
name = name.replace(/-/g, ".");
}

const v = semver.parse(name);
if (!v) {
console.debug({ ignoring: pre_strip_name, reason: "unparsable" });
Expand All @@ -208,9 +216,15 @@ async function handleAPIResponse(
} else {
console.debug({ ignoring: pre_strip_name, reason: "prerelease" });
}
} else {
verstrs.push(name)
}
}

if (transform) {
rv.push(...await handleTransformer(transform, verstrs))
}

if (rv.length == 0) {
console.warn("no versions parsed. Re-run with DEBUG=1 to see output.");
}
Expand Down Expand Up @@ -268,3 +282,41 @@ async function handleNPMVersions(versions: PlainObject): Promise<SemVer[]> {
}
return rv;
}

import undent from "outdent"

async function handleTransformer(transform: string, versions: string[]): Promise<SemVer[]> {
/// sadly deno built binaries cannot `eval` so we have to run a whole script 😕

const cmd = new Deno.Command("pkgx", {
args: ["deno", "run", "-"],
stdin: "piped",
stdout: "piped",
}).spawn()

const vv = versions.map(x => `"${x}"`).join(',')

const writer = cmd.stdin!.getWriter()
await writer.write(new TextEncoder().encode(undent`
const transform = ${transform}
for (const v of [${vv}]) {
console.log(transform(v), v)
}
`));
await writer.close()

const read = cmd.stdout.getReader().read().then(x => x.value)

const [out, { success: ok }] = await Promise.all([read, cmd.status])

if (!ok) throw new Error("failed to run version transformer")

return new TextDecoder().decode(out).split('\n').compact(x => {
const [transformed, original] = x.split(' ')
const v = semver.parse(transformed)
if (v) {
(v as unknown as { tag: string }).tag = original
return v
}
})
}
11 changes: 11 additions & 0 deletions projects/version-transformer.com/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
versions:
github: ImageMagick/ImageMagick
transform: v => v.replace('-', '.')

build:
working-directory: ${{prefix}}
script: |
echo {{version}} > VERSION

test:
test $(cat {{prefix}}/VERSION) = {{version}}