| 931 | #endif |
| 932 | |
| 933 | bool HandleJSONCommand(std::vector<std::string> const& arguments, |
| 934 | cmExecutionStatus& status) |
| 935 | { |
| 936 | #if !defined(CMAKE_BOOTSTRAP) |
| 937 | |
| 938 | auto& makefile = status.GetMakefile(); |
| 939 | Args args{ arguments.begin() + 1, arguments.end() }; |
| 940 | |
| 941 | std::string const* errorVariable = nullptr; |
| 942 | std::string const* outputVariable = nullptr; |
| 943 | bool success = true; |
| 944 | |
| 945 | try { |
| 946 | outputVariable = &args.PopFront("missing out-var argument"_s); |
| 947 | |
| 948 | if (!args.empty() && *args.begin() == "ERROR_VARIABLE"_s) { |
| 949 | args.PopFront(""); |
| 950 | errorVariable = &args.PopFront("missing error-var argument"_s); |
| 951 | makefile.AddDefinition(*errorVariable, "NOTFOUND"_s); |
| 952 | } |
| 953 | |
| 954 | auto const& mode = args.PopFront("missing mode argument"_s); |
| 955 | if (mode != "GET"_s && mode != "GET_RAW"_s && mode != "TYPE"_s && |
| 956 | mode != "MEMBER"_s && mode != "LENGTH"_s && mode != "REMOVE"_s && |
| 957 | mode != "SET"_s && mode != "EQUAL"_s && mode != "STRING_ENCODE"_s) { |
| 958 | throw json_error(cmStrCat( |
| 959 | "got an invalid mode '"_s, mode, |
| 960 | "', expected one of GET, GET_RAW, TYPE, MEMBER, LENGTH, REMOVE, SET, " |
| 961 | " EQUAL, STRING_ENCODE"_s)); |
| 962 | } |
| 963 | |
| 964 | auto const& jsonstr = args.PopFront("missing json string argument"_s); |
| 965 | |
| 966 | if (mode == "STRING_ENCODE"_s) { |
| 967 | Json::Value json(jsonstr); |
| 968 | makefile.AddDefinition(*outputVariable, WriteJson(json)); |
| 969 | } else { |
| 970 | Json::Value json = ReadJson(jsonstr); |
| 971 | |
| 972 | if (mode == "GET"_s) { |
| 973 | auto const& value = ResolvePath(json, args); |
| 974 | if (value.isObject() || value.isArray()) { |
| 975 | makefile.AddDefinition(*outputVariable, WriteJson(value)); |
| 976 | } else if (value.isBool()) { |
| 977 | makefile.AddDefinitionBool(*outputVariable, value.asBool()); |
| 978 | } else { |
| 979 | makefile.AddDefinition(*outputVariable, value.asString()); |
| 980 | } |
| 981 | |
| 982 | } else if (mode == "GET_RAW"_s) { |
| 983 | auto const& value = ResolvePath(json, args); |
| 984 | makefile.AddDefinition(*outputVariable, WriteJson(value)); |
| 985 | |
| 986 | } else if (mode == "TYPE"_s) { |
| 987 | auto const& value = ResolvePath(json, args); |
| 988 | makefile.AddDefinition(*outputVariable, |
| 989 | JsonTypeToString(value.type())); |
| 990 |
nothing calls this directly
no test coverage detected
searching dependent graphs…