Skip to content

Commit

Permalink
use libtea (#155)
Browse files Browse the repository at this point in the history
Fixes #158

We now use vercel/ncc as a distribution pre-req because otherwise we’re way too big.

Also we prune the koffi modules down to only what we need for the same reasons.
  • Loading branch information
mxcl committed May 28, 2023
1 parent 972ff14 commit c9d315c
Show file tree
Hide file tree
Showing 15 changed files with 336 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
echo "::set-output name=result::cancel"
fi
- uses: andymckay/cancel-action@0.2
- uses: andymckay/cancel-action@0.3
if: ${{ steps.rev-parse.outputs.result == 'cancel' }}

ci:
Expand Down
23 changes: 18 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ jobs:
verbose: VERBOSE
steps:
- uses: actions/checkout@v3
- run: echo foo > foo
- run: ./install.sh --yes node --eval 'console.log(1)'
- run: ./install.sh node --eval 'console.log(1)'
env:
VERBOSE: ${{ matrix.verbose }}
- run: ./install.sh sh -c "which tea"
Expand All @@ -44,7 +43,7 @@ jobs:
- [self-hosted, macOS, ARM64]
- [self-hosted, linux, ARM64]
prefix:
- ~
- null
- foo
include:
- os: ubuntu-latest
Expand Down Expand Up @@ -74,6 +73,7 @@ jobs:
gha:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- macos-latest
Expand Down Expand Up @@ -169,9 +169,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ludeeus/action-shellcheck@1.1.0
- uses: ludeeus/action-shellcheck@2.0.0
with:
severity: warning
ignore_paths: scripts # this action is badly designed you can’t be more specific that a directory or filename (not path)
env:
SHELLCHECK_OPTS: --shell dash

Expand All @@ -196,6 +197,9 @@ jobs:
- 0.26.3
- 0.18.1
- 0.17.3
include:
- version: ^0.33
system: gha
steps:
- uses: actions/checkout@v3
- uses: ./
Expand All @@ -204,7 +208,8 @@ jobs:
version: ${{ matrix.version }}
- run: ./install.sh --yes --version ${{ matrix.version }}
if: ${{ matrix.system == 'direct' }}
- run: test "$(tea --version)" = "tea ${{ matrix.version }}"
- run: tea --version | grep ${{ matrix.version }}
if: ${{ matrix.version != '^0.33' }}

sudo-not-required:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -295,3 +300,11 @@ jobs:
if line.startswith(' '):
print(f'Error: Space indentation found on line {line_number}')
sys.exit(1)
check-dist-was-run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./
- run: ./scripts/dist.sh
- run: git diff --exit-code
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/out.gif
.DS_Store
/node_modules
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* [`install.sh`](./install.sh) is delivered when you `curl tea.xyz`.
* This repository also provides the `tea` GitHub Action.

# GitHub Action 0.17.1
# GitHub Action 0.18.0

```yaml
- uses: teaxyz/setup@v0
Expand Down
167 changes: 66 additions & 101 deletions action.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const { execSync, spawn } = require('child_process')
const https = require('https')
const { prefab, hooks, Path, utils, semver, SemVer } = require("@teaxyz/lib")
const { install, link, resolve, hydrate } = prefab
const { getExecOutput, exec } = require("@actions/exec")
const { useConfig, useSync, useCellar } = hooks
const core = require('@actions/core')
const path = require('path')
const fs = require('fs')
const os = require("os")

async function go() {
process.stderr.write("determining latest tea version…\n")
const TEA_PREFIX = core.getInput('prefix') || `${os.homedir()}/.tea`

const PREFIX = process.env['INPUT_PREFIX'] || `${os.homedir()}/.tea`
const TEA_DIR = (() => {
let TEA_DIR = process.env['INPUT_SRCROOT']
if (!TEA_DIR) return
TEA_DIR = TEA_DIR.trim()
let TEA_DIR = core.getInput('srcroot').trim()
if (!TEA_DIR) return
if (!TEA_DIR.startsWith("/")) {
// for security this must be an absolute path
Expand All @@ -20,149 +19,115 @@ async function go() {
return path.normalize(TEA_DIR)
})()

const additional_pkgs = []
let vtea = core.getInput('version') ?? ""
if (vtea && !/^[*^~@=]/.test(vtea)) {
vtea = `@${vtea}`
}

const pkgs = [`tea.xyz${vtea}`]
for (let key in process.env) {
if (key.startsWith("INPUT_+")) {
const value = process.env[key]
if (key == 'INPUT_+') {
for (const item of value.split(/\s+/)) {
if (item.trim()) {
additional_pkgs.push(`+${item}`)
pkgs.push(item)
}}} else {
key = key.slice(6).toLowerCase()
additional_pkgs.push(key+value)
key = key.slice(7).toLowerCase()
pkgs.push(key+value)
}}}

// we build to /opt and special case this action so people new to
// building aren’t immediatelyt flumoxed
if (PREFIX == '/opt' && os.platform == 'darwin') {
execSync('sudo chown $(whoami):staff /opt')
// building aren’t immediately flumoxed
if (TEA_PREFIX == '/opt' && os.platform == 'darwin') {
await exec('sudo', ['chown', `${os.userInfo().username}:staff`, '/opt'])
}

const midfix = (() => {
switch (process.arch) {
case 'arm64':
return `${process.platform}/aarch64`
case 'x64':
return `${process.platform}/x86-64`
default:
throw new Error(`unsupported platform: ${process.platform}/${process.arch}`)
}
})()

const v = process.env['INPUT_VERSION'] || await new Promise((resolve, reject) => {
https.get(`https://dist.tea.xyz/tea.xyz/${midfix}/versions.txt`, rsp => {
if (rsp.statusCode != 200) return reject(rsp.statusCode)
rsp.setEncoding('utf8')
const chunks = []
rsp.on("data", x => chunks.push(x))
rsp.on("end", () => {
resolve(chunks.join("").trim().split("\n").at(-1))
})
}).on('error', reject)
})

