forked from pkgxdev/pkgx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
558 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { VirtualEnv } from "hooks/useVirtualEnv.ts" | ||
import { print, undent } from "utils" | ||
import useShellEnv from "hooks/useShellEnv.ts" | ||
import usePantry from "hooks/usePantry.ts" | ||
|
||
interface Options { | ||
env: VirtualEnv | undefined | ||
} | ||
|
||
//TODO needs to take a argish parameter | ||
|
||
export default async function dump({ env: blueprint }: Options) { | ||
console.verbose({ blueprint }) | ||
|
||
const shell = Deno.env.get("SHELL")?.split('/').pop() | ||
const [ setEnv, unsetEnv ]= (() => { | ||
switch (shell) { | ||
case "fish": | ||
return [ | ||
(name: string, val: string) => `set -gx ${name} ${val};`, | ||
(name: string) => `set -e ${name};` | ||
] | ||
default: | ||
return [ | ||
(name: string, val: string) => `export ${name}=${val}`, | ||
(name: string) => `unset ${name}` | ||
] | ||
}})() | ||
if (blueprint?.srcroot) { | ||
await print(setEnv("SRCROOT", blueprint.srcroot.string)) | ||
} else if (Deno.env.get("SRCROOT")) { | ||
await print(unsetEnv("SRCROOT")) | ||
} | ||
|
||
const { combinedStrings: vars, pending } = await useShellEnv(blueprint?.requirements ?? []) | ||
|
||
//TODO if PATH is the same as the current PATH maybe don't do it | ||
// though that makes the behavior of --env --dump very specific | ||
|
||
for (const [key, value] of Object.entries(vars)) { | ||
await print(value | ||
? setEnv(key, value) | ||
: unsetEnv(key)) | ||
} | ||
if (blueprint?.version) { | ||
await print(setEnv("VERSION", blueprint.version.toString())) | ||
} | ||
|
||
// TODO: implement command-not-found for fish | ||
if (shell !== "fish") { return } | ||
|
||
if (pending.length) { | ||
const pantry = usePantry() | ||
let rv = undent` | ||
command_not_found_handler() { | ||
case $0 in | ||
` | ||
for (const pkg of pending) { | ||
const cmds = (await pantry.getProvides(pkg)).join("|") | ||
rv += ` ${cmds}) tea -xmP ${pkg.project}@'${pkg.constraint}' -- "$@";;\n` | ||
} | ||
rv += ` *)\n printf 'zsh: command not found: %s\\n' "$1";;\n esac\n}` | ||
|
||
await print(rv) | ||
} else { | ||
//TODO unless there's a default! | ||
await print("if typeset -f command_not_found_handler >/dev/null; then unset -f command_not_found_handler; fi") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { run, undent } from "utils" | ||
import hydrate from "./prefab/hydrate.ts" | ||
import resolve from "./prefab/resolve.ts" | ||
import base_install from "./prefab/install.ts" | ||
import { lvl1 as link } from "./prefab/link.ts" | ||
import useShellEnv from "hooks/useShellEnv.ts" | ||
import useCellar from "hooks/useCellar.ts" | ||
import { PackageRequirement, Path } from "types" | ||
import { VirtualEnv } from "hooks/useVirtualEnv.ts" | ||
import useExecutableMarkdown from "hooks/useExecutableMarkdown.ts" | ||
|
||
type Options = { | ||
args: string[] | ||
env: VirtualEnv | undefined | ||
pkgs: PackageRequirement[] | ||
} | ||
|
||
export default async function exec({ args, ...opts }: Options) { | ||
const cellar = useCellar() | ||
|
||
if (args.length < 1) throw "contract violation" | ||
|
||
await install(opts.pkgs) | ||
|
||
const filename = Path.cwd().join(args[0]).isFile() | ||
if (filename?.extname() == '.md') { | ||
const target = args[1] | ||
const sh = await useExecutableMarkdown({ filename }).findScript(target) | ||
const path = Path.mktemp().join('script').write({ text: undent` | ||
#!/bin/sh | ||
${sh} | ||
` }).chmod(0o500).string | ||
args = [path, ...args.slice(2)] | ||
} | ||
|
||
const env = (await useShellEnv(opts.pkgs)).combinedStrings | ||
if (opts.env) { | ||
env["SRCROOT"] = opts.env.srcroot.string | ||
if (opts.env.version) env["VERSION"] = opts.env.version.toString() | ||
} | ||
|
||
const cmd = [...args] | ||
await run({ cmd, env }) //TODO implement `execvp` | ||
|
||
///////////////////////////////////////////////////////////// | ||
async function install(dry: PackageRequirement[]) { | ||
const wet = await hydrate(dry) ; console.debug({wet}) | ||
const gas = await resolve(wet) ; console.debug({gas}) | ||
for (const pkg of gas) { | ||
if (await cellar.isInstalled(pkg)) continue | ||
console.info({ installing: pkg }) | ||
const installation = await base_install(pkg) | ||
await link(installation) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import useFlags from "hooks/useFlags.ts" | ||
import { print, undent } from "utils" | ||
|
||
export default async function help() { | ||
const { verbose } = useFlags() | ||
|
||
// tea -mx +deno.land^1.18 foo.ts -- bar | ||
// tea -mx +deno.land^1.18 deno -- ./script-file foo bar baz | ||
// tea build | ||
// tea -mx ./README.md -- build | ||
|
||
//TODO make the stuff in brackets grayed out a bit | ||
|
||
if (!verbose) { | ||
// 10| 20| 30| 40| 50| 60| 70| | 80| | ||
await print(undent` | ||
usage: | ||
tea [-xd] [flags] [file|URL|target|cmd|interpreter] [+package~x.y] -- [arg…] | ||
modes: magical? | ||
05 𝑜𝑚𝑖𝑡𝑡𝑒𝑑 infer operation ✨ | ||
--exec,-x execute | ||
--dump,-d dump | ||
flags: | ||
10 --env,-E inject virtual environment ✨ | ||
--muggle,-m disable magic | ||
--verbose,-v eg. tea -vv | ||
--silent,-s no chat, no errors | ||
--cd,-C change directory first | ||
15 | ||
more: | ||
tea -vh | ||
18 open github.com/teaxyz/cli | ||
`) | ||
//HEYU! did you exceed 22 lines? Don’t! That’s the limit! | ||
} else { | ||
// 10| 20| 30| 40| 50| 60| 70| | 80| | ||
await print(undent` | ||
usage: | ||
tea [-xd] [flags] [file|URL|target|cmd|interpreter] [+package~x.y] -- [arg…] | ||
modes: magical? env-aware | ||
--exec,-x execute (omittable if ✨) ✨ 𐄂 | ||
--dump,-d dump ✨ 𐄂 | ||
aliases: | ||
--help,-h --dump=usage | ||
--version,-v --dump=version 𐄂 | ||
flags: | ||
--env,-E inject virtual environment ✨ | ||
--json,-j output json | ||
--muggle,-m disable magic | ||
--verbose,-v short form accumulates, shows version first | ||
--silent,-s no chat, no errors | ||
--cd,-C,--chdir change directory first | ||
environment variables: | ||
VERBOSE {-1: silent, 0: default, 1: verbose, 2: debug} | ||
MAGIC [0,1] | ||
DEBUG [0,1]: alias for \`VERBOSE=2\` | ||
TEA_DIR \`--chdir \${directory}\` | ||
notes: | ||
- explicit flags override any environment variables | ||
- the results of magic can be observed if verbosity is > 0 | ||
ideology: | ||
> A successful tool is one that was used to do something undreamed of | ||
> by its author | ||
—𝑠ℎ𝑎𝑑𝑜𝑤𝑦 𝑠𝑢𝑝𝑒𝑟 𝑐𝑜𝑑𝑒𝑟 | ||
`) | ||
} | ||
} |
Oops, something went wrong.