CMake Nightly Date Stamp
[kiteware-cmake.git] / Source / cmFileAPICache.cxx
blobba38ef7981c282402194de126e7119b4283c6155
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmFileAPICache.h"
5 #include <algorithm>
6 #include <string>
7 #include <utility>
8 #include <vector>
10 #include <cm3p/json/value.h>
12 #include "cmFileAPI.h"
13 #include "cmState.h"
14 #include "cmValue.h"
15 #include "cmake.h"
17 namespace {
19 class Cache
21 cmFileAPI& FileAPI;
22 unsigned long Version;
23 cmState* State;
25 Json::Value DumpEntries();
26 Json::Value DumpEntry(std::string const& name);
27 Json::Value DumpEntryProperties(std::string const& name);
28 Json::Value DumpEntryProperty(std::string const& name,
29 std::string const& prop);
31 public:
32 Cache(cmFileAPI& fileAPI, unsigned long version);
33 Json::Value Dump();
36 Cache::Cache(cmFileAPI& fileAPI, unsigned long version)
37 : FileAPI(fileAPI)
38 , Version(version)
39 , State(this->FileAPI.GetCMakeInstance()->GetState())
41 static_cast<void>(this->Version);
44 Json::Value Cache::Dump()
46 Json::Value cache = Json::objectValue;
47 cache["entries"] = this->DumpEntries();
48 return cache;
51 Json::Value Cache::DumpEntries()
53 Json::Value entries = Json::arrayValue;
55 std::vector<std::string> names = this->State->GetCacheEntryKeys();
56 std::sort(names.begin(), names.end());
58 for (std::string const& name : names) {
59 entries.append(this->DumpEntry(name));
62 return entries;
65 Json::Value Cache::DumpEntry(std::string const& name)
67 Json::Value entry = Json::objectValue;
68 entry["name"] = name;
69 entry["type"] =
70 cmState::CacheEntryTypeToString(this->State->GetCacheEntryType(name));
71 entry["value"] = this->State->GetSafeCacheEntryValue(name);
73 Json::Value properties = this->DumpEntryProperties(name);
74 if (!properties.empty()) {
75 entry["properties"] = std::move(properties);
78 return entry;
81 Json::Value Cache::DumpEntryProperties(std::string const& name)
83 Json::Value properties = Json::arrayValue;
84 std::vector<std::string> props =
85 this->State->GetCacheEntryPropertyList(name);
86 std::sort(props.begin(), props.end());
87 for (std::string const& prop : props) {
88 properties.append(this->DumpEntryProperty(name, prop));
90 return properties;
93 Json::Value Cache::DumpEntryProperty(std::string const& name,
94 std::string const& prop)
96 Json::Value property = Json::objectValue;
97 property["name"] = prop;
98 cmValue p = this->State->GetCacheEntryProperty(name, prop);
99 property["value"] = p ? *p : "";
100 return property;
104 Json::Value cmFileAPICacheDump(cmFileAPI& fileAPI, unsigned long version)
106 Cache cache(fileAPI, version);
107 return cache.Dump();