[vcpkg] Split vcpkg::Commands::Fetch into backend and frontend

This commit is contained in:
Robert Schumacher 2018-07-09 06:29:37 -07:00
parent 1258c413f6
commit 802f51a142
11 changed files with 127 additions and 72 deletions

View File

@ -139,8 +139,6 @@ namespace vcpkg::Commands
namespace Fetch
{
fs::path get_tool_path(const VcpkgPaths& paths, const std::string& tool);
std::string get_tool_version(const VcpkgPaths& paths, const std::string& tool);
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
}

View File

@ -0,0 +1,21 @@
#pragma once
#include <vcpkg/base/files.h>
#include <string>
#include <utility>
namespace vcpkg
{
struct VcpkgPaths;
struct ToolCache
{
virtual ~ToolCache() {}
virtual const fs::path& get_tool_path(const VcpkgPaths& paths, const std::string& tool) const = 0;
virtual const std::string& get_tool_version(const VcpkgPaths& paths, const std::string& tool) const = 0;
};
std::unique_ptr<ToolCache> get_tool_cache();
}

View File

@ -2,6 +2,7 @@
#include <vcpkg/binaryparagraph.h>
#include <vcpkg/packagespec.h>
#include <vcpkg/tools.h>
#include <vcpkg/base/cache.h>
#include <vcpkg/base/expected.h>
@ -78,6 +79,7 @@ namespace vcpkg
fs::path ports_cmake;
const fs::path& get_tool_exe(const std::string& tool) const;
const std::string& get_tool_version(const std::string& tool) const;
/// <summary>Retrieve a toolset matching a VS version</summary>
/// <remarks>
@ -89,10 +91,11 @@ namespace vcpkg
private:
Lazy<std::vector<std::string>> available_triplets;
Cache<std::string, fs::path> tool_paths;
Lazy<std::vector<Toolset>> toolsets;
Lazy<std::vector<Toolset>> toolsets_vs2013;
fs::path default_vs_path;
mutable std::unique_ptr<ToolCache> m_tool_cache;
};
}

View File

@ -104,7 +104,7 @@ static void inner(const VcpkgCmdArguments& args)
"Error: Invalid vcpkg root directory %s: %s",
vcpkg_root_dir.string(),
expected_paths.error().message());
const VcpkgPaths paths = expected_paths.value_or_exit(VCPKG_LINE_INFO);
const VcpkgPaths& paths = expected_paths.value_or_exit(VCPKG_LINE_INFO);
#if defined(_WIN32)
const int exit_code = _wchdir(paths.root.c_str());

View File

@ -21,7 +21,7 @@ namespace vcpkg::Archives
static bool recursion_limiter_sevenzip_old = false;
Checks::check_exit(VCPKG_LINE_INFO, !recursion_limiter_sevenzip_old);
recursion_limiter_sevenzip_old = true;
const auto nuget_exe = Commands::Fetch::get_tool_path(paths, Tools::NUGET);
const auto nuget_exe = paths.get_tool_exe(Tools::NUGET);
const std::string stem = archive.stem().u8string();
// assuming format of [name].[version in the form d.d.d]
@ -56,7 +56,7 @@ namespace vcpkg::Archives
static bool recursion_limiter_sevenzip = false;
Checks::check_exit(VCPKG_LINE_INFO, !recursion_limiter_sevenzip);
recursion_limiter_sevenzip = true;
const auto seven_zip = Commands::Fetch::get_tool_path(paths, Tools::SEVEN_ZIP);
const auto seven_zip = paths.get_tool_exe(Tools::SEVEN_ZIP);
const auto code_and_output = System::cmd_execute_and_capture_output(Strings::format(
R"("%s" x "%s" -o"%s" -y)", seven_zip.u8string(), archive.u8string(), to_path_partial.u8string()));
Checks::check_exit(VCPKG_LINE_INFO,
@ -103,5 +103,4 @@ namespace vcpkg::Archives
to_path.u8string(),
ec.message());
}
}

View File

