aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/commands_export_ifw.cpp
blob: 191dbb763ab1cabed7cf58c2516816fe44f792c8 (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include "pch.h"

#include "vcpkg_Commands.h"
#include "vcpkg_Commands_Export.h"
#include "vcpkg_Commands_Export_IFW.h"

namespace vcpkg::Commands::Export::IFW
{
    using Dependencies::ExportPlanAction;
    using Dependencies::ExportPlanType;
    using Install::InstallDir;

    static std::string create_release_date()
    {
        const tm date_time = System::get_current_date_time();

        // Format is: YYYY-mm-dd
        // 10 characters + 1 null terminating character will be written for a total of 11 chars
        char mbstr[11];
        const size_t bytes_written = std::strftime(mbstr, sizeof(mbstr), "%Y-%m-%d", &date_time);
        Checks::check_exit(VCPKG_LINE_INFO,
                           bytes_written == 10,
                           "Expected 10 bytes to be written, but %u were written",
                           bytes_written);
        const std::string date_time_as_string(mbstr);
        return date_time_as_string;
    }

    fs::path get_packages_dir_path(const std::string& export_id, const Options& ifw_options, const VcpkgPaths& paths)
    {
        return ifw_options.maybe_packages_dir_path.has_value()
                   ? fs::path(ifw_options.maybe_packages_dir_path.value_or_exit(VCPKG_LINE_INFO))
                   : paths.root / (export_id + "-ifw-packages");
    }

    fs::path get_repository_dir_path(const std::string& export_id, const Options& ifw_options, const VcpkgPaths& paths)
    {
        return ifw_options.maybe_repository_dir_path.has_value()
                   ? fs::path(ifw_options.maybe_repository_dir_path.value_or_exit(VCPKG_LINE_INFO))
                   : paths.root / (export_id + "-ifw-repository");
    }

    fs::path get_config_file_path(const std::string& export_id, const Options& ifw_options, const VcpkgPaths& paths)
    {
        return ifw_options.maybe_config_file_path.has_value()
                   ? fs::path(ifw_options.maybe_config_file_path.value_or_exit(VCPKG_LINE_INFO))
                   : paths.root / (export_id + "-ifw-configuration.xml");
    }

    fs::path get_installer_file_path(const std::string& export_id, const Options& ifw_options, const VcpkgPaths& paths)
    {
        return ifw_options.maybe_installer_file_path.has_value()
                   ? fs::path(ifw_options.maybe_installer_file_path.value_or_exit(VCPKG_LINE_INFO))
                   : paths.root / (export_id + "-ifw-installer.exe");
    }

    fs::path export_real_package(const fs::path& ifw_packages_dir_path,
                                 const ExportPlanAction& action,
                                 Files::Filesystem& fs)
    {
        std::error_code ec;

        const BinaryParagraph& binary_paragraph =
            action.any_paragraph.binary_control_file.value_or_exit(VCPKG_LINE_INFO).core_paragraph;

        // Prepare meta dir
        const fs::path package_xml_file_path =
            ifw_packages_dir_path /
            Strings::format("packages.%s.%s", action.spec.name(), action.spec.triplet().canonical_name()) / "meta" /
            "package.xml";
        const fs::path package_xml_dir_path = package_xml_file_path.parent_path();
        fs.create_directories(package_xml_dir_path, ec);
        Checks::check_exit(VCPKG_LINE_INFO,
                           !ec,
                           "Could not create directory for package file %s",
                           package_xml_file_path.generic_string());

        auto deps = Strings::join(
            ",", binary_paragraph.depends, [](const std::string& dep) { return "packages." + dep + ":"; });

        if (!deps.empty()) deps = "\n    <Dependencies>" + deps + "</Dependencies>";

        fs.write_contents(package_xml_file_path,
                          Strings::format(
                              R"###(<?xml version="1.0"?>
<Package>
    <DisplayName>%s</DisplayName>
    <Version>%s</Version>
    <ReleaseDate>%s</ReleaseDate>
    <AutoDependOn>packages.%s:,triplets.%s:</AutoDependOn>%s
    <Virtual>true</Virtual>
</Package>
)###",
                              action.spec.to_string(),
                              binary_paragraph.version,
                              create_release_date(),
                              action.spec.name(),
                              action.spec.triplet().canonical_name(),
                              deps));

        // Return dir path for export package data
        return ifw_packages_dir_path /
               Strings::format("packages.%s.%s", action.spec.name(), action.spec.triplet().canonical_name()) / "data" /
               "installed";
    }

    void export_unique_packages(const fs::path& raw_exported_dir_path,
                                std::map<std::string, const ExportPlanAction*> unique_packages,
                                Files::Filesystem& fs)
    {
        std::error_code ec;

        // packages

        fs::path package_xml_file_path = raw_exported_dir_path / "packages" / "meta" / "package.xml";
        fs::path package_xml_dir_path = package_xml_file_path.parent_path();
        fs.create_directories(package_xml_dir_path, ec);
        Checks::check_exit(VCPKG_LINE_INFO,
                           !ec,
                           "Could not create directory for package file %s",
                           package_xml_file_path.generic_string());
        fs.write_contents(package_xml_file_path,
                          Strings::format(
                              R"###(<?xml version="1.0"?>
<Package>
    <DisplayName>Packages</DisplayName>
    <Version>1.0.0</Version>
    <ReleaseDate>%s</ReleaseDate>
</Package>
)###",
                              create_release_date()));

        for (auto package = unique_packages.begin(); package != unique_packages.end(); ++package)
        {
            const ExportPlanAction& action = *(package->second);
            const BinaryParagraph& binary_paragraph =
                action.any_paragraph.binary_control_file.value_or_exit(VCPKG_LINE_INFO).core_paragraph;

            package_xml_file_path =
                raw_exported_dir_path / Strings::format("packages.%s", package->first) / "meta" / "package.xml";
            package_xml_dir_path = package_xml_file_path.parent_path();
            fs.create_directories(package_xml_dir_path, ec);
            Checks::check_exit(VCPKG_LINE_INFO,
                               !ec,
                               "Could not create directory for package file %s",
                               package_xml_file_path.generic_string());

            fs.write_contents(package_xml_file_path,
                              Strings::format(
                                  R"###(<?xml version="1.0"?>
<Package>
    <DisplayName>%s</DisplayName>
    <Description>%s</Description>
    <Version>%s</Version>
    <ReleaseDate>%s</ReleaseDate>
</Package>
)###",
                                  action.spec.name(),
                                  binary_paragraph.description,
                                  binary_paragraph.version,
                                  create_release_date()));
        }
    }

    void export_unique_triplets(const fs::path& raw_exported_dir_path,
                                std::set<std::string> unique_triplets,
                                Files::Filesystem& fs)
    {
        std::error_code ec;

        // triplets

        fs::path package_xml_file_path = raw_exported_dir_path / "triplets" / "meta" / "package.xml";
        fs::path package_xml_dir_path = package_xml_file_path.parent_path();
        fs.create_directories(package_xml_dir_path, ec);
        Checks::check_exit(VCPKG_LINE_INFO,
                           !ec,
                           "Could not create directory for package file %s",
                           package_xml_file_path.generic_string());
        fs.write_contents(package_xml_file_path,
                          Strings::format(
                              R"###(<?xml version="1.0"?>
<Package>
    <DisplayName>Triplets</DisplayName>
    <Version>1.0.0</Version>
    <ReleaseDate>%s</ReleaseDate>
</Package>
)###",
                              create_release_date()));

        for (const std::string& triplet : unique_triplets)
        {
            package_xml_file_path =
                raw_exported_dir_path / Strings::format("triplets.%s", triplet) / "meta" / "package.xml";
            package_xml_dir_path = package_xml_file_path.parent_path();
            fs.create_directories(package_xml_dir_path, ec);
            Checks::check_exit(VCPKG_LINE_INFO,
                               !ec,
                               "Could not create directory for package file %s",
                               package_xml_file_path.generic_string());
            fs.write_contents(package_xml_file_path,
                              Strings::format(
                                  R"###(<?xml version="1.0"?>
<Package>
    <DisplayName>%s</DisplayName>
    <Version>1.0.0</Version>
    <ReleaseDate>%s</ReleaseDate>
</Package>
)###",
                                  triplet,
                                  create_release_date()));
        }
    }

    void export_integration(const fs::path& raw_exported_dir_path, Files::Filesystem& fs)
    {
        std::error_code ec;

        // integration
        fs::path package_xml_file_path = raw_exported_dir_path / "integration" / "meta" / "package.xml";
        fs::path package_xml_dir_path = package_xml_file_path.parent_path();
        fs.create_directories(package_xml_dir_path, ec);
        Checks::check_exit(VCPKG_LINE_INFO,
                           !ec,
                           "Could not create directory for package file %s",
                           package_xml_file_path.generic_string());

        fs.write_contents(package_xml_file_path,
                          Strings::format(
                              R"###(<?xml version="1.0"?>
<Package>
    <DisplayName>Integration</DisplayName>
    <Version>1.0.0</Version>
    <ReleaseDate>%s</ReleaseDate>
</Package>
)###",
                              create_release_date()));
    }

    void export_config(const std::string& export_id, const Options& ifw_options, const VcpkgPaths& paths)
    {
        std::error_code ec;
        Files::Filesystem& fs = paths.get_filesystem();

        const fs::path config_xml_file_path = get_config_file_path(export_id, ifw_options, paths);

        fs::path config_xml_dir_path = config_xml_file_path.parent_path();
        fs.create_directories(config_xml_dir_path, ec);
        Checks::check_exit(VCPKG_LINE_INFO,
                           !ec,
                           "Could not create directory for configuration file %s",
                           config_xml_file_path.generic_string());

        std::string formatted_repo_url;
        std::string ifw_repo_url = ifw_options.maybe_repository_url.value_or("");
        if (!ifw_repo_url.empty())
        {
            formatted_repo_url = Strings::format(R"###(
    <RemoteRepositories>
        <Repository>
            <Url>%s</Url>
        </Repository>
    </RemoteRepositories>)###",
                                                 ifw_repo_url);
        }

        fs.write_contents(config_xml_file_path,
                          Strings::format(
                              R"###(<?xml version="1.0"?>
<Installer>
    <Name>vcpkg</Name>
    <Version>1.0.0</Version>
    <StartMenuDir>vcpkg</StartMenuDir>
    <TargetDir>@RootDir@/src/vcpkg</TargetDir>%s
</Installer>
)###",
                              formatted_repo_url));
    }

    void export_maintenance_tool(const fs::path& ifw_packages_dir_path, const VcpkgPaths& paths)
    {
        System::println("Exporting maintenance tool... ");

        std::error_code ec;
        Files::Filesystem& fs = paths.get_filesystem();

        const fs::path& installerbase_exe = paths.get_ifw_installerbase_exe();
        fs::path tempmaintenancetool = ifw_packages_dir_path / "maintenance" / "data" / "tempmaintenancetool.exe";
        fs.create_directories(tempmaintenancetool.parent_path(), ec);
        Checks::check_exit(VCPKG_LINE_INFO,
                           !ec,
                           "Could not create directory for package file %s",
                           tempmaintenancetool.generic_string());
        fs.copy_file(installerbase_exe, tempmaintenancetool, fs::copy_options::overwrite_existing, ec);
        Checks::check_exit(
            VCPKG_LINE_INFO, !ec, "Could not write package file %s", tempmaintenancetool.generic_string());

        fs::path package_xml_file_path = ifw_packages_dir_path / "maintenance" / "meta" / "package.xml";
        fs::path package_xml_dir_path = package_xml_file_path.parent_path();
        fs.create_directories(package_xml_dir_path, ec);
        Checks::check_exit(VCPKG_LINE_INFO,
                           !ec,
                           "Could not create directory for package file %s",
                           package_xml_file_path.generic_string());
        fs.write_contents(package_xml_file_path,
                          Strings::format(
                              R"###(<?xml version="1.0"?>
<Package>
    <DisplayName>Maintenance Tool</DisplayName>
    <Description>Maintenance Tool</Description>
    <Version>1.0.0</Version>
    <ReleaseDate>%s</ReleaseDate>
    <Script>maintenance.qs</Script>
    <Essential>true</Essential>
    <Virtual>true</Virtual>
    <ForcedInstallation>true</ForcedInstallation>
</Package>
)###",
                              create_release_date()));
        const fs::path script_source = paths.root / "scripts" / "ifw" / "maintenance.qs";
        const fs::path script_destination = ifw_packages_dir_path / "maintenance" / "meta" / "maintenance.qs";
        fs.copy_file(script_source, script_destination, fs::copy_options::overwrite_existing, ec);
        Checks::check_exit(
            VCPKG_LINE_INFO, !ec, "Could not write package file %s", script_destination.generic_string());

        System::println("Exporting maintenance tool... done");
    }

    void do_repository(const std::string& export_id, const Options& ifw_options, const VcpkgPaths& paths)
    {
        const fs::path& repogen_exe = paths.get_ifw_repogen_exe();
        const fs::path packages_dir = get_packages_dir_path(export_id, ifw_options, paths);
        const fs::path repository_dir = get_repository_dir_path(export_id, ifw_options, paths);

        System::println("Generating repository %s...", repository_dir.generic_string());

        std::error_code ec;
        Files::Filesystem& fs = paths.get_filesystem();

        fs.remove_all(repository_dir, ec);
        Checks::check_exit(
            VCPKG_LINE_INFO, !ec, "Could not remove outdated repository directory %s", repository_dir.generic_string());

        const std::wstring cmd_line = Strings::wformat(LR"("%s" --packages "%s" "%s" > nul)",
                                                       repogen_exe.native(),
                                                       packages_dir.native(),
                                                       repository_dir.native());

        const int exit_code = System::cmd_execute_clean(cmd_line);
        Checks::check_exit(VCPKG_LINE_INFO, exit_code == 0, "Error: IFW repository generating failed");

        System::println(System::Color::success, "Generating repository %s... done.", repository_dir.generic_string());
    }

    void do_installer(const std::string& export_id, const Options& ifw_options, const VcpkgPaths& paths)
    {
        const fs::path& binarycreator_exe = paths.get_ifw_binarycreator_exe();
        const fs::path config_file = get_config_file_path(export_id, ifw_options, paths);
        const fs::path packages_dir = get_packages_dir_path(export_id, ifw_options, paths);
        const fs::path repository_dir = get_repository_dir_path(export_id, ifw_options, paths);
        const fs::path installer_file = get_installer_file_path(export_id, ifw_options, paths);

        System::println("Generating installer %s...", installer_file.generic_string());

        std::wstring cmd_line;

        std::string ifw_repo_url = ifw_options.maybe_repository_url.value_or("");
        if (!ifw_repo_url.empty())
        {
            cmd_line = Strings::wformat(LR"("%s" --online-only --config "%s" --repository "%s" "%s" > nul)",
                                        binarycreator_exe.native(),
                                        config_file.native(),
                                        repository_dir.native(),
                                        installer_file.native());
        }
        else
        {
            cmd_line = Strings::wformat(LR"("%s" --config "%s" --packages "%s" "%s" > nul)",
                                        binarycreator_exe.native(),
                                        config_file.native(),
                                        packages_dir.native(),
                                        installer_file.native());
        }

        const int exit_code = System::cmd_execute_clean(cmd_line);
        Checks::check_exit(VCPKG_LINE_INFO, exit_code == 0, "Error: IFW installer generating failed");

        System::println(System::Color::success, "Generating installer %s... done.", installer_file.generic_string());
    }

    void do_export(const std::vector<ExportPlanAction>& export_plan,
                   const std::string& export_id,
                   const Options& ifw_options,
                   const VcpkgPaths& paths)
    {
        std::error_code ec;
        Files::Filesystem& fs = paths.get_filesystem();

        // Prepare packages directory
        const fs::path ifw_packages_dir_path = get_packages_dir_path(export_id, ifw_options, paths);

        fs.remove_all(ifw_packages_dir_path, ec);
        Checks::check_exit(VCPKG_LINE_INFO,
                           !ec,
                           "Could not remove outdated packages directory %s",
                           ifw_packages_dir_path.generic_string());

        fs.create_directory(ifw_packages_dir_path, ec);
        Checks::check_exit(
            VCPKG_LINE_INFO, !ec, "Could not create packages directory %s", ifw_packages_dir_path.generic_string());

        // Export maintenance tool
        export_maintenance_tool(ifw_packages_dir_path, paths);

        System::println("Exporting packages %s... ", ifw_packages_dir_path.generic_string());

        // execute the plan
        std::map<std::string, const ExportPlanAction*> unique_packages;
        std::set<std::string> unique_triplets;
        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_control_file.value_or_exit(VCPKG_LINE_INFO).core_paragraph;

            unique_packages[action.spec.name()] = &action;
            unique_triplets.insert(action.spec.triplet().canonical_name());

            // Export real package and return data dir for installation
            fs::path ifw_package_dir_path = export_real_package(ifw_packages_dir_path, action, fs);

            // Copy package data
            const InstallDir dirs = InstallDir::from_destination_root(ifw_package_dir_path,
                                                                      action.spec.triplet().to_string(),
                                                                      ifw_package_dir_path / "vcpkg" / "info" /
                                                                          (binary_paragraph.fullstem() + ".list"));

            Install::install_files_and_write_listfile(paths.get_filesystem(), paths.package_dir(action.spec), dirs);
            System::println("Exporting package %s... done", display_name);
        }

        System::println("Exporting packages %s... done", ifw_packages_dir_path.generic_string());

        const fs::path config_file = get_config_file_path(export_id, ifw_options, paths);

        System::println("Generating configuration %s...", config_file.generic_string());

        // Unique packages
        export_unique_packages(ifw_packages_dir_path, unique_packages, fs);

        // Unique triplets
        export_unique_triplets(ifw_packages_dir_path, unique_triplets, fs);

        // Copy files needed for integration
        export_integration_files(ifw_packages_dir_path / "integration" / "data", paths);
        // Integration
        export_integration(ifw_packages_dir_path, fs);

        // Configuration
        export_config(export_id, ifw_options, paths);

        System::println("Generating configuration %s... done.", config_file.generic_string());

        // Do repository (optional)
        std::string ifw_repo_url = ifw_options.maybe_repository_url.value_or("");
        if (!ifw_repo_url.empty())
        {
            do_repository(export_id, ifw_options, paths);
        }

        // Do installer
        do_installer(export_id, ifw_options, paths);
    }
}