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,15 +34,14 @@ 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); auto pos = state->forceString(*v2);
@ -72,7 +71,6 @@ struct CmdEdit : InstallablesCommand
throw SysError("cannot run editor '%s'", editor); throw SysError("cannot run editor '%s'", editor);
} }
}
}; };
static RegisterCommand r1(make_ref<CmdEdit>()); static RegisterCommand r1(make_ref<CmdEdit>());

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;
bool found = false;
for (auto & sub : subs) { for (auto & sub : subs) {
auto log = sub->getBuildLog(path); auto log = b.second.drvPath != "" ? sub->getBuildLog(b.second.drvPath) : nullptr;
if (!log) {
log = sub->getBuildLog(b.first);
if (!log) continue; if (!log) continue;
}
stopProgressBar();
printInfo("got build log for '%s' from '%s'", b.first, sub->getUri());
std::cout << *log; std::cout << *log;
found = true; return;
break;
}
if (!found)
throw Error("build log of path '%s' is not available", path);
} }
} }
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 != "")