2023-01-11 06:51:14 +00:00
|
|
|
#include "util.hh"
|
2023-01-10 16:27:19 +00:00
|
|
|
#include "outputs-spec.hh"
|
|
|
|
#include "nlohmann/json.hpp"
|
|
|
|
|
|
|
|
#include <regex>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2023-01-11 23:57:18 +00:00
|
|
|
bool OutputsSpec::contains(const std::string & outputName) const
|
2023-01-10 16:27:19 +00:00
|
|
|
{
|
2023-01-11 23:57:18 +00:00
|
|
|
return std::visit(overloaded {
|
|
|
|
[&](const OutputsSpec::All &) {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[&](const OutputsSpec::Names & outputNames) {
|
|
|
|
return outputNames.count(outputName) > 0;
|
|
|
|
},
|
|
|
|
}, raw());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::optional<OutputsSpec> OutputsSpec::parseOpt(std::string_view s)
|
|
|
|
{
|
|
|
|
static std::regex regex(R"((\*)|([a-z]+(,[a-z]+)*))");
|
2023-01-10 16:27:19 +00:00
|
|
|
|
|
|
|
std::smatch match;
|
2023-01-11 23:57:18 +00:00
|
|
|
std::string s2 { s }; // until some improves std::regex
|
|
|
|
if (!std::regex_match(s2, match, regex))
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
if (match[1].matched)
|
|
|
|
return { OutputsSpec::All {} };
|
|
|
|
|
|
|
|
if (match[2].matched)
|
|
|
|
return { tokenizeString<OutputsSpec::Names>(match[2].str(), ",") };
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
}
|
2023-01-10 16:27:19 +00:00
|
|
|
|
|
|
|
|
2023-01-11 23:57:18 +00:00
|
|
|
OutputsSpec OutputsSpec::parse(std::string_view s)
|
|
|
|
{
|
|
|
|
std::optional spec = OutputsSpec::parseOpt(s);
|
|
|
|
if (!spec)
|
|
|
|
throw Error("Invalid outputs specifier: '%s'", s);
|
|
|
|
return *spec;
|
2023-01-10 16:27:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 23:57:18 +00:00
|
|
|
|
|
|
|
std::pair<std::string_view, ExtendedOutputsSpec> ExtendedOutputsSpec::parse(std::string_view s)
|
|
|
|
{
|
|
|
|
auto found = s.rfind('^');
|
|
|
|
|
|
|
|
if (found == std::string::npos)
|
|
|
|
return { s, ExtendedOutputsSpec::Default {} };
|
|
|
|
|
|
|
|
auto spec = OutputsSpec::parse(s.substr(found + 1));
|
|
|
|
return { s.substr(0, found), ExtendedOutputsSpec::Explicit { spec } };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string OutputsSpec::to_string() const
|
|
|
|
{
|
|
|
|
return std::visit(overloaded {
|
|
|
|
[&](const OutputsSpec::All &) -> std::string {
|
|
|
|
return "*";
|
|
|
|
},
|
|
|
|
[&](const OutputsSpec::Names & outputNames) -> std::string {
|
|
|
|
return concatStringsSep(",", outputNames);
|
|
|
|
},
|
|
|
|
}, raw());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 07:00:44 +00:00
|
|
|
std::string ExtendedOutputsSpec::to_string() const
|
2023-01-10 16:27:19 +00:00
|
|
|
{
|
2023-01-11 06:51:14 +00:00
|
|
|
return std::visit(overloaded {
|
2023-01-11 07:00:44 +00:00
|
|
|
[&](const ExtendedOutputsSpec::Default &) -> std::string {
|
2023-01-11 06:51:14 +00:00
|
|
|
return "";
|
|
|
|
},
|
2023-01-11 23:57:18 +00:00
|
|
|
[&](const ExtendedOutputsSpec::Explicit & outputSpec) -> std::string {
|
|
|
|
return "^" + outputSpec.to_string();
|
|
|
|
},
|
|
|
|
}, raw());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool OutputsSpec::merge(const OutputsSpec & that)
|
|
|
|
{
|
|
|
|
return std::visit(overloaded {
|
|
|
|
[&](OutputsSpec::All &) {
|
|
|
|
/* If we already refer to all outputs, there is nothing to do. */
|
|
|
|
return false;
|
2023-01-11 06:51:14 +00:00
|
|
|
},
|
2023-01-11 23:57:18 +00:00
|
|
|
[&](OutputsSpec::Names & theseNames) {
|
|
|
|
return std::visit(overloaded {
|
|
|
|
[&](const OutputsSpec::All &) {
|
|
|
|
*this = OutputsSpec::All {};
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[&](const OutputsSpec::Names & thoseNames) {
|
|
|
|
bool ret = false;
|
|
|
|
for (auto & i : thoseNames)
|
|
|
|
if (theseNames.insert(i).second)
|
|
|
|
ret = true;
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
}, that.raw());
|
2023-01-11 06:51:14 +00:00
|
|
|
},
|
|
|
|
}, raw());
|
2023-01-10 16:27:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 23:57:18 +00:00
|
|
|
|
|
|
|
void to_json(nlohmann::json & json, const OutputsSpec & outputsSpec)
|
|
|
|
{
|
|
|
|
std::visit(overloaded {
|
|
|
|
[&](const OutputsSpec::All &) {
|
|
|
|
json = std::vector<std::string>({"*"});
|
|
|
|
},
|
|
|
|
[&](const OutputsSpec::Names & names) {
|
|
|
|
json = names;
|
|
|
|
},
|
|
|
|
}, outputsSpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 07:00:44 +00:00
|
|
|
void to_json(nlohmann::json & json, const ExtendedOutputsSpec & extendedOutputsSpec)
|
2023-01-10 16:27:19 +00:00
|
|
|
{
|
2023-01-11 23:57:18 +00:00
|
|
|
std::visit(overloaded {
|
|
|
|
[&](const ExtendedOutputsSpec::Default &) {
|
|
|
|
json = nullptr;
|
|
|
|
},
|
|
|
|
[&](const ExtendedOutputsSpec::Explicit & e) {
|
|
|
|
to_json(json, e);
|
|
|
|
},
|
|
|
|
}, extendedOutputsSpec);
|
|
|
|
}
|
2023-01-10 16:27:19 +00:00
|
|
|
|
|
|
|
|
2023-01-11 23:57:18 +00:00
|
|
|
void from_json(const nlohmann::json & json, OutputsSpec & outputsSpec)
|
|
|
|
{
|
|
|
|
auto names = json.get<OutputNames>();
|
|
|
|
if (names == OutputNames({"*"}))
|
|
|
|
outputsSpec = OutputsSpec::All {};
|
|
|
|
else
|
|
|
|
outputsSpec = names;
|
2023-01-10 16:27:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 23:57:18 +00:00
|
|
|
|
2023-01-11 07:00:44 +00:00
|
|
|
void from_json(const nlohmann::json & json, ExtendedOutputsSpec & extendedOutputsSpec)
|
2023-01-10 16:27:19 +00:00
|
|
|
{
|
|
|
|
if (json.is_null())
|
2023-01-11 23:57:18 +00:00
|
|
|
extendedOutputsSpec = ExtendedOutputsSpec::Default {};
|
2023-01-10 16:27:19 +00:00
|
|
|
else {
|
2023-01-11 23:57:18 +00:00
|
|
|
extendedOutputsSpec = ExtendedOutputsSpec::Explicit { json.get<OutputsSpec>() };
|
2023-01-10 16:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|