cmCPackNuGetGenerator.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "cmCPackNuGetGenerator.h"
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <map>
  7. #include <ostream>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "cmCPackComponentGroup.h"
  12. #include "cmCPackLog.h"
  13. #include "cmList.h"
  14. #include "cmSystemTools.h"
  15. #include "cmValue.h"
  16. bool cmCPackNuGetGenerator::SupportsComponentInstallation() const
  17. {
  18. return this->IsOn("CPACK_NUGET_COMPONENT_INSTALL");
  19. }
  20. int cmCPackNuGetGenerator::PackageFiles()
  21. {
  22. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  23. "Toplevel: " << this->toplevel << std::endl);
  24. /* Reset package file name list it will be populated after the
  25. * `CPackNuGet.cmake` run */
  26. this->packageFileNames.clear();
  27. /* Are we in the component packaging case */
  28. if (this->WantsComponentInstallation()) {
  29. if (this->componentPackageMethod == ONE_PACKAGE) {
  30. // CASE 1 : COMPONENT ALL-IN-ONE package
  31. // Meaning that all per-component pre-installed files
  32. // goes into the single package.
  33. this->SetOption("CPACK_NUGET_ALL_IN_ONE", "TRUE");
  34. this->SetupGroupComponentVariables(true);
  35. } else {
  36. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  37. // There will be 1 package for each component group
  38. // however one may require to ignore component group and
  39. // in this case you'll get 1 package for each component.
  40. this->SetupGroupComponentVariables(this->componentPackageMethod ==
  41. ONE_PACKAGE_PER_COMPONENT);
  42. }
  43. } else {
  44. // CASE 3 : NON COMPONENT package.
  45. this->SetOption("CPACK_NUGET_ORDINAL_MONOLITIC", "TRUE");
  46. }
  47. auto retval = this->ReadListFile("Internal/CPack/CPackNuGet.cmake");
  48. if (retval) {
  49. this->AddGeneratedPackageNames();
  50. } else {
  51. cmCPackLogger(cmCPackLog::LOG_ERROR,
  52. "Error while execution CPackNuGet.cmake" << std::endl);
  53. }
  54. return retval;
  55. }
  56. void cmCPackNuGetGenerator::SetupGroupComponentVariables(bool ignoreGroup)
  57. {
  58. // The default behavior is to have one package by component group
  59. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  60. if (!ignoreGroup) {
  61. std::vector<std::string> groups;
  62. for (auto const& compG : this->ComponentGroups) {
  63. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  64. "Packaging component group: " << compG.first << std::endl);
  65. groups.push_back(compG.first);
  66. auto compGUp =
  67. cmSystemTools::UpperCase(cmSystemTools::MakeCidentifier(compG.first));
  68. // Collect components for this group
  69. std::vector<std::string> components;
  70. std::transform(begin(compG.second.Components),
  71. end(compG.second.Components),
  72. std::back_inserter(components),
  73. [](cmCPackComponent const* comp) { return comp->Name; });
  74. this->SetOption("CPACK_NUGET_" + compGUp + "_GROUP_COMPONENTS",
  75. cmList::to_string(components));
  76. }
  77. if (!groups.empty()) {
  78. this->SetOption("CPACK_NUGET_GROUPS", cmList::to_string(groups));
  79. }
  80. // Handle Orphan components (components not belonging to any groups)
  81. std::vector<std::string> components;
  82. for (auto const& comp : this->Components) {
  83. // Does the component belong to a group?
  84. if (!comp.second.Group) {
  85. cmCPackLogger(
  86. cmCPackLog::LOG_VERBOSE,
  87. "Component <"
  88. << comp.second.Name
  89. << "> does not belong to any group, package it separately."
  90. << std::endl);
  91. components.push_back(comp.first);
  92. }
  93. }
  94. if (!components.empty()) {
  95. this->SetOption("CPACK_NUGET_COMPONENTS", cmList::to_string(components));
  96. }
  97. } else {
  98. std::vector<std::string> components;
  99. components.reserve(this->Components.size());
  100. std::transform(begin(this->Components), end(this->Components),
  101. std::back_inserter(components),
  102. [](std::pair<std::string, cmCPackComponent> const& comp) {
  103. return comp.first;
  104. });
  105. this->SetOption("CPACK_NUGET_COMPONENTS", cmList::to_string(components));
  106. }
  107. }
  108. void cmCPackNuGetGenerator::AddGeneratedPackageNames()
  109. {
  110. cmValue const files_list = this->GetOption("GEN_CPACK_OUTPUT_FILES");
  111. if (!files_list) {
  112. cmCPackLogger(
  113. cmCPackLog::LOG_ERROR,
  114. "Error while execution CPackNuGet.cmake: No NuGet package has generated"
  115. << std::endl);
  116. return;
  117. }
  118. // add the generated packages to package file names list
  119. const std::string& fileNames = *files_list;
  120. const char sep = ';';
  121. std::string::size_type pos1 = 0;
  122. std::string::size_type pos2 = fileNames.find(sep, pos1 + 1);
  123. while (pos2 != std::string::npos) {
  124. this->packageFileNames.push_back(fileNames.substr(pos1, pos2 - pos1));
  125. pos1 = pos2 + 1;
  126. pos2 = fileNames.find(sep, pos1 + 1);
  127. }
  128. this->packageFileNames.push_back(fileNames.substr(pos1, pos2 - pos1));
  129. }