Add command x-vsinstances

This commit is contained in:
Alexander Karatarakis 2018-09-18 20:55:35 -07:00
parent f89341566c
commit 1f79c92eb0
7 changed files with 78 additions and 13 deletions

View File

@ -119,6 +119,12 @@ namespace vcpkg::Commands
void perform_and_exit(const VcpkgCmdArguments& args); void perform_and_exit(const VcpkgCmdArguments& args);
} }
namespace X_VSInstances
{
extern const CommandStructure COMMAND_STRUCTURE;
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
}
namespace Hash namespace Hash
{ {
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths); void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);

View File

@ -6,6 +6,8 @@
namespace vcpkg::VisualStudio namespace vcpkg::VisualStudio
{ {
std::vector<std::string> get_visual_studio_instances(const VcpkgPaths& paths);
std::vector<Toolset> find_toolset_instances_preferred_first(const VcpkgPaths& paths); std::vector<Toolset> find_toolset_instances_preferred_first(const VcpkgPaths& paths);
} }

View File

@ -46,6 +46,7 @@ namespace vcpkg::Commands
{"autocomplete", &Autocomplete::perform_and_exit}, {"autocomplete", &Autocomplete::perform_and_exit},
{"hash", &Hash::perform_and_exit}, {"hash", &Hash::perform_and_exit},
{"fetch", &Fetch::perform_and_exit}, {"fetch", &Fetch::perform_and_exit},
{"x-vsinstances", &X_VSInstances::perform_and_exit},
}; };
return t; return t;
} }

View File

@ -0,0 +1,29 @@
#include "pch.h"
#include <vcpkg/commands.h>
#include <vcpkg/help.h>
#include <vcpkg/visualstudio.h>
namespace vcpkg::Commands::X_VSInstances
{
const CommandStructure COMMAND_STRUCTURE = {
Help::create_example_string("x-vsinstances"),
0,
0,
{{}, {}},
nullptr,
};
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
const ParsedArguments parsed_args = args.parse_arguments(COMMAND_STRUCTURE);
const auto instances = vcpkg::VisualStudio::get_visual_studio_instances(paths);
for (const std::string& instance : instances)
{
System::println(instance);
}
Checks::exit_success(VCPKG_LINE_INFO);
}
}

View File

