Skip to content

Commit

Permalink
move interpreter detection to package.yml (pkgxdev#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhheider authored and mxcl committed Nov 26, 2022
1 parent 9658c03 commit d0de85c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ All you need is `tea`.
 


# tea/cli 0.14.2
# tea/cli 0.14.3

Open source is a treasure trove—yet those chests are sealed with gnarly locks.
tea is the key:
Expand Down
17 changes: 5 additions & 12 deletions src/app.exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { flatten } from "hooks/useShellEnv.ts"
import { gray, Logger, red } from "hooks/useLogger.ts"
import * as semver from "semver"
import Path from "path"
import { Interpreter } from "hooks/usePantry.ts";

//TODO specifying explicit pkgs or versions on the command line should replace anything deeper
// RATIONALE: so you can override if you are testing locally
Expand Down Expand Up @@ -148,25 +149,17 @@ async function exec(ass: RV1, pkgs: PackageSpecification[], opts: {env: boolean}
}

} else {
const unshift = (project: string, ...new_args: string[]) => {
const unshift = ({ project, args: new_args }: Interpreter) => {
if (!yaml?.pkgs.length) {
pkgs.unshift({ project, constraint: new semver.Range("*") })
}
if (!yaml?.args.length) {
cmd.unshift(...new_args)
}
}
//FIXME package.ymls should define these behaviors
switch (ass.path.extname()) {
case ".py": unshift("python.org", "python"); break
case ".js": unshift("nodejs.org", "node"); break
case ".ts": unshift("deno.land", "deno", "run"); break
case ".go": unshift("go.dev", "go", "run"); break
case ".pl": unshift("perl.org", "perl"); break
case ".rb": unshift("ruby-lang.org", "ruby"); break
case ".bash": unshift("gnu.org/bash", "bash", "-e"); break
case ".sh": cmd.unshift("sh"); break
}

const interpreter = await usePantry().getInterpreter(ass.path.extname())
if (interpreter) unshift(interpreter)
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/hooks/usePantry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ interface Entry {
versions: Path
}

export interface Interpreter {
project: string // FIXME: should probably be a stronger type
args: string[]
}

export default function usePantry() {
return {
getClosestPackageSuggestion,
Expand All @@ -25,6 +30,7 @@ export default function usePantry() {
getScript,
getProvides,
getYAML,
getInterpreter,
resolve,
ls,
prefix
Expand Down Expand Up @@ -160,6 +166,25 @@ const getCompanions = async (pkg: {project: string}) => {
return parse_pkgs_node(node)
}

const getInterpreter = async (_extension: string): Promise<Interpreter | undefined> => {
const extension = _extension.slice(1)
for await (const pkg of ls()) {
const yml = await entry(pkg).yml()
const node = yml["interprets"]
if (!isPlainObject(node)) continue
try {
const { extensions, args } = yml["interprets"]
if ((isString(extensions) && extensions === extension) ||
(isArray(extensions) && extensions.includes(extension))) {
return { project: pkg.project, args: isArray(args) ? args : [args] }
}
} catch {
continue
}
}
return undefined
}

// deno-lint-ignore no-explicit-any
function coerceNumber(input: any) {
if (isNumber(input)) return input
Expand Down

0 comments on commit d0de85c

Please sign in to comment.