Skip to content

Commit 65325cf

Browse files
committed
allow if conditions for script steps
1 parent 1294eb9 commit 65325cf

File tree

3 files changed

+123
-103
lines changed

3 files changed

+123
-103
lines changed

lib/usePantry.getScript.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { SupportedPlatforms } from "https://raw.githubusercontent.com/teaxyz/lib/v0.4.2/src/utils/host.ts"
2+
import { isArray, isString, isPlainObject, PlainObject } from "is-what"
3+
import { Package, Installation, hooks, utils, semver } from "libtea"
4+
import undent from "outdent"
5+
6+
const { validate, host } = utils
7+
const { useMoustaches } = hooks
8+
9+
export const getScript = async (pkg: Package, key: 'build' | 'test', deps: Installation[]) => {
10+
const yml = await hooks.usePantry().project(pkg).yaml()
11+
const node = yml[key]
12+
13+
const mm = useMoustaches()
14+
const script = (input: unknown) => {
15+
const tokens = mm.tokenize.all(pkg, deps)
16+
if (isArray(input)) input = input.map(obj => {
17+
if (isPlainObject(obj)) {
18+
19+
const condition = obj["if"]
20+
if (condition) {
21+
if (SupportedPlatforms.includes(condition) && host().platform != condition) return ''
22+
23+
const range = semver.Range.parse(condition)
24+
if (range && !range.satisfies(pkg.version)) return ''
25+
}
26+
27+
let run = obj['run']
28+
if (!isString(run)) throw new Error('every node in a script YAML array must contain a `run` key')
29+
30+
let cd = obj['working-directory']
31+
if (cd) {
32+
cd = mm.apply(validate.str(cd), tokens)
33+
run = undent`
34+
OLDWD="$PWD"
35+
mkdir -p "${cd}"
36+
cd "${cd}"
37+
${run.trim()}
38+
cd "$OLDWD"
39+
unset OLDWD
40+
`
41+
}
42+
43+
let fixture_key = key == 'build' ? 'prop' : 'fixture'
44+
let fixture = obj[fixture_key]
45+
if (fixture) {
46+
fixture_key = fixture_key.toUpperCase()
47+
fixture = mm.apply(validate.str(fixture), tokens)
48+
run = undent`
49+
OLD_${fixture_key}=$${fixture_key}
50+
${fixture_key}=$(mktemp)
51+
52+
cat <<XYZ_TEA_EOF > $${fixture_key}
53+
${fixture}
54+
XYZ_TEA_EOF
55+
56+
${run}
57+
58+
rm -f $${fixture_key}
59+
60+
if test -n "$${fixture_key}"; then
61+
${fixture_key}=$OLD_${fixture_key}
62+
else
63+
unset ${fixture_key}
64+
fi
65+
`
66+
}
67+
68+
return run.trim()
69+
} else {
70+
return `${obj}`.trim()
71+
}
72+
}).join("\n\n")
73+
return mm.apply(validate.str(input), tokens)
74+
}
75+
76+
if (isPlainObject(node)) {
77+
let raw = script(node.script)
78+
79+
let wd = node["working-directory"]
80+
if (wd) {
81+
wd = mm.apply(wd, [
82+
...mm.tokenize.version(pkg.version),
83+
...mm.tokenize.host(),
84+
...mm.tokenize.pkg(pkg)
85+
])
86+
raw = undent`
87+
mkdir -p ${wd}
88+
cd ${wd}
89+
90+
${raw}
91+
`
92+
}
93+
94+
const env = node.env
95+
if (isPlainObject(env)) {
96+
raw = `${expand_env(env, pkg, deps)}\n\n${raw}`
97+
}
98+
return raw
99+
} else {
100+
return script(node)
101+
}
102+
}
103+
104+
function expand_env(env: PlainObject, pkg: Package, deps: Installation[]): string {
105+
const { expand_env_obj } = hooks.usePantry()
106+
return Object.entries(expand_env_obj(env, pkg, deps)).map(([key,value]) => {
107+
// weird POSIX string escaping/concat stuff
108+
// eg. export FOO="bar ""$baz"" bun"
109+
value = `"${value.trim().replace(/"/g, '""')}"`
110+
while (value.startsWith('""')) value = value.slice(1) //FIXME lol better pls
111+
while (value.endsWith('""')) value = value.slice(0,-1) //FIXME lol better pls
112+
113+
return `export ${key}=${value}`
114+
}).join("\n")
115+
}

lib/usePantry.ts

Lines changed: 8 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Package, PackageRequirement, Installation, SemVer, semver, utils, hooks } from "libtea"
1+
import { Package, PackageRequirement, SemVer, semver, utils, hooks } from "libtea"
22
import { isNumber, isPlainObject, isString, isArray, PlainObject } from "is-what"
3+
import { getScript } from "./usePantry.getScript.ts"
34
import useGitLabAPI from "./useGitLabAPI.ts"
45
import useGitHubAPI from "./useGitHubAPI.ts"
6+
57
const { flatmap, validate } = utils
68
const { useMoustaches } = hooks
7-
import undent from "outdent"
89

