nix edit / log: Operate on a single Installable

This commit is contained in:
Eelco Dolstra 2017-08-29 16:18:00 +02:00
parent 9b82ecbae0
commit e9c07a3b26
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
6 changed files with 109 additions and 98 deletions

View file

@ -51,7 +51,7 @@ struct Installable
{ {
virtual std::string what() = 0; virtual std::string what() = 0;
virtual Buildables toBuildable() virtual Buildables toBuildable(bool singular = false)
{ {
throw Error("argument '%s' cannot be built", what()); throw Error("argument '%s' cannot be built", what());
} }
@ -97,8 +97,6 @@ struct InstallablesCommand : virtual Args, SourceExprCommand
expectArgs("installables", &_installables); expectArgs("installables", &_installables);
} }
std::vector<std::shared_ptr<Installable>> parseInstallables(ref<Store> store, Strings ss);
enum ToStorePathsMode { Build, NoBuild, DryRun }; enum ToStorePathsMode { Build, NoBuild, DryRun };
PathSet toStorePaths(ref<Store> store, ToStorePathsMode mode); PathSet toStorePaths(ref<Store> store, ToStorePathsMode mode);
@ -112,6 +110,22 @@ private:
Strings _installables; Strings _installables;
}; };
struct InstallableCommand : virtual Args, SourceExprCommand
{
std::shared_ptr<Installable> installable;
InstallableCommand()
{
expectArg("installable", &_installable);
}
void prepare() override;
private:
std::string _installable;
};
/* A command that operates on zero or more store paths. */ /* A command that operates on zero or more store paths. */
struct StorePathsCommand : public InstallablesCommand struct StorePathsCommand : public InstallablesCommand
{ {

View file

@ -8,7 +8,7 @@
using namespace nix; using namespace nix;
struct CmdEdit : InstallablesCommand struct CmdEdit : InstallableCommand
{ {
std::string name() override std::string name() override
{ {
@ -34,44 +34,42 @@ struct CmdEdit : InstallablesCommand
{ {
auto state = getEvalState(); auto state = getEvalState();
for (auto & i : installables) { auto v = installable->toValue(*state);
auto v = i->toValue(*state);
Value * v2; Value * v2;
try { try {
auto dummyArgs = state->allocBindings(0); auto dummyArgs = state->allocBindings(0);
v2 = findAlongAttrPath(*state, "meta.position", *dummyArgs, *v); v2 = findAlongAttrPath(*state, "meta.position", *dummyArgs, *v);
} catch (Error &) { } catch (Error &) {
throw Error("package '%s' has no source location information", i->what()); throw Error("package '%s' has no source location information", installable->what());
}
auto pos = state->forceString(*v2);
debug("position is %s", pos);
auto colon = pos.rfind(':');
if (colon == std::string::npos)
throw Error("cannot parse meta.position attribute '%s'", pos);
std::string filename(pos, 0, colon);
int lineno = std::stoi(std::string(pos, colon + 1));
auto editor = getEnv("EDITOR", "cat");
Strings args{editor};
if (editor.find("emacs") != std::string::npos ||
editor.find("nano") != std::string::npos ||
editor.find("vim") != std::string::npos)
args.push_back(fmt("+%d", lineno));
args.push_back(filename);
stopProgressBar();
execvp(editor.c_str(), stringsToCharPtrs(args).data());
throw SysError("cannot run editor '%s'", editor);
} }
auto pos = state->forceString(*v2);
debug("position is %s", pos);
auto colon = pos.rfind(':');
if (colon == std::string::npos)
throw Error("cannot parse meta.position attribute '%s'", pos);
std::string filename(pos, 0, colon);
int lineno = std::stoi(std::string(pos, colon + 1));
auto editor = getEnv("EDITOR", "cat");
Strings args{editor};
if (editor.find("emacs") != std::string::npos ||
editor.find("nano") != std::string::npos ||
editor.find("vim") != std::string::npos)
args.push_back(fmt("+%d", lineno));
args.push_back(filename);
stopProgressBar();
execvp(editor.c_str(), stringsToCharPtrs(args).data());
throw SysError("cannot run editor '%s'", editor);
} }
}; };

View file

@ -78,7 +78,7 @@ struct InstallableStoreDrv : Installable
std::string what() override { return storePath; } std::string what() override { return storePath; }
Buildables toBuildable() override Buildables toBuildable(bool singular) override
{ {
return {{storePath, {}}}; return {{storePath, {}}};
} }
@ -92,25 +92,21 @@ struct InstallableStorePath : Installable
std::string what() override { return storePath; } std::string what() override { return storePath; }
Buildables toBuildable() override Buildables toBuildable(bool singular) override
{ {
return {{storePath, {}}}; return {{storePath, {}}};
} }
}; };
struct InstallableExpr : Installable struct InstallableValue : Installable
{ {
InstallablesCommand & installables; SourceExprCommand & cmd;
std::string text;
InstallableExpr(InstallablesCommand & installables, const std::string & text) InstallableValue(SourceExprCommand & cmd) : cmd(cmd) { }
: installables(installables), text(text) { }
std::string what() override { return text; } Buildables toBuildable(bool singular) override
Buildables toBuildable() override
{ {
auto state = installables.getEvalState(); auto state = cmd.getEvalState();
auto v = toValue(*state); auto v = toValue(*state);
@ -121,6 +117,9 @@ struct InstallableExpr : Installable
DrvInfos drvs; DrvInfos drvs;
getDerivations(*state, *v, "", autoArgs, drvs, false); getDerivations(*state, *v, "", autoArgs, drvs, false);
if (singular && drvs.size() != 1)
throw Error("installable '%s' evaluates to %d derivations, where only one is expected", what(), drvs.size());
Buildables res; Buildables res;
for (auto & drv : drvs) for (auto & drv : drvs)
@ -129,6 +128,16 @@ struct InstallableExpr : Installable
return res; return res;
} }
};
struct InstallableExpr : InstallableValue
{
std::string text;
InstallableExpr(SourceExprCommand & cmd, const std::string & text)
: InstallableValue(cmd), text(text) { }
std::string what() override { return text; }
Value * toValue(EvalState & state) override Value * toValue(EvalState & state) override
{ {
@ -138,42 +147,19 @@ struct InstallableExpr : Installable
} }
}; };
struct InstallableAttrPath : Installable struct InstallableAttrPath : InstallableValue
{ {
InstallablesCommand & installables;
std::string attrPath; std::string attrPath;
InstallableAttrPath(InstallablesCommand & installables, const std::string & attrPath) InstallableAttrPath(SourceExprCommand & cmd, const std::string & attrPath)
: installables(installables), attrPath(attrPath) : InstallableValue(cmd), attrPath(attrPath)
{ } { }
std::string what() override { return attrPath; } std::string what() override { return attrPath; }
Buildables toBuildable() override
{
auto state = installables.getEvalState();
auto v = toValue(*state);
// FIXME
std::map<string, string> autoArgs_;
Bindings & autoArgs(*evalAutoArgs(*state, autoArgs_));
DrvInfos drvs;
getDerivations(*state, *v, "", autoArgs, drvs, false);
Buildables res;
for (auto & drv : drvs)
for (auto & output : drv.queryOutputs())
res.emplace(output.second, Whence{output.first, drv.queryDrvPath()});
return res;
}
Value * toValue(EvalState & state) override Value * toValue(EvalState & state) override
{ {
auto source = installables.getSourceExpr(state); auto source = cmd.getSourceExpr(state);
// FIXME // FIXME
std::map<string, string> autoArgs_; std::map<string, string> autoArgs_;
@ -190,20 +176,21 @@ struct InstallableAttrPath : Installable
std::string attrRegex = R"([A-Za-z_][A-Za-z0-9-_+]*)"; std::string attrRegex = R"([A-Za-z_][A-Za-z0-9-_+]*)";
static std::regex attrPathRegex(fmt(R"(%1%(\.%1%)*)", attrRegex)); static std::regex attrPathRegex(fmt(R"(%1%(\.%1%)*)", attrRegex));
std::vector<std::shared_ptr<Installable>> InstallablesCommand::parseInstallables(ref<Store> store, Strings ss) static std::vector<std::shared_ptr<Installable>> parseInstallables(
SourceExprCommand & cmd, ref<Store> store, Strings ss, bool useDefaultInstallables)
{ {
std::vector<std::shared_ptr<Installable>> result; std::vector<std::shared_ptr<Installable>> result;
if (ss.empty() && useDefaultInstallables()) { if (ss.empty() && useDefaultInstallables) {
if (file == "") if (cmd.file == "")
file = "."; cmd.file = ".";
ss = Strings{""}; ss = Strings{""};
} }
for (auto & s : ss) { for (auto & s : ss) {
if (s.compare(0, 1, "(") == 0) if (s.compare(0, 1, "(") == 0)
result.push_back(std::make_shared<InstallableExpr>(*this, s)); result.push_back(std::make_shared<InstallableExpr>(cmd, s));
else if (s.find("/") != std::string::npos) { else if (s.find("/") != std::string::npos) {
@ -218,7 +205,7 @@ std::vector<std::shared_ptr<Installable>> InstallablesCommand::parseInstallables
} }
else if (s == "" || std::regex_match(s, attrPathRegex)) else if (s == "" || std::regex_match(s, attrPathRegex))
result.push_back(std::make_shared<InstallableAttrPath>(*this, s)); result.push_back(std::make_shared<InstallableAttrPath>(cmd, s));
else else
throw UsageError("don't know what to do with argument '%s'", s); throw UsageError("don't know what to do with argument '%s'", s);
@ -250,7 +237,14 @@ PathSet InstallablesCommand::toStorePaths(ref<Store> store, ToStorePathsMode mod
void InstallablesCommand::prepare() void InstallablesCommand::prepare()
{ {
installables = parseInstallables(getStore(), _installables); installables = parseInstallables(*this, getStore(), _installables, useDefaultInstallables());
}
void InstallableCommand::prepare()
{
auto installables = parseInstallables(*this, getStore(), {_installable}, false);
assert(installables.size() == 1);
installable = installables.front();
} }
} }

View file

@ -2,10 +2,11 @@
#include "common-args.hh" #include "common-args.hh"
#include "shared.hh" #include "shared.hh"
#include "store-api.hh" #include "store-api.hh"
#include "progress-bar.hh"
using namespace nix; using namespace nix;
struct CmdLog : InstallablesCommand struct CmdLog : InstallableCommand
{ {
CmdLog() CmdLog()
{ {
@ -27,21 +28,22 @@ struct CmdLog : InstallablesCommand
subs.push_front(store); subs.push_front(store);
for (auto & inst : installables) { for (auto & b : installable->toBuildable(true)) {
for (auto & b : inst->toBuildable()) {
auto path = b.second.drvPath != "" ? b.second.drvPath : b.first; for (auto & sub : subs) {
bool found = false; auto log = b.second.drvPath != "" ? sub->getBuildLog(b.second.drvPath) : nullptr;
for (auto & sub : subs) { if (!log) {
auto log = sub->getBuildLog(path); log = sub->getBuildLog(b.first);
if (!log) continue; if (!log) continue;
std::cout << *log;
found = true;
break;
} }
if (!found) stopProgressBar();
throw Error("build log of path '%s' is not available", path); printInfo("got build log for '%s' from '%s'", b.first, sub->getUri());
std::cout << *log;
return;
} }
} }
throw Error("build log of '%s' is not available", installable->what());
} }
}; };

View file

@ -8,6 +8,7 @@
#include "shared.hh" #include "shared.hh"
#include "store-api.hh" #include "store-api.hh"
#include "progress-bar.hh" #include "progress-bar.hh"
#include "finally.hh"
extern std::string chrootHelperName; extern std::string chrootHelperName;
@ -84,6 +85,8 @@ void mainWrapped(int argc, char * * argv)
if (!args.command) args.showHelpAndExit(); if (!args.command) args.showHelpAndExit();
Finally f([]() { stopProgressBar(); });
if (isatty(STDERR_FILENO)) if (isatty(STDERR_FILENO))
startProgressBar(); startProgressBar();

View file

@ -116,7 +116,7 @@ public:
{ {
auto state(state_.lock()); auto state(state_.lock());
if (!state->active) return; if (!state->active) return;
state->active = true; state->active = false;
std::string status = getStatus(*state); std::string status = getStatus(*state);
writeToStderr("\r\e[K"); writeToStderr("\r\e[K");
if (status != "") if (status != "")