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"
4 
5 #include <algorithm>
6 #include <string>
7 #include <utility>
8 #include <vector>
9 
10 #include <cm3p/json/value.h>
11 
12 #include "cmFileAPI.h"
13 #include "cmState.h"
14 #include "cmValue.h"
15 #include "cmake.h"
16 
17 namespace {
18 
19 class Cache
20 {
21   cmFileAPI& FileAPI;
22   unsigned long Version;
23   cmState* State;
24 
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);
30 
31 public:
32   Cache(cmFileAPI& fileAPI, unsigned long version);
33   Json::Value Dump();
34 };
35 
Cache(cmFileAPI & fileAPI,unsigned long version)36 Cache::Cache(cmFileAPI& fileAPI, unsigned long version)
37   : FileAPI(fileAPI)
38   , Version(version)
39   , State(this->FileAPI.GetCMakeInstance()->GetState())
40 {
41   static_cast<void>(this->Version);
42 }
43 
Dump()44 Json::Value Cache::Dump()
45 {
46   Json::Value cache = Json::objectValue;
47   cache["entries"] = this->DumpEntries();
48   return cache;
49 }
50 
DumpEntries()51 Json::Value Cache::DumpEntries()
52 {
53   Json::Value entries = Json::arrayValue;
54 
55   std::vector<std::string> names = this->State->GetCacheEntryKeys();
56   std::sort(names.begin(), names.end());
57 
58   for (std::string const& name : names) {
59     entries.append(this->DumpEntry(name));
60   }
61 
62   return entries;
63 }
64 
DumpEntry(std::string const & name)65 Json::Value Cache::DumpEntry(std::string const& name)
66 {
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);
72 
73   Json::Value properties = this->DumpEntryProperties(name);
74   if (!properties.empty()) {
75     entry["properties"] = std::move(properties);
76   }
77 
78   return entry;
79 }
80 
DumpEntryProperties(std::string const & name)81 Json::Value Cache::DumpEntryProperties(std::string const& name)
82 {
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));
89   }
90   return properties;
91 }
92 
DumpEntryProperty(std::string const & name,std::string const & prop)93 Json::Value Cache::DumpEntryProperty(std::string const& name,
94                                      std::string const& prop)
95 {
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;
101 }
102 }
103 
cmFileAPICacheDump(cmFileAPI & fileAPI,unsigned long version)104 Json::Value cmFileAPICacheDump(cmFileAPI& fileAPI, unsigned long version)
105 {
106   Cache cache(fileAPI, version);
107   return cache.Dump();
108 }
109