diff options
| author | Alexander Karatarakis <alkarata@microsoft.com> | 2017-11-26 02:49:23 -0800 |
|---|---|---|
| committer | Alexander Karatarakis <alkarata@microsoft.com> | 2017-11-26 02:49:23 -0800 |
| commit | 2af7fe8690a9b78f1eab1e532670ed133f82aff9 (patch) | |
| tree | 8e172dce1b907bd28c6a024b75afdbbd5aca7905 | |
| parent | 92872439b9e76ec881edae187d5fb137f02ce39b (diff) | |
| download | vcpkg-2af7fe8690a9b78f1eab1e532670ed133f82aff9.tar.gz vcpkg-2af7fe8690a9b78f1eab1e532670ed133f82aff9.zip | |
Add System::powershell_execute()
| -rw-r--r-- | toolsrc/include/vcpkg/base/system.h | 4 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg/base/system.cpp | 43 |
2 files changed, 41 insertions, 6 deletions
diff --git a/toolsrc/include/vcpkg/base/system.h b/toolsrc/include/vcpkg/base/system.h index db537db4b..31034f6b4 100644 --- a/toolsrc/include/vcpkg/base/system.h +++ b/toolsrc/include/vcpkg/base/system.h @@ -44,6 +44,10 @@ namespace vcpkg::System ExitCodeAndOutput cmd_execute_and_capture_output(const CStringView cmd_line); + 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 = {}); diff --git a/toolsrc/src/vcpkg/base/system.cpp b/toolsrc/src/vcpkg/base/system.cpp index 9d8e1ce84..e3d3ad292 100644 --- a/toolsrc/src/vcpkg/base/system.cpp +++ b/toolsrc/src/vcpkg/base/system.cpp @@ -119,6 +119,16 @@ namespace vcpkg::System { } + 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) { #if defined(_WIN32) @@ -317,16 +327,37 @@ namespace vcpkg::System #endif } + void powershell_execute(const std::string& title, + const fs::path& script_path, + const std::vector<PowershellParameter>& parameters) + { + const std::string cmd = make_powershell_cmd(script_path, parameters); + const int rc = System::cmd_execute(cmd); + + 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); + } + } + std::string powershell_execute_and_capture_output(const std::string& title, 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 - const std::string cmd = Strings::format( - R"(powershell -NoProfile -ExecutionPolicy Bypass -Command "& {& '%s' %s}")", script_path.u8string(), args); - + const std::string cmd = make_powershell_cmd(script_path, parameters); auto rc = System::cmd_execute_and_capture_output(cmd); if (rc.exit_code) |
