Skip to content

Commit

Permalink
Pass forward version.raw etc. (#220)
Browse files Browse the repository at this point in the history
Pass forward version.raw etc.

The GitHub API is not reliable anymore. So let’s ping it as little as possible.

Closes #160
  • Loading branch information
mxcl committed Dec 14, 2023
1 parent 6c4badd commit 00d036d
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 19 deletions.
18 changes: 18 additions & 0 deletions .github/modify-pantry/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
runs:
using: composite
steps:
- run: pkgx --sync #FIXME bug where shebangs don’t cause an auto-sync
shell: bash
- uses: fregante/setup-git-user@v2
- run: git update-ref refs/remotes/origin/main HEAD
working-directory: ${{ github.workspace }}
shell: bash
- run: |
! ${GITHUB_ACTION_PATH}/../../bin/bk status
shell: bash
- run: cp package-mod.yml package.yml
working-directory: ${{ github.workspace }}/projects/stark.com/foo
shell: bash
- run: |
${GITHUB_ACTION_PATH}/../../bin/bk status
shell: bash
35 changes: 35 additions & 0 deletions .github/workflows/ci.actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,38 @@ jobs:

- run: exit 1
if: ${{steps.build.outputs.noop != 'true'}}

id:
env:
PKGX_PANTRY_PATH: ${{github.workspace}}
runs-on: ubuntu-latest
outputs:
pkgs: ${{ steps.id.outputs.pkgs }}
steps:
- uses: actions/checkout@v4
- uses: pkgxdev/setup@v2
- uses: ./id
id: id
with:
pkg: stark.com/foo
env:
PKGX_PANTRY_PATH: ${{github.workspace}}
- run: test ${{ steps.id.outputs.version }} = 2.3.4
- run: test ${{ steps.id.outputs.version-raw }} = 2.3.4
- run: test -z "${{ steps.id.outputs.version-tag }}"

- run: bin/bk build '${{ steps.id.outputs.pkg-json }}'

- uses: ./id
id: id2
with:
pkg: toolchain.com

- run: test v${{ steps.id2.outputs.version }} = ${{ steps.id2.outputs.version-tag }}
- run: test ${{ steps.id2.outputs.version-raw }} = ${{ steps.id2.outputs.version }}

# test that running off the `BREWKIT_PKGJSON` that the id action sets works
- run: bin/bk build
id: build

- run: test ${{ steps.build.outputs.project }} = 'toolchain.com'
7 changes: 1 addition & 6 deletions .github/workflows/ci.cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: pkgxdev/setup@v2
- run: pkgx --sync #FIXME bug where shebangs don’t cause an auto-sync
- uses: fregante/setup-git-user@v2
- run: git update-ref refs/remotes/origin/main HEAD
- run: '! bin/bk status'
- run: cp package-mod.yml package.yml
working-directory: projects/stark.com/foo
- uses: ./.github/modify-pantry
- run: test $(bin/bk status) = stark.com/foo
- run: bin/bk build
- run: bin/bk test
Expand Down
14 changes: 12 additions & 2 deletions bin/cmd/build
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { host } = utils
const { options, args } = await new Command()
.name("build")
.description("Build pkgx pantry pkgs with brewkit")
.option("-C, --clean=", "Clean everything first")
.option("-C, --clean", "Clean everything first")
.arguments("[pkgspec]")
.parse();

Expand Down Expand Up @@ -121,7 +121,7 @@ await fix_up(config)
const ghout = Deno.env.get("GITHUB_OUTPUT")
if (ghout) {
const { platform, arch } = host()
const pkgspec = `${utils.pkg.str(config.pkg)}`
const pkgspec = utils.pkg.str(config.pkg)
Deno.writeTextFileSync(ghout, `pkgspec=${pkgspec}\n`, { append: true})
Deno.writeTextFileSync(ghout, `project=${config.pkg.project}\n`, { append: true})
Deno.writeTextFileSync(ghout, `version=${config.pkg.version}\n`, { append: true})
Expand All @@ -130,6 +130,16 @@ if (ghout) {
Deno.writeTextFileSync(ghout, `arch=${arch}\n`, { append: true})
const ghenv = Deno.env.get("GITHUB_ENV")
if (ghenv) {
const json = {
project: config.pkg.project,
version: {
value: config.pkg.version.toString(),
raw: config.pkg.version.raw,
tag: config.pkg.version.tag
}
}

Deno.writeTextFileSync(ghenv, `BREWKIT_PKGJSON=${JSON.stringify(json)}\n`, { append: true })
Deno.writeTextFileSync(ghenv, `BREWKIT_PKGSPEC=${pkgspec}\n`, { append: true })
Deno.writeTextFileSync(ghenv, `BREWKIT_PREFIX=${config.path.install.string}\n`, { append: true })
}
Expand Down
2 changes: 1 addition & 1 deletion build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ runs:
fi
set +e
${GITHUB_ACTION_PATH}/../bin/cmd/build ${{ inputs.pkg }}
${GITHUB_ACTION_PATH}/../bin/cmd/build '${{ inputs.pkg }}'
status=$?
if [ $status -eq 2 ]; then
echo noop=true >> $GITHUB_OUTPUT
Expand Down
50 changes: 50 additions & 0 deletions id/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name:
pkgx/brewkit/id
description:
resolve a specific version, including the raw version and tag (if available)

inputs:
pkg:
description: eg. `pkgx.sh@1.1`.
required: true
token:
default: ${{github.token}}
required: true

outputs:
version:
description: eg 1.2.3
value: ${{ steps.version.outputs.value }}
version-raw:
description: eg 1.2
value: ${{ steps.version.outputs.raw }}
version-tag:
description: eg v1.2
value: ${{ steps.version.outputs.tag }}
pkg-json:
description: json that can be fed to build/test etc.
value: ${{ steps.version.outputs.json }}

runs:
using: composite
steps:
- name: prep
run: |
if ! pkgx --sync; then
echo "::error::you must use: pkgxdev/setup before using this action"
exit 1
fi
shell: bash

- name: fetch deno deps
run: |
echo "::group::fetch deno deps"
pkgx deno cache ${GITHUB_ACTION_PATH}/id.ts --config=${GITHUB_ACTION_PATH}/../deno.jsonc
echo "::endgroup::"
shell: bash

- run: ${GITHUB_ACTION_PATH}/id.ts ${{ inputs.pkg }}
id: version
shell: bash
env:
GH_TOKEN: ${{ inputs.token }}
30 changes: 30 additions & 0 deletions id/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env -S pkgx +git +gh deno run --allow-env --allow-net --allow-write --allow-run=gh --allow-read

import get_config from "brewkit/config.ts"

const config = await get_config(Deno.args[0])

const ghout = Deno.env.get("GITHUB_OUTPUT")

Deno.writeTextFileSync(ghout!, `value=${config.pkg.version.toString()}\n`, {append: true})
if (config.pkg.version.raw) {
Deno.writeTextFileSync(ghout!, `raw=${config.pkg.version.raw}\n`, {append: true})
}
if (config.pkg.version.tag) {
Deno.writeTextFileSync(ghout!, `tag=${config.pkg.version.tag}\n`, {append: true})
}

const json = {
project: config.pkg.project,
version: {
value: config.pkg.version.toString(),
raw: config.pkg.version.raw,
tag: config.pkg.version.tag
}
}

Deno.writeTextFileSync(ghout!, `json=${JSON.stringify(json)}\n`, {append: true})

Deno.writeTextFileSync(Deno.env.get("GITHUB_ENV")!, `BREWKIT_PKGJSON=${JSON.stringify(json)}\n`, {append: true})

console.log(json)
37 changes: 27 additions & 10 deletions lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useConfig from "libpkgx/hooks/useConfig.ts"
import { Path, Package, PackageRequirement, utils, hooks, plumbing, Installation } from "pkgx"
import { Path, Package, PackageRequirement, utils, hooks, plumbing, Installation, SemVer, semver } from "pkgx"
const { flatmap, host } = utils
const { usePantry } = hooks
const { hydrate } = plumbing
Expand Down Expand Up @@ -34,17 +34,34 @@ export interface ConfigPath {

export default async function config(arg?: string): Promise<Config> {
if (!arg) {
arg ??= Deno.env.get("BREWKIT_PKGSPEC") ?? (await get_pantry_status())?.[0]
arg ??= Deno.env.get("BREWKIT_PKGJSON")
arg ??= Deno.env.get("BREWKIT_PKGSPEC")
arg ??= (await get_pantry_status())?.[0]
if (!arg) throw new Error(`usage: ${Deno.execPath()} <pkgspec>`)
}

const { constraint, project } = utils.pkg.parse(arg)
const [found, ...rest] = await usePantry().find(project)
if (rest.length) throw new Error("ambiguous pkg spec")
const pkg = await usePantry().resolve({project: found.project, constraint})
const { pkg, constraint, path } = await (async () => {
if (arg.startsWith("{")) {
const json = JSON.parse(arg)
const project = json.project
const version = new SemVer(json.version.raw)
const [found] = await usePantry().find(project)
return {
pkg: {project, version},
constraint: new semver.Range(`=${version}`),
path: found.path
}
} else {
const { constraint, project } = utils.pkg.parse(arg)
const [found, ...rest] = await usePantry().find(project)
if (rest.length) throw new Error("ambiguous pkg spec")
const pkg = await usePantry().resolve({project: found.project, constraint})
return { constraint, path: found.path, pkg }
}
})()

let pantry = found.path
for (let x = 0, N = found.project.split('/').length + 1; x < N; x++) {
let pantry = path
for (let x = 0, N = pkg.project.split('/').length + 1; x < N; x++) {
pantry = pantry.parent()
}

Expand Down Expand Up @@ -89,7 +106,7 @@ export default async function config(arg?: string): Promise<Config> {

return {
pkg,
pkgspec: {project: found.project, constraint},
pkgspec: {project: pkg.project, constraint},
deps: {
dry,
wet,
Expand All @@ -98,7 +115,7 @@ export default async function config(arg?: string): Promise<Config> {
path: {
cache,
pantry,
yaml: found.path,
yaml: path,
src,
build,
install,
Expand Down

0 comments on commit 00d036d

Please sign in to comment.