@ -462,7 +462,7 @@ namespace vcpkg::Build
std::vector<AbiEntry> abi_tag_entries;
abi_tag_entries.emplace_back(AbiEntry{"cmake", Commands::Fetch::get_tool_version(paths, Tools::CMAKE)});
abi_tag_entries.emplace_back(AbiEntry{"cmake", paths.get_tool_version(Tools::CMAKE)});
abi_tag_entries.insert(abi_tag_entries.end(), dependency_abis.begin(), dependency_abis.end());

View File

@ -57,3 +57,24 @@ namespace vcpkg::Commands
return t;
}
}
namespace vcpkg::Commands::Fetch
{
const CommandStructure COMMAND_STRUCTURE = {
Strings::format("The argument should be tool name\n%s", Help::create_example_string("fetch cmake")),
1,
1,
{},
nullptr,
};
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
Util::unused(args.parse_arguments(COMMAND_STRUCTURE));
const std::string tool = args.command_arguments[0];
const fs::path tool_path = paths.get_tool_exe(tool);
System::println(tool_path.u8string());
Checks::exit_success(VCPKG_LINE_INFO);
}
}

View File

@ -1,16 +1,16 @@
#include "pch.h"
#include <vcpkg/base/archives.h>
#include <vcpkg/base/checks.h>
#include <vcpkg/base/downloads.h>
#include <vcpkg/base/stringrange.h>
#include <vcpkg/base/strings.h>
#include <vcpkg/base/system.h>
#include <vcpkg/base/util.h>
#include <vcpkg/commands.h>
#include <vcpkg/help.h>
#include <vcpkg/tools.h>
#include <vcpkg/vcpkgpaths.h>
namespace vcpkg::Commands::Fetch
#include <vcpkg/base/archives.h>
#include <vcpkg/base/downloads.h>
#include <vcpkg/base/files.h>
#include <vcpkg/base/optional.h>
#include <vcpkg/base/stringrange.h>
#include <vcpkg/base/util.h>
namespace vcpkg
{
struct ToolData
{
@ -307,7 +307,7 @@ namespace vcpkg::Commands::Fetch
}
/* Sample output:
1.8.2
1.8.2
*/
return rc.output;
}
@ -348,11 +348,11 @@ namespace vcpkg::Commands::Fetch
}
/* Sample output:
NuGet Version: 4.6.2.5055
usage: NuGet <command> [args] [options]
Type 'NuGet help <command>' for help on a specific command.
NuGet Version: 4.6.2.5055
usage: NuGet <command> [args] [options]
Type 'NuGet help <command>' for help on a specific command.
[[[List of available commands follows]]]
[[[List of available commands follows]]]
*/
return StringRange::find_exactly_one_enclosed(rc.output, "NuGet Version: ", "\n").to_string();
}
@ -393,7 +393,7 @@ Type 'NuGet help <command>' for help on a specific command.
}
/* Sample output:
git version 2.17.1.windows.2
git version 2.17.1.windows.2
*/
const auto idx = rc.output.find("git version ");
Checks::check_exit(VCPKG_LINE_INFO,
@ -446,7 +446,7 @@ git version 2.17.1.windows.2
}
/* Sample output:
3.1.81
3.1.81
*/
return rc.output;
}
@ -478,55 +478,54 @@ git version 2.17.1.windows.2
}
}
fs::path get_tool_path(const VcpkgPaths& paths, const std::string& tool)
struct ToolCacheImpl final : ToolCache
{
// First deal with specially handled tools.
// For these we may look in locations like Program Files, the PATH etc as well as the auto-downloaded location.
if (tool == Tools::SEVEN_ZIP) return get_7za_path(paths);
if (tool == Tools::CMAKE) return CMake::get_path(paths).path;
if (tool == Tools::GIT) return Git::get_path(paths).path;
if (tool == Tools::NINJA) return Ninja::get_path(paths).path;
if (tool == Tools::NUGET) return Nuget::get_path(paths).path;
if (tool == Tools::IFW_INSTALLER_BASE) return IfwInstallerBase::get_path(paths).path;
if (tool == Tools::IFW_BINARYCREATOR)
return IfwInstallerBase::get_path(paths).path.parent_path() / "binarycreator.exe";
if (tool == Tools::IFW_REPOGEN) return IfwInstallerBase::get_path(paths).path.parent_path() / "repogen.exe";
vcpkg::Cache<std::string, fs::path> path_only_cache;
vcpkg::Cache<std::string, PathAndVersion> path_version_cache;
// For other tools, we simply always auto-download them.
const ToolData tool_data = parse_tool_data_from_xml(paths, tool);
if (paths.get_filesystem().exists(tool_data.exe_path))
virtual const fs::path& get_tool_path(const VcpkgPaths& paths, const std::string& tool) const override
{
return tool_data.exe_path;
return path_only_cache.get_lazy(tool, [&]() {
// First deal with specially handled tools.
// For these we may look in locations like Program Files, the PATH etc as well as the auto-downloaded
// location.
if (tool == Tools::SEVEN_ZIP) return get_7za_path(paths);
if (tool == Tools::CMAKE || tool == Tools::GIT || tool == Tools::NINJA || tool == Tools::NUGET ||
tool == Tools::IFW_INSTALLER_BASE)
return get_tool_pathversion(paths, tool).path;
if (tool == Tools::IFW_BINARYCREATOR)
return IfwInstallerBase::get_path(paths).path.parent_path() / "binarycreator.exe";
if (tool == Tools::IFW_REPOGEN)
return IfwInstallerBase::get_path(paths).path.parent_path() / "repogen.exe";
// For other tools, we simply always auto-download them.
const ToolData tool_data = parse_tool_data_from_xml(paths, tool);
if (paths.get_filesystem().exists(tool_data.exe_path))
{
return tool_data.exe_path;
}
return fetch_tool(paths, tool, tool_data);
});
}
return fetch_tool(paths, tool, tool_data);
}
std::string get_tool_version(const VcpkgPaths& paths, const std::string& tool)
{
if (tool == Tools::CMAKE) return CMake::get_path(paths).version;
if (tool == Tools::GIT) return Git::get_path(paths).version;
if (tool == Tools::NINJA) return Ninja::get_path(paths).version;
if (tool == Tools::NUGET) return Nuget::get_path(paths).version;
if (tool == Tools::IFW_INSTALLER_BASE) return IfwInstallerBase::get_path(paths).version;
const PathAndVersion& get_tool_pathversion(const VcpkgPaths& paths, const std::string& tool) const
{
return path_version_cache.get_lazy(tool, [&]() {
if (tool == Tools::CMAKE) return CMake::get_path(paths);
if (tool == Tools::GIT) return Git::get_path(paths);
if (tool == Tools::NINJA) return Ninja::get_path(paths);
if (tool == Tools::NUGET) return Nuget::get_path(paths);
if (tool == Tools::IFW_INSTALLER_BASE) return IfwInstallerBase::get_path(paths);
Checks::exit_with_message(VCPKG_LINE_INFO, "Finding version for %s is not implemented yet.", tool);
}
Checks::exit_with_message(VCPKG_LINE_INFO, "Finding version for %s is not implemented yet.", tool);
});
}
const CommandStructure COMMAND_STRUCTURE = {
Strings::format("The argument should be tool name\n%s", Help::create_example_string("fetch cmake")),
1,
1,
{},
nullptr,
virtual const std::string& get_tool_version(const VcpkgPaths& paths, const std::string& tool) const override
{
return get_tool_pathversion(paths, tool).version;
}
};
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
Util::unused(args.parse_arguments(COMMAND_STRUCTURE));
const std::string tool = args.command_arguments[0];
const fs::path tool_path = get_tool_path(paths, tool);
System::println(tool_path.u8string());
Checks::exit_success(VCPKG_LINE_INFO);
}
std::unique_ptr<ToolCache> get_tool_cache() { return std::make_unique<ToolCacheImpl>(); }
}

