2021-04-05 17:33:28 +03:00
|
|
|
|
#include "derived-path.hh"
|
2021-03-01 07:48:01 +02:00
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
2022-05-04 08:44:32 +03:00
|
|
|
|
#include <optional>
|
|
|
|
|
|
2021-03-01 07:48:01 +02:00
|
|
|
|
namespace nix {
|
|
|
|
|
|
2021-04-05 16:48:18 +03:00
|
|
|
|
nlohmann::json DerivedPath::Opaque::toJSON(ref<Store> store) const {
|
2021-03-01 07:48:01 +02:00
|
|
|
|
nlohmann::json res;
|
|
|
|
|
res["path"] = store->printStorePath(path);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 12:34:31 +02:00
|
|
|
|
nlohmann::json DerivedPath::Built::toJSON(ref<Store> store) const {
|
|
|
|
|
nlohmann::json res;
|
|
|
|
|
res["drvPath"] = store->printStorePath(drvPath);
|
|
|
|
|
// Fallback for the input-addressed derivation case: We expect to always be
|
|
|
|
|
// able to print the output paths, so let’s do it
|
2023-01-12 01:57:18 +02:00
|
|
|
|
const auto outputMap = store->queryPartialDerivationOutputMap(drvPath);
|
|
|
|
|
for (const auto & [output, outputPathOpt] : outputMap) {
|
|
|
|
|
if (!outputs.contains(output)) continue;
|
|
|
|
|
if (outputPathOpt)
|
|
|
|
|
res["outputs"][output] = store->printStorePath(*outputPathOpt);
|
2022-12-15 17:00:46 +02:00
|
|
|
|
else
|
|
|
|
|
res["outputs"][output] = nullptr;
|
2022-03-17 12:34:31 +02:00
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 11:49:01 +02:00
|
|
|
|
std::string DerivedPath::Opaque::to_string(const Store & store) const
|
|
|
|
|
{
|
2021-03-02 05:50:41 +02:00
|
|
|
|
return store.printStorePath(path);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 03:45:11 +03:00
|
|
|
|
std::string DerivedPath::Built::to_string(const Store & store) const
|
2022-11-21 11:49:01 +02:00
|
|
|
|
{
|
2021-03-02 05:50:41 +02:00
|
|
|
|
return store.printStorePath(drvPath)
|
2023-04-15 03:45:11 +03:00
|
|
|
|
+ '^'
|
2023-01-12 01:57:18 +02:00
|
|
|
|
+ outputs.to_string();
|
2021-03-02 05:50:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 03:45:11 +03:00
|
|
|
|
std::string DerivedPath::Built::to_string_legacy(const Store & store) const
|
|
|
|
|
{
|
|
|
|
|
return store.printStorePath(drvPath)
|
|
|
|
|
+ '!'
|
|
|
|
|
+ outputs.to_string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string DerivedPath::to_string(const Store & store) const
|
2021-03-02 05:50:41 +02:00
|
|
|
|
{
|
2023-04-13 17:22:45 +03:00
|
|
|
|
return std::visit(overloaded {
|
2023-04-15 03:45:11 +03:00
|
|
|
|
[&](const DerivedPath::Built & req) { return req.to_string(store); },
|
|
|
|
|
[&](const DerivedPath::Opaque & req) { return req.to_string(store); },
|
|
|
|
|
}, this->raw());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string DerivedPath::to_string_legacy(const Store & store) const
|
|
|
|
|
{
|
|
|
|
|
return std::visit(overloaded {
|
|
|
|
|
[&](const DerivedPath::Built & req) { return req.to_string_legacy(store); },
|
2023-04-13 17:22:45 +03:00
|
|
|
|
[&](const DerivedPath::Opaque & req) { return req.to_string(store); },
|
|
|
|
|
}, this->raw());
|
2021-03-02 05:50:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-05 16:48:18 +03:00
|
|
|
|
DerivedPath::Opaque DerivedPath::Opaque::parse(const Store & store, std::string_view s)
|
2021-03-02 05:50:41 +02:00
|
|
|
|
{
|
|
|
|
|
return {store.parseStorePath(s)};
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-12 23:10:02 +03:00
|
|
|
|
DerivedPath::Built DerivedPath::Built::parse(const Store & store, std::string_view drvS, std::string_view outputsS)
|
2021-03-02 05:50:41 +02:00
|
|
|
|
{
|
2023-01-12 01:57:18 +02:00
|
|
|
|
return {
|
|
|
|
|
.drvPath = store.parseStorePath(drvS),
|
|
|
|
|
.outputs = OutputsSpec::parse(outputsS),
|
|
|
|
|
};
|
2021-03-02 05:50:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 03:45:11 +03:00
|
|
|
|
static inline DerivedPath parseWith(const Store & store, std::string_view s, std::string_view separator)
|
2021-03-02 05:50:41 +02:00
|
|
|
|
{
|
2023-04-15 03:45:11 +03:00
|
|
|
|
size_t n = s.find(separator);
|
2021-03-02 05:50:41 +02:00
|
|
|
|
return n == s.npos
|
2021-04-05 16:48:18 +03:00
|
|
|
|
? (DerivedPath) DerivedPath::Opaque::parse(store, s)
|
2022-05-12 23:10:02 +03:00
|
|
|
|
: (DerivedPath) DerivedPath::Built::parse(store, s.substr(0, n), s.substr(n + 1));
|
2021-03-02 05:50:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 03:45:11 +03:00
|
|
|
|
DerivedPath DerivedPath::parse(const Store & store, std::string_view s)
|
|
|
|
|
{
|
|
|
|
|
return parseWith(store, s, "^");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DerivedPath DerivedPath::parseLegacy(const Store & store, std::string_view s)
|
|
|
|
|
{
|
|
|
|
|
return parseWith(store, s, "!");
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 07:48:01 +02:00
|
|
|
|
}
|