Skip to content

Commit

Permalink
on linux, you can't just run our git, it needs env
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Oct 22, 2022
1 parent e8b1258 commit 91a5742
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ that part; it’s going to *change the world*.
 


# tea/cli 0.8.9
# tea/cli 0.8.10

tea is a universal virtual‑environment manager:

Expand Down
5 changes: 2 additions & 3 deletions src/app.exec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useShellEnv, usePantry, useExecutableMarkdown, useVirtualEnv, useDownload, usePackageYAMLFrontMatter } from "hooks"
import { useShellEnv, useExecutableMarkdown, useVirtualEnv, useDownload, usePackageYAMLFrontMatter } from "hooks"
import useFlags, { Args } from "hooks/useFlags.ts"
import { hydrate, resolve, install as base_install, link } from "prefab"
import { PackageRequirement, PackageSpecification } from "types"
Expand Down Expand Up @@ -39,8 +39,7 @@ export default async function exec(opts: Args) {

/////////////////////////////////////////////////////////////
async function install(dry: PackageSpecification[]) {
const get = (x: PackageSpecification) => usePantry().getDeps(x).then(x => x.runtime)
const wet = await hydrate(dry, get) ; console.debug({wet})
const wet = await hydrate(dry) ; console.debug({wet})
const gas = await resolve(wet.pkgs) ; console.debug({gas})

for (const pkg of gas.pending) {
Expand Down
41 changes: 21 additions & 20 deletions src/hooks/usePantry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { run, host, flatmap, undent, validate_plain_obj, validate_str, validate_
import { useCellar, useGitHubAPI, usePrefix, useDownload } from "hooks"
import { validatePackageRequirement } from "utils/hacks.ts"
import { isNumber, isPlainObject, isString, isArray, isPrimitive, PlainObject, isBoolean } from "is_what"
import useShellEnv, { flatten } from "./useShellEnv.ts"
import SemVer, * as semver from "semver"
import tea_install from "prefab/install.ts"
import { install as tea_install, hydrate, resolve as tea_resolve } from "prefab"
import Path from "path"

interface Entry {
Expand Down Expand Up @@ -166,27 +167,25 @@ function coerceNumber(input: any) {
if (isNumber(input)) return input
}

const find_git = async () => {
const in_cellar = await useCellar().has({
project: 'git-scm.org',
constraint: new semver.Range('*')
})
if (in_cellar) {
return in_cellar.path.join('bin/git')
}

async function find_git(): Promise<[Path | string, Record<string, string[]>] | undefined> {
for (const path_ of Deno.env.get('PATH')?.split(':') ?? []) {
const path = Path.root.join(path_, 'git')
if (path.isExecutableFile()) {
return path
return [path, {}]
}
}

try {
const project = 'git-scm.org'
const version = await useInventory().select({ project, constraint: new semver.Range('*') }) ?? panic()
const install = await tea_install({ project, version })
return install.path.join('bin/git')
const installations = await (async () => {
const { pkgs: wet } = await hydrate({ project: 'git-scm.org', constraint: new semver.Range('*') })
const { pending: gas, installed } = await tea_resolve(wet)
return [
...await Promise.all(gas.map(tea_install)),
...installed
]
})()
const env = useShellEnv({ installations })
return ['git', env]
} catch (err) {
console.warn(err)
}
Expand All @@ -196,17 +195,19 @@ const find_git = async () => {
async function install(): Promise<true | 'not-git' | 'noop'> {
if (prefix.exists()) return 'noop'

const git = await find_git()
const found = await find_git()
const cwd = prefix.parent().mkpath()

if (git) {
if (found) {
const [git, preenv] = found
const env = flatten(preenv)
const { rid } = Deno.openSync(cwd.string)
await Deno.flock(rid, true)
try {
if (prefix.exists()) return 'noop' // another instance of tea did it
await run({
cmd: [git, "clone", "https://github.com/teaxyz/pantry", "."],
cwd
cwd, env
})
} finally {
//TODO if this gets stuck then nothing will work so need a handler for that
Expand All @@ -228,7 +229,8 @@ const update = async () => {
const git = await find_git()
const cwd = prefix.parent()
if (git) {
await run({cmd: [git, "pull", "origin", "HEAD", "--no-edit"], cwd})
const env = flatten(git[1])
await run({cmd: [git[0], "pull", "origin", "HEAD", "--no-edit"], cwd, env })
}
}

Expand Down Expand Up @@ -403,7 +405,6 @@ function expand_env(env_: PlainObject, pkg: Package, deps: Installation[]): stri

//////////////////////////////////////////// useMoustaches() additions
import useMoustachesBase from "./useMoustaches.ts"
import useInventory from "./useInventory.ts"

function useMoustaches() {
const base = useMoustachesBase()
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/useShellEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ export function expand(env: Record<string, string[]>) {
return rv
}

export function flatten(env: Record<string, string[]>) {
const rv: Record<string, string> = {}
for (const [key, value] of Object.entries(env)) {
rv[key] = value.join(":")
}
return rv
}

function find_tea() {
for (const bindir of Deno.env.get("PATH")?.split(":") ?? []) {
const file = new Path(bindir).join("tea").isExecutableFile()
Expand Down
7 changes: 5 additions & 2 deletions src/prefab/hydrate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PackageRequirement, Package } from "types"
import { PackageRequirement, Package, PackageSpecification } from "types"
import { isArray } from "is_what"
import * as semver from "semver"
import { usePantry } from "hooks"
import "utils"


Expand Down Expand Up @@ -29,12 +30,14 @@ interface ReturnValue {
bootstrap_required: Set<string>
}

const get = (x: PackageSpecification) => usePantry().getDeps(x).then(x => x.runtime)

/// sorts a list of packages topologically based on their
/// dependencies. Throws if there is a cycle in the input.
/// ignores changes in dependencies based on versions
export default async function hydrate(
input: (PackageRequirement | Package)[] | (PackageRequirement | Package),
get_deps: (pkg: PackageRequirement, dry: boolean) => Promise<PackageRequirement[]>,
get_deps: (pkg: PackageRequirement, dry: boolean) => Promise<PackageRequirement[]> = get,
): Promise<ReturnValue>
{
if (!isArray(input)) input = [input]
Expand Down

0 comments on commit 91a5742

Please sign in to comment.