Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for expressions in pxt.json:fileDependencies #7383

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/extensions/pxt-json.md
Expand Up @@ -152,6 +152,11 @@ the entire extension in the editor.
There is no point in adding them as regular `dependencies` - that would negate the
effects of `fileDependencies` and always include both the dependencies and files.

The `fileDependencies` allows expressions with `!`, `&&` and `||` (with the usual priorities).
Parenthesis are not supported.

In addition to packages, one can also say something like `target:microbit` or `target:arcade`.

In future, we may allow things like `"radio >= 1.2.3"`, but for now the package identifier is
the only thing supported.

Expand Down
44 changes: 30 additions & 14 deletions pxtlib/package.ts
Expand Up @@ -730,24 +730,40 @@ namespace pxt {
else
res = this.config.files.slice(0);
const fd = this.config.fileDependencies
if (this.config.fileDependencies)
if (fd) {
res = res.filter(fn => {
let cond = U.lookup(fd, fn)
if (!cond) return true
cond = cond.trim()
if (!cond) return true
if (/^[\w-]+$/.test(cond)) {
const dep = this.parent.resolveDep(cond)
if (dep && !dep.cppOnly)
return true
const checkCond = (cond: string): boolean => {
if (!cond) return true
cond = cond.trim()
if (!cond) return true
if (cond[0] == "!")
return !checkCond(cond.slice(1))
const spl = cond.split(":")
if (spl.length == 2) {
switch (spl[0]) {
case "target":
return spl[1] == pxt.appTarget.id
default:
break
}
} else if (/^[\w-]+$/.test(cond)) {
const dep = this.parent.resolveDep(cond)
if (dep && !dep.cppOnly)
return true
return false
}
if (!Package.depWarnings[cond]) {
Package.depWarnings[cond] = true
pxt.log(`invalid dependency expression: ${cond} in ${this.id}/${fn}`)
}
return false
}
if (!Package.depWarnings[cond]) {
Package.depWarnings[cond] = true
pxt.log(`invalid dependency expression: ${cond} in ${this.id}/${fn}`)
}
return false

const cond = U.lookup(fd, fn)
const dysj = cond.split("||")
return dysj.some(d => d.split("&&").every(checkCond))
})
}
return res
}

Expand Down