Skip to content

Commit eaced1e

Browse files
committed
Move FlakeCommand into a header, allow separate registration of subcommands
This allows us to start splitting up src/nix/flake.cc.
1 parent df2d5f2 commit eaced1e

File tree

3 files changed

+67
-53
lines changed

3 files changed

+67
-53
lines changed

src/libcmd/include/nix/cmd/common-eval-args.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "nix/util/canon-path.hh"
66
#include "nix/main/common-args.hh"
77
#include "nix/expr/search-path.hh"
8+
#include "nix/expr/eval-settings.hh"
89

910
#include <filesystem>
1011

@@ -15,10 +16,8 @@ class Store;
1516
namespace fetchers { struct Settings; }
1617

1718
class EvalState;
18-
struct EvalSettings;
1919
struct CompatibilitySettings;
2020
class Bindings;
21-
struct SourcePath;
2221

2322
namespace flake { struct Settings; }
2423

src/nix/flake-command.hh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include "nix/cmd/command.hh"
4+
#include "nix/cmd/installable-flake.hh"
5+
#include "nix/flake/flake.hh"
6+
7+
namespace nix {
8+
9+
using namespace nix::flake;
10+
11+
class FlakeCommand : virtual Args, public MixFlakeOptions
12+
{
13+
protected:
14+
std::string flakeUrl = ".";
15+
16+
public:
17+
18+
FlakeCommand();
19+
20+
FlakeRef getFlakeRef();
21+
22+
LockedFlake lockFlake();
23+
24+
std::vector<FlakeRef> getFlakeRefsForCompletion() override;
25+
};
26+
27+
}

src/nix/flake.cc

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#include "nix/cmd/command.hh"
2-
#include "nix/cmd/installable-flake.hh"
1+
#include "flake-command.hh"
32
#include "nix/main/common-args.hh"
43
#include "nix/main/shared.hh"
54
#include "nix/expr/eval.hh"
65
#include "nix/expr/eval-inline.hh"
76
#include "nix/expr/eval-settings.hh"
8-
#include "nix/flake/flake.hh"
97
#include "nix/expr/get-drvs.hh"
108
#include "nix/util/signals.hh"
119
#include "nix/store/store-open.hh"
@@ -33,43 +31,36 @@ using namespace nix::flake;
3331
using json = nlohmann::json;
3432

3533
struct CmdFlakeUpdate;
36-
class FlakeCommand : virtual Args, public MixFlakeOptions
37-
{
38-
protected:
39-
std::string flakeUrl = ".";
40-
41-
public:
4234

43-
FlakeCommand()
44-
{
45-
expectArgs({
46-
.label = "flake-url",
47-
.optional = true,
48-
.handler = {&flakeUrl},
49-
.completer = {[&](AddCompletions & completions, size_t, std::string_view prefix) {
50-
completeFlakeRef(completions, getStore(), prefix);
51-
}}
52-
});
53-
}
35+
FlakeCommand::FlakeCommand()
36+
{
37+
expectArgs({
38+
.label = "flake-url",
39+
.optional = true,
40+
.handler = {&flakeUrl},
41+
.completer = {[&](AddCompletions & completions, size_t, std::string_view prefix) {
42+
completeFlakeRef(completions, getStore(), prefix);
43+
}}
44+
});
45+
}
5446

55-
FlakeRef getFlakeRef()
56-
{
57-
return parseFlakeRef(fetchSettings, flakeUrl, std::filesystem::current_path().string()); //FIXME
58-
}
47+
FlakeRef FlakeCommand::getFlakeRef()
48+
{
49+
return parseFlakeRef(fetchSettings, flakeUrl, std::filesystem::current_path().string()); //FIXME
50+
}
5951

60-
LockedFlake lockFlake()
61-
{
62-
return flake::lockFlake(flakeSettings, *getEvalState(), getFlakeRef(), lockFlags);
63-
}
52+
LockedFlake FlakeCommand::lockFlake()
53+
{
54+
return flake::lockFlake(flakeSettings, *getEvalState(), getFlakeRef(), lockFlags);
55+
}
6456

65-
std::vector<FlakeRef> getFlakeRefsForCompletion() override
66-
{
67-
return {
68-
// Like getFlakeRef but with expandTilde called first
69-
parseFlakeRef(fetchSettings, expandTilde(flakeUrl), std::filesystem::current_path().string())
70-
};
71-
}
72-
};
57+
std::vector<FlakeRef> FlakeCommand::getFlakeRefsForCompletion()
58+
{
59+
return {
60+
// Like getFlakeRef but with expandTilde called first
61+
parseFlakeRef(fetchSettings, expandTilde(flakeUrl), std::filesystem::current_path().string())
62+
};
63+
}
7364

7465
struct CmdFlakeUpdate : FlakeCommand
7566
{
@@ -1528,21 +1519,7 @@ struct CmdFlakePrefetch : FlakeCommand, MixJSON
15281519
struct CmdFlake : NixMultiCommand
15291520
{
15301521
CmdFlake()
1531-
: NixMultiCommand(
1532-
"flake",
1533-
{
1534-
{"update", []() { return make_ref<CmdFlakeUpdate>(); }},
1535-
{"lock", []() { return make_ref<CmdFlakeLock>(); }},
1536-
{"metadata", []() { return make_ref<CmdFlakeMetadata>(); }},
1537-
{"info", []() { return make_ref<CmdFlakeInfo>(); }},
1538-
{"check", []() { return make_ref<CmdFlakeCheck>(); }},
1539-
{"init", []() { return make_ref<CmdFlakeInit>(); }},
1540-
{"new", []() { return make_ref<CmdFlakeNew>(); }},
1541-
{"clone", []() { return make_ref<CmdFlakeClone>(); }},
1542-
{"archive", []() { return make_ref<CmdFlakeArchive>(); }},
1543-
{"show", []() { return make_ref<CmdFlakeShow>(); }},
1544-
{"prefetch", []() { return make_ref<CmdFlakePrefetch>(); }},
1545-
})
1522+
: NixMultiCommand("flake", RegisterCommand::getCommandsFor({"flake"}))
15461523
{
15471524
}
15481525

@@ -1566,3 +1543,14 @@ struct CmdFlake : NixMultiCommand
15661543
};
15671544

15681545
static auto rCmdFlake = registerCommand<CmdFlake>("flake");
1546+
static auto rCmdFlakeArchive = registerCommand2<CmdFlakeArchive>({"flake", "archive"});
1547+
static auto rCmdFlakeCheck = registerCommand2<CmdFlakeCheck>({"flake", "check"});
1548+
static auto rCmdFlakeClone = registerCommand2<CmdFlakeClone>({"flake", "clone"});
1549+
static auto rCmdFlakeInfo = registerCommand2<CmdFlakeInfo>({"flake", "info"});
1550+
static auto rCmdFlakeInit = registerCommand2<CmdFlakeInit>({"flake", "init"});
1551+
static auto rCmdFlakeLock = registerCommand2<CmdFlakeLock>({"flake", "lock"});
1552+
static auto rCmdFlakeMetadata = registerCommand2<CmdFlakeMetadata>({"flake", "metadata"});
1553+
static auto rCmdFlakeNew = registerCommand2<CmdFlakeNew>({"flake", "new"});
1554+
static auto rCmdFlakePrefetch = registerCommand2<CmdFlakePrefetch>({"flake", "prefetch"});
1555+
static auto rCmdFlakeShow = registerCommand2<CmdFlakeShow>({"flake", "show"});
1556+
static auto rCmdFlakeUpdate = registerCommand2<CmdFlakeUpdate>({"flake", "update"});

0 commit comments

Comments
 (0)