View File

@ -93,7 +93,13 @@ namespace vcpkg
const fs::path& VcpkgPaths::get_tool_exe(const std::string& tool) const
{
return this->tool_paths.get_lazy(tool, [&]() { return Commands::Fetch::get_tool_path(*this, tool); });
if (!m_tool_cache) m_tool_cache = get_tool_cache();
return m_tool_cache->get_tool_path(*this, tool);
}
const std::string& VcpkgPaths::get_tool_version(const std::string& tool) const
{
if (!m_tool_cache) m_tool_cache = get_tool_cache();
return m_tool_cache->get_tool_version(*this, tool);
}
const Toolset& VcpkgPaths::get_toolset(const Build::PreBuildInfo& prebuildinfo) const

View File

@ -138,6 +138,7 @@
<ItemGroup>
<ClInclude Include="..\include\pch.h" />
<ClInclude Include="..\include\vcpkg\base\archives.h" />
<ClInclude Include="..\include\vcpkg\base\cache.h" />
<ClInclude Include="..\include\vcpkg\base\checks.h" />
<ClInclude Include="..\include\vcpkg\base\chrono.h" />
<ClInclude Include="..\include\vcpkg\base\cofffilereader.h" />
@ -180,6 +181,7 @@
<ClInclude Include="..\include\vcpkg\sourceparagraph.h" />
<ClInclude Include="..\include\vcpkg\statusparagraph.h" />
<ClInclude Include="..\include\vcpkg\statusparagraphs.h" />
<ClInclude Include="..\include\vcpkg\tools.h" />
<ClInclude Include="..\include\vcpkg\triplet.h" />
<ClInclude Include="..\include\vcpkg\update.h" />
<ClInclude Include="..\include\vcpkg\userconfig.h" />
@ -221,7 +223,6 @@
<ClCompile Include="..\src\vcpkg\commands.edit.cpp" />
<ClCompile Include="..\src\vcpkg\commands.env.cpp" />
<ClCompile Include="..\src\vcpkg\commands.exportifw.cpp" />
<ClCompile Include="..\src\vcpkg\commands.fetch.cpp" />
<ClCompile Include="..\src\vcpkg\commands.hash.cpp" />
<ClCompile Include="..\src\vcpkg\commands.import.cpp" />
<ClCompile Include="..\src\vcpkg\commands.integrate.cpp" />
@ -249,6 +250,7 @@
<ClCompile Include="..\src\vcpkg\sourceparagraph.cpp" />
<ClCompile Include="..\src\vcpkg\statusparagraph.cpp" />
<ClCompile Include="..\src\vcpkg\statusparagraphs.cpp" />
<ClCompile Include="..\src\vcpkg\tools.cpp" />
<ClCompile Include="..\src\vcpkg\triplet.cpp" />
<ClCompile Include="..\src\vcpkg\update.cpp" />
<ClCompile Include="..\src\vcpkg\userconfig.cpp" />

