Skip to content

Commit

Permalink
Merge pull request #7748 from obsidiansystems/split-other-installables
Browse files Browse the repository at this point in the history
Keep splitting libcmd headers & files
  • Loading branch information
tomberek committed Feb 20, 2023
2 parents de71483 + 1bd03ad commit 924ef67
Show file tree
Hide file tree
Showing 21 changed files with 676 additions and 469 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ outputs/

*.a
*.o
*.o.tmp
*.so
*.dylib
*.dll
Expand Down
17 changes: 2 additions & 15 deletions src/libcmd/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "derivations.hh"
#include "nixexpr.hh"
#include "profiles.hh"
#include "repl.hh"

#include <nlohmann/json.hpp>

Expand Down Expand Up @@ -121,7 +122,7 @@ ref<EvalState> EvalCommand::getEvalState()
;

if (startReplOnEvalErrors) {
evalState->debugRepl = &runRepl;
evalState->debugRepl = &AbstractNixRepl::runSimple;
};
}
return ref<EvalState>(evalState);
Expand Down Expand Up @@ -218,20 +219,6 @@ void StorePathCommand::run(ref<Store> store, std::vector<StorePath> && storePath
run(store, *storePaths.begin());
}

Strings editorFor(const Path & file, uint32_t line)
{
auto editor = getEnv("EDITOR").value_or("cat");
auto args = tokenizeString<Strings>(editor);
if (line > 0 && (
editor.find("emacs") != std::string::npos ||
editor.find("nano") != std::string::npos ||
editor.find("vim") != std::string::npos ||
editor.find("kak") != std::string::npos))
args.push_back(fmt("+%d", line));
args.push_back(file);
return args;
}

