aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/commands_other.cpp
blob: c7dcc2586d45506fba56005dcbd38475664fafc3 (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
#include "vcpkg_Commands.h"
#include <iostream>
#include <unordered_set>
#include "vcpkg_Environment.h"
#include "vcpkg.h"
#include "vcpkg_System.h"
#include "vcpkg_Files.h"

namespace vcpkg
{
    void print_usage()
    {
        std::cout << "Commands:\n"
            "  vcpkg search [pat]              Search for packages available to be built\n"
            "  vcpkg install <pkg>             Install a package\n"
            "  vcpkg remove <pkg>              Uninstall a package. \n"
            "  vcpkg remove --purge <pkg>      Uninstall and delete a package. \n"
            "  vcpkg list                      List installed packages\n"
            "  vcpkg update                    Display list of packages for updating\n"
            "\n"
            << INTEGRATE_COMMAND_HELPSTRING <<
            "\n"
            "  vcpkg edit <pkg>                Open up a port for editing (uses %EDITOR%, default 'code')\n"
            "  vcpkg import <pkg>              Import a pre-built library\n"
            "  vcpkg create <pkg> <url>\n"
            "             [archivename]        Create a new package\n"
            "  vcpkg owns <pat>                Search for files in installed packages\n"
            "  vcpkg cache                     List cached compiled packages\n"
            "  vcpkg version                   Display version information\n"
            "  vcpkg contact                   Display contact information to send feedback\n"
            "\n"
            //"internal commands:\n"
            //"  --check-build-deps <controlfile>\n"
            //"  --create-binary-control <controlfile>\n"
            //"\n"
            "Options:\n"
            "  --triplet <t>                   Specify the target architecture triplet.\n"
            "                                  (default: x86-windows, see 'vcpkg help triplet')\n"
            "\n"
            "  --vcpkg-root <path>             Specify the vcpkg root directory\n"
            "                                  (default: %VCPKG_ROOT%)\n"
            "\n"
            "For more help (including examples) see the accompanying README.md."
            "\n";
    }

    void print_example(const char* command_and_arguments)
    {
        std::cout <<
            "Example:\n"
            "  vcpkg " << command_and_arguments << "\n";
    }

    void update_command(const vcpkg_cmd_arguments& /*args*/, const vcpkg_paths& paths)
    {
        auto status_db = database_load_check(paths);

        std::unordered_map<std::string, std::string> src_names_to_versions;

        auto begin_it = fs::directory_iterator(paths.ports);
        auto end_it = fs::directory_iterator();
        for (; begin_it != end_it; ++begin_it)
        {
            const auto& path = begin_it->path();
            try
            {
                auto pghs = get_paragraphs(path / "CONTROL");
                if (pghs.empty())
                    continue;
                auto srcpgh = SourceParagraph(pghs[0]);
                src_names_to_versions.emplace(srcpgh.name, srcpgh.version);
            }
            catch (std::runtime_error const&)
            {
            }
        }

        std::string packages_list;

        std::vector<std::string> packages_output;
        for (auto&& pgh : database_load_check(paths))
        {
            if (pgh->state == install_state_t::not_installed && pgh->want == want_t::purge)
                continue;
            auto it = src_names_to_versions.find(pgh->package.name);
            if (it == src_names_to_versions.end())
            {
                // Package was not installed from portfile
                continue;
            }
            if (it->second != pgh->package.version)
            {
                packages_output.push_back(Strings::format("%-27s %s -> %s",
                                                          pgh->package.displayname(),
                                                          pgh->package.version,
                                                          it->second));
                packages_list.append(" " + pgh->package.displayname());
            }
        }
        std::sort(packages_output.begin(), packages_output.end());
        if (packages_output.empty())
        {
            System::println("No packages need updating.");
        }
        else
        {
            System::println("The following packages differ from their port versions:");
            for (auto&& package : packages_output)
            {
                System::println("    %s", package.c_str());
            }
            System::println("\nTo update these packages, run\n    vcpkg remove --purge <pkgs>...\n    vcpkg install <pkgs>...");
        }

        auto version_file = Files::get_contents(paths.root / "toolsrc" / "VERSION.txt");
        if (auto version_contents = version_file.get())
        {
            int maj1, min1, rev1;
            auto num1 = sscanf_s(version_contents->c_str(), "\"%d.%d.%d\"", &maj1, &min1, &rev1);

            int maj2, min2, rev2;
            auto num2 = sscanf_s(version().c_str(), "%d.%d.%d-", &maj2, &min2, &rev2);

            if (num1 == 3 && num2 == 3)
            {
                if (maj1 != maj2 || min1 != min2 || rev1 != rev2)
                {
                    System::println("Different source is available for vcpkg (%d.%d.%d -> %d.%d.%d). Use scripts\\bootstrap.ps1 to update.",
                                    maj2, min2, rev2,
                                    maj1, min1, rev1);
                }
            }
        }

        exit(EXIT_SUCCESS);
    }

    void edit_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths, const triplet& default_target_triplet)
    {
        static auto example = "edit zlib";
        args.check_max_args(1, example);
        package_spec spec = args.parse_all_arguments_as_package_specs(default_target_triplet, example).at(0);

        // Find editor
        std::wstring env_EDITOR = System::wdupenv_str(L"EDITOR");
        if (env_EDITOR.empty())
            env_EDITOR = LR"(C:\Program Files (x86)\Microsoft VS Code\Code.exe)";

        auto portpath = paths.ports / spec.name;
        std::wstring cmdLine = Strings::format(LR"("%s" "%s" "%s")", env_EDITOR, portpath.native(), (portpath / "portfile.cmake").native());
        exit(System::cmd_execute(cmdLine));
    }

    void create_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths, const triplet& default_target_triplet)
    {
        args.check_max_args(3);
        package_spec spec = args.parse_all_arguments_as_package_specs(default_target_triplet).at(0);
        if (args.command_arguments.size() < 2)
        {
            System::println(System::color::error, "Error: create requires the archive's URL as the second argument.");
            print_usage();
            exit(EXIT_FAILURE);
        }
        Environment::ensure_utilities_on_path(paths);

        // Space OR define the FILENAME with proper spacing
        std::wstring custom_filename = L" ";
        if (args.command_arguments.size() >= 3)
        {
            custom_filename = Strings::format(L" -DFILENAME=%s ", Strings::utf8_to_utf16(args.command_arguments.at(2)));
        }

        const std::wstring cmdline = Strings::format(LR"(cmake -DCMD=SCAFFOLD -DPORT=%s -DTARGET_TRIPLET=%s -DURL=%s%s-P "%s")",
                                                     Strings::utf8_to_utf16(spec.name),
                                                     Strings::utf8_to_utf16(spec.target_triplet.value),
                                                     Strings::utf8_to_utf16(args.command_arguments.at(1)),
                                                     custom_filename,
                                                     paths.ports_cmake.generic_wstring());

        exit(System::cmd_execute(cmdline));
    }

    void list_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths)
    {
        args.check_max_args(0);

        std::vector<std::string> packages_output;
        for (auto&& pgh : database_load_check(paths))
        {
            if (pgh->state == install_state_t::not_installed && pgh->want == want_t::purge)
                continue;
            packages_output.push_back(Strings::format("%-27s %-16s %s",
                                                      pgh->package.displayname(),
                                                      pgh->package.version,
                                                      shorten_description(pgh->package.description)));
        }
        std::sort(packages_output.begin(), packages_output.end());
        for (auto&& package : packages_output)
        {
            System::println(package.c_str());
        }
        if (packages_output.empty())
        {
            System::println("No packages are installed. Did you mean `search`?");
        }
        exit(EXIT_SUCCESS);
    }

    void import_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths)
    {
        if (args.command_arguments.size() != 3)
        {
            System::println(System::color::error, "Error: %s requires 3 parameters", args.command);
            print_example(Strings::format(R"(%s C:\path\to\CONTROLfile C:\path\to\includedir C:\path\to\projectdir)", args.command).c_str());
            exit(EXIT_FAILURE);
        }

        const fs::path control_file_path(args.command_arguments[0]);
        const fs::path include_directory(args.command_arguments[1]);
        const fs::path project_directory(args.command_arguments[2]);

        auto pghs = get_paragraphs(control_file_path);
        Checks::check_throw(pghs.size() == 1, "Invalid control file for package");

        StatusParagraph spgh;
        spgh.package = BinaryParagraph(pghs[0]);
        auto& control_file_data = spgh.package;

        vcpkg::binary_import(paths, include_directory, project_directory, control_file_data);
        exit(EXIT_SUCCESS);
    }

    void owns_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths)
    {
        args.check_max_args(1);
        if (args.command_arguments.size() == 0)
        {
            System::println(System::color::error, "Error: owns requires a pattern to search for as the first argument.");
            std::cout <<
                "example:\n"
                "    vcpkg owns .dll\n";
            exit(EXIT_FAILURE);
        }
        StatusParagraphs status_db = database_load_check(paths);
        search_file(paths, args.command_arguments[0], status_db);
        exit(EXIT_SUCCESS);
    }

    void internal_test_command(const vcpkg_cmd_arguments& /*args*/, const vcpkg_paths& /*paths*/)
    {
        //        auto data = FormatEventData("test");
        //        Track(data);
        exit(EXIT_SUCCESS);
    }

    const std::vector<package_name_and_function<command_type_a>>& get_available_commands_type_a()
    {
        static std::vector<package_name_and_function<command_type_a>> t = {
            {"install", install_command},
            {"remove", remove_command},
            {"build", build_command},
            {"edit", edit_command},
            {"create", create_command},
            {"build_external", build_external_command}
        };
        return t;
    }

    const std::vector<package_name_and_function<command_type_b>>& get_available_commands_type_b()
    {
        static std::vector<package_name_and_function<command_type_b>> t = {
            {"help", help_command},
            {"search", search_command},
            {"list", list_command},
            {"integrate", integrate_command},
            {"owns", owns_command},
            {"update", update_command},
            {"import", import_command},
            {"cache", cache_command},
            {"internal_test", internal_test_command},
        };
        return t;
    }

    const std::vector<package_name_and_function<command_type_c>>& get_available_commands_type_c()
    {
        static std::vector<package_name_and_function<command_type_c>> t = {
            {"version", &version_command},
            {"contact", &contact_command}
        };
        return t;
    }
}