Skip to content

Commit

Permalink
Merge branch 'fix-magic-cascade'
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Jul 8, 2022
2 parents 3fdf2a4 + fb9feb3 commit 2419805
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Change how your team works.
 


# tea/cli 0.3.1
# tea/cli 0.3.2

tea is a universal virtual‑environment manager:

Expand Down Expand Up @@ -528,7 +528,7 @@ If you got this error message, you need to install tea:
| Project | Version | Lock |
|-------------|---------|--------|
| deno.land | ^1.18 | 1.20.3 |
| tea.xyz | ^0 | 0.3.1 |
| tea.xyz | ^0 | 0.3.2 |
[pantry]: ../../../../pantry
Expand Down
42 changes: 26 additions & 16 deletions src/hooks/useVirtualEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import useFlags from "hooks/useFlags.ts"
export interface VirtualEnv {
srcroot: Path
requirements: PackageRequirement[]
requirementsFile: Path
requirementsFile: Path //NOTE maybe incorrect
version?: SemVer
}

Expand All @@ -38,18 +38,30 @@ export default async function useVirtualEnv(): Promise<VirtualEnv> {
const requirementsFile = srcroot.join(filename).isFile()
if (!requirementsFile) return
const subset = await fn(requirementsFile)
if (subset) return { ...subset, requirementsFile, srcroot }
if (subset) {
const requirements = subset.requirements ?? await domagic(srcroot) ?? []
return { ...subset, requirements, requirementsFile, srcroot }
}
}

let bp: VirtualEnv | undefined
if (bp = await attempt("package.json", extractFromJSON)) return bp
if (bp = await attempt("README.md", extractFromMarkdown)) return bp
if (bp = await domagic(srcroot)) return bp

const requirements = await domagic(srcroot)
if (requirements) return {
requirements,
requirementsFile: srcroot.join(".null"),
srcroot
}

throw "not-found:virtual-env"
}

type VirtualEnvSubset = Omit<VirtualEnv, 'srcroot' | 'requirementsFile'>
type VirtualEnvSubset = {
requirements: PackageRequirement[] | undefined
version: SemVer | undefined
}

//TODO support windows newlines
//TODO use a markdown parser lol
Expand All @@ -58,7 +70,7 @@ async function extractFromMarkdown(path: Path): Promise<VirtualEnvSubset | undef
const lines = text.split("\n")

const findTable = (header: string) => {
const rows: [string, string][] = []
let rows: [string, string][] | undefined = undefined
let found: 'nope' | 'header' | 'table' = 'nope'
done: for (const line of lines) {
switch (found) {
Expand All @@ -69,6 +81,7 @@ async function extractFromMarkdown(path: Path): Promise<VirtualEnvSubset | undef
case 'table': {
const match = line.match(/^\|([^|]+)\|([^|]+)\|/)
if (!match) break done
if (!rows) rows = []
rows.push([match[1].trim(), match[2].trim()])
} break
case 'nope':
Expand All @@ -81,7 +94,7 @@ async function extractFromMarkdown(path: Path): Promise<VirtualEnvSubset | undef
}

const requirements = (() => {
return findTable("Dependencies").compactMap(([project, constraint]) => {
return findTable("Dependencies")?.compactMap(([project, constraint]) => {
switch (project) {
case "cc":
case "c++":
Expand All @@ -97,7 +110,7 @@ async function extractFromMarkdown(path: Path): Promise<VirtualEnvSubset | undef
})()

const fromMetadataTable = () => flatMap(
findTable("Metadata").find(([key, value]) => key.toLowerCase() == "version" && value),
findTable("Metadata")?.find(([key, value]) => key.toLowerCase() == "version" && value),
([,x]) => new SemVer(x)
)

Expand All @@ -108,21 +121,19 @@ async function extractFromMarkdown(path: Path): Promise<VirtualEnvSubset | undef

const version = fromMetadataTable() ?? fromFirstHeader()

if (requirements.chuzzle() || version) {
return { requirements, version }
}
return { requirements, version }
}

async function extractFromJSON(path: Path): Promise<VirtualEnvSubset | undefined> {
const json = await path.readJSON()
if (!isPlainObject(json)) throw "bad-json"
if (!json.tea) return
const requirements = (() => {
if (!json.tea.dependencies) return []
if (!json.tea.dependencies) return
if (!isPlainObject(json.tea?.dependencies)) throw "bad-json"
return parsePackageRequirements(json.tea.dependencies)
})()
const version = json.version
const version = flatMap(json.version, x => new SemVer(x))
return { requirements, version }
}

Expand All @@ -138,7 +149,8 @@ function parsePackageRequirements(input: PlainObject): PackageRequirement[] {
return rv
}

async function domagic(srcroot: Path): Promise<VirtualEnv | undefined> {
//TODO get version too
async function domagic(srcroot: Path): Promise<PackageRequirement[] | undefined> {
let path: Path | undefined

const requirements = await (async () => {
Expand Down Expand Up @@ -169,7 +181,5 @@ async function domagic(srcroot: Path): Promise<VirtualEnv | undefined> {
}
})()

if (requirements) return {
srcroot, requirements, requirementsFile: path!
}
return requirements
}

0 comments on commit 2419805

Please sign in to comment.