Skip to content

Commit

Permalink
Update to libtea v0.26
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Mar 28, 2023
1 parent 741ca31 commit ca516fa
Show file tree
Hide file tree
Showing 25 changed files with 189 additions and 39 deletions.
9 changes: 6 additions & 3 deletions actions/bottle/bottle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ args:
--- */

import { Installation } from "types"
import { useCellar, usePrefix, useFlags, useCache } from "hooks"
import { backticks, panic, run } from "utils"
import { useCellar, usePrefix, useCache } from "hooks"
import { panic } from "utils"
import run from "hooks/useRun.ts"
import { crypto } from "deno/crypto/mod.ts"
import { encode } from "deno/encoding/hex.ts"
import { encode as base64Encode } from "deno/encoding/base64.ts"
import { set_output } from "../utils/gha.ts"
import * as ARGV from "../utils/args.ts"
import tea_init from "../../lib/init().ts"
import { backticks } from "../../lib/utils.ts"
import Path from "path"

const cellar = useCellar()
Expand All @@ -32,7 +35,7 @@ const cellar = useCellar()
//-------------------------------------------------------------------------- main

if (import.meta.main) {
useFlags()
tea_init()

const compression = Deno.env.get("COMPRESSION") == 'xz' ? 'xz' : 'gz'
const gpgKey = Deno.env.get("GPG_KEY_ID") ?? panic("missing GPG_KEY_ID")
Expand Down
4 changes: 2 additions & 2 deletions actions/setup-brewkit/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ runs:
id: tea
with:
prefix: ${{ inputs.prefix }}
+: tea.xyz/brewkit^0.7.1
# prevent pantry from reassigning TEA_PREFIX etc.
+: tea.xyz/brewkit
# prevent pantry from reassigning TEA_PANTRY_PATH
srcroot: null

- uses: teaxyz/brewkit/actions/cache@v0
Expand Down
5 changes: 3 additions & 2 deletions actions/upload/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ args:

import { S3, S3Bucket } from "s3"
import { pkg as pkgutils } from "utils"
import { useFlags, useOffLicense, usePrefix } from "hooks"
import { useOffLicense, usePrefix } from "hooks"
import { Package, PackageRequirement } from "types"
import SemVer, * as semver from "semver"
import { basename, dirname } from "deno/path/mod.ts"
Expand Down Expand Up @@ -84,7 +84,8 @@ async function put(key: string, body: string | Path | Uint8Array, bucket: S3Buck
}

//------------------------------------------------------------------------- main
useFlags()
import tea_init from "../../lib/init().ts"
tea_init()

if (Deno.args.length === 0) throw new Error("no args supplied")

Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"deno.land": "^1.30"
}
},
"importMap": "https://raw.githubusercontent.com/teaxyz/cli/v0.25/import-map.json"
"importMap": "https://raw.githubusercontent.com/teaxyz/cli/v0.26.0/import-map.json"
}
69 changes: 69 additions & 0 deletions lib/init().ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { applyConfig, Config, Env } from "hooks/useConfig.ts"
import { isNumber } from "is_what"
import { Verbosity } from "types"
import { flatmap } from "utils"
import Path from "path"

export default function() {
applyConfig(createConfig())
}

export function createConfig(): Config {
const env = collectEnv()
const isCI = !!env.CI
const execPath = new Path(Deno.execPath())
const loggerGlobalPrefix = (!Deno.isatty(Deno.stdout.rid) || isCI) ? "tea:" : undefined
const teaPrefix = findTeaPrefix(env.TEA_PREFIX)
const verbosity = getVerbosity(env)

return {
isCI,
execPath,
loggerGlobalPrefix,
teaPrefix,
verbosity,
dryrun: false,
keepGoing: false,
verbose: verbosity >= Verbosity.loud,
debug: verbosity >= Verbosity.debug,
silent: verbosity <= Verbosity.quiet,
env,
}
}

