Add System::powershell_execute()

This commit is contained in:
Alexander Karatarakis 2017-11-26 02:49:23 -08:00
parent 92872439b9
commit 2af7fe8690
2 changed files with 41 additions and 6 deletions

View File

@ -44,6 +44,10 @@ namespace vcpkg::System
ExitCodeAndOutput cmd_execute_and_capture_output(const CStringView cmd_line); 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, std::string powershell_execute_and_capture_output(const std::string& title,
const fs::path& script_path, const fs::path& script_path,
const std::vector<PowershellParameter>& parameters = {}); const std::vector<PowershellParameter>& parameters = {});

View File

@ -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) int cmd_execute_clean(const CStringView cmd_line)
{ {
#if defined(_WIN32) #if defined(_WIN32)
@ -317,16 +327,37 @@ namespace vcpkg::System
#endif #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, std::string powershell_execute_and_capture_output(const std::string& title,
const fs::path& script_path, const fs::path& script_path,
const std::vector<PowershellParameter>& parameters) 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);
// 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);
auto rc = System::cmd_execute_and_capture_output(cmd); auto rc = System::cmd_execute_and_capture_output(cmd);
if (rc.exit_code) if (rc.exit_code)