Skip to content

Commit

Permalink
Don't export from scripts, since importing runs the script logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhheider committed Jul 20, 2022
1 parent 97c3f2f commit bb2f8e1
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 102 deletions.
97 changes: 97 additions & 0 deletions scripts/_shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import useCache from "hooks/useCache.ts";
import useCellar from "hooks/useCellar.ts";
import { Package, PackageRequirement, Path } from "types";
import { run } from "utils";
import { join } from "deno/path/mod.ts";
import usePantry from "hooks/usePantry.ts";
import useSourceUnarchiver from "hooks/useSourceUnarchiver.ts";

export async function bottle(req: PackageRequirement) {
const cellar = useCellar()
const { path: kegdir, pkg } = await cellar.resolve(req)
const tarball = useCache().bottle(pkg)

if (tarball.exists()) {
console.verbose({ alreadyExists: tarball.string })
return
}

const filesListName = "files.txt"

const files = await walk(kegdir, path => {
/// HACK: `go` requires including the `src` dir
const isGo = kegdir.string.match(/\/go.dev\//)
switch (path.relative({ to: kegdir })) {
case 'src':
return isGo ? 'accumulate' : 'skip'
case 'build.sh':
case filesListName:
return 'skip'
default:
return 'accumulate'
}
})
const relativePaths = files.map(x => x.relative({ to: cellar.prefix }))

const filelist = kegdir.join(filesListName).write({ text: relativePaths.join("\n"), force: true })

await run({
cmd: [
"tar", "zcf", tarball, "--files-from", filelist
],
cwd: cellar.prefix
})
}

// using our own because of: https://github.com/denoland/deno_std/issues/1359
// but frankly this also is more suitable for our needs here
type Continuation = 'accumulate' | 'skip'

export async function walk(root: Path, body: (entry: Path) => Continuation): Promise<Path[]> {
const rv: Path[] = []
const stack: Path[] = [root]

do {
root = stack.pop()!
for await (const [path, entry] of root.ls()) {
switch (body(path)) {
case 'accumulate':
if (entry.isDirectory) {
stack.push(path)
} else {
rv.push(path)
}
break
case 'skip':
continue
}
}
} while (stack.length > 0)

return rv
}

export async function* lsPantry(path = ""): AsyncGenerator<string, void, void> {
for await (const p of Deno.readDir(`/opt/tea.xyz/var/pantry/projects/${path}`)) {
if (p.name === "package.yml") {
yield path
} else if (p.isDirectory) {
for await (const p1 of lsPantry(join(path, p.name))) {
if (p1) { yield p1 }
}
}
}
}

export async function prepare(pkg: Package) {
const pantry = usePantry()
const dstdir = useCellar().mkpath(pkg).join("src")
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
})
}
15 changes: 1 addition & 14 deletions scripts/bottle-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ args:
---*/

import usePantry from "hooks/usePantry.ts"
import { bottle } from "./bottle.ts";
import { bottle, lsPantry } from "./_shared.ts";
import { semver } from "types";
import useCellar from "hooks/useCellar.ts";
import { join } from "deno/path/mod.ts";

const pantry = usePantry();

Expand All @@ -34,15 +33,3 @@ for await (const project of lsPantry()) {
}
}
}

export async function* lsPantry(path = ""): AsyncGenerator<string, void, void> {
for await (const p of Deno.readDir(`/opt/tea.xyz/var/pantry/projects/${path}`)) {
if (p.name === "package.yml") {
yield path
} else if (p.isDirectory) {
for await (const p1 of lsPantry(join(path, p.name))) {
if (p1) { yield p1 }
}
}
}
}
73 changes: 2 additions & 71 deletions scripts/bottle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,78 +11,9 @@ args:
- --import-map={{ srcroot }}/import-map.json
--- */

import { PackageRequirement, parsePackageRequirement, Path } from "types"
import useCellar from "hooks/useCellar.ts"
import useCache from "hooks/useCache.ts"
import { run } from "utils"

const cellar = useCellar()
import { parsePackageRequirement } from "types"
import { bottle } from "./_shared.ts"

for (const req of Deno.args.map(parsePackageRequirement)) {
await bottle(req)
}

export async function bottle(req: PackageRequirement) {
const { path: kegdir, pkg } = await cellar.resolve(req)
const tarball = useCache().bottle(pkg)

if (tarball.exists()) {
console.verbose({ alreadyExists: tarball.string })
return
}

const filesListName = "files.txt"

const files = await walk(kegdir, path => {
/// HACK: `go` requires including the `src` dir
const isGo = kegdir.string.match(/\/go.dev\//)
switch (path.relative({ to: kegdir })) {
case 'src':
return isGo ? 'accumulate' : 'skip'
case 'build.sh':
case filesListName:
return 'skip'
default:
return 'accumulate'
}
})
const relativePaths = files.map(x => x.relative({ to: cellar.prefix }))

const filelist = kegdir.join(filesListName).write({ text: relativePaths.join("\n"), force: true })

await run({
cmd: [
"tar", "zcf", tarball, "--files-from", filelist
],
cwd: cellar.prefix
})
}


// using our own because of: https://github.com/denoland/deno_std/issues/1359
// but frankly this also is more suitable for our needs here
type Continuation = 'accumulate' | 'skip'

export async function walk(root: Path, body: (entry: Path) => Continuation): Promise<Path[]> {
const rv: Path[] = []
const stack: Path[] = [root]

do {
root = stack.pop()!
for await (const [path, entry] of root.ls()) {
switch (body(path)) {
case 'accumulate':
if (entry.isDirectory) {
stack.push(path)
} else {
rv.push(path)
}
break
case 'skip':
continue
}
}
} while (stack.length > 0)

return rv
}
2 changes: 1 addition & 1 deletion scripts/build-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import useCache from "hooks/useCache.ts"
import useCellar from "hooks/useCellar.ts"
import useSourceUnarchiver from "hooks/useSourceUnarchiver.ts"
import { S3 } from "s3";
import { lsPantry } from "./bottle-all.ts";
import { lsPantry } from "./_shared.ts";

const pantry = usePantry();

Expand Down
18 changes: 2 additions & 16 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ args:
import build from "prefab/build.ts"
import { lvl1 as link } from "prefab/link.ts"
import usePantry from "hooks/usePantry.ts"
import useCache from "hooks/useCache.ts"
import useCellar from "hooks/useCellar.ts"
import useSourceUnarchiver from "hooks/useSourceUnarchiver.ts"
import { parsePackageRequirement, semver, Package } from "types"
import { parsePackageRequirement, semver } from "types"
import { Command } from "cliffy/command/mod.ts"
import { prepare } from "./_shared.ts";

const { args, options: { skipExtract } } = await new Command()
.name("tea-build")
Expand Down Expand Up @@ -51,15 +49,3 @@ for (const req of args[0].map(parsePackageRequirement)) {
const path = await build({ pkg, deps, prebuild })
await link({ path, pkg })
}

export async function prepare(pkg: Package) {
const dstdir = useCellar().mkpath(pkg).join("src")
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
})
}

0 comments on commit bb2f8e1

Please sign in to comment.