Skip to content

Commit

Permalink
[vcpkg, jsonnet, openssl-uwp] Enable use of the system powershell-cor…
Browse files Browse the repository at this point in the history
…e if it is present. (microsoft#13805)
  • Loading branch information
BillyONeal committed Oct 28, 2020
1 parent 95a9f9a commit dab0b54
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 13 deletions.
13 changes: 11 additions & 2 deletions include/vcpkg/base/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,19 @@ namespace vcpkg::Files

void print_paths(const std::vector<fs::path>& paths);

/// Performs "lhs / rhs" according to the C++17 Filesystem Library Specification.
/// This function exists as a workaround for TS implementations.
// Performs "lhs / rhs" according to the C++17 Filesystem Library Specification.
// This function exists as a workaround for TS implementations.
fs::path combine(const fs::path& lhs, const fs::path& rhs);

#if defined(_WIN32)
constexpr char preferred_separator = '\\';
#else
constexpr char preferred_separator = '/';
#endif // _WIN32

// Adds file as a new path element to the end of base, with an additional slash if necessary
std::string add_filename(StringView base, StringView file);

#if defined(_WIN32)
fs::path win32_fix_path_case(const fs::path& source);
#endif // _WIN32
Expand Down
1 change: 1 addition & 0 deletions include/vcpkg/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace vcpkg
static const std::string GIT = "git";
static const std::string MONO = "mono";
static const std::string NINJA = "ninja";
static const std::string POWERSHELL_CORE = "powershell-core";
static const std::string NUGET = "nuget";
static const std::string IFW_INSTALLER_BASE = "ifw_installerbase";
static const std::string IFW_BINARYCREATOR = "ifw_binarycreator";
Expand Down
15 changes: 15 additions & 0 deletions src/vcpkg-test/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,21 @@ TEST_CASE ("win32_fix_path_case", "[files]")
}
#endif // _WIN32

TEST_CASE ("add_filename", "[files]")
{
using vcpkg::Files::add_filename;
using vcpkg::Files::preferred_separator;

CHECK(add_filename("a/b", "c") == std::string("a/b") + preferred_separator + "c");
CHECK(add_filename("a/b/", "c") == "a/b/c");
CHECK(add_filename("a/b\\", "c") == "a/b\\c");
CHECK(add_filename("", "c") == "c");

// note that we don't special case slashes in the second argument; the caller shouldn't do that
CHECK(add_filename("a/b/", "\\c") == "a/b/\\c");
CHECK(add_filename("a/b\\", "/c") == "a/b\\/c");
}

#if defined(CATCH_CONFIG_ENABLE_BENCHMARKING)
TEST_CASE ("remove all -- benchmarks", "[files][!benchmark]")
{
Expand Down
33 changes: 29 additions & 4 deletions src/vcpkg/base/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <algorithm>
#include <string>

#if defined(_WIN32)
namespace
{
struct IsSlash
Expand All @@ -32,7 +31,11 @@ namespace
};

constexpr IsSlash is_slash;
} // unnamed namespace

#if defined(_WIN32)
namespace
{
template<size_t N>
bool wide_starts_with(const std::wstring& haystack, const wchar_t (&needle)[N]) noexcept
{
Expand Down Expand Up @@ -1182,14 +1185,14 @@ namespace vcpkg::Files
auto paths = Strings::split_paths(System::get_environment_variable("PATH").value_or_exit(VCPKG_LINE_INFO));

std::vector<fs::path> ret;
std::error_code ec;
for (auto&& path : paths)
{
auto base = path + "/" + name;
auto base = add_filename(path, name);

for (auto&& ext : EXTS)
{
auto p = fs::u8path(base + ext.c_str());
if (Util::find(ret, p) == ret.end() && this->exists(p, ec))
if (Util::find(ret, p) == ret.end() && this->exists(p, ignore_errors))
{
ret.push_back(p);
Debug::print("Found path: ", fs::u8string(p), '\n');
Expand Down Expand Up @@ -1372,4 +1375,26 @@ namespace vcpkg::Files
return fs::path(std::move(in_progress));
}
#endif // _WIN32

std::string add_filename(StringView base, StringView file)
{
std::string result;
const auto base_size = base.size();
const auto file_size = file.size();
if (base_size != 0 && !is_slash(base.data()[base_size - 1]))
{
result.reserve(base_size + file_size + 1);
result.append(base.data(), base_size);
result.push_back(preferred_separator);
result.append(file.data(), file_size);
}
else
{
result.reserve(base_size + file_size);
result.append(base.data(), base_size);
result.append(file.data(), file_size);
}

return result;
}
}
7 changes: 0 additions & 7 deletions src/vcpkg/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,6 @@ namespace vcpkg::Build

return base_env.cmd_cache.get_lazy(build_env_cmd, [&]() {
const fs::path& powershell_exe_path = paths.get_tool_exe("powershell-core");
auto& fs = paths.get_filesystem();
if (!fs.exists(powershell_exe_path.parent_path() / "powershell.exe"))
{
fs.copy(
powershell_exe_path, powershell_exe_path.parent_path() / "powershell.exe", fs::copy_options::none);
}

auto clean_env = System::get_modified_clean_environment(
base_env.env_map, fs::u8string(powershell_exe_path.parent_path()) + ";");
if (build_env_cmd.empty())
Expand Down
38 changes: 38 additions & 0 deletions src/vcpkg/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,36 @@ Mono JIT compiler version 6.8.0.105 (Debian 6.8.0.105+dfsg-2 Wed Feb 26 23:23:50
}
};

struct PowerShellCoreProvider : ToolProvider
{
std::string m_exe = "pwsh";

virtual const std::string& tool_data_name() const override { return m_exe; }
virtual const std::string& exe_stem() const override { return m_exe; }
virtual std::array<int, 3> default_min_version() const override { return {7, 0, 3}; }

virtual Optional<std::string> get_version(const VcpkgPaths&, const fs::path& path_to_exe) const override
{
const auto pwsh_invocation = System::cmd_execute_and_capture_output(
System::CmdLineBuilder().path_arg(path_to_exe).string_arg("--version").extract());
if (pwsh_invocation.exit_code != 0)
{
return nullopt;
}

// Sample output: PowerShell 7.0.3\r\n
auto output = std::move(pwsh_invocation.output);
if (!Strings::starts_with(output, "PowerShell "))
{
Checks::exit_with_message(
VCPKG_LINE_INFO, "Unexpected format of powershell-core version string: %s", output);
}

output.erase(0, 11);
return Strings::trim(std::move(output));
}
};

struct ToolCacheImpl final : ToolCache
{
vcpkg::Cache<std::string, fs::path> path_only_cache;
Expand Down Expand Up @@ -511,6 +541,14 @@ Mono JIT compiler version 6.8.0.105 (Debian 6.8.0.105+dfsg-2 Wed Feb 26 23:23:50
}
return get_path(paths, NinjaProvider());
}
if (tool == Tools::POWERSHELL_CORE)
{
if (System::get_environment_variable("VCPKG_FORCE_SYSTEM_BINARIES").has_value())
{
return {"pwsh", "0"};
}
return get_path(paths, PowerShellCoreProvider());
}
if (tool == Tools::NUGET) return get_path(paths, NuGetProvider());
if (tool == Tools::IFW_INSTALLER_BASE) return get_path(paths, IfwInstallerBaseProvider());
if (tool == Tools::MONO) return get_path(paths, MonoProvider());
Expand Down

0 comments on commit dab0b54

Please sign in to comment.