Fix integrate to always know its subcommands. (#1161)

This commit is contained in:
Billy O'Neal 2023-08-22 17:27:11 -07:00 коммит произвёл GitHub
Родитель 95d864dc82
Коммит 6b2f36a769
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 107 добавлений и 71 удалений

Просмотреть файл

@ -28,8 +28,8 @@ $privateCommands = @(
'portsdiff'
)
function getOptionsForPrefix($prefix, $commands) {
($commands | Sort-Object | ? { $_.StartsWith($prefix) }) -join '`n'
function getOptionsForPrefix([string]$prefix, $commands) {
($commands | Sort-Object | ? { $_.StartsWith($prefix.ToLowerInvariant()) }) -join '`n'
}
function arraysEqual($arr1, $arr2) {
if ($arr1.Length -ne $arr2.Length) {
@ -48,6 +48,7 @@ $publicPrefixesToTest = @(
'in'
's'
'rem'
'rEm'
'h'
'upgra'
'e'

Просмотреть файл

@ -1522,23 +1522,34 @@ DECLARE_MESSAGE(InstallWithSystemManagerPkg,
DECLARE_MESSAGE(IntegrateBashHelp,
(),
"'bash' is a terminal program which should be unlocalized.",
"Enable bash tab-completion.")
"Enable bash tab-completion. Non-Windows only.")
DECLARE_MESSAGE(IntegrateFishHelp,
(),
"'fish' is a terminal program which should be unlocalized.",
"Enable fish tab-completion.")
"Enable fish tab-completion. Non-Windows only.")
DECLARE_MESSAGE(IntegrateInstallHelpLinux, (), "", "Make installed packages available user-wide.")
DECLARE_MESSAGE(IntegrateInstallHelpWindows,
(),
"",
"Make installed packages available user-wide. Requires admin privileges on first use.")
DECLARE_MESSAGE(IntegratePowerShellHelp, (), "", "Enable PowerShell tab-completion.")
DECLARE_MESSAGE(IntegrateProjectHelp, (), "", "Generate a referencing NuGet package for individual VS project use.")
DECLARE_MESSAGE(IntegrateNonWindowsOnly,
(msg::command_line),
"",
"{command_line} is non-Windows-only and not supported on this system.")
DECLARE_MESSAGE(IntegratePowerShellHelp, (), "", "Enable PowerShell tab-completion. Windows-only.")
DECLARE_MESSAGE(IntegrateProjectHelp,
(),
"",
"Generate a referencing NuGet package for individual Visual Studio project use. Windows-only.")
DECLARE_MESSAGE(IntegrateRemoveHelp, (), "", "Remove user-wide integration.")
DECLARE_MESSAGE(IntegrateWindowsOnly,
(msg::command_line),
"",
"{command_line} is Windows-only and not supported on this system.")
DECLARE_MESSAGE(IntegrateZshHelp,
(),
"'zsh' is a terminal program which should be unlocalized.",
"Enable zsh tab-completion.")
"Enable zsh tab-completion. Non-Windows only.")
DECLARE_MESSAGE(IntegrationFailedVS2015, (), "", "Integration was not applied for Visual Studio 2015.")
DECLARE_MESSAGE(InternalCICommand,
(),

Просмотреть файл

@ -863,16 +863,20 @@
"_InstallingMavenFile.comment": "Printed after a filesystem operation error An example of {path} is /foo/bar.",
"InstallingPackage": "Installing {action_index}/{count} {spec}...",
"_InstallingPackage.comment": "An example of {action_index} is 340. An example of {count} is 42. An example of {spec} is zlib:x64-windows.",
"IntegrateBashHelp": "Enable bash tab-completion.",
"IntegrateBashHelp": "Enable bash tab-completion. Non-Windows only.",
"_IntegrateBashHelp.comment": "'bash' is a terminal program which should be unlocalized.",
"IntegrateFishHelp": "Enable fish tab-completion.",
"IntegrateFishHelp": "Enable fish tab-completion. Non-Windows only.",
"_IntegrateFishHelp.comment": "'fish' is a terminal program which should be unlocalized.",
"IntegrateInstallHelpLinux": "Make installed packages available user-wide.",
"IntegrateInstallHelpWindows": "Make installed packages available user-wide. Requires admin privileges on first use.",
"IntegratePowerShellHelp": "Enable PowerShell tab-completion.",
"IntegrateProjectHelp": "Generate a referencing NuGet package for individual VS project use.",
"IntegrateNonWindowsOnly": "{command_line} is non-Windows-only and not supported on this system.",
"_IntegrateNonWindowsOnly.comment": "An example of {command_line} is vcpkg install zlib.",
"IntegratePowerShellHelp": "Enable PowerShell tab-completion. Windows-only.",
"IntegrateProjectHelp": "Generate a referencing NuGet package for individual Visual Studio project use. Windows-only.",
"IntegrateRemoveHelp": "Remove user-wide integration.",
"IntegrateZshHelp": "Enable zsh tab-completion.",
"IntegrateWindowsOnly": "{command_line} is Windows-only and not supported on this system.",
"_IntegrateWindowsOnly.comment": "An example of {command_line} is vcpkg install zlib.",
"IntegrateZshHelp": "Enable zsh tab-completion. Non-Windows only.",
"_IntegrateZshHelp.comment": "'zsh' is a terminal program which should be unlocalized.",
"IntegrationFailedVS2015": "Integration was not applied for Visual Studio 2015.",
"InternalCICommand": "vcpkg ci is an internal command which will change incompatibly or be removed at any time.",

Просмотреть файл

@ -97,7 +97,7 @@ namespace vcpkg::Commands::Autocomplete
const auto& command_name = command_arguments[0];
// Handles vcpkg install package:<triplet>
if (command_name == "install")
if (Strings::case_insensitive_ascii_equals(command_name, "install"))
{
StringView last_arg = command_arguments.back();
auto colon = Util::find(last_arg, ':');
@ -140,7 +140,7 @@ namespace vcpkg::Commands::Autocomplete
for (auto&& command : COMMANDS)
{
if (command_name == command.name)
if (Strings::case_insensitive_ascii_equals(command_name, command.name))
{
StringView prefix = command_arguments.back();
std::vector<std::string> results;
@ -173,7 +173,8 @@ namespace vcpkg::Commands::Autocomplete
return !Strings::case_insensitive_ascii_starts_with(s, prefix);
});
if (command.name == "install" && results.size() == 1 && !is_option)
if (Strings::case_insensitive_ascii_equals(command.name, "install") && results.size() == 1 &&
!is_option)
{
const auto port_at_each_triplet =
combine_port_with_triplets(results[0], paths.get_triplet_db().available_triplets);

Просмотреть файл

@ -122,12 +122,8 @@ namespace vcpkg::Commands::Integrate
target_path,
target_path);
}
#endif
#if defined(_WIN32)
static std::string create_system_targets_shortcut()
{
return R"###(
static constexpr StringLiteral SystemTargetsShortcut = R"###(
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- version 1 -->
<PropertyGroup>
@ -137,10 +133,7 @@ namespace vcpkg::Commands::Integrate
<Import Condition="'$(VCLibPackagePath)' != '' and Exists('$(VCLibPackagePath).targets')" Project="$(VCLibPackagePath).targets" />
</Project>
)###";
}
#endif
#if defined(_WIN32)
static std::string create_nuget_targets_file_contents(const Path& msbuild_vcpkg_targets_file)
{
return fmt::format(R"###(
@ -154,7 +147,7 @@ namespace vcpkg::Commands::Integrate
msbuild_vcpkg_targets_file,
msbuild_vcpkg_targets_file);
}
#endif
#endif // ^^^ _WIN32
#if defined(_WIN32)
static constexpr StringLiteral NUGET_PROPS_FILE_CONTENTS = R"###(
@ -164,7 +157,7 @@ namespace vcpkg::Commands::Integrate
</PropertyGroup>
</Project>
)###";
#endif
#endif // ^^^ _WIN32
#if defined(_WIN32)
static std::string get_nuget_id(const Path& vcpkg_root_dir)
@ -176,10 +169,9 @@ namespace vcpkg::Commands::Integrate
// NuGet id cannot have invalid characters. We will only use alphanumeric and dot.
Util::erase_remove_if(dir_id, [](char c) { return !isalnum(static_cast<unsigned char>(c)) && (c != '.'); });
const std::string nuget_id = "vcpkg." + dir_id;
return nuget_id;
return "vcpkg." + dir_id;
}
#endif
#endif // ^^^ _WIN32
#if defined(_WIN32)
static std::string create_nuspec_file_contents(const Path& vcpkg_root_dir,
@ -208,7 +200,7 @@ namespace vcpkg::Commands::Integrate
Strings::inplace_replace_all(content, "@VERSION@", nupkg_version);
return content;
}
#endif
#endif // ^^^ _WIN32
#if defined(_WIN32)
enum class ElevationPromptChoice
@ -244,7 +236,7 @@ namespace vcpkg::Commands::Integrate
CloseHandle(sh_ex_info.hProcess);
return ElevationPromptChoice::YES;
}
#endif
#endif // ^^^ _WIN32
static constexpr StringLiteral vcpkg_path_txt = "vcpkg.path.txt";
@ -291,7 +283,7 @@ namespace vcpkg::Commands::Integrate
const auto tmp_dir = fs.create_or_get_temp_directory(VCPKG_LINE_INFO);
const auto sys_src_path = tmp_dir / "vcpkg.system.targets";
fs.write_contents(sys_src_path, create_system_targets_shortcut(), VCPKG_LINE_INFO);
fs.write_contents(sys_src_path, SystemTargetsShortcut, VCPKG_LINE_INFO);
const std::string param = fmt::format(R"(/c "mkdir "{}" & copy "{}" "{}" /Y > nul")",
SYSTEM_WIDE_TARGETS_FILE.parent_path(),
@ -308,7 +300,7 @@ namespace vcpkg::Commands::Integrate
return true;
}
#endif
#endif // ^^^ _WIN32
static void integrate_install(const VcpkgPaths& paths)
{
@ -337,7 +329,7 @@ namespace vcpkg::Commands::Integrate
}
message.append_raw("\n\n").append(msgAutomaticLinkingForMSBuildProjects);
#endif
#endif // ^^^ _WIN32
msg::println(Color::success, msgAppliedUserIntegration);
msg::println(message);
@ -351,7 +343,7 @@ namespace vcpkg::Commands::Integrate
#if defined(_WIN32)
was_deleted |= fs.remove(user_configuration_home / vcpkg_user_props, VCPKG_LINE_INFO);
was_deleted |= fs.remove(user_configuration_home / vcpkg_user_targets, VCPKG_LINE_INFO);
#endif
#endif // ^^^ _WIN32
was_deleted |= fs.remove(user_configuration_home / vcpkg_path_txt, VCPKG_LINE_INFO);
if (was_deleted)
@ -366,9 +358,9 @@ namespace vcpkg::Commands::Integrate
Checks::exit_success(VCPKG_LINE_INFO);
}
#if defined(WIN32)
static void integrate_project(const VcpkgPaths& paths)
{
#if defined(WIN32)
auto& fs = paths.get_filesystem();
const Path& nuget_exe = paths.get_tool_exe(Tools::NUGET, stdout_sink);
@ -416,12 +408,16 @@ namespace vcpkg::Commands::Integrate
msg::println(msgInstallPackageInstruction, msg::value = nuget_id, msg::path = source_path);
Checks::exit_success(VCPKG_LINE_INFO);
#else // ^^^ _WIN32 // !_WIN32 vvv
(void)paths;
Checks::msg_exit_with_error(
VCPKG_LINE_INFO, msgIntegrateWindowsOnly, msg::command_line = "vcpkg integrate project");
#endif // ^^^ !_WIN32
}
#endif
#if defined(_WIN32)
static void integrate_powershell(const VcpkgPaths& paths)
{
#if defined(_WIN32)
static constexpr StringLiteral TITLE = "PowerShell Tab-Completion";
const auto script_path = paths.scripts / "addPoshVcpkgToPowershellProfile.ps1";
@ -442,10 +438,20 @@ namespace vcpkg::Commands::Integrate
}
Checks::exit_with_code(VCPKG_LINE_INFO, rc);
#else // ^^^ _WIN32 // !_WIN32 vvv
(void)paths;
Checks::msg_exit_with_error(
VCPKG_LINE_INFO, msgIntegrateWindowsOnly, msg::command_line = "vcpkg integrate powershell");
#endif // ^^^ !_WIN32
}
#else
static void integrate_bash(const VcpkgPaths& paths)
{
#if defined(_WIN32)
(void)paths;
Checks::msg_exit_with_error(
VCPKG_LINE_INFO, msgIntegrateNonWindowsOnly, msg::command_line = "vcpkg integrate bash");
#else // ^^^ _WIN32 // !_WIN32 vvv
const auto home_path = get_environment_variable("HOME").value_or_exit(VCPKG_LINE_INFO);
#if defined(__APPLE__)
const auto bashrc_path = Path{home_path} / ".bash_profile";
@ -473,10 +479,16 @@ namespace vcpkg::Commands::Integrate
bashrc_content.push_back('\n');
fs.write_contents(bashrc_path, bashrc_content, VCPKG_LINE_INFO);
Checks::exit_success(VCPKG_LINE_INFO);
#endif // ^^^ !_WIN32
}
static void integrate_zsh(const VcpkgPaths& paths)
{
#if defined(_WIN32)
(void)paths;
Checks::msg_exit_with_error(
VCPKG_LINE_INFO, msgIntegrateNonWindowsOnly, msg::command_line = "vcpkg integrate zsh");
#else // ^^^ _WIN32 // !_WIN32 vvv
const auto home_path = get_environment_variable("HOME").value_or_exit(VCPKG_LINE_INFO);
const auto zshrc_path = Path{home_path} / ".zshrc";
@ -510,10 +522,16 @@ namespace vcpkg::Commands::Integrate
zshrc_content.push_back('\n');
fs.write_contents(zshrc_path, zshrc_content, VCPKG_LINE_INFO);
Checks::exit_success(VCPKG_LINE_INFO);
#endif // ^^^ !_WIN32
}
static void integrate_fish(const VcpkgPaths& paths)
{
#if defined(_WIN32)
(void)paths;
Checks::msg_exit_with_error(
VCPKG_LINE_INFO, msgIntegrateNonWindowsOnly, msg::command_line = "vcpkg integrate x-fish");
#else // ^^^ _WIN32 // !_WIN32 vvv
Path fish_completions_path;
const auto config_path = get_environment_variable("XDG_CONFIG_HOME");
if (config_path.has_value())
@ -545,23 +563,24 @@ namespace vcpkg::Commands::Integrate
msg::println(msgAddingCompletionEntry, msg::path = fish_completions_path);
fs.create_symlink(completion_script_path, fish_completions_path, VCPKG_LINE_INFO);
Checks::exit_success(VCPKG_LINE_INFO);
#endif // ^^^ !_WIN32
}
#endif
void append_helpstring(HelpTableFormatter& table)
{
#if defined(_WIN32)
table.format("vcpkg integrate install", msg::format(msgIntegrateInstallHelpWindows));
table.format("vcpkg integrate remove", msg::format(msgIntegrateRemoveHelp));
table.format("vcpkg integrate project", msg::format(msgIntegrateProjectHelp));
table.format("vcpkg integrate powershell", msg::format(msgIntegratePowerShellHelp));
#else // ^^^ defined(_WIN32) // !defined(_WIN32) vvv
table.format("vcpkg integrate install", msg::format(msgIntegrateInstallHelpLinux));
table.format("vcpkg integrate remove", msg::format(msgIntegrateRemoveHelp));
table.format("vcpkg integrate bash", msg::format(msgIntegrateBashHelp));
table.format("vcpkg integrate zsh", msg::format(msgIntegrateZshHelp));
table.format("vcpkg integrate x-fish", msg::format(msgIntegrateFishHelp));
#endif // ^^^ !defined(_WIN32)
table.format("vcpkg integrate remove", msg::format(msgIntegrateRemoveHelp));
table.blank();
table.format("vcpkg integrate project", msg::format(msgIntegrateProjectHelp));
table.blank();
table.format("vcpkg integrate bash", msg::format(msgIntegrateBashHelp));
table.format("vcpkg integrate x-fish", msg::format(msgIntegrateFishHelp));
table.format("vcpkg integrate powershell", msg::format(msgIntegratePowerShellHelp));
table.format("vcpkg integrate zsh", msg::format(msgIntegrateZshHelp));
}
LocalizedString get_helpstring()
@ -571,28 +590,24 @@ namespace vcpkg::Commands::Integrate
return msg::format(msgCommands).append_raw('\n').append_raw(table.m_str);
}
namespace Subcommand
{
static constexpr StringLiteral INSTALL = "install";
static constexpr StringLiteral REMOVE = "remove";
#if defined(_WIN32)
static constexpr StringLiteral PROJECT = "project";
static constexpr StringLiteral POWERSHELL = "powershell";
#endif // ^^^ _WIN32
static constexpr StringLiteral BASH = "bash";
static constexpr StringLiteral ZSH = "zsh";
static constexpr StringLiteral FISH = "x-fish";
}
static constexpr StringLiteral INSTALL = "install";
static constexpr StringLiteral REMOVE = "remove";
static constexpr StringLiteral PROJECT = "project";
static constexpr StringLiteral POWERSHELL = "powershell";
static constexpr StringLiteral BASH = "bash";
static constexpr StringLiteral ZSH = "zsh";
static constexpr StringLiteral FISH = "x-fish";
static std::vector<std::string> valid_arguments(const VcpkgPaths&)
{
// Note that help lists all supported args, but we only want to autocomplete the ones valid on this platform
return
{
Subcommand::INSTALL.to_string(), Subcommand::REMOVE.to_string(),
INSTALL.to_string(), REMOVE.to_string(),
#if defined(_WIN32)
Subcommand::PROJECT.to_string(), Subcommand::POWERSHELL.to_string(),
PROJECT.to_string(), POWERSHELL.to_string(),
#else
Subcommand::BASH.to_string(), Subcommand::FISH.to_string(),
BASH.to_string(), FISH.to_string(), ZSH.to_string()
#endif
};
}
@ -609,37 +624,41 @@ namespace vcpkg::Commands::Integrate
{
const auto parsed = args.parse_arguments(COMMAND_STRUCTURE);
if (parsed.command_arguments[0] == Subcommand::INSTALL)
if (Strings::case_insensitive_ascii_equals(parsed.command_arguments[0], INSTALL))
{
return integrate_install(paths);
}
if (parsed.command_arguments[0] == Subcommand::REMOVE)
if (Strings ::case_insensitive_ascii_equals(parsed.command_arguments[0], REMOVE))
{
return integrate_remove(paths.get_filesystem());
}
#if defined(_WIN32)
if (parsed.command_arguments[0] == Subcommand::PROJECT)
if (Strings ::case_insensitive_ascii_equals(parsed.command_arguments[0], PROJECT))
{
return integrate_project(paths);
}
if (parsed.command_arguments[0] == Subcommand::POWERSHELL)
if (Strings ::case_insensitive_ascii_equals(parsed.command_arguments[0], POWERSHELL))
{
return integrate_powershell(paths);
}
#else
if (parsed.command_arguments[0] == Subcommand::BASH)
if (Strings ::case_insensitive_ascii_equals(parsed.command_arguments[0], BASH))
{
return integrate_bash(paths);
}
if (parsed.command_arguments[0] == Subcommand::ZSH)
if (Strings ::case_insensitive_ascii_equals(parsed.command_arguments[0], ZSH))
{
return integrate_zsh(paths);
}
if (parsed.command_arguments[0] == Subcommand::FISH)
if (Strings ::case_insensitive_ascii_equals(parsed.command_arguments[0], FISH))
{
return integrate_fish(paths);
}
#endif
Checks::msg_exit_maybe_upgrade(
VCPKG_LINE_INFO, msgUnknownParameterForIntegrate, msg::value = parsed.command_arguments[0]);
}