Add System::powershell_execute()
MODIFY
include/vcpkg/base/system.h
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 = {});

MODIFY
src/vcpkg/base/system.cpp
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
}
- std::string powershell_execute_and_capture_output(const std::string& title,
+ void powershell_execute(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; });
+ const std::string cmd = make_powershell_cmd(script_path, parameters);
+ const int rc = System::cmd_execute(cmd);
- // 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);
+ 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 cmd = make_powershell_cmd(script_path, parameters);
auto rc = System::cmd_execute_and_capture_output(cmd);
if (rc.exit_code)

Add System::powershell_execute()