aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/commands_autocomplete.cpp
blob: 6ded74f9b9ab74e24f36d0b295af979bef960f11 (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
#include "pch.h"

#include "Paragraphs.h"
#include "metrics.h"
#include "vcpkg_Commands.h"
#include "vcpkg_System.h"
#include "vcpkglib.h"
#include <regex>

namespace vcpkg::Commands::Autocomplete
{
    std::vector<std::string> autocomplete_install(
        const std::vector<std::unique_ptr<SourceControlFile>>& source_paragraphs, const std::string& start_with)
    {
        std::vector<std::string> results;

        for (const auto& source_control_file : source_paragraphs)
        {
            auto&& sp = *source_control_file->core_paragraph;

            if (Strings::case_insensitive_ascii_starts_with(sp.name, start_with))
            {
                results.push_back(sp.name);
            }
        }
        return results;
    }

    std::vector<std::string> autocomplete_remove(std::vector<StatusParagraph*> installed_packages,
                                                 const std::string& start_with)
    {
        std::vector<std::string> results;

        for (const auto& installed_package : installed_packages)
        {
            const auto sp = installed_package->package.displayname();

            if (Strings::case_insensitive_ascii_starts_with(sp, start_with))
            {
                results.push_back(sp);
            }
        }
        return results;
    }

    [[noreturn]] static void output_sorted_results_and_exit(const LineInfo& line_info,
                                                            std::vector<std::string>&& results)
    {
        const SortedVector<std::string> sorted_results(results);
        System::println(Strings::join("\n", sorted_results));
        Checks::exit_success(line_info);
    }

    void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
    {
        Metrics::g_metrics.lock()->set_send_metrics(false);
        const std::string to_autocomplete = Strings::join(" ", args.command_arguments);
        const std::vector<std::string> tokens = Strings::split(to_autocomplete, " ");

        std::smatch match;

        // Handles vcpkg <command>
        if (std::regex_match(to_autocomplete, match, std::regex{R"###(^(\S*)$)###"}))
        {
            const std::string requested_command = match[1].str();

            std::vector<std::string> valid_commands = {"install",
                                                       "search",
                                                       "remove",
                                                       "list",
                                                       "update",
                                                       "hash",
                                                       "help",
                                                       "integrate",
                                                       "export",
                                                       "edit",
                                                       "create",
                                                       "owns",
                                                       "cache",
                                                       "version",
                                                       "contact"};

            Util::unstable_keep_if(valid_commands, [&](const std::string& s) {
                return Strings::case_insensitive_ascii_starts_with(s, requested_command);
            });

            output_sorted_results_and_exit(VCPKG_LINE_INFO, std::move(valid_commands));
        }

        // Handles vcpkg install <package>
        if (std::regex_match(to_autocomplete, match, std::regex{R"###(^install.* (\S*)$)###"}))
        {
            const std::string start_with = match[1].str();
            auto sources_and_errors = Paragraphs::try_load_all_ports(paths.get_filesystem(), paths.ports);
            auto& source_paragraphs = sources_and_errors.paragraphs;
            output_sorted_results_and_exit(VCPKG_LINE_INFO, autocomplete_install(source_paragraphs, start_with));
        }

        // Handles vcpkg remove <package>
        if (std::regex_match(to_autocomplete, match, std::regex{R"###(^remove.* (\S*)$)###"}))
        {
            const std::string start_with = match[1].str();
            const StatusParagraphs status_db = database_load_check(paths);
            const std::vector<StatusParagraph*> installed_packages = get_installed_ports(status_db);
            output_sorted_results_and_exit(VCPKG_LINE_INFO, autocomplete_remove(installed_packages, start_with));
        }

        Checks::exit_success(VCPKG_LINE_INFO);
    }
}