1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
#include "pch.h"
#include "vcpkg_Commands.h"
#include "vcpkglib.h"
#include "vcpkg_System.h"
#include "vcpkg_Dependencies.h"
#include "vcpkg_Input.h"
#include "vcpkg_Util.h"
#include "Paragraphs.h"
#include <regex>
namespace vcpkg::Commands::Export
{
using Install::InstallDir;
using Dependencies::ExportPlanAction;
using Dependencies::RequestType;
using Dependencies::ExportPlanType;
static std::string create_nuspec_file_contents(const std::string& exported_dir_filename, const std::string& nuget_id, const std::string& nupkg_version)
{
static constexpr auto content_template = R"(
<package>
<metadata>
<id>@NUGET_ID@</id>
<version>@VERSION@</version>
<authors>vcpkg</authors>
<description>
Vcpkg NuGet export
</description>
</metadata>
<files>
<file src="@EXPORTED_DIR@\**" target="" />
<file src="@EXPORTED_DIR@\.vcpkg-root" target="" />
<file src="scripts\buildsystems\msbuild\applocal.ps1" target="build\native\applocal.ps1" />
<file src="scripts\buildsystems\msbuild\vcpkg.targets" target="build\native\@NUGET_ID@.targets" />
<file src="scripts\buildsystems\vcpkg.cmake" target="build\native\vcpkg.cmake" />
</files>
</package>
)";
std::string nuspec_file_content = std::regex_replace(content_template, std::regex("@NUGET_ID@"), nuget_id);
nuspec_file_content = std::regex_replace(nuspec_file_content, std::regex("@VERSION@"), nupkg_version);
nuspec_file_content = std::regex_replace(nuspec_file_content, std::regex("@EXPORTED_DIR@"), exported_dir_filename);
return nuspec_file_content;
}
static void print_plan(const std::map<ExportPlanType, std::vector<const ExportPlanAction*>>& group_by_plan_type)
{
static constexpr std::array<ExportPlanType, 2> order = { ExportPlanType::ALREADY_BUILT, ExportPlanType::PORT_AVAILABLE_BUT_NOT_BUILT };
for (const ExportPlanType plan_type : order)
{
auto it = group_by_plan_type.find(plan_type);
if (it == group_by_plan_type.cend())
{
continue;
}
std::vector<const ExportPlanAction*> cont = it->second;
std::sort(cont.begin(), cont.end(), &ExportPlanAction::compare_by_name);
const std::string as_string = Strings::join("\n", cont, [](const ExportPlanAction* p)
{
return Dependencies::to_output_string(p->request_type, p->spec.to_string());
});
switch (plan_type)
{
case ExportPlanType::ALREADY_BUILT:
System::println("The following packages are already built and will be exported:\n%s", as_string);
continue;
case ExportPlanType::PORT_AVAILABLE_BUT_NOT_BUILT:
System::println("The following packages need to be built:\n%s", as_string);
continue;
default:
Checks::unreachable(VCPKG_LINE_INFO);
}
}
}
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths, const Triplet& default_triplet)
{
static const std::string OPTION_DRY_RUN = "--dry-run";
// input sanitization
static const std::string example = Commands::Help::create_example_string("export zlib zlib:x64-windows curl boost");
args.check_min_arg_count(1, example);
const std::vector<PackageSpec> specs = Util::fmap(args.command_arguments, [&](auto&& arg)
{
return Input::check_and_get_package_spec(arg, default_triplet, example);
});
for (auto&& spec : specs)
Input::check_triplet(spec.triplet(), paths);
const std::unordered_set<std::string> options = args.check_and_get_optional_command_arguments({ OPTION_DRY_RUN });
const bool dryRun = options.find(OPTION_DRY_RUN) != options.cend();
// create the plan
StatusParagraphs status_db = database_load_check(paths);
std::vector<ExportPlanAction> export_plan = Dependencies::create_export_plan(paths, specs, status_db);
Checks::check_exit(VCPKG_LINE_INFO, !export_plan.empty(), "Export plan cannot be empty");
std::map<ExportPlanType, std::vector<const ExportPlanAction*>> group_by_plan_type;
Util::group_by(export_plan, &group_by_plan_type, [](const ExportPlanAction& p) { return p.plan_type; });
print_plan(group_by_plan_type);
const bool has_non_user_requested_packages = Util::find_if(export_plan, [](const ExportPlanAction& package)-> bool
{
return package.request_type != RequestType::USER_REQUESTED;
}) != export_plan.cend();
if (has_non_user_requested_packages)
{
System::println(System::Color::warning, "Additional packages (*) need to be exported to complete this operation.");
}
auto it = group_by_plan_type.find(ExportPlanType::PORT_AVAILABLE_BUT_NOT_BUILT);
if (it != group_by_plan_type.cend() && !it->second.empty())
{
System::println(System::Color::error, "There are packages that have not been built.");
// No need to show all of them, just the user-requested ones. Dependency resolution will handle the rest.
std::vector<const ExportPlanAction*> unbuilt = it->second;
Util::erase_remove_if(unbuilt, [](const ExportPlanAction* a)
{
return a->request_type != RequestType::USER_REQUESTED;
});
auto s = Strings::join(" ", unbuilt, [](const ExportPlanAction* a) { return a->spec.to_string(); });
System::println("To build them, run:\n"
" vcpkg install %s", s);
Checks::exit_fail(VCPKG_LINE_INFO);
}
if (dryRun)
{
Checks::exit_success(VCPKG_LINE_INFO);
}
const tm date_time = System::get_current_date_time();
// Format is: YYYY-mm-dd_HH-MM-SS
// 19 characters + 1 null terminating character will be written for a total of 20 chars
char mbstr[20];
const size_t bytes_written = std::strftime(mbstr, sizeof(mbstr), "%Y-%m-%d_%H-%M-%S", &date_time);
Checks::check_exit(VCPKG_LINE_INFO, bytes_written == 19, "Expected 19 bytes to be written, but %u were written", bytes_written);
const std::string date_time_as_string(mbstr);
const std::string exported_dir_filename = ("exported-" + date_time_as_string);
Files::Filesystem& fs = paths.get_filesystem();
const fs::path exported_dir_path = paths.root / exported_dir_filename;
std::error_code ec;
fs.remove_all(exported_dir_path, ec);
fs.create_directory(exported_dir_path, ec);
// execute the plan
for (const ExportPlanAction& action : export_plan)
{
if (action.plan_type != ExportPlanType::ALREADY_BUILT)
{
Checks::unreachable(VCPKG_LINE_INFO);
}
const std::string display_name = action.spec.to_string();
System::println("Exporting package %s... ", display_name);
const BinaryParagraph& binary_paragraph = action.any_paragraph.binary_paragraph.value_or_exit(VCPKG_LINE_INFO);
const InstallDir dirs = InstallDir::from_destination_root(
exported_dir_path / "installed",
action.spec.triplet().to_string(),
exported_dir_path / "installed" / "vcpkg" / "info" / (binary_paragraph.fullstem() + ".list"));
Install::install_files_and_write_listfile(paths.get_filesystem(), paths.package_dir(action.spec), dirs);
System::println(System::Color::success, "Exporting package %s... done", display_name);
}
const fs::path vcpkg_root_file = (exported_dir_path / ".vcpkg-root");
fs.write_contents(vcpkg_root_file, "");
System::println(System::Color::success, R"(Files exported at: "%s")", exported_dir_path.generic_string());
const std::string nuget_id = "vcpkg-" + exported_dir_filename;
const std::string nupkg_version = "1.0.0";
const std::string nuspec_file_content = create_nuspec_file_contents(exported_dir_path.filename().string(), nuget_id, nupkg_version);
const fs::path nuspec_file_path = paths.root / "export.nuspec";
fs.write_contents(nuspec_file_path, nuspec_file_content);
const fs::path& nuget_exe = paths.get_nuget_exe();
// -NoDefaultExcludes is needed for ".vcpkg-root"
const std::wstring cmd_line = Strings::wformat(LR"("%s" pack -OutputDirectory "%s" "%s" -NoDefaultExcludes)", nuget_exe.native(), paths.root.native(), nuspec_file_path.native());
const int exit_code = System::cmd_execute_clean(cmd_line);
Checks::check_exit(VCPKG_LINE_INFO, exit_code == 0, "Error: NuGet package creation failed");
Checks::exit_success(VCPKG_LINE_INFO);
}
}
|