Move code around

This commit is contained in:
Eelco Dolstra 2017-04-25 12:06:32 +02:00
parent 6267d74889
commit c769841bc4
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
8 changed files with 68 additions and 80 deletions

View file

@ -1,12 +1,11 @@
#include "command.hh" #include "command.hh"
#include "common-args.hh" #include "common-args.hh"
#include "installables.hh"
#include "shared.hh" #include "shared.hh"
#include "store-api.hh" #include "store-api.hh"
using namespace nix; using namespace nix;
struct CmdBuild : MixDryRun, MixInstallables struct CmdBuild : MixDryRun, InstallablesCommand
{ {
CmdBuild() CmdBuild()
{ {

View file

@ -4,6 +4,9 @@
namespace nix { namespace nix {
struct Value;
class EvalState;
/* A command is an argument parser that can be executed by calling its /* A command is an argument parser that can be executed by calling its
run() method. */ run() method. */
struct Command : virtual Args struct Command : virtual Args
@ -61,6 +64,57 @@ public:
void run(ref<Store> store) override; void run(ref<Store> store) override;
}; };
struct Installable
{
virtual std::string what() = 0;
virtual PathSet toBuildable()
{
throw Error("argument %s cannot be built", what());
}
virtual Value * toValue(EvalState & state)
{
throw Error("argument %s cannot be evaluated", what());
}
};
/* A command that operates on a list of "installables", which can be
store paths, attribute paths, Nix expressions, etc. */
struct InstallablesCommand : virtual Args, StoreCommand
{
std::vector<std::shared_ptr<Installable>> installables;
Path file;
InstallablesCommand()
{
mkFlag('f', "file", "file", "evaluate FILE rather than the default", &file);
expectArgs("installables", &_installables);
}
/* Return a value representing the Nix expression from which we
are installing. This is either the file specified by --file,
or an attribute set constructed from $NIX_PATH, e.g. { nixpkgs
= import ...; bla = import ...; }. */
Value * getSourceExpr(EvalState & state);
std::vector<std::shared_ptr<Installable>> parseInstallables(ref<Store> store, Strings installables);
PathSet buildInstallables(ref<Store> store, bool dryRun);
ref<EvalState> getEvalState();
void prepare() override;
private:
Strings _installables;
std::shared_ptr<EvalState> evalState;
Value * vSourceExpr = 0;
};
typedef std::map<std::string, ref<Command>> Commands; typedef std::map<std::string, ref<Command>> Commands;
/* An argument parser that supports multiple subcommands, /* An argument parser that supports multiple subcommands,

View file

@ -1,6 +1,5 @@
#include "command.hh" #include "command.hh"
#include "common-args.hh" #include "common-args.hh"
#include "installables.hh"
#include "shared.hh" #include "shared.hh"
#include "store-api.hh" #include "store-api.hh"
#include "eval.hh" #include "eval.hh"
@ -9,7 +8,7 @@
using namespace nix; using namespace nix;
struct CmdEval : MixJSON, MixInstallables struct CmdEval : MixJSON, InstallablesCommand
{ {
std::string name() override std::string name() override
{ {

View file

@ -1,10 +1,10 @@
#include "command.hh"
#include "attr-path.hh" #include "attr-path.hh"
#include "common-opts.hh" #include "common-opts.hh"
#include "derivations.hh" #include "derivations.hh"
#include "eval-inline.hh" #include "eval-inline.hh"
#include "eval.hh" #include "eval.hh"
#include "get-drvs.hh" #include "get-drvs.hh"
#include "installables.hh"
#include "store-api.hh" #include "store-api.hh"
#include "shared.hh" #include "shared.hh"
@ -12,7 +12,7 @@
namespace nix { namespace nix {
Value * MixInstallables::getSourceExpr(EvalState & state) Value * InstallablesCommand::getSourceExpr(EvalState & state)
{ {
if (vSourceExpr) return vSourceExpr; if (vSourceExpr) return vSourceExpr;
@ -89,10 +89,10 @@ struct InstallableStorePath : Installable
struct InstallableExpr : Installable struct InstallableExpr : Installable
{ {
MixInstallables & installables; InstallablesCommand & installables;
std::string text; std::string text;
InstallableExpr(MixInstallables & installables, const std::string & text) InstallableExpr(InstallablesCommand & installables, const std::string & text)
: installables(installables), text(text) { } : installables(installables), text(text) { }
std::string what() override { return text; } std::string what() override { return text; }
@ -128,10 +128,10 @@ struct InstallableExpr : Installable
struct InstallableAttrPath : Installable struct InstallableAttrPath : Installable
{ {
MixInstallables & installables; InstallablesCommand & installables;
std::string attrPath; std::string attrPath;
InstallableAttrPath(MixInstallables & installables, const std::string & attrPath) InstallableAttrPath(InstallablesCommand & installables, const std::string & attrPath)
: installables(installables), attrPath(attrPath) : installables(installables), attrPath(attrPath)
{ } { }
@ -177,7 +177,7 @@ 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>> MixInstallables::parseInstallables(ref<Store> store, Strings installables) std::vector<std::shared_ptr<Installable>> InstallablesCommand::parseInstallables(ref<Store> store, Strings installables)
{ {
std::vector<std::shared_ptr<Installable>> result; std::vector<std::shared_ptr<Installable>> result;
@ -212,7 +212,7 @@ std::vector<std::shared_ptr<Installable>> MixInstallables::parseInstallables(ref
return result; return result;
} }
PathSet MixInstallables::buildInstallables(ref<Store> store, bool dryRun) PathSet InstallablesCommand::buildInstallables(ref<Store> store, bool dryRun)
{ {
PathSet buildables; PathSet buildables;
@ -229,14 +229,14 @@ PathSet MixInstallables::buildInstallables(ref<Store> store, bool dryRun)
return buildables; return buildables;
} }
ref<EvalState> MixInstallables::getEvalState() ref<EvalState> InstallablesCommand::getEvalState()
{ {
if (!evalState) if (!evalState)
evalState = std::make_shared<EvalState>(Strings{}, getStore()); evalState = std::make_shared<EvalState>(Strings{}, getStore());
return ref<EvalState>(evalState); return ref<EvalState>(evalState);
} }
void MixInstallables::prepare() void InstallablesCommand::prepare()
{ {
installables = parseInstallables(getStore(), _installables); installables = parseInstallables(getStore(), _installables);
} }

View file

@ -1,61 +0,0 @@
#pragma once
#include "args.hh"
#include "command.hh"
namespace nix {
struct Value;
class EvalState;
class Expr;
struct Installable
{
virtual std::string what() = 0;
virtual PathSet toBuildable()
{
throw Error("argument %s cannot be built", what());
}
virtual Value * toValue(EvalState & state)
{
throw Error("argument %s cannot be evaluated", what());
}
};
struct MixInstallables : virtual Args, StoreCommand
{
std::vector<std::shared_ptr<Installable>> installables;
Path file;
MixInstallables()
{
mkFlag('f', "file", "file", "evaluate FILE rather than the default", &file);
expectArgs("installables", &_installables);
}
/* Return a value representing the Nix expression from which we
are installing. This is either the file specified by --file,
or an attribute set constructed from $NIX_PATH, e.g. { nixpkgs
= import ...; bla = import ...; }. */
Value * getSourceExpr(EvalState & state);
std::vector<std::shared_ptr<Installable>> parseInstallables(ref<Store> store, Strings installables);
PathSet buildInstallables(ref<Store> store, bool dryRun);
ref<EvalState> getEvalState();
void prepare() override;
private:
Strings _installables;
std::shared_ptr<EvalState> evalState;
Value * vSourceExpr = 0;
};
}

View file

@ -1,12 +1,11 @@
#include "command.hh" #include "command.hh"
#include "common-args.hh" #include "common-args.hh"
#include "installables.hh"
#include "shared.hh" #include "shared.hh"
#include "store-api.hh" #include "store-api.hh"
using namespace nix; using namespace nix;
struct CmdLog : MixInstallables struct CmdLog : InstallablesCommand
{ {
CmdLog() CmdLog()
{ {

View file

@ -1,6 +1,5 @@
#include "command.hh" #include "command.hh"
#include "common-args.hh" #include "common-args.hh"
#include "installables.hh"
#include "shared.hh" #include "shared.hh"
#include "store-api.hh" #include "store-api.hh"
#include "derivations.hh" #include "derivations.hh"
@ -13,7 +12,7 @@
using namespace nix; using namespace nix;
struct CmdRun : MixInstallables struct CmdRun : InstallablesCommand
{ {
CmdRun() CmdRun()
{ {

View file

@ -1,6 +1,5 @@
#include "command.hh" #include "command.hh"
#include "common-args.hh" #include "common-args.hh"
#include "installables.hh"
#include "shared.hh" #include "shared.hh"
#include "store-api.hh" #include "store-api.hh"
#include "json.hh" #include "json.hh"