Add :reload command

This commit is contained in:
Eelco Dolstra 2013-09-09 16:02:46 +02:00
parent ddd22c37c5
commit adde4f0c8d

View file

@ -27,6 +27,8 @@ struct NixRepl
string curDir; string curDir;
EvalState state; EvalState state;
Strings loadedFiles;
StaticEnv staticEnv; StaticEnv staticEnv;
Env * env; Env * env;
int displ; int displ;
@ -41,6 +43,7 @@ struct NixRepl
bool getLine(string & line); bool getLine(string & line);
bool processLine(string line); bool processLine(string line);
void loadFile(const Path & path); void loadFile(const Path & path);
void reloadFiles();
void addAttrsToScope(Value & attrs); void addAttrsToScope(Value & attrs);
void addVarToScope(const Symbol & name, Value & v); void addVarToScope(const Symbol & name, Value & v);
Expr * parseString(string s); Expr * parseString(string s);
@ -84,9 +87,11 @@ void NixRepl::mainLoop(const Strings & args)
{ {
std::cout << "Welcome to Nix version " << NIX_VERSION << ". Type :? for help." << std::endl << std::endl; std::cout << "Welcome to Nix version " << NIX_VERSION << ". Type :? for help." << std::endl << std::endl;
foreach (Strings::const_iterator, i, args) { foreach (Strings::const_iterator, i, args)
std::cout << format("Loading %1%...") % *i << std::endl; loadedFiles.push_back(*i);
loadFile(*i);
if (!loadedFiles.empty()) {
reloadFiles();
std::cout << std::endl; std::cout << std::endl;
} }
@ -110,7 +115,6 @@ void NixRepl::mainLoop(const Strings & args)
std::cout << std::endl; std::cout << std::endl;
} }
} }
@ -271,7 +275,7 @@ bool NixRepl::processLine(string line)
arg = line; arg = line;
} }
if (command == ":?") { if (command == ":?" || command == ":help") {
cout << "The following commands are available:\n" cout << "The following commands are available:\n"
<< "\n" << "\n"
<< " <expr> Evaluate and print expression\n" << " <expr> Evaluate and print expression\n"
@ -281,21 +285,27 @@ bool NixRepl::processLine(string line)
<< " :l <path> Load Nix expression and add it to scope\n" << " :l <path> Load Nix expression and add it to scope\n"
<< " :p <expr> Evaluate and print expression recursively\n" << " :p <expr> Evaluate and print expression recursively\n"
<< " :q Exit nix-repl\n" << " :q Exit nix-repl\n"
<< " :r Reload all files\n"
<< " :s <expr> Build dependencies of derivation, then start nix-shell\n" << " :s <expr> Build dependencies of derivation, then start nix-shell\n"
<< " :t <expr> Describe result of evaluation\n"; << " :t <expr> Describe result of evaluation\n";
} }
else if (command == ":a") { else if (command == ":a" || command == ":add") {
Value v; Value v;
evalString(arg, v); evalString(arg, v);
addAttrsToScope(v); addAttrsToScope(v);
} }
else if (command == ":l") { else if (command == ":l" || command == ":load") {
state.resetFileCache(); state.resetFileCache();
loadFile(arg); loadFile(arg);
} }
else if (command == ":r" || command == ":reload") {
state.resetFileCache();
reloadFiles();
}
else if (command == ":t") { else if (command == ":t") {
Value v; Value v;
evalString(arg, v); evalString(arg, v);
@ -326,7 +336,7 @@ bool NixRepl::processLine(string line)
runProgram("nix-shell", Strings{drvPath}); runProgram("nix-shell", Strings{drvPath});
} }
else if (command == ":p") { else if (command == ":p" || command == ":print") {
Value v; Value v;
evalString(arg, v); evalString(arg, v);
printValue(std::cout, v, 1000000000) << std::endl; printValue(std::cout, v, 1000000000) << std::endl;
@ -363,6 +373,8 @@ bool NixRepl::processLine(string line)
void NixRepl::loadFile(const Path & path) void NixRepl::loadFile(const Path & path)
{ {
loadedFiles.remove(path);
loadedFiles.push_back(path);
Value v, v2; Value v, v2;
state.evalFile(lookupFileArg(state, path), v); state.evalFile(lookupFileArg(state, path), v);
Bindings bindings; Bindings bindings;
@ -371,6 +383,19 @@ void NixRepl::loadFile(const Path & path)
} }
void NixRepl::reloadFiles()
{
Strings old = loadedFiles;
loadedFiles.clear();
foreach (Strings::iterator, i, old) {
if (i != old.begin()) std::cout << std::endl;
std::cout << format("Loading %1%...") % *i << std::endl;
loadFile(*i);
}
}
void NixRepl::addAttrsToScope(Value & attrs) void NixRepl::addAttrsToScope(Value & attrs)
{ {
state.forceAttrs(attrs); state.forceAttrs(attrs);