cmCPackNuGetGenerator.cxx 4.9 KB

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