pantry/scripts/build.ts

92 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-08-01 22:43:40 +03:00
#!/usr/bin/env -S tea -E
/*---
args:
- deno
- run
- --allow-net
- --allow-run
2022-09-08 23:40:35 +03:00
- --allow-read
2022-09-09 16:37:26 +03:00
- --allow-write={{tea.prefix}}
2022-08-01 22:43:40 +03:00
- --allow-env
- --import-map={{ srcroot }}/import-map.json
---*/
import useSourceUnarchiver from "hooks/useSourceUnarchiver.ts"
import useCellar from "hooks/useCellar.ts"
import usePantry from "hooks/usePantry.ts"
import useCache from "hooks/useCache.ts"
import { lvl1 as link } from "prefab/link.ts"
import build from "prefab/build.ts"
2022-09-01 20:49:14 +03:00
import { Package, parsePackageRequirement, semver } from "types"
2022-08-17 15:22:22 +03:00
import useFlags from "hooks/useFlags.ts"
2022-08-26 17:17:20 +03:00
import usePlatform from "hooks/usePlatform.ts"
2022-09-02 17:51:31 +03:00
import hydrate from "prefab/hydrate.ts"
2022-08-10 21:58:22 +03:00
2022-08-17 15:22:22 +03:00
useFlags()
2022-08-01 22:43:40 +03:00
const pantry = usePantry()
const cellar = useCellar()
2022-09-02 17:51:31 +03:00
const dry = Deno.args.map(parsePackageRequirement)
2022-09-08 23:40:35 +03:00
const gha = !!Deno.env.get("GITHUB_ACTIONS")
2022-08-01 22:43:40 +03:00
const rv: Package[] = []
2022-09-01 20:49:14 +03:00
for (const pkgrq of dry) {
const versions = await pantry.getVersions(pkgrq)
const version = semver.maxSatisfying(versions, pkgrq.constraint)
2022-08-01 22:43:40 +03:00
if (!version) throw "no-version-found"
2022-09-01 20:49:14 +03:00
const pkg = { project: pkgrq.project, version }
2022-08-01 22:43:40 +03:00
2022-09-08 23:40:35 +03:00
const installation = await cellar.isInstalled(pkg)
if (installation) {
console.log({ cleaning: installation.path })
for await (const [path, {name}] of installation.path.ls()) {
if (name == 'src') continue
path.rm({ recursive: true })
}
}
if (gha) {
console.log("::group::", `${pkg.project}@${pkg.version}`)
} else {
console.log({ building: pkgrq.project })
}
2022-08-01 22:43:40 +03:00
// Get the source
const prebuild = async () => {
2022-09-01 20:49:14 +03:00
const dstdir = cellar.mkpath(pkg).join("src")
2022-08-01 22:43:40 +03:00
const { url, stripComponents } = await pantry.getDistributable(pkg)
const { download } = useCache()
const zip = await download({ pkg, url, type: 'src' })
await useSourceUnarchiver().unarchive({
dstdir,
zipfile: zip,
stripComponents
})
}
2022-09-02 17:51:31 +03:00
//TODO this was already calculated in `sort` and should not be recalculated
2022-08-01 22:43:40 +03:00
const deps = await pantry.getDeps(pkg)
2022-09-02 17:51:31 +03:00
const wet = await hydrate(deps.runtime, pkg => pantry.getDeps(pkg).then(x => x.runtime))
deps.runtime.push(...wet.pkgs)
2022-08-01 22:43:40 +03:00
2022-08-26 17:17:20 +03:00
const env = usePlatform().platform == 'darwin'
? {MACOSX_DEPLOYMENT_TARGET: ['11.0']}
: undefined
2022-08-01 22:43:40 +03:00
// Build and link
2022-08-26 17:17:20 +03:00
const path = await build({ pkg, deps, prebuild, env })
2022-08-01 22:43:40 +03:00
await link({ path, pkg })
2022-09-01 20:49:14 +03:00
rv.push(pkg)
2022-09-08 23:40:35 +03:00
if (gha) {
console.log("::endgroup::")
}
2022-08-01 22:43:40 +03:00
}
2022-09-01 20:49:14 +03:00
const built_pkgs = rv.map(({ project, version }) => `${project}@${version}`).join(" ")
const txt = `::set-output name=pkgs::${built_pkgs}\n`
await Deno.stdout.write(new TextEncoder().encode(txt))