2022-08-01 22:43:40 +03:00
|
|
|
|
#!/usr/bin/env -S tea -E
|
|
|
|
|
|
|
|
|
|
/*---
|
|
|
|
|
args:
|
|
|
|
|
- deno
|
|
|
|
|
- run
|
|
|
|
|
- --allow-net
|
2022-09-08 23:40:35 +03:00
|
|
|
|
- --allow-read
|
2022-08-17 15:22:22 +03:00
|
|
|
|
- --allow-env
|
2022-08-01 22:43:40 +03:00
|
|
|
|
- --import-map={{ srcroot }}/import-map.json
|
|
|
|
|
---*/
|
|
|
|
|
|
|
|
|
|
import { S3 } from "s3"
|
2022-09-27 23:42:26 +03:00
|
|
|
|
import { pkg as pkgutils } from "utils"
|
2022-10-17 20:45:32 +03:00
|
|
|
|
import { useFlags, useOffLicense, useCache } from "hooks"
|
2022-09-27 22:11:35 +03:00
|
|
|
|
import { Package, PackageRequirement } from "types"
|
2022-09-20 14:53:40 +03:00
|
|
|
|
import SemVer, * as semver from "semver"
|
2022-09-14 22:48:36 +03:00
|
|
|
|
import { dirname, basename } from "deno/path/mod.ts"
|
2022-09-27 22:11:35 +03:00
|
|
|
|
import Path from "path"
|
2022-09-28 18:14:46 +03:00
|
|
|
|
import { set_output } from "./utils/gha.ts"
|
2022-10-03 20:22:25 +03:00
|
|
|
|
import { sha256 } from "./bottle.ts"
|
2022-08-17 15:22:22 +03:00
|
|
|
|
|
|
|
|
|
useFlags()
|
2022-08-01 22:43:40 +03:00
|
|
|
|
|
|
|
|
|
if (Deno.args.length === 0) throw new Error("no args supplied")
|
|
|
|
|
|
|
|
|
|
const s3 = new S3({
|
|
|
|
|
accessKeyID: Deno.env.get("AWS_ACCESS_KEY_ID")!,
|
|
|
|
|
secretKey: Deno.env.get("AWS_SECRET_ACCESS_KEY")!,
|
|
|
|
|
region: "us-east-1",
|
|
|
|
|
})
|
|
|
|
|
|
2022-09-13 16:18:47 +03:00
|
|
|
|
const bucket = s3.getBucket(Deno.env.get("AWS_S3_BUCKET")!)
|
2022-08-26 02:59:37 +03:00
|
|
|
|
const encode = (() => { const e = new TextEncoder(); return e.encode.bind(e) })()
|
2022-09-28 18:14:46 +03:00
|
|
|
|
const cache = useCache()
|
2022-08-26 02:59:37 +03:00
|
|
|
|
|
2022-09-27 22:11:35 +03:00
|
|
|
|
const pkgs = args_get("pkgs").map(pkgutils.parse).map(assert_pkg)
|
2022-10-03 20:22:25 +03:00
|
|
|
|
const srcs = args_get("srcs")
|
2022-09-14 22:48:36 +03:00
|
|
|
|
const bottles = args_get("bottles")
|
|
|
|
|
const checksums = args_get("checksums")
|
2022-08-26 02:59:37 +03:00
|
|
|
|
|
2022-09-27 22:11:35 +03:00
|
|
|
|
|
2022-09-14 22:48:36 +03:00
|
|
|
|
function args_get(key: string): string[] {
|
|
|
|
|
const it = Deno.args[Symbol.iterator]()
|
|
|
|
|
while (true) {
|
|
|
|
|
const { value, done } = it.next()
|
|
|
|
|
if (done) throw new Error()
|
|
|
|
|
if (value === `--${key}`) break
|
|
|
|
|
}
|
|
|
|
|
const rv: string[] = []
|
|
|
|
|
while (true) {
|
|
|
|
|
const { value, done } = it.next()
|
|
|
|
|
if (done) return rv
|
|
|
|
|
if (value.startsWith('--')) return rv
|
|
|
|
|
rv.push(value)
|
2022-09-08 23:40:35 +03:00
|
|
|
|
}
|
2022-08-26 02:59:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 18:14:46 +03:00
|
|
|
|
const rv: string[] = []
|
|
|
|
|
const put = async (key: string, body: string | Path | Uint8Array) => {
|
|
|
|
|
console.log({ uploading: body, to: key })
|
|
|
|
|
rv.push(`/${key}`)
|
|
|
|
|
if (body instanceof Path) {
|
|
|
|
|
body = await Deno.readFile(body.string)
|
|
|
|
|
} else if (typeof body === "string") {
|
|
|
|
|
body = encode(body)
|
|
|
|
|
}
|
|
|
|
|
return bucket.putObject(key, body)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-14 22:48:36 +03:00
|
|
|
|
for (const [index, pkg] of pkgs.entries()) {
|
2022-09-27 22:11:35 +03:00
|
|
|
|
const bottle = new Path(bottles[index])
|
2022-09-14 22:48:36 +03:00
|
|
|
|
const checksum = checksums[index]
|
2022-09-28 18:14:46 +03:00
|
|
|
|
const stowed = cache.decode(bottle)!
|
|
|
|
|
const key = useOffLicense('s3').key(stowed)
|
2022-09-27 22:11:35 +03:00
|
|
|
|
const versions = await get_versions(key, pkg)
|
2022-08-01 22:43:40 +03:00
|
|
|
|
|
2022-09-28 18:14:46 +03:00
|
|
|
|
//FIXME stream the bottle (at least) to S3
|
|
|
|
|
await put(key, bottle)
|
|
|
|
|
await put(`${key}.sha256sum`, `${checksum} ${basename(key)}`)
|
|
|
|
|
await put(`${dirname(key)}/versions.txt`, versions.join("\n"))
|
2022-10-03 20:22:25 +03:00
|
|
|
|
|
2022-10-17 20:45:32 +03:00
|
|
|
|
// mirror the sources
|
|
|
|
|
if (srcs[index] != "~") {
|
|
|
|
|
const src = new Path(srcs[index])
|
|
|
|
|
const srcKey = useOffLicense('s3').key({
|
|
|
|
|
pkg: stowed.pkg,
|
|
|
|
|
type: "src",
|
|
|
|
|
extname: src.extname()
|
|
|
|
|
})
|
|
|
|
|
const srcChecksum = await sha256(src)
|
|
|
|
|
const srcVersions = await get_versions(srcKey, pkg)
|
|
|
|
|
await put(srcKey, src)
|
|
|
|
|
await put(`${srcKey}.sha256sum`, `${srcChecksum} ${basename(srcKey)}`)
|
|
|
|
|
await put(`${dirname(srcKey)}/versions.txt`, srcVersions.join("\n"))
|
|
|
|
|
}
|
2022-08-01 22:43:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 18:14:46 +03:00
|
|
|
|
await set_output('cf-invalidation-paths', rv)
|
|
|
|
|
|
2022-08-26 02:59:37 +03:00
|
|
|
|
//end
|
|
|
|
|
|
2022-09-27 22:11:35 +03:00
|
|
|
|
async function get_versions(key: string, pkg: Package): Promise<SemVer[]> {
|
|
|
|
|
const prefix = dirname(key)
|
2022-08-01 22:43:40 +03:00
|
|
|
|
const rsp = await bucket.listObjects({ prefix })
|
|
|
|
|
|
|
|
|
|
//FIXME? API isn’t clear if these nulls indicate failure or not
|
|
|
|
|
//NOTE if this is a new package then some empty results is expected
|
2022-08-02 05:50:33 +03:00
|
|
|
|
const got = rsp
|
2022-08-01 22:43:40 +03:00
|
|
|
|
?.contents
|
2022-09-21 10:46:24 +03:00
|
|
|
|
?.compact(x => x.key)
|
2022-08-04 18:28:23 +03:00
|
|
|
|
.map(x => basename(x))
|
2022-09-22 22:37:54 +03:00
|
|
|
|
.filter(x => x.match(/v.*\.tar\.gz$/))
|
|
|
|
|
.map(x => x.replace(/v(.*)\.tar\.gz/, "$1"))
|
2022-08-02 05:50:33 +03:00
|
|
|
|
?? []
|
|
|
|
|
|
|
|
|
|
// have to add pkg.version as put and get are not atomic
|
2022-09-29 23:53:44 +03:00
|
|
|
|
return [...new Set([...got, pkg.version.toString()])]
|
|
|
|
|
.compact(semver.parse)
|
|
|
|
|
.sort(semver.compare)
|
2022-09-27 22:11:35 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function assert_pkg(pkg: Package | PackageRequirement) {
|
|
|
|
|
if ("version" in pkg) {
|
|
|
|
|
return pkg
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
project: pkg.project,
|
|
|
|
|
version: new SemVer(pkg.constraint)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-29 14:45:36 +03:00
|
|
|
|
}
|