910
export interface Interpreter {
1011
project: string
@@ -38,7 +39,10 @@ async function resolve(spec: Package | PackageRequirement): Promise<Package> {
3839
const constraint = "constraint" in spec ? spec.constraint : new semver.Range(`=${spec.version}`)
3940
const versions = await getVersions(spec)
4041
const version = constraint.max(versions)
41-
if (!version) throw new Error(`not-found: version: ${utils.pkg.str(spec)}`)
42+
if (!version) {
43+
console.error({versions})
44+
throw new Error(`not-found: version: ${utils.pkg.str(spec)}`)
45+
}
4246
console.debug({selected: version})
4347
return { project: spec.project, version };
4448
}
@@ -116,90 +120,6 @@ const getDistributable = async (pkg: Package) => {
116120
return { url, ref: undefined, stripComponents, type: 'url' }
117121
}
118122

119-
const getScript = async (pkg: Package, key: 'build' | 'test', deps: Installation[]) => {
120-
const yml = await hooks.usePantry().project(pkg).yaml()
121-
const node = yml[key]
122-
123-
const mm = useMoustaches()
124-
const script = (input: unknown) => {
125-
const tokens = mm.tokenize.all(pkg, deps)
126-
if (isArray(input)) input = input.map(obj => {
127-
if (isPlainObject(obj)) {
128-
let run = obj['run']
129-
if (!isString(run)) throw new Error('every node in a script YAML array must contain a `run` key')
130-
let cd = obj['working-directory']
131-
if (cd) {
132-
cd = mm.apply(validate.str(cd), tokens)
133-
run = undent`
134-
OLDWD="$PWD"
135-
mkdir -p "${cd}"
136-
cd "${cd}"
137-
${run.trim()}
138-
cd "$OLDWD"
139-
unset OLDWD
140-
`
141-
}
142-
let fixture_key = key == 'build' ? 'prop' : 'fixture'
143-
let fixture = obj[fixture_key]
144-
if (fixture) {
145-
fixture_key = fixture_key.toUpperCase()
146-
fixture = mm.apply(validate.str(fixture), tokens)
147-
run = undent`
148-
OLD_${fixture_key}=$${fixture_key}
149-
${fixture_key}=$(mktemp)
150-
151-
cat <<XYZ_TEA_EOF > $${fixture_key}
152-
${fixture}
153-
XYZ_TEA_EOF
154-
155-
${run}
156-
157-
rm -f $${fixture_key}
158-
159-
if test -n "$${fixture_key}"; then
160-
${fixture_key}=$OLD_${fixture_key}
161-
else
162-
unset ${fixture_key}
163-
fi
164-
`
165-
}
166-
167-
return run.trim()
168-
} else {
169-
return `${obj}`.trim()
170-
}
171-
}).join("\n\n")
172-
return mm.apply(validate.str(input), tokens)
173-
}
174-
175-
if (isPlainObject(node)) {
176-
let raw = script(node.script)
177-
178-
let wd = node["working-directory"]
179-
if (wd) {
180-
wd = mm.apply(wd, [
181-
...mm.tokenize.version(pkg.version),
182-
...mm.tokenize.host(),
183-
...mm.tokenize.pkg(pkg)
184-
])
185-
raw = undent`
186-
mkdir -p ${wd}
187-
cd ${wd}
188-
189-
${raw}
190-
`
191-
}
192-
193-
const env = node.env
194-
if (isPlainObject(env)) {
195-
raw = `${expand_env(env, pkg, deps)}\n\n${raw}`
196-
}
197-
return raw
198-
} else {
199-
return script(node)
200-
}
201-
}
202-
203123
// deno-lint-ignore no-explicit-any
204124
function coerceNumber(input: any) {
205125
if (isNumber(input)) return input
@@ -422,23 +342,10 @@ async function handleURLVersions(versions: PlainObject): Promise<SemVer[]> {
422342
return rv
423343
}
424344

425-
function expand_env(env: PlainObject, pkg: Package, deps: Installation[]): string {
426-
const { expand_env_obj } = hooks.usePantry()
427-
return Object.entries(expand_env_obj(env, pkg, deps)).map(([key,value]) => {
428-
// weird POSIX string escaping/concat stuff
429-
// eg. export FOO="bar ""$baz"" bun"
430-
value = `"${value.trim().replace(/"/g, '""')}"`
431-
while (value.startsWith('""')) value = value.slice(1) //FIXME lol better pls
432-
while (value.endsWith('""')) value = value.slice(0,-1) //FIXME lol better pls
433-
434-
return `export ${key}=${value}`
435-
}).join("\n")
436-
}
437-
438345
//FIXME inefficient, should be in libtea as part of .project()
439346
async function filepath(project: string) {
440347
for await (const pkg of hooks.usePantry().ls()) {
441348
if (project == pkg.project) return pkg.path
442349
}
443350
throw new Error(`package.yml not found: ${project}`)
444-
}
351+
}

libexec/stage.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ if (blddir.string.includes(" ")) {
4343
console.error("warning: build directory contains spaces. build tools *may choke*")
4444
}
4545

46-
console.error(blddir.isDirectory(), blddir.isDirectory()?.isEmpty(), blddir.isDirectory()?.isEmpty() ?? true)
47-
4846
if (!blddir.isDirectory() || blddir.exists()?.isEmpty()) {
4947
await copy(srcdir.string, blddir.string, { overwrite: true })
5048
}

0 commit comments

Comments
 (0)