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 07:00:44 +00:00
|
|
|
std::pair<std::string, ExtendedOutputsSpec> ExtendedOutputsSpec::parse(std::string s)
|
2023-01-10 16:27:19 +00:00
|
|
|
{
|
|
|
|
static std::regex regex(R"((.*)\^((\*)|([a-z]+(,[a-z]+)*)))");
|
|
|
|
|
|
|
|
std::smatch match;
|
|
|
|
if (!std::regex_match(s, match, regex))
|
|
|
|
return {s, DefaultOutputs()};
|
|
|
|
|
|
|
|
if (match[3].matched)
|
|
|
|
return {match[1], AllOutputs()};
|
|
|
|
|
|
|
|
return {match[1], tokenizeString<OutputNames>(match[4].str(), ",")};
|
|
|
|
}
|
|
|
|
|
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 07:00:44 +00:00
|
|
|
[&](const ExtendedOutputsSpec::All &) -> std::string {
|
2023-01-11 06:51:14 +00:00
|
|
|
return "*";
|
|
|
|
},
|
2023-01-11 07:00:44 +00:00
|
|
|
[&](const ExtendedOutputsSpec::Names & outputNames) -> std::string {
|
2023-01-11 06:51:14 +00:00
|
|
|
return "^" + concatStringsSep(",", outputNames);
|
|
|
|
},
|
|
|
|
}, raw());
|
2023-01-10 16:27:19 +00:00
|
|
|
}
|
|
|
|
|
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 07:00:44 +00:00
|
|
|
if (std::get_if<DefaultOutputs>(&extendedOutputsSpec))
|
2023-01-10 16:27:19 +00:00
|
|
|
json = nullptr;
|
|
|
|
|
2023-01-11 07:00:44 +00:00
|
|
|
else if (std::get_if<AllOutputs>(&extendedOutputsSpec))
|
2023-01-10 16:27:19 +00:00
|
|
|
json = std::vector<std::string>({"*"});
|
|
|
|
|
2023-01-11 07:00:44 +00:00
|
|
|
else if (auto outputNames = std::get_if<OutputNames>(&extendedOutputsSpec))
|
2023-01-10 16:27:19 +00:00
|
|
|
json = *outputNames;
|
|
|
|
}
|
|
|
|
|
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 07:00:44 +00:00
|
|
|
extendedOutputsSpec = DefaultOutputs();
|
2023-01-10 16:27:19 +00:00
|
|
|
else {
|
|
|
|
auto names = json.get<OutputNames>();
|
|
|
|
if (names == OutputNames({"*"}))
|
2023-01-11 07:00:44 +00:00
|
|
|
extendedOutputsSpec = AllOutputs();
|
2023-01-10 16:27:19 +00:00
|
|
|
else
|
2023-01-11 07:00:44 +00:00
|
|
|
extendedOutputsSpec = names;
|
2023-01-10 16:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|