MixProfile::MixProfile()
{
addFlag({
Expand Down
8 changes: 0 additions & 8 deletions src/libcmd/command.hh
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,6 @@ static RegisterCommand registerCommand2(std::vector<std::string> && name)
return RegisterCommand(std::move(name), [](){ return make_ref<T>(); });
}

/* Helper function to generate args that invoke $EDITOR on
filename:lineno. */
Strings editorFor(const Path & file, uint32_t line);

struct MixProfile : virtual StoreCommand
{
std::optional<Path> profile;
Expand Down Expand Up @@ -284,8 +280,4 @@ void printClosureDiff(
const StorePath & afterPath,
std::string_view indent);


void runRepl(
ref<EvalState> evalState,
const ValMap & extraEnv);
}
20 changes: 20 additions & 0 deletions src/libcmd/editor-for.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "util.hh"
#include "editor-for.hh"

namespace nix {

Strings editorFor(const Path & file, uint32_t line)
{
auto editor = getEnv("EDITOR").value_or("cat");
auto args = tokenizeString<Strings>(editor);
if (line > 0 && (
editor.find("emacs") != std::string::npos ||
editor.find("nano") != std::string::npos ||
editor.find("vim") != std::string::npos ||
editor.find("kak") != std::string::npos))
args.push_back(fmt("+%d", line));
args.push_back(file);
return args;
}

}
11 changes: 11 additions & 0 deletions src/libcmd/editor-for.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "types.hh"

namespace nix {

/* Helper function to generate args that invoke $EDITOR on
filename:lineno. */
Strings editorFor(const Path & file, uint32_t line);

}
109 changes: 109 additions & 0 deletions src/libcmd/installable-attr-path.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "globals.hh"
#include "installable-attr-path.hh"
#include "outputs-spec.hh"
#include "util.hh"
#include "command.hh"
#include "attr-path.hh"
#include "common-eval-args.hh"
#include "derivations.hh"
#include "eval-inline.hh"
#include "eval.hh"
#include "get-drvs.hh"
#include "store-api.hh"
#include "shared.hh"
#include "flake/flake.hh"
#include "eval-cache.hh"
#include "url.hh"
#include "registry.hh"
#include "build-result.hh"

#include <regex>
#include <queue>

#include <nlohmann/json.hpp>

namespace nix {

InstallableAttrPath::InstallableAttrPath(
ref<EvalState> state,
SourceExprCommand & cmd,
Value * v,
const std::string & attrPath,
ExtendedOutputsSpec extendedOutputsSpec)
: InstallableValue(state)
, cmd(cmd)
, v(allocRootValue(v))
, attrPath(attrPath)
, extendedOutputsSpec(std::move(extendedOutputsSpec))
{ }

std::pair<Value *, PosIdx> InstallableAttrPath::toValue(EvalState & state)
{
auto [vRes, pos] = findAlongAttrPath(state, attrPath, *cmd.getAutoArgs(state), **v);
state.forceValue(*vRes, pos);
return {vRes, pos};
}

DerivedPathsWithInfo InstallableAttrPath::toDerivedPaths()
{
auto v = toValue(*state).first;

Bindings & autoArgs = *cmd.getAutoArgs(*state);

DrvInfos drvInfos;
getDerivations(*state, *v, "", autoArgs, drvInfos, false);

// Backward compatibility hack: group results by drvPath. This
// helps keep .all output together.
std::map<StorePath, OutputsSpec> byDrvPath;

for (auto & drvInfo : drvInfos) {
auto drvPath = drvInfo.queryDrvPath();
if (!drvPath)
throw Error("'%s' is not a derivation", what());

auto newOutputs = std::visit(overloaded {
[&](const ExtendedOutputsSpec::Default & d) -> OutputsSpec {
std::set<std::string> outputsToInstall;
for (auto & output : drvInfo.queryOutputs(false, true))
outputsToInstall.insert(output.first);
return OutputsSpec::Names { std::move(outputsToInstall) };
},
[&](const ExtendedOutputsSpec::Explicit & e) -> OutputsSpec {
return e;
},
}, extendedOutputsSpec.raw());

auto [iter, didInsert] = byDrvPath.emplace(*drvPath, newOutputs);

if (!didInsert)
iter->second = iter->second.union_(newOutputs);
}

DerivedPathsWithInfo res;
for (auto & [drvPath, outputs] : byDrvPath)
res.push_back({
.path = DerivedPath::Built {
.drvPath = drvPath,
.outputs = outputs,
},
});

return res;
}

InstallableAttrPath InstallableAttrPath::parse(
ref<EvalState> state,
SourceExprCommand & cmd,
Value * v,
std::string_view prefix,
ExtendedOutputsSpec extendedOutputsSpec)
{
return {
state, cmd, v,
prefix == "." ? "" : std::string { prefix },
extendedOutputsSpec
};
}

}
56 changes: 56 additions & 0 deletions src/libcmd/installable-attr-path.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "globals.hh"
#include "installable-value.hh"
#include "outputs-spec.hh"
#include "util.hh"
#include "command.hh"
#include "attr-path.hh"
#include "common-eval-args.hh"
#include "derivations.hh"
#include "eval-inline.hh"
#include "eval.hh"
#include "get-drvs.hh"
#include "store-api.hh"
#include "shared.hh"
#include "eval-cache.hh"
#include "url.hh"
#include "registry.hh"
#include "build-result.hh"

#include <regex>
#include <queue>

#include <nlohmann/json.hpp>

namespace nix {

class InstallableAttrPath : public InstallableValue
{
SourceExprCommand & cmd;
RootValue v;
std::string attrPath;
ExtendedOutputsSpec extendedOutputsSpec;

InstallableAttrPath(
ref<EvalState> state,
SourceExprCommand & cmd,
Value * v,
const std::string & attrPath,
ExtendedOutputsSpec extendedOutputsSpec);

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

std::pair<Value *, PosIdx> toValue(EvalState & state) override;

DerivedPathsWithInfo toDerivedPaths() override;

public:

static InstallableAttrPath parse(
ref<EvalState> state,
SourceExprCommand & cmd,
Value * v,
std::string_view prefix,
ExtendedOutputsSpec extendedOutputsSpec);
};

}
Loading

0 comments on commit 924ef67

Please sign in to comment.