* Strip off the `.nix' suffix from the attribute name for files in

~/.nix-defexpr,  otherwise the attribute cannot be selected with the
  `-A' option.  Useful if you want to stick a Nix expression directly
  in ~/.nix-defexpr.
This commit is contained in:
Eelco Dolstra 2008-08-25 13:31:57 +00:00
parent cc826dc03e
commit b428adc267
5 changed files with 27 additions and 6 deletions

View file

@ -2,6 +2,7 @@
#include "store-api.hh" #include "store-api.hh"
#include "aterm.hh" #include "aterm.hh"
#include "globals.hh" #include "globals.hh"
#include "util.hh"
#include "derivations-ast.hh" #include "derivations-ast.hh"
#include "derivations-ast.cc" #include "derivations-ast.cc"
@ -163,9 +164,7 @@ ATerm unparseDerivation(const Derivation & drv)
bool isDerivation(const string & fileName) bool isDerivation(const string & fileName)
{ {
return return hasSuffix(fileName, drvExtension);
fileName.size() >= drvExtension.size() &&
string(fileName, fileName.size() - drvExtension.size()) == drvExtension;
} }

View file

@ -257,6 +257,7 @@ Hash hashString(HashType ht, const string & s)
start(ht, ctx); start(ht, ctx);
update(ht, ctx, (const unsigned char *) s.c_str(), s.length()); update(ht, ctx, (const unsigned char *) s.c_str(), s.length());
finish(ht, ctx, hash.hash); finish(ht, ctx, hash.hash);
//printMsg(lvlError, format("hashString %1% --> %2%:\n%3%\n===========END======") % s.size() % printHash(hash) % s);
return hash; return hash;
} }

View file

@ -1046,6 +1046,12 @@ bool string2Int(const string & s, long long & n)
} }
bool hasSuffix(const string & s, const string & suffix)
{
return s.size() >= suffix.size() && string(s, s.size() - suffix.size()) == suffix;
}
void ignoreException() void ignoreException()
{ {
try { try {

View file

@ -294,6 +294,10 @@ bool string2Int(const string & s, int & n);
bool string2Int(const string & s, long long & n); bool string2Int(const string & s, long long & n);
/* Return true iff `s' ends in `suffix'. */
bool hasSuffix(const string & s, const string & suffix);
/* Exception handling in destructors: print an error message, then /* Exception handling in destructors: print an error message, then
ignore the exception. */ ignore the exception. */
void ignoreException(); void ignoreException();

View file

@ -115,18 +115,29 @@ static void getAllExprs(EvalState & state,
const Path & path, ATermMap & attrs) const Path & path, ATermMap & attrs)
{ {
Strings names = readDirectory(path); Strings names = readDirectory(path);
StringSet namesSorted(names.begin(), names.end());
for (Strings::iterator i = names.begin(); i != names.end(); ++i) { foreach (StringSet::iterator, i, namesSorted) {
Path path2 = path + "/" + *i; Path path2 = path + "/" + *i;
struct stat st; struct stat st;
if (stat(path2.c_str(), &st) == -1) if (stat(path2.c_str(), &st) == -1)
continue; // ignore dangling symlinks in ~/.nix-defexpr continue; // ignore dangling symlinks in ~/.nix-defexpr
if (isNixExpr(path2)) if (isNixExpr(path2)) {
attrs.set(toATerm(*i), makeAttrRHS( /* Strip off the `.nix' filename suffix (if applicable),
otherwise the attribute cannot be selected with the
`-A' option. Useful if you want to stick a Nix
expression directly in ~/.nix-defexpr. */
string attrName = *i;
if (hasSuffix(attrName, ".nix"))
attrName = string(attrName, 0, attrName.size() - 4);
attrs.set(toATerm(attrName), makeAttrRHS(
parseExprFromFile(state, absPath(path2)), makeNoPos())); parseExprFromFile(state, absPath(path2)), makeNoPos()));
}
else else
/* `path2' is a directory (with no default.nix in it);
recurse into it. */
getAllExprs(state, path2, attrs); getAllExprs(state, path2, attrs);
} }
} }