[c++] Condense powershell helper code into the remaining single usage

`vcpkg integrate powershell` uses it
This commit is contained in:
Alexander Karatarakis 2018-05-19 19:01:10 -07:00
parent 6ccd43dfa7
commit 285c69b0fa
3 changed files with 39 additions and 128 deletions

View File

@ -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,

View File

@ -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); }

View File

@ -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
@ -368,6 +369,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 "
@ -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