export function collectEnv(): Env {
return {
CI: Deno.env.get("CI"),
CLICOLOR: Deno.env.get("CLICOLOR"),
CLICOLOR_FORCE: Deno.env.get("CLICOLOR_FORCE"),
DEBUG: Deno.env.get("DEBUG"),
GITHUB_ACTIONS: Deno.env.get("GITHUB_ACTIONS"),
GITHUB_TOKEN: Deno.env.get("GITHUB_TOKEN"),
NO_COLOR: Deno.env.get("NO_COLOR"),
PATH: Deno.env.get("PATH"),
RUNNER_DEBUG: Deno.env.get("RUNNER_DEBUG"),
SHELL: Deno.env.get("SHELL"),
SRCROOT: Deno.env.get("SRCROOT"),
TEA_DIR: Deno.env.get("TEA_DIR"),
TEA_FILES: Deno.env.get("TEA_FILES"),
TEA_FORK_BOMB_PROTECTOR: Deno.env.get("TEA_FORK_BOMB_PROTECTOR"),
TEA_PANTRY_PATH: Deno.env.get("TEA_PANTRY_PATH"),
TEA_PKGS: Deno.env.get("TEAK_PKGS"),
TEA_PREFIX: Deno.env.get("TEA_PREFIX"),
TEA_REWIND: Deno.env.get("TEA_REWIND"),
VERBOSE: Deno.env.get("VERBOSE"),
VERSION: Deno.env.get("VERSION")
}
}

export const findTeaPrefix = (envVar?: string) => {
return flatmap(envVar, x => new Path(x)) ?? Path.home().join(".tea")
}

function getVerbosity(env: Env): Verbosity {
const { DEBUG, GITHUB_ACTIONS, RUNNER_DEBUG, VERBOSE } = env
if (DEBUG == '1') return Verbosity.debug
if (GITHUB_ACTIONS == 'true' && RUNNER_DEBUG == '1') return Verbosity.debug
const verbosity = flatmap(VERBOSE, parseInt)
return isNumber(verbosity) ? verbosity : Verbosity.normal
}
21 changes: 19 additions & 2 deletions lib/useGitHubAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GET2, undent, validate_arr, validate_str } from "utils"
import { isArray } from "is_what"
import { undent, validate_arr, validate_str } from "utils"
import { isArray, isString } from "is_what"

//TODO pagination

Expand All @@ -18,6 +18,23 @@ export default function useGitHubAPI() {
return { getVersions }
}


async function GET2<T>(url: URL | string, headers?: Headers): Promise<[T, Response]> {
if (isString(url)) url = new URL(url)
if (url.host == "api.github.com") {
const token = Deno.env.get("GITHUB_TOKEN")
if (token) {
headers ??= new Headers()
headers.append("Authorization", `bearer ${token}`)
}
}
const rsp = await fetch(url, { headers })
if (!rsp.ok) throw new Error(`http: ${url}`)
const json = await rsp.json()
return [json as T, rsp]
}


async function *getVersions({ user, repo, type }: GetVersionsOptions): AsyncGenerator<string> {
//TODO set `Accept: application/vnd.github+json`
//TODO we can use ETags to check if the data we have cached is still valid
Expand Down
6 changes: 3 additions & 3 deletions lib/useSourceUnarchiver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Unarchiver, TarballUnarchiver, ZipUnarchiver } from "./Unarchiver.ts"
import { Verbosity } from "types"
import { useFlags } from "hooks"
import { run } from "utils"
import { useConfig } from "hooks"
import run from "hooks/useRun.ts"
import Path from "path"