process.stderr.write(`fetching tea.xyz@${v}\n`)

fs.mkdirSync(PREFIX, { recursive: true })
core.info(`fetching ${pkgs.join(", ")}…`)

const exitcode = await new Promise((resolve, reject) => {
https.get(`https://dist.tea.xyz/tea.xyz/${midfix}/v${v}.tar.gz`, rsp => {
if (rsp.statusCode != 200) return reject(rsp.statusCode)
const tar = spawn('tar', ['xzf', '-'], { stdio: ['pipe', 'inherit', 'inherit'], cwd: PREFIX })
rsp.pipe(tar.stdin)
tar.on("close", resolve)
}).on('error', reject)
useConfig({
prefix: new Path(TEA_PREFIX),
pantries: [],
cache: new Path(TEA_PREFIX).join('tea.xyz/var/www'),
UserAgent: 'tea.setup/0.1.0', //TODO version
options: { compression: 'gz' }
})

if (exitcode != 0) {
throw new Error(`tar: ${exitcode}`)
await useSync()
const { pkgs: tree } = await hydrate(pkgs.map(utils.pkg.parse))
const { pending } = await resolve(tree)
for (const pkg of pending) {
core.info(`installing ${utils.pkg.str(pkg)}`)
const installation = await install(pkg)
await link(installation)
}

const oldwd = process.cwd()
process.chdir(`${PREFIX}/tea.xyz`)
if (fs.existsSync(`v*`)) fs.unlinkSync(`v*`)
fs.symlinkSync(`v${v}`, `v*`, 'dir')
if (fs.existsSync(`v0`)) fs.unlinkSync(`v0`)
fs.symlinkSync(`v${v}`, `v0`, 'dir') //FIXME
process.chdir(oldwd)

const GITHUB_PATH = process.env['GITHUB_PATH']
const bindir = `${PREFIX}/tea.xyz/v${v}/bin`
fs.appendFileSync(GITHUB_PATH, `${bindir}\n`, {encoding: 'utf8'})
const tea = await useCellar().resolve({project: 'tea.xyz', constraint: new semver.Range('*')})
const teafile = tea.path.join('bin/tea').string
const env_args = ['--env']

const teafile = `${bindir}/tea`

const env = {
TEA_DIR,
...process.env
if (TEA_DIR && tea.pkg.version.gte(new SemVer("0.19"))) {
env_args.push('--keep-going')
}

const GITHUB_ENV = process.env['GITHUB_ENV']
const GITHUB_OUTPUT = process.env['GITHUB_OUTPUT']

const vv = parseFloat(v)
const env_flag = TEA_DIR ? vv >= 0.19 ? '--env --keep-going' : '--env' : ''
let args = tea.pkg.version.gte(new SemVer("0.21"))
? []
: tea.pkg.version.gte(new SemVer("0.19"))
? ["--dry-run"]
: ["--dump"]

// get env FIXME one call should do init

let args = vv >= 0.21
? ""
: vv >= 0.19
? "--dry-run"
: "--dump"

if (process.env["INPUT_CHASTE"] == "true") {
args += " --chaste"
if (core.getBooleanInput("chaste")) {
args.push('--chaste')
}

out = execSync(`${teafile} --sync ${env_flag} ${args} ${additional_pkgs.join(" ")}`, {env}).toString()
//FIXME we’re running tea/cli since dev-envs are not in libtea
// and we don’t want them in libtea, but we may need a libteacli as a result lol
const { stdout: out } = await getExecOutput(
teafile,
[...env_args, ...args, ...pkgs.map(x=>`+${x}`)],
{env: { ...process.env, TEA_DIR, TEA_PREFIX }})

const lines = out.split("\n")
for (const line of lines) {
const match = line.match(/(export )?([A-Za-z0-9_]+)=['"]?(.*)/)
if (!match) continue
const [,,key,value] = match
if (key == 'VERSION') {
fs.appendFileSync(GITHUB_OUTPUT, `version=${value}\n`, {encoding: 'utf8'})
}
if (key == 'PATH') {
for (const part of value.split(":").reverse()) {
fs.appendFileSync(GITHUB_PATH, `${part}\n`, {encoding: 'utf8'})
core.addPath(part)
}
} else {
fs.appendFileSync(GITHUB_ENV, `${key}=${value}\n`, {encoding: 'utf8'})
core.exportVariable(key, value)
if (key == 'VERSION') {
core.setOutput('version', value)
}
}
}

if (TEA_DIR) {
fs.appendFileSync(GITHUB_OUTPUT, `srcroot=${TEA_DIR}\n`, {encoding: 'utf8'})
fs.appendFileSync(GITHUB_ENV, `TEA_DIR=${TEA_DIR}\n`, {encoding: 'utf8'})
core.setOutput('srcroot', TEA_DIR)
core.exportVariable('TEA_DIR', TEA_DIR)
}

if (os.platform() != 'darwin') {
const sh = path.join(path.dirname(__filename), "install-pre-reqs.sh")
if (process.getuid() == 0) {
execSync(sh)
await exec(sh)
} else {
execSync(`sudo ${sh}`)
await exec('sudo', [sh])
}
}

//TODO deprecated exe/md
//NOTE BUT LEAVE BECAUSE WE ONCE SUPPORTED THIS
const target = process.env['INPUT_TARGET']
const target = core.getInput('target')
if (target) {
execSync(`${teafile} ${target}`, {stdio: "inherit", env})
await exec(teafile, [target], { env: { ...process.env, TEA_DIR, TEA_PREFIX } })
}

fs.appendFileSync(GITHUB_ENV, `TEA_PREFIX=${PREFIX}\n`, {encoding: 'utf8'})
fs.appendFileSync(GITHUB_OUTPUT, `prefix=${PREFIX}\n`, {encoding: 'utf8'})
core.exportVariable('TEA_PREFIX', TEA_PREFIX)
core.setOutput('prefix', TEA_PREFIX)

process.stderr.write(`installed ${PREFIX}/tea.xyz/v${v}\n`)
core.info(`installed ${tea.path}`)
}

go().catch(err => {
console.error(err)
process.exitCode = 1
})
go().catch(core.setFailed)
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ outputs:
description: Your project’s version.
runs:
using: node16
main: action.js
main: dist/out/index.js
Binary file added dist/build/2.3.20/koffi_darwin_arm64/koffi.node
Binary file not shown.
Binary file added dist/build/2.3.20/koffi_darwin_x64/koffi.node
Binary file not shown.
Binary file added dist/build/2.3.20/koffi_linux_arm64/koffi.node
Binary file not shown.
Binary file added dist/build/2.3.20/koffi_linux_x64/koffi.node
Binary file not shown.
37 changes: 37 additions & 0 deletions dist/out/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/out/install-pre-reqs.sh
Loading

0 comments on commit c9d315c

Please sign in to comment.