@ -22,6 +22,17 @@ namespace vcpkg::VisualStudio
LEGACY LEGACY
}; };
static std::string release_type_to_string(const ReleaseType& release_type)
{
switch (release_type)
{
case ReleaseType::STABLE: return "STABLE";
case ReleaseType::PRERELEASE: return "PRERELEASE";
case ReleaseType::LEGACY: return "LEGACY";
default: Checks::unreachable(VCPKG_LINE_INFO);
}
}
static bool preferred_first_comparator(const VisualStudioInstance& left, const VisualStudioInstance& right) static bool preferred_first_comparator(const VisualStudioInstance& left, const VisualStudioInstance& right)
{ {
const auto get_preference_weight = [](const ReleaseType& type) -> int { const auto get_preference_weight = [](const ReleaseType& type) -> int {
@ -51,10 +62,15 @@ namespace vcpkg::VisualStudio
std::string version; std::string version;
ReleaseType release_type; ReleaseType release_type;
std::string to_string() const
{
return Strings::format("%s, %s, %s", root_path.u8string(), version, release_type_to_string(release_type));
}
std::string major_version() const { return version.substr(0, 2); } std::string major_version() const { return version.substr(0, 2); }
}; };
static std::vector<VisualStudioInstance> get_visual_studio_instances(const VcpkgPaths& paths) static std::vector<VisualStudioInstance> get_visual_studio_instances_internal(const VcpkgPaths& paths)
{ {
const auto& fs = paths.get_filesystem(); const auto& fs = paths.get_filesystem();
std::vector<VisualStudioInstance> instances; std::vector<VisualStudioInstance> instances;
@ -114,9 +130,9 @@ namespace vcpkg::VisualStudio
{ {
// We want lexically_normal(), but it is not available // We want lexically_normal(), but it is not available
// Correct root path might be 2 or 3 levels up, depending on if the path has trailing backslash. Try both. // Correct root path might be 2 or 3 levels up, depending on if the path has trailing backslash. Try both.
auto common7_tools = fs::path{*path_as_string}; auto common7_tools = fs::path {*path_as_string};
append_if_has_cl(fs::path{*path_as_string}.parent_path().parent_path()); append_if_has_cl(fs::path {*path_as_string}.parent_path().parent_path());
append_if_has_cl(fs::path{*path_as_string}.parent_path().parent_path().parent_path()); append_if_has_cl(fs::path {*path_as_string}.parent_path().parent_path().parent_path());
} }
// VS2015 instance from Program Files // VS2015 instance from Program Files
@ -125,6 +141,13 @@ namespace vcpkg::VisualStudio
return instances; return instances;
} }
std::vector<std::string> get_visual_studio_instances(const VcpkgPaths& paths)
{
std::vector<VisualStudioInstance> sorted {get_visual_studio_instances_internal(paths)};
std::sort(sorted.begin(), sorted.end(), VisualStudioInstance::preferred_first_comparator);
return Util::fmap(sorted, [](const VisualStudioInstance& instance) { return instance.to_string(); });
}
std::vector<Toolset> find_toolset_instances_preferred_first(const VcpkgPaths& paths) std::vector<Toolset> find_toolset_instances_preferred_first(const VcpkgPaths& paths)
{ {
using CPU = System::CPUArchitecture; using CPU = System::CPUArchitecture;
@ -137,8 +160,8 @@ namespace vcpkg::VisualStudio
std::vector<Toolset> found_toolsets; std::vector<Toolset> found_toolsets;
std::vector<Toolset> excluded_toolsets; std::vector<Toolset> excluded_toolsets;
const SortedVector<VisualStudioInstance> sorted{get_visual_studio_instances(paths), const SortedVector<VisualStudioInstance> sorted {get_visual_studio_instances_internal(paths),
VisualStudioInstance::preferred_first_comparator}; VisualStudioInstance::preferred_first_comparator};
const bool v140_is_available = Util::find_if(sorted, [&](const VisualStudioInstance& vs_instance) { const bool v140_is_available = Util::find_if(sorted, [&](const VisualStudioInstance& vs_instance) {
return vs_instance.major_version() == "14"; return vs_instance.major_version() == "14";
@ -194,7 +217,7 @@ namespace vcpkg::VisualStudio
paths_examined.push_back(dumpbin_path); paths_examined.push_back(dumpbin_path);
if (fs.exists(dumpbin_path)) if (fs.exists(dumpbin_path))
{ {
const Toolset v141_toolset{ const Toolset v141_toolset {
vs_instance.root_path, dumpbin_path, vcvarsall_bat, {}, V_141, supported_architectures}; vs_instance.root_path, dumpbin_path, vcvarsall_bat, {}, V_141, supported_architectures};
const auto english_language_pack = dumpbin_path.parent_path() / "1033"; const auto english_language_pack = dumpbin_path.parent_path() / "1033";
@ -209,12 +232,12 @@ namespace vcpkg::VisualStudio
if (v140_is_available) if (v140_is_available)
{ {
const Toolset v140_toolset{vs_instance.root_path, const Toolset v140_toolset {vs_instance.root_path,
dumpbin_path, dumpbin_path,
vcvarsall_bat, vcvarsall_bat,
{"-vcvars_ver=14.0"}, {"-vcvars_ver=14.0"},
V_140, V_140,
supported_architectures}; supported_architectures};
found_toolsets.push_back(v140_toolset); found_toolsets.push_back(v140_toolset);
} }

View File

@ -233,6 +233,7 @@
<ClCompile Include="..\src\vcpkg\commands.search.cpp" /> <ClCompile Include="..\src\vcpkg\commands.search.cpp" />
<ClCompile Include="..\src\vcpkg\commands.upgrade.cpp" /> <ClCompile Include="..\src\vcpkg\commands.upgrade.cpp" />
<ClCompile Include="..\src\vcpkg\commands.version.cpp" /> <ClCompile Include="..\src\vcpkg\commands.version.cpp" />
<ClCompile Include="..\src\vcpkg\commands.xvsinstances.cpp" />
<ClCompile Include="..\src\vcpkg\dependencies.cpp" /> <ClCompile Include="..\src\vcpkg\dependencies.cpp" />
<ClCompile Include="..\src\vcpkg\export.cpp" /> <ClCompile Include="..\src\vcpkg\export.cpp" />
<ClCompile Include="..\src\vcpkg\globalstate.cpp" /> <ClCompile Include="..\src\vcpkg\globalstate.cpp" />

View File

@ -213,6 +213,9 @@
<ClCompile Include="..\src\vcpkg\base\downloads.cpp"> <ClCompile Include="..\src\vcpkg\base\downloads.cpp">
<Filter>Source Files\vcpkg\base</Filter> <Filter>Source Files\vcpkg\base</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\vcpkg\commands.xvsinstances.cpp">
<Filter>Source Files\vcpkg</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\include\pch.h"> <ClInclude Include="..\include\pch.h">