cmFileAPICache.cxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include <algorithm>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include <cm3p/json/value.h>
  9. #include "cmFileAPI.h"
  10. #include "cmProperty.h"
  11. #include "cmState.h"
  12. #include "cmake.h"
  13. namespace {
  14. class Cache
  15. {
  16. cmFileAPI& FileAPI;
  17. unsigned long Version;
  18. cmState* State;
  19. Json::Value DumpEntries();
  20. Json::Value DumpEntry(std::string const& name);
  21. Json::Value DumpEntryProperties(std::string const& name);
  22. Json::Value DumpEntryProperty(std::string const& name,
  23. std::string const& prop);
  24. public:
  25. Cache(cmFileAPI& fileAPI, unsigned long version);
  26. Json::Value Dump();
  27. };
  28. Cache::Cache(cmFileAPI& fileAPI, unsigned long version)
  29. : FileAPI(fileAPI)
  30. , Version(version)
  31. , State(this->FileAPI.GetCMakeInstance()->GetState())
  32. {
  33. static_cast<void>(this->Version);
  34. }
  35. Json::Value Cache::Dump()
  36. {
  37. Json::Value cache = Json::objectValue;
  38. cache["entries"] = this->DumpEntries();
  39. return cache;
  40. }
  41. Json::Value Cache::DumpEntries()
  42. {
  43. Json::Value entries = Json::arrayValue;
  44. std::vector<std::string> names = this->State->GetCacheEntryKeys();
  45. std::sort(names.begin(), names.end());
  46. for (std::string const& name : names) {
  47. entries.append(this->DumpEntry(name));
  48. }
  49. return entries;
  50. }
  51. Json::Value Cache::DumpEntry(std::string const& name)
  52. {
  53. Json::Value entry = Json::objectValue;
  54. entry["name"] = name;
  55. entry["type"] =
  56. cmState::CacheEntryTypeToString(this->State->GetCacheEntryType(name));
  57. entry["value"] = this->State->GetSafeCacheEntryValue(name);
  58. Json::Value properties = this->DumpEntryProperties(name);
  59. if (!properties.empty()) {
  60. entry["properties"] = std::move(properties);
  61. }
  62. return entry;
  63. }
  64. Json::Value Cache::DumpEntryProperties(std::string const& name)
  65. {
  66. Json::Value properties = Json::arrayValue;
  67. std::vector<std::string> props =
  68. this->State->GetCacheEntryPropertyList(name);
  69. std::sort(props.begin(), props.end());
  70. for (std::string const& prop : props) {
  71. properties.append(this->DumpEntryProperty(name, prop));
  72. }
  73. return properties;
  74. }
  75. Json::Value Cache::DumpEntryProperty(std::string const& name,
  76. std::string const& prop)
  77. {
  78. Json::Value property = Json::objectValue;
  79. property["name"] = prop;
  80. cmProp p = this->State->GetCacheEntryProperty(name, prop);
  81. property["value"] = p ? *p : "";
  82. return property;
  83. }
  84. }
  85. Json::Value cmFileAPICacheDump(cmFileAPI& fileAPI, unsigned long version)
  86. {
  87. Cache cache(fileAPI, version);
  88. return cache.Dump();
  89. }