diff options
| author | Alexander Karatarakis <alkarata@microsoft.com> | 2018-05-19 19:01:10 -0700 |
|---|---|---|
| committer | Alexander Karatarakis <alkarata@microsoft.com> | 2018-05-19 19:23:33 -0700 |
| commit | 285c69b0faa220311f3ccc60febaa2e7c6d87bb1 (patch) | |
| tree | e32f06e376706f0b1049e6c718f715ca2e1a9fd1 | |
| parent | 6ccd43dfa7bedbfe9323e13085a476cca5d616d5 (diff) | |
| download | vcpkg-285c69b0faa220311f3ccc60febaa2e7c6d87bb1.tar.gz vcpkg-285c69b0faa220311f3ccc60febaa2e7c6d87bb1.zip | |
[c++] Condense powershell helper code into the remaining single usage
`vcpkg integrate powershell` uses it
| -rw-r--r-- | toolsrc/include/vcpkg/base/system.h | 19 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg/base/system.cpp | 106 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg/commands.integrate.cpp | 42 |
3 files changed, 39 insertions, 128 deletions
diff --git a/toolsrc/include/vcpkg/base/system.h b/toolsrc/include/vcpkg/base/system.h index cf9c78868..813d600cd 100644 --- a/toolsrc/include/vcpkg/base/system.h +++ b/toolsrc/include/vcpkg/base/system.h @@ -25,15 +25,6 @@ namespace vcpkg::System const fs::path& cmake_script, const std::vector<CMakeVariable>& pass_variables); - struct PowershellParameter - { - PowershellParameter(const CStringView varname, const char* varvalue); - PowershellParameter(const CStringView varname, const std::string& varvalue); - PowershellParameter(const CStringView varname, const fs::path& path); - - std::string s; - }; - struct ExitCodeAndOutput { int exit_code; @@ -47,16 +38,6 @@ namespace vcpkg::System ExitCodeAndOutput cmd_execute_and_capture_output(const CStringView cmd_line); -#if defined(_WIN32) - void powershell_execute(const std::string& title, - const fs::path& script_path, - const std::vector<PowershellParameter>& parameters = {}); - - std::string powershell_execute_and_capture_output(const std::string& title, - const fs::path& script_path, - const std::vector<PowershellParameter>& parameters = {}); -#endif - enum class Color { success = 10, diff --git a/toolsrc/src/vcpkg/base/system.cpp b/toolsrc/src/vcpkg/base/system.cpp index 73a92f2da..1aa12d0a4 100644 --- a/toolsrc/src/vcpkg/base/system.cpp +++ b/toolsrc/src/vcpkg/base/system.cpp @@ -129,31 +129,6 @@ namespace vcpkg::System R"("%s" %s -P "%s")", cmake_exe.u8string(), cmd_cmake_pass_variables, cmake_script.generic_u8string()); } - PowershellParameter::PowershellParameter(const CStringView varname, const char* varvalue) - : s(Strings::format(R"(-%s '%s')", varname, varvalue)) - { - } - - PowershellParameter::PowershellParameter(const CStringView varname, const std::string& varvalue) - : PowershellParameter(varname, varvalue.c_str()) - { - } - - PowershellParameter::PowershellParameter(const CStringView varname, const fs::path& path) - : PowershellParameter(varname, path.generic_u8string()) - { - } - - static std::string make_powershell_cmd(const fs::path& script_path, - const std::vector<PowershellParameter>& parameters) - { - const std::string args = Strings::join(" ", parameters, [](auto&& v) { return v.s; }); - - // TODO: switch out ExecutionPolicy Bypass with "Remove Mark Of The Web" code and restore RemoteSigned - return Strings::format( - R"(powershell -NoProfile -ExecutionPolicy Bypass -Command "& {& '%s' %s}")", script_path.u8string(), args); - } - int cmd_execute_clean(const CStringView cmd_line, const std::unordered_map<std::string, std::string>& extra_env) { auto timer = Chrono::ElapsedTimer::create_started(); @@ -371,87 +346,6 @@ namespace vcpkg::System #endif } -#if defined(_WIN32) - void powershell_execute(const std::string& title, - const fs::path& script_path, - const std::vector<PowershellParameter>& parameters) - { - SetConsoleCP(437); - SetConsoleOutputCP(437); - - const std::string cmd = make_powershell_cmd(script_path, parameters); - const int rc = System::cmd_execute(cmd); - - SetConsoleCP(CP_UTF8); - SetConsoleOutputCP(CP_UTF8); - - if (rc) - { - System::println(Color::error, - "%s\n" - "Could not run:\n" - " '%s'", - title, - script_path.generic_string()); - - { - auto locked_metrics = Metrics::g_metrics.lock(); - locked_metrics->track_property("error", "powershell script failed"); - locked_metrics->track_property("title", title); - } - - Checks::exit_with_code(VCPKG_LINE_INFO, rc); - } - } -#endif - -#if defined(_WIN32) - std::string powershell_execute_and_capture_output(const std::string& title, - const fs::path& script_path, - const std::vector<PowershellParameter>& parameters) - { - SetConsoleCP(437); - SetConsoleOutputCP(437); - - const std::string cmd = make_powershell_cmd(script_path, parameters); - auto rc = System::cmd_execute_and_capture_output(cmd); - - SetConsoleCP(CP_UTF8); - SetConsoleOutputCP(CP_UTF8); - - if (rc.exit_code) - { - System::println(Color::error, - "%s\n" - "Could not run:\n" - " '%s'\n" - "Error message was:\n" - " %s", - title, - script_path.generic_string(), - rc.output); - - { - auto locked_metrics = Metrics::g_metrics.lock(); - locked_metrics->track_property("error", "powershell script failed"); - locked_metrics->track_property("title", title); - } - - Checks::exit_with_code(VCPKG_LINE_INFO, rc.exit_code); - } - - // Remove newline from all output. - // Powershell returns newlines when it hits the column count of the console. - // For example, this is 80 in cmd on Windows 7. If the expected output is longer than 80 lines, we get - // newlines in-between the data. - // To solve this, we design our interaction with powershell to not depend on newlines, - // and then strip all newlines here. - rc.output = Strings::replace_all(std::move(rc.output), "\n", ""); - - return rc.output; - } -#endif - void println() { putchar('\n'); } void print(const CStringView message) { fputs(message.c_str(), stdout); } diff --git a/toolsrc/src/vcpkg/commands.integrate.cpp b/toolsrc/src/vcpkg/commands.integrate.cpp index 8897ea138..82172e363 100644 --- a/toolsrc/src/vcpkg/commands.integrate.cpp +++ b/toolsrc/src/vcpkg/commands.integrate.cpp @@ -5,6 +5,7 @@ #include <vcpkg/base/system.h> #include <vcpkg/base/util.h> #include <vcpkg/commands.h> +#include <vcpkg/metrics.h> #include <vcpkg/userconfig.h> namespace vcpkg::Commands::Integrate @@ -369,6 +370,43 @@ With a project open, go to Tools->NuGet Package Manager->Package Manager Console #endif #if defined(_WIN32) + static void integrate_powershell(const VcpkgPaths& paths) + { + static constexpr StringLiteral TITLE = "PowerShell Tab-Completion"; + const fs::path script_path = paths.scripts / "addPoshVcpkgToPowershellProfile.ps1"; + + // Console font corruption workaround + SetConsoleCP(437); + SetConsoleOutputCP(437); + + const std::string cmd = Strings::format( + R"(powershell -NoProfile -ExecutionPolicy Bypass -Command "& {& '%s' %s}")", script_path.u8string(), ""); + const int rc = System::cmd_execute(cmd); + + SetConsoleCP(CP_UTF8); + SetConsoleOutputCP(CP_UTF8); + + if (rc) + { + System::println(System::Color::error, + "%s\n" + "Could not run:\n" + " '%s'", + TITLE, + script_path.generic_string()); + + { + auto locked_metrics = Metrics::g_metrics.lock(); + locked_metrics->track_property("error", "powershell script failed"); + locked_metrics->track_property("title", TITLE); + } + } + + Checks::exit_with_code(VCPKG_LINE_INFO, rc); + } +#endif + +#if defined(_WIN32) const char* const INTEGRATE_COMMAND_HELPSTRING = " vcpkg integrate install Make installed packages available user-wide. Requires admin privileges on " "first use\n" @@ -423,9 +461,7 @@ With a project open, go to Tools->NuGet Package Manager->Package Manager Console } if (args.command_arguments[0] == Subcommand::POWERSHELL) { - System::powershell_execute("PowerShell Tab-Completion", - paths.scripts / "addPoshVcpkgToPowershellProfile.ps1"); - Checks::exit_success(VCPKG_LINE_INFO); + return integrate_powershell(paths); } #endif |
