Skip to content

Commit

Permalink
infra for pantry package tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Jul 28, 2022
1 parent d8021b3 commit 9dc3ec1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
34 changes: 27 additions & 7 deletions src/hooks/usePantry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ interface Response {
/// returns sorted versions
getVersions(rq: PackageRequirement | Package): Promise<SemVer[]>
getDeps(pkg: Package | PackageRequirement): Promise<{ runtime: PackageRequirement[], build: PackageRequirement[] }>
getBuildScript(pkg: Package): Promise<string>
getScript(pkg: Package, key: 'build' | 'test'): Promise<string>
update(): Promise<void>
getProvides(rq: PackageRequirement | Package): Promise<string[]>

//TODO take `T` and then type check it
getYAML(rq: PackageRequirement | Package): Promise<[PlainObject, Path]>

prefix(rq: PackageRequirement | Package): Path
}

interface Entry {
Expand Down Expand Up @@ -79,6 +84,12 @@ export default function usePantry(): Response {
}
}

const getYAML = async (pkg: Package | PackageRequirement): Promise<[PlainObject, Path]> => {
const foo = entry(pkg)
const yml = await foo.yml()
return [yml, foo.dir.join("package.yml")]
}

const getDeps = async (pkg: Package | PackageRequirement) => {
const yml = await entry(pkg).yml()
return {
Expand Down Expand Up @@ -114,12 +125,15 @@ export default function usePantry(): Response {
return { url, stripComponents }
}

const getBuildScript = async (pkg: Package) => {
const getScript = async (pkg: Package, key: 'build' | 'test') => {
const yml = await entry(pkg).yml()
let raw = validateString(validatePlainObject(yml.build).script)
const obj = validatePlainObject(yml[key])

const wd = yml.build["working-directory"]
let raw = validateString(obj.script)

let wd = obj["working-directory"]
if (wd) {
wd = remapTokens(wd, pkg)
raw = undent`
mkdir -p ${wd}
cd ${wd}
Expand All @@ -128,7 +142,7 @@ export default function usePantry(): Response {
`
}

const env = yml.build.env
const env = obj.env
if (isPlainObject(env)) {
const expanded_env = Object.entries(env).map(([key,value]) => {
if (isArray(value)) {
Expand Down Expand Up @@ -179,7 +193,8 @@ export default function usePantry(): Response {
{ from: "hw.target", to: platform.target },
{ from: "hw.platform", to: platform.platform },
{ from: "prefix", to: prefix.string },
{ from: "hw.concurrency", to: navigator.hardwareConcurrency.toString() }
{ from: "hw.concurrency", to: navigator.hardwareConcurrency.toString() },
{ from: "pkg.pantry-prefix", to: getPrefix(pkg).string }
].reduce((acc, map) => acc.replace(new RegExp(`\\$?{{\\s*${map.from}\\s*}}`, "g"), map.to), input)
}

Expand All @@ -204,7 +219,12 @@ export default function usePantry(): Response {
})
}

return { getVersions, getDeps, getDistributable, getBuildScript, update, getProvides }
const getPrefix = (pkg: Package | PackageRequirement) => prefix.join(pkg.project)

return { getVersions, getDeps, getDistributable, getScript, update, getProvides,
getYAML,
prefix: getPrefix
}
}


Expand Down
9 changes: 9 additions & 0 deletions src/hooks/useShellEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,12 @@ function suffixes(key: string) {
throw new Error("unhandled")
}
}

export function expand(env: Record<string, string[]>) {
let rv = ''
for (let [key, value] of Object.entries(env)) {
if (key == 'PATH') value = value.concat("/usr/bin:/bin:/usr/sbin:/sbin") //FIXME
rv += `export ${key}='${value.join(":")}'\n`
}
return rv
}
13 changes: 2 additions & 11 deletions src/prefab/build.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Path, Package, PackageRequirement, semver } from "types"
import usePantry from "hooks/usePantry.ts"
import useCellar from "hooks/useCellar.ts"
import useShellEnv from "hooks/useShellEnv.ts"
import useShellEnv, { expand } from "hooks/useShellEnv.ts"
import { run, undent } from "utils"
import usePlatform from "hooks/usePlatform.ts"
import hydrate from "prefab/hydrate.ts"
Expand All @@ -22,7 +22,7 @@ export default async function build({ pkg, deps, prebuild }: Options): Promise<P
const src = dst.join("src")
const runtime_deps = await filterAndHydrate(deps.runtime)
const env = await useShellEnv([...deps.build, ...runtime_deps])
const sh = await pantry.getBuildScript(pkg)
const sh = await pantry.getScript(pkg, 'build')
const { platform } = usePlatform()

if (env.pending.length) {
Expand Down Expand Up @@ -78,15 +78,6 @@ async function filterAndHydrate(pkgs: PackageRequirement[]): Promise<PackageRequ
}
}

function expand(env: Record<string, string[]>) {
let rv = ''
for (let [key, value] of Object.entries(env)) {
if (key == 'PATH') value = value.concat("/usr/bin:/bin:/usr/sbin:/sbin") //FIXME
rv += `export ${key}='${value.join(":")}'\n`
}
return rv
}

async function* exefiles(prefix: Path) {
for (const basename of ["bin", "lib"]) { //TODO the rest
const d = prefix.join(basename).isDirectory()
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ export function parsePackageRequirement(input: string): PackageRequirement {
}
}

export function parsePackage(input: string): Package {
const splat = input.split('@') //FIXME we do specs with eg. foo^1
if (splat.length == 2) {
return {
project: splat[0],
version: new SemVer(splat[1])
}
} else {
throw "invalid-pkgspec"
}
}

/////////////////////////////////////////////////////////////////////// semver
import SemVer, * as semver from "semver"

Expand Down

0 comments on commit 9dc3ec1

Please sign in to comment.