* Primop `toPath' to convert a string to a path.
* Primop `pathExists' to check for path existence.
This commit is contained in:
parent
e47e0c2dbe
commit
0e705391db
3 changed files with 30 additions and 7 deletions
|
@ -49,13 +49,6 @@ static Expr primImport(EvalState & state, const ATermVector & args)
|
|||
if (matchPath(arg, arg2))
|
||||
path = aterm2String(arg2);
|
||||
|
||||
else if (matchStr(arg, arg2)) {
|
||||
path = aterm2String(arg2);
|
||||
if (path == "" || path[0] != '/')
|
||||
throw EvalError("`import' requires an absolute path name");
|
||||
path = canonPath(path);
|
||||
}
|
||||
|
||||
else if (matchAttrs(arg, es)) {
|
||||
Expr a = queryAttr(arg, "type");
|
||||
|
||||
|
@ -80,6 +73,17 @@ static Expr primImport(EvalState & state, const ATermVector & args)
|
|||
}
|
||||
|
||||
|
||||
static Expr primPathExists(EvalState & state, const ATermVector & args)
|
||||
{
|
||||
Expr arg = evalExpr(state, args[0]), arg2;
|
||||
|
||||
if (!matchPath(arg, arg2))
|
||||
throw TypeError("`pathExists' requires a path as its argument");
|
||||
|
||||
return makeBool(pathExists(aterm2String(arg2)));
|
||||
}
|
||||
|
||||
|
||||
static void flattenList(EvalState & state, Expr e, ATermList & result)
|
||||
{
|
||||
ATermList es;
|
||||
|
@ -476,6 +480,19 @@ static Expr primToString(EvalState & state, const ATermVector & args)
|
|||
}
|
||||
|
||||
|
||||
/* Convert the argument to a path. */
|
||||
static Expr primToPath(EvalState & state, const ATermVector & args)
|
||||
{
|
||||
Expr e = evalExpr(state, args[0]);
|
||||
ATerm t = coerceToString(e);
|
||||
if (!t) throw TypeError(format("cannot coerce %1% to a path in `toPath'") % showType(e));
|
||||
Path path = aterm2String(t);
|
||||
if (path == "" || path[0] != '/')
|
||||
throw EvalError("string doesn't represent an absolute path in `toPath'");
|
||||
return makePath(toATerm(canonPath(path)));
|
||||
}
|
||||
|
||||
|
||||
/* Convert the argument (which can be any Nix expression) to an XML
|
||||
representation returned in a string. Not all Nix expressions can
|
||||
be sensibly or completely represented (e.g., functions). */
|
||||
|
@ -809,11 +826,13 @@ void EvalState::addPrimOps()
|
|||
addPrimOp("__currentTime", 0, primCurrentTime);
|
||||
|
||||
addPrimOp("import", 1, primImport);
|
||||
addPrimOp("__pathExists", 1, primPathExists);
|
||||
addPrimOp("derivation!", 1, primDerivationStrict);
|
||||
addPrimOp("derivation", 1, primDerivationLazy);
|
||||
addPrimOp("baseNameOf", 1, primBaseNameOf);
|
||||
addPrimOp("dirOf", 1, primDirOf);
|
||||
addPrimOp("toString", 1, primToString);
|
||||
addPrimOp("__toPath", 1, primToPath);
|
||||
addPrimOp("__toXML", 1, primToXML);
|
||||
addPrimOp("__toFile", 1, primToFile);
|
||||
addPrimOp("isNull", 1, primIsNull);
|
||||
|
|
1
tests/lang/eval-okay-pathexists.exp
Normal file
1
tests/lang/eval-okay-pathexists.exp
Normal file
|
@ -0,0 +1 @@
|
|||
Bool(True)
|
3
tests/lang/eval-okay-pathexists.nix
Normal file
3
tests/lang/eval-okay-pathexists.nix
Normal file
|
@ -0,0 +1,3 @@
|
|||
builtins.pathExists (builtins.toPath ./lib.nix)
|
||||
&& builtins.pathExists ./lib.nix
|
||||
&& !builtins.pathExists ./bla.nix
|
Loading…
Reference in a new issue