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