//FIXME assuming strip 1 on components is going to trip people up
Expand All @@ -18,7 +18,7 @@ interface Response {

export default function useSourceUnarchiver(): Response {
const unarchive = async (opts: Options) => {
const { verbosity } = useFlags()
const { verbosity } = useConfig()

let unarchiver: Unarchiver
if (ZipUnarchiver.supports(opts.zipfile)) {
Expand Down
12 changes: 12 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { RunOptions } from "hooks/useRun.ts"
import { isArray } from "is_what"

export async function backticks(opts: RunOptions): Promise<string> {
const cmd = isArray(opts.cmd) ? opts.cmd.map(x => `${x}`) : [opts.cmd.string]
const cwd = opts.cwd?.toString()
console.verbose({ cwd, ...opts, cmd })
const proc = Deno.run({ ...opts, cwd, cmd, stdout: "piped" })
const out = await proc.output()
const txt = new TextDecoder().decode(out)
return txt
}
3 changes: 3 additions & 0 deletions libexec/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { parseFlags } from "cliffy/flags/mod.ts"
import { useCellar, usePantry, useMoustaches } from "hooks"
import { parse } from "utils/pkg.ts"

import tea_init from "../lib/init().ts"
tea_init()

const { unknown: pkgnames } = parseFlags(Deno.args)

const pantry = usePantry()
Expand Down
5 changes: 4 additions & 1 deletion libexec/deps.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env -S deno run --allow-net --allow-read --allow-env=TEA_PREFIX,TEA_PANTRY_PATH
#!/usr/bin/env -S deno run --allow-net --allow-read --allow-env

import { parseFlags } from "cliffy/flags/mod.ts"
import usePantry from "../lib/usePantry.ts"
import { parse, str } from "utils/pkg.ts"
import { hydrate } from "prefab"

import tea_init from "../lib/init().ts"
tea_init()

const { flags: { build, test }, unknown: [pkgname] } = parseFlags(Deno.args, {
flags: [{
name: "build"
Expand Down
5 changes: 4 additions & 1 deletion libexec/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ args:
- --allow-read
- --allow-run=tar,unzip
- --allow-write
- --allow-env=TEA_PREFIX,TEA_PANTRY_PATH,GITHUB_TOKEN
- --allow-env
---*/

//TODO verify the sha
Expand All @@ -26,6 +26,9 @@ import { parse } from "utils/pkg.ts"
import { panic } from "utils"
import Path from "path"

import tea_init from "../lib/init().ts"
tea_init()

const { flags: { outputDir, pkg: pkgname }, unknown } = parseFlags(Deno.args, {
flags: [{
name: "output-dir",
Expand Down
7 changes: 5 additions & 2 deletions libexec/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ dependencies:
import { useOffLicense, useCache, useDownload } from "hooks"
import { parseFlags } from "cliffy/flags/mod.ts"
import usePantry from "../lib/usePantry.ts"
import { print, panic } from "utils"
import { panic } from "utils"
import { parse } from "utils/pkg.ts"
import { Stowage} from "types"
import Path from "path"

import tea_init from "../lib/init().ts"
tea_init()

const pantry = usePantry()

const { flags, unknown: [pkgname] } = parseFlags(Deno.args, {
Expand Down Expand Up @@ -66,7 +69,7 @@ const zipfile = await (async () => {
}
})()

await print(`${zipfile}\n`)
console.log(zipfile!.string)

async function download({ dst, src }: { dst: Path, src: URL }) {
if (Deno.env.get("GITHUB_ACTIONS")) {
Expand Down
3 changes: 3 additions & 0 deletions libexec/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import { usePantry } from "hooks"

import tea_init from "../lib/init().ts"
tea_init()

const pantry = usePantry()
const arg = Deno.args[0]

Expand Down
8 changes: 5 additions & 3 deletions libexec/fixup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/usr/bin/env -S deno run --allow-run --allow-read --allow-write --allow-env

import { parseFlags } from "cliffy/flags/mod.ts"
import { useCellar, useFlags, usePrefix } from "hooks"
import { useCellar, usePrefix } from "hooks"
import tea_init from "../lib/init().ts"
import { str } from "utils/pkg.ts"
import { run, host } from "utils"
import run from "hooks/useRun.ts"
import { host } from "utils"
import Path from "path"

useFlags()
tea_init()

const { flags, unknown } = parseFlags(Deno.args, {
flags: [{
Expand Down
3 changes: 3 additions & 0 deletions libexec/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { install, link } from "prefab"
import { Installation } from "types"
import { panic } from "utils"

import tea_init from "../lib/init().ts"
tea_init()

const cellar = useCellar()
const inventory = useInventory()

Expand Down
3 changes: 3 additions & 0 deletions libexec/inventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { useInventory } from "hooks"
import SemVer from "semver"
import { parse } from "utils/pkg.ts"

import tea_init from "../lib/init().ts"
tea_init()

const { unknown: pkgnames } = parseFlags(Deno.args)

const rv: Record<string, SemVer[]> = {}
Expand Down
5 changes: 4 additions & 1 deletion libexec/link.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-env=TEA_PREFIX,TEA_PANTRY_PATH --allow-run=/bin/ln
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-env --allow-run=/bin/ln

import { PackageRequirement, Package } from "types"
import { parse } from "utils/pkg.ts"
import { panic } from "utils"
import { link } from "prefab"
import Path from "path"

import tea_init from "../lib/init().ts"
tea_init()

let pkg: Package | PackageRequirement = parse(Deno.args[1])
pkg = { project: pkg.project, version: pkg.constraint.single() ?? panic() }

Expand Down
19 changes: 11 additions & 8 deletions libexec/query.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/usr/bin/env -S deno run --allow-read --allow-env=TEA_PREFIX,TEA_PANTRY_PATH,SRCROOT,GITHUB_TOKEN --allow-write --allow-net
#!/usr/bin/env -S deno run --allow-read --allow-env --allow-write --allow-net

import { Package, PackageRequirement, Stowage } from "types"
import { flatmap, panic, print, host } from "utils"
import { flatmap, panic, host } from "utils"
import { parseFlags } from "cliffy/flags/mod.ts"
import { useCache, useCellar } from "hooks"
import usePantry from "../lib/usePantry.ts"
import { parse, str } from "utils/pkg.ts"
import Path from "path"

import tea_init from "../lib/init().ts"
tea_init()

const { flags: { prefix, srcdir, src, testdir, versions }, unknown: [pkgname] } = parseFlags(Deno.args, {
flags: [{
name: "prefix",
Expand All @@ -31,7 +34,7 @@ let pkg: PackageRequirement | Package = parse(pkgname)

if (versions) {
const versions = await usePantry().getVersions(pkg)
await print(versions.sort().join("\n"))
console.log(versions.sort().join("\n"))
Deno.exit(0)
}

Expand All @@ -48,13 +51,13 @@ if (src) {
const path = flatmap(Deno.env.get("SRCROOT"), x => new Path(x))
const cache_path = useCache().path(stowage)
if (path?.join("projects").isDirectory()) {
await print(`${path.join("srcs", cache_path.basename())}`)
await console.log(`${path.join("srcs", cache_path.basename())}`)
} else {
await print(cache_path.string)
await console.log(cache_path.string)
}
} else if (prefix) {
const path = useCellar().keg(pkg)
await print(path.string)
await console.log(path.string)
} else if (srcdir) {
let path = flatmap(Deno.env.get("SRCROOT"), x => new Path(x))
if (path?.join("projects").isDirectory()) {
Expand All @@ -64,7 +67,7 @@ if (src) {
} else {
path = new Path(Deno.makeTempDirSync())
}
await print(path.string)
await console.log(path.string)
} else if (testdir) {
let path = flatmap(Deno.env.get("SRCROOT"), x => new Path(x))
if (path?.join("projects").isDirectory()) {
Expand All @@ -74,7 +77,7 @@ if (src) {
} else {
path = new Path(Deno.makeTempDirSync())
}
await print(path.string)
await console.log(path.string)
} else {
Deno.exit(1)
}
Loading

0 comments on commit ca516fa

Please sign in to comment.