2022-08-01 22:43:40 +03:00
|
|
|
#!/usr/bin/env -S tea -E
|
|
|
|
|
|
|
|
/* ---
|
|
|
|
args:
|
|
|
|
- deno
|
|
|
|
- run
|
|
|
|
- --allow-net
|
|
|
|
- --allow-run
|
2022-08-17 15:22:22 +03:00
|
|
|
- --allow-env
|
2022-09-08 23:40:35 +03:00
|
|
|
- --allow-read
|
|
|
|
- --allow-write
|
2022-08-01 22:43:40 +03:00
|
|
|
- --import-map={{ srcroot }}/import-map.json
|
|
|
|
--- */
|
|
|
|
|
2022-09-20 14:53:40 +03:00
|
|
|
import { Installation } from "types"
|
2022-09-20 18:29:21 +03:00
|
|
|
import { useCellar, useCache, usePrefix, useFlags } from "hooks"
|
2022-09-21 10:46:24 +03:00
|
|
|
import { run, pkg as pkgutils } from "utils"
|
2022-08-26 02:59:37 +03:00
|
|
|
import { crypto } from "deno/crypto/mod.ts"
|
2022-09-20 14:53:40 +03:00
|
|
|
import { encode } from "deno/encoding/hex.ts"
|
|
|
|
import Path from "path"
|
2022-08-17 15:22:22 +03:00
|
|
|
|
2022-08-01 22:43:40 +03:00
|
|
|
const cellar = useCellar()
|
|
|
|
|
|
|
|
|
2022-09-08 23:40:35 +03:00
|
|
|
//-------------------------------------------------------------------------- main
|
2022-08-01 22:43:40 +03:00
|
|
|
|
2022-09-08 23:40:35 +03:00
|
|
|
if (import.meta.main) {
|
|
|
|
useFlags()
|
2022-08-01 22:43:40 +03:00
|
|
|
|
2022-09-08 23:40:35 +03:00
|
|
|
const bottles: Path[] = []
|
2022-09-14 22:48:36 +03:00
|
|
|
const checksums: Path[] = []
|
|
|
|
const artifacts: Path[] = []
|
2022-09-21 10:46:24 +03:00
|
|
|
for (const pkg of Deno.args.map(pkgutils.parse)) {
|
2022-09-08 23:40:35 +03:00
|
|
|
console.log({ bottling: { pkg } })
|
2022-08-01 22:43:40 +03:00
|
|
|
|
2022-09-08 23:40:35 +03:00
|
|
|
const installation = await cellar.resolve(pkg)
|
|
|
|
const path = await bottle(installation)
|
|
|
|
const checksum = await sha256(path)
|
2022-08-01 22:43:40 +03:00
|
|
|
|
2022-09-08 23:40:35 +03:00
|
|
|
console.log({ bottled: { path } })
|
2022-08-26 02:59:37 +03:00
|
|
|
|
2022-09-08 23:40:35 +03:00
|
|
|
bottles.push(path)
|
2022-09-14 22:48:36 +03:00
|
|
|
checksums.push(checksum)
|
2022-09-08 23:40:35 +03:00
|
|
|
}
|
2022-08-26 02:59:37 +03:00
|
|
|
|
2022-09-08 23:40:35 +03:00
|
|
|
if (bottles.length === 0) throw new Error("no input provided")
|
|
|
|
|
|
|
|
const encode = (() => { const e = new TextEncoder(); return e.encode.bind(e) })()
|
|
|
|
|
2022-09-14 22:48:36 +03:00
|
|
|
const bottles_out = bottles.map(x => x.string).join(' ')
|
|
|
|
await Deno.stdout.write(encode(`::set-output name=bottles::${bottles_out}\n`))
|
2022-09-08 23:40:35 +03:00
|
|
|
|
2022-09-14 22:48:36 +03:00
|
|
|
const checksums_out = checksums.map(x => x.string).join(' ')
|
|
|
|
await Deno.stdout.write(encode(`::set-output name=checksums::${checksums_out}\n`))
|
|
|
|
|
|
|
|
// newline separated for the upload-artifact action
|
|
|
|
artifacts.push(...bottles, ...checksums)
|
|
|
|
await Deno.stdout.write(encode(`::set-output name=artifacts::${artifacts.join('%0A')}\n`))
|
2022-09-08 23:40:35 +03:00
|
|
|
}
|
2022-08-01 22:43:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------- funcs
|
2022-09-08 23:40:35 +03:00
|
|
|
export async function bottle({ path: kegdir, pkg }: Installation): Promise<Path> {
|
2022-08-01 22:43:40 +03:00
|
|
|
const tarball = useCache().bottle(pkg)
|
2022-09-21 09:35:03 +03:00
|
|
|
const cwd = usePrefix()
|
|
|
|
const cmd = ["tar", "zcf", tarball, kegdir.relative({ to: cwd })]
|
|
|
|
await run({ cmd, cwd })
|
2022-08-01 22:43:40 +03:00
|
|
|
return tarball
|
|
|
|
}
|
|
|
|
|
2022-08-26 02:59:37 +03:00
|
|
|
async function sha256(file: Path): Promise<Path> {
|
2022-09-20 14:53:40 +03:00
|
|
|
const sha = await Deno.open(file.string, { read: true })
|
|
|
|
.then(file => crypto.subtle.digest("SHA-256", file.readable))
|
|
|
|
.then(buf => new TextDecoder().decode(encode(new Uint8Array(buf))))
|
|
|
|
const text = `${sha} ${file.basename()}`
|
2022-09-21 09:35:03 +03:00
|
|
|
return new Path(`${file}.sha256sum`).write({ text, force: true })
|
2022-09-08 23:40:35 +03:00
|
|
|
}
|