Skip to content

Commit e9c07a3

Browse files
committed
nix edit / log: Operate on a single Installable
1 parent 9b82ecb commit e9c07a3

File tree

6 files changed

+100
-89
lines changed

6 files changed

+100
-89
lines changed

src/nix/command.hh

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct Installable
5151
{
5252
virtual std::string what() = 0;
5353

54-
virtual Buildables toBuildable()
54+
virtual Buildables toBuildable(bool singular = false)
5555
{
5656
throw Error("argument '%s' cannot be built", what());
5757
}
@@ -97,8 +97,6 @@ struct InstallablesCommand : virtual Args, SourceExprCommand
9797
expectArgs("installables", &_installables);
9898
}
9999

100-
std::vector<std::shared_ptr<Installable>> parseInstallables(ref<Store> store, Strings ss);
101-
102100
enum ToStorePathsMode { Build, NoBuild, DryRun };
103101

104102
PathSet toStorePaths(ref<Store> store, ToStorePathsMode mode);
@@ -112,6 +110,22 @@ private:
112110
Strings _installables;
113111
};
114112

113+
struct InstallableCommand : virtual Args, SourceExprCommand
114+
{
115+
std::shared_ptr<Installable> installable;
116+
117+
InstallableCommand()
118+
{
119+
expectArg("installable", &_installable);
120+
}
121+
122+
void prepare() override;
123+
124+
private:
125+
126+
std::string _installable;
127+
};
128+
115129
/* A command that operates on zero or more store paths. */
116130
struct StorePathsCommand : public InstallablesCommand
117131
{

src/nix/edit.cc

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
using namespace nix;
1010

11-
struct CmdEdit : InstallablesCommand
11+
struct CmdEdit : InstallableCommand
1212
{
1313
std::string name() override
1414
{
@@ -34,44 +34,42 @@ struct CmdEdit : InstallablesCommand
3434
{
3535
auto state = getEvalState();
3636

37-
for (auto & i : installables) {
38-
auto v = i->toValue(*state);
37+
auto v = installable->toValue(*state);
3938

40-
Value * v2;
41-
try {
42-
auto dummyArgs = state->allocBindings(0);
43-
v2 = findAlongAttrPath(*state, "meta.position", *dummyArgs, *v);
44-
} catch (Error &) {
45-
throw Error("package '%s' has no source location information", i->what());
46-
}
39+
Value * v2;
40+
try {
41+
auto dummyArgs = state->allocBindings(0);
42+
v2 = findAlongAttrPath(*state, "meta.position", *dummyArgs, *v);
43+
} catch (Error &) {
44+
throw Error("package '%s' has no source location information", installable->what());
45+
}
4746

48-
auto pos = state->forceString(*v2);
49-
debug("position is %s", pos);
47+
auto pos = state->forceString(*v2);
48+
debug("position is %s", pos);
5049

51-
auto colon = pos.rfind(':');
52-
if (colon == std::string::npos)
53-
throw Error("cannot parse meta.position attribute '%s'", pos);
50+
auto colon = pos.rfind(':');
51+
if (colon == std::string::npos)
52+
throw Error("cannot parse meta.position attribute '%s'", pos);
5453

55-
std::string filename(pos, 0, colon);
56-
int lineno = std::stoi(std::string(pos, colon + 1));
54+
std::string filename(pos, 0, colon);
55+
int lineno = std::stoi(std::string(pos, colon + 1));
5756

58-
auto editor = getEnv("EDITOR", "cat");
57+
auto editor = getEnv("EDITOR", "cat");
5958

60-
Strings args{editor};
59+
Strings args{editor};
6160

62-
if (editor.find("emacs") != std::string::npos ||
63-
editor.find("nano") != std::string::npos ||
64-
editor.find("vim") != std::string::npos)
65-
args.push_back(fmt("+%d", lineno));
61+
if (editor.find("emacs") != std::string::npos ||
62+
editor.find("nano") != std::string::npos ||
63+
editor.find("vim") != std::string::npos)
64+
args.push_back(fmt("+%d", lineno));
6665

67-
args.push_back(filename);
66+
args.push_back(filename);
6867

69-
stopProgressBar();
68+
stopProgressBar();
7069

71-
execvp(editor.c_str(), stringsToCharPtrs(args).data());
70+
execvp(editor.c_str(), stringsToCharPtrs(args).data());
7271

73-
throw SysError("cannot run editor '%s'", editor);
74-
}
72+
throw SysError("cannot run editor '%s'", editor);
7573
}
7674
};
7775

src/nix/installables.cc

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct InstallableStoreDrv : Installable
7878

7979
std::string what() override { return storePath; }
8080

81-
Buildables toBuildable() override
81+
Buildables toBuildable(bool singular) override
8282
{
8383
return {{storePath, {}}};
8484
}
@@ -92,25 +92,21 @@ struct InstallableStorePath : Installable
9292

9393
std::string what() override { return storePath; }
9494

95-
Buildables toBuildable() override
95+
Buildables toBuildable(bool singular) override
9696
{
9797
return {{storePath, {}}};
9898
}
9999
};
100100

101-
struct InstallableExpr : Installable
101+
struct InstallableValue : Installable
102102
{
103-
InstallablesCommand & installables;
104-
std::string text;
105-
106-
InstallableExpr(InstallablesCommand & installables, const std::string & text)
107-
: installables(installables), text(text) { }
103+
SourceExprCommand & cmd;
108104

109-
std::string what() override { return text; }
105+
InstallableValue(SourceExprCommand & cmd) : cmd(cmd) { }
110106

111-
Buildables toBuildable() override
107+
Buildables toBuildable(bool singular) override
112108
{
113-
auto state = installables.getEvalState();
109+
auto state = cmd.getEvalState();
114110

115111
auto v = toValue(*state);
116112

@@ -121,6 +117,9 @@ struct InstallableExpr : Installable
121117
DrvInfos drvs;
122118
getDerivations(*state, *v, "", autoArgs, drvs, false);
123119

120+
if (singular && drvs.size() != 1)
121+
throw Error("installable '%s' evaluates to %d derivations, where only one is expected", what(), drvs.size());
122+
124123
Buildables res;
125124

126125
for (auto & drv : drvs)
@@ -129,6 +128,16 @@ struct InstallableExpr : Installable
129128

130129
return res;
131130
}
131+
};
132+
133+
struct InstallableExpr : InstallableValue
134+
{
135+
std::string text;
136+
137+
InstallableExpr(SourceExprCommand & cmd, const std::string & text)
138+
: InstallableValue(cmd), text(text) { }
139+
140+
std::string what() override { return text; }
132141

133142
Value * toValue(EvalState & state) override
134143
{
@@ -138,42 +147,19 @@ struct InstallableExpr : Installable
138147
}
139148
};
140149

141-
struct InstallableAttrPath : Installable
150+
struct InstallableAttrPath : InstallableValue
142151
{
143-
InstallablesCommand & installables;
144152
std::string attrPath;
145153

146-
InstallableAttrPath(InstallablesCommand & installables, const std::string & attrPath)
147-
: installables(installables), attrPath(attrPath)
154+
InstallableAttrPath(SourceExprCommand & cmd, const std::string & attrPath)
155+
: InstallableValue(cmd), attrPath(attrPath)
148156
{ }
149157

150158
std::string what() override { return attrPath; }
151159

152-
Buildables toBuildable() override
153-
{
154-
auto state = installables.getEvalState();
155-
156-
auto v = toValue(*state);
157-
158-
// FIXME
159-
std::map<string, string> autoArgs_;
160-
Bindings & autoArgs(*evalAutoArgs(*state, autoArgs_));
161-
162-
DrvInfos drvs;
163-
getDerivations(*state, *v, "", autoArgs, drvs, false);
164-
165-
Buildables res;
166-
167-
for (auto & drv : drvs)
168-
for (auto & output : drv.queryOutputs())
169-
res.emplace(output.second, Whence{output.first, drv.queryDrvPath()});
170-
171-
return res;
172-
}
173-
174160
Value * toValue(EvalState & state) override
175161
{
176-
auto source = installables.getSourceExpr(state);
162+
auto source = cmd.getSourceExpr(state);
177163

178164
// FIXME
179165
std::map<string, string> autoArgs_;
@@ -190,20 +176,21 @@ struct InstallableAttrPath : Installable
190176
std::string attrRegex = R"([A-Za-z_][A-Za-z0-9-_+]*)";
191177
static std::regex attrPathRegex(fmt(R"(%1%(\.%1%)*)", attrRegex));
192178

193-
std::vector<std::shared_ptr<Installable>> InstallablesCommand::parseInstallables(ref<Store> store, Strings ss)
179+
static std::vector<std::shared_ptr<Installable>> parseInstallables(
180+
SourceExprCommand & cmd, ref<Store> store, Strings ss, bool useDefaultInstallables)
194181
{
195182
std::vector<std::shared_ptr<Installable>> result;
196183

197-
if (ss.empty() && useDefaultInstallables()) {
198-
if (file == "")
199-
file = ".";
184+
if (ss.empty() && useDefaultInstallables) {
185+
if (cmd.file == "")
186+
cmd.file = ".";
200187
ss = Strings{""};
201188
}
202189

203190
for (auto & s : ss) {
204191

205192
if (s.compare(0, 1, "(") == 0)
206-
result.push_back(std::make_shared<InstallableExpr>(*this, s));
193+
result.push_back(std::make_shared<InstallableExpr>(cmd, s));
207194

208195
else if (s.find("/") != std::string::npos) {
209196

@@ -218,7 +205,7 @@ std::vector<std::shared_ptr<Installable>> InstallablesCommand::parseInstallables
218205
}
219206

220207
else if (s == "" || std::regex_match(s, attrPathRegex))
221-
result.push_back(std::make_shared<InstallableAttrPath>(*this, s));
208+
result.push_back(std::make_shared<InstallableAttrPath>(cmd, s));
222209

223210
else
224211
throw UsageError("don't know what to do with argument '%s'", s);
@@ -250,7 +237,14 @@ PathSet InstallablesCommand::toStorePaths(ref<Store> store, ToStorePathsMode mod
250237

251238
void InstallablesCommand::prepare()
252239
{
253-
installables = parseInstallables(getStore(), _installables);
240+
installables = parseInstallables(*this, getStore(), _installables, useDefaultInstallables());
241+
}
242+
243+
void InstallableCommand::prepare()
244+
{
245+
auto installables = parseInstallables(*this, getStore(), {_installable}, false);
246+
assert(installables.size() == 1);
247+
installable = installables.front();
254248
}
255249

256250
}

src/nix/log.cc

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
#include "common-args.hh"
33
#include "shared.hh"
44
#include "store-api.hh"
5+
#include "progress-bar.hh"
56

67
using namespace nix;
78

8-
struct CmdLog : InstallablesCommand
9+
struct CmdLog : InstallableCommand
910
{
1011
CmdLog()
1112
{
@@ -27,21 +28,22 @@ struct CmdLog : InstallablesCommand
2728

2829
subs.push_front(store);
2930

30-
for (auto & inst : installables) {
31-
for (auto & b : inst->toBuildable()) {
32-
auto path = b.second.drvPath != "" ? b.second.drvPath : b.first;
33-
bool found = false;
34-
for (auto & sub : subs) {
35-
auto log = sub->getBuildLog(path);
31+
for (auto & b : installable->toBuildable(true)) {
32+
33+
for (auto & sub : subs) {
34+
auto log = b.second.drvPath != "" ? sub->getBuildLog(b.second.drvPath) : nullptr;
35+
if (!log) {
36+
log = sub->getBuildLog(b.first);
3637
if (!log) continue;
37-
std::cout << *log;
38-
found = true;
39-
break;
4038
}
41-
if (!found)
42-
throw Error("build log of path '%s' is not available", path);
39+
stopProgressBar();
40+
printInfo("got build log for '%s' from '%s'", b.first, sub->getUri());
41+
std::cout << *log;
42+
return;
4343
}
4444
}
45+
46+
throw Error("build log of '%s' is not available", installable->what());
4547
}
4648
};
4749

src/nix/main.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "shared.hh"
99
#include "store-api.hh"
1010
#include "progress-bar.hh"
11+
#include "finally.hh"
1112

1213
extern std::string chrootHelperName;
1314

@@ -84,6 +85,8 @@ void mainWrapped(int argc, char * * argv)
8485

8586
if (!args.command) args.showHelpAndExit();
8687

88+
Finally f([]() { stopProgressBar(); });
89+
8790
if (isatty(STDERR_FILENO))
8891
startProgressBar();
8992

src/nix/progress-bar.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class ProgressBar : public Logger
116116
{
117117
auto state(state_.lock());
118118
if (!state->active) return;
119-
state->active = true;
119+
state->active = false;
120120
std::string status = getStatus(*state);
121121
writeToStderr("\r\e[K");
122122
if (status != "")

0 commit comments

Comments
 (0)