View File

@ -198,9 +198,6 @@
<ClCompile Include="..\src\vcpkg\commands.upgrade.cpp">
<Filter>Source Files\vcpkg</Filter>
</ClCompile>
<ClCompile Include="..\src\vcpkg\commands.fetch.cpp">
<Filter>Source Files\vcpkg</Filter>
</ClCompile>
<ClCompile Include="..\src\vcpkg\base\downloads.cpp">
<Filter>Source Files\vcpkg\base</Filter>
</ClCompile>
@ -213,6 +210,9 @@
<ClCompile Include="..\src\vcpkg\base\stringrange.cpp">
<Filter>Source Files\vcpkg\base</Filter>
</ClCompile>
<ClCompile Include="..\src\vcpkg\tools.cpp">
<Filter>Source Files\vcpkg</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\pch.h">
@ -371,5 +371,11 @@
<ClInclude Include="..\include\vcpkg\base\stringrange.h">
<Filter>Header Files\vcpkg\base</Filter>
</ClInclude>
<ClInclude Include="..\include\vcpkg\base\cache.h">
<Filter>Header Files\vcpkg\base</Filter>
</ClInclude>
<ClInclude Include="..\include\vcpkg\tools.h">
<Filter>Header Files\vcpkg</Filter>
</ClInclude>
</ItemGroup>
</Project>