Make DerivedPathWithHints a newtype

This allows us to namespace its constructors under it.
This commit is contained in:
John Ericson 2021-04-05 10:05:21 -04:00
parent 9b805d36ac
commit 179582872d
8 changed files with 44 additions and 31 deletions

View file

@ -170,10 +170,10 @@ void MixProfile::updateProfile(const DerivedPathsWithHints & buildables)
for (auto & buildable : buildables) {
std::visit(overloaded {
[&](DerivedPathOpaque bo) {
[&](DerivedPathWithHints::Opaque bo) {
result.push_back(bo.path);
},
[&](DerivedPathWithHintsBuilt bfd) {
[&](DerivedPathWithHints::Built bfd) {
for (auto & output : bfd.outputs) {
/* Output path should be known because we just tried to
build it. */
@ -181,7 +181,7 @@ void MixProfile::updateProfile(const DerivedPathsWithHints & buildables)
result.push_back(*output.second);
}
},
}, buildable);
}, buildable.raw());
}
if (result.size() != 1)

View file

@ -329,14 +329,14 @@ struct InstallableStorePath : Installable
for (auto & [name, output] : drv.outputsAndOptPaths(*store))
outputs.emplace(name, output.second);
return {
DerivedPathWithHintsBuilt {
DerivedPathWithHints::Built {
.drvPath = storePath,
.outputs = std::move(outputs)
}
};
} else {
return {
DerivedPathOpaque {
DerivedPathWithHints::Opaque {
.path = storePath,
}
};
@ -364,7 +364,7 @@ DerivedPathsWithHints InstallableValue::toDerivedPathsWithHints()
}
for (auto & i : drvsToOutputs)
res.push_back(DerivedPathWithHintsBuilt { i.first, i.second });
res.push_back(DerivedPathWithHints::Built { i.first, i.second });
return res;
}
@ -684,17 +684,17 @@ DerivedPathsWithHints build(ref<Store> store, Realise mode,
for (auto & i : installables) {
for (auto & b : i->toDerivedPathsWithHints()) {
std::visit(overloaded {
[&](DerivedPathOpaque bo) {
[&](DerivedPathWithHints::Opaque bo) {
pathsToBuild.push_back(bo);
},
[&](DerivedPathWithHintsBuilt bfd) {
[&](DerivedPathWithHints::Built bfd) {
StringSet outputNames;
for (auto & output : bfd.outputs)
outputNames.insert(output.first);
pathsToBuild.push_back(
DerivedPath::Built{bfd.drvPath, outputNames});
},
}, b);
}, b.raw());
buildables.push_back(std::move(b));
}
}
@ -717,10 +717,10 @@ std::set<RealisedPath> toRealisedPaths(
if (operateOn == OperateOn::Output) {
for (auto & b : build(store, mode, installables))
std::visit(overloaded {
[&](DerivedPathOpaque bo) {
[&](DerivedPathWithHints::Opaque bo) {
res.insert(bo.path);
},
[&](DerivedPathWithHintsBuilt bfd) {
[&](DerivedPathWithHints::Built bfd) {
auto drv = store->readDerivation(bfd.drvPath);
auto outputHashes = staticOutputHashes(*store, drv);
for (auto & output : bfd.outputs) {
@ -745,14 +745,14 @@ std::set<RealisedPath> toRealisedPaths(
}
}
},
}, b);
}, b.raw());
} else {
if (mode == Realise::Nothing)
settings.readOnlyMode = true;
for (auto & i : installables)
for (auto & b : i->toDerivedPathsWithHints())
if (auto bfd = std::get_if<DerivedPathWithHintsBuilt>(&b))
if (auto bfd = std::get_if<DerivedPathWithHints::Built>(&b))
res.insert(bfd->drvPath);
}
@ -789,7 +789,7 @@ StorePathSet toDerivations(ref<Store> store,
for (auto & i : installables)
for (auto & b : i->toDerivedPathsWithHints())
std::visit(overloaded {
[&](DerivedPathOpaque bo) {
[&](DerivedPathWithHints::Opaque bo) {
if (!useDeriver)
throw Error("argument '%s' did not evaluate to a derivation", i->what());
auto derivers = store->queryValidDerivers(bo.path);
@ -798,10 +798,10 @@ StorePathSet toDerivations(ref<Store> store,
// FIXME: use all derivers?
drvPaths.insert(*derivers.begin());
},
[&](DerivedPathWithHintsBuilt bfd) {
[&](DerivedPathWithHints::Built bfd) {
drvPaths.insert(bfd.drvPath);
},
}, b);
}, b.raw());
return drvPaths;
}

View file

@ -11,7 +11,7 @@ nlohmann::json DerivedPath::Opaque::toJSON(ref<Store> store) const {
return res;
}
nlohmann::json DerivedPathWithHintsBuilt::toJSON(ref<Store> store) const {
nlohmann::json DerivedPathWithHints::Built::toJSON(ref<Store> store) const {
nlohmann::json res;
res["drvPath"] = store->printStorePath(drvPath);
for (const auto& [output, path] : outputs) {
@ -25,7 +25,7 @@ nlohmann::json derivedPathsWithHintsToJSON(const DerivedPathsWithHints & buildab
for (const DerivedPathWithHints & buildable : buildables) {
std::visit([&res, store](const auto & buildable) {
res.push_back(buildable.toJSON(store));
}, buildable);
}, buildable.raw());
}
return res;
}

View file

@ -56,11 +56,24 @@ struct DerivedPathWithHintsBuilt {
static DerivedPathWithHintsBuilt parse(const Store & store, std::string_view);
};
using DerivedPathWithHints = std::variant<
using _DerivedPathWithHintsRaw = std::variant<
DerivedPath::Opaque,
DerivedPathWithHintsBuilt
>;
struct DerivedPathWithHints : _DerivedPathWithHintsRaw {
using Raw = _DerivedPathWithHintsRaw;
using Raw::Raw;
using Opaque = DerivedPathOpaque;
using Built = DerivedPathWithHintsBuilt;
inline const Raw & raw() const {
return static_cast<const Raw &>(*this);
}
};
typedef std::vector<DerivedPathWithHints> DerivedPathsWithHints;
nlohmann::json derivedPathsWithHintsToJSON(const DerivedPathsWithHints & buildables, ref<Store> store);

View file

@ -61,12 +61,12 @@ struct CmdBuild : InstallablesCommand, MixDryRun, MixJSON, MixProfile
for (const auto & [_i, buildable] : enumerate(buildables)) {
auto i = _i;
std::visit(overloaded {
[&](DerivedPathOpaque bo) {
[&](DerivedPathWithHints::Opaque bo) {
std::string symlink = outLink;
if (i) symlink += fmt("-%d", i);
store2->addPermRoot(bo.path, absPath(symlink));
},
[&](DerivedPathWithHintsBuilt bfd) {
[&](DerivedPathWithHints::Built bfd) {
auto builtOutputs = store->queryDerivationOutputMap(bfd.drvPath);
for (auto & output : builtOutputs) {
std::string symlink = outLink;
@ -75,7 +75,7 @@ struct CmdBuild : InstallablesCommand, MixDryRun, MixJSON, MixProfile
store2->addPermRoot(output.second, absPath(symlink));
}
},
}, buildable);
}, buildable.raw());
}
updateProfile(buildables);

View file

@ -277,14 +277,14 @@ struct Common : InstallableCommand, MixProfile
}
};
std::visit(overloaded {
[&](const DerivedPathOpaque & bo) {
[&](const DerivedPathWithHints::Opaque & bo) {
doRedirect(bo.path);
},
[&](const DerivedPathWithHintsBuilt & bfd) {
[&](const DerivedPathWithHints::Built & bfd) {
for (auto & [outputName, path] : bfd.outputs)
if (path) doRedirect(*path);
},
}, buildable);
}, buildable.raw());
}
return rewriteStrings(script, rewrites);

View file

@ -35,13 +35,13 @@ struct CmdLog : InstallableCommand
RunPager pager;
for (auto & sub : subs) {
auto log = std::visit(overloaded {
[&](DerivedPathOpaque bo) {
[&](DerivedPathWithHints::Opaque bo) {
return sub->getBuildLog(bo.path);
},
[&](DerivedPathWithHintsBuilt bfd) {
[&](DerivedPathWithHints::Built bfd) {
return sub->getBuildLog(bfd.drvPath);
},
}, b);
}, b.raw());
if (!log) continue;
stopProgressBar();
printInfo("got build log for '%s' from '%s'", installable->what(), sub->getUri());

View file

@ -259,11 +259,11 @@ struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile
ProfileElement element;
std::visit(overloaded {
[&](DerivedPathOpaque bo) {
[&](DerivedPathWithHints::Opaque bo) {
pathsToBuild.push_back(bo);
element.storePaths.insert(bo.path);
},
[&](DerivedPathWithHintsBuilt bfd) {
[&](DerivedPathWithHints::Built bfd) {
// TODO: Why are we querying if we know the output
// names already? Is it just to figure out what the
// default one is?
@ -272,7 +272,7 @@ struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile
element.storePaths.insert(output.second);
}
},
}, buildable);
}, buildable.raw());
manifest.elements.emplace_back(std::move(element));
}