vcpkg/toolsrc/src/commands_import.cpp

92 lines
4.0 KiB
C++
Raw Normal View History

2017-01-28 04:49:09 +08:00
#include "pch.h"
#include "vcpkg_Commands.h"
2016-11-08 08:06:36 +08:00
#include "Paragraphs.h"
#include "StatusParagraph.h"
#include "vcpkg_Files.h"
namespace vcpkg::Commands::Import
{
struct Binaries
{
std::vector<fs::path> dlls;
std::vector<fs::path> libs;
};
2017-04-01 08:57:45 +08:00
void check_is_directory(const LineInfo& line_info, const fs::path& dirpath)
{
Checks::check_exit(line_info, fs::is_directory(dirpath), "The path %s is not a directory", dirpath.string());
}
2017-03-02 08:22:06 +08:00
static Binaries find_binaries_in_dir(const fs::path& path)
{
2017-04-01 08:57:45 +08:00
check_is_directory(VCPKG_LINE_INFO, path);
Binaries binaries;
2017-03-02 08:22:06 +08:00
binaries.dlls = Files::recursive_find_files_with_extension_in_dir(path, ".dll");
binaries.libs = Files::recursive_find_files_with_extension_in_dir(path, ".lib");
return binaries;
}
static void copy_files_into_directory(const std::vector<fs::path>& files, const fs::path& destination_folder)
{
fs::create_directory(destination_folder);
for (auto const& src_path : files)
{
fs::path dest_path = destination_folder / src_path.filename();
fs::copy(src_path, dest_path, fs::copy_options::overwrite_existing);
}
}
static void place_library_files_in(const fs::path& include_directory, const fs::path& project_directory, const fs::path& destination_path)
{
2017-04-01 08:57:45 +08:00
check_is_directory(VCPKG_LINE_INFO, include_directory);
check_is_directory(VCPKG_LINE_INFO, project_directory);
check_is_directory(VCPKG_LINE_INFO, destination_path);
2017-03-02 08:22:06 +08:00
Binaries debug_binaries = find_binaries_in_dir(project_directory / "Debug");
Binaries release_binaries = find_binaries_in_dir(project_directory / "Release");
fs::path destination_include_directory = destination_path / "include";
fs::copy(include_directory, destination_include_directory, fs::copy_options::recursive | fs::copy_options::overwrite_existing);
copy_files_into_directory(release_binaries.dlls, destination_path / "bin");
copy_files_into_directory(release_binaries.libs, destination_path / "lib");
fs::create_directory(destination_path / "debug");
copy_files_into_directory(debug_binaries.dlls, destination_path / "debug" / "bin");
copy_files_into_directory(debug_binaries.libs, destination_path / "debug" / "lib");
}
2017-04-04 07:29:11 +08:00
static void do_import(const VcpkgPaths& paths, const fs::path& include_directory, const fs::path& project_directory, const BinaryParagraph& control_file_data)
{
fs::path library_destination_path = paths.package_dir(control_file_data.spec);
fs::create_directory(library_destination_path);
place_library_files_in(include_directory, project_directory, library_destination_path);
fs::path control_file_path = library_destination_path / "CONTROL";
std::ofstream(control_file_path) << control_file_data;
}
2017-04-04 07:29:11 +08:00
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
static const std::string example = Commands::Help::create_example_string(R"(import C:\path\to\CONTROLfile C:\path\to\includedir C:\path\to\projectdir)");
args.check_exact_arg_count(3, example);
2017-02-18 07:38:39 +08:00
args.check_and_get_optional_command_arguments({});
const fs::path control_file_path(args.command_arguments[0]);
const fs::path include_directory(args.command_arguments[1]);
const fs::path project_directory(args.command_arguments[2]);
2017-04-04 07:26:54 +08:00
const Expected<std::unordered_map<std::string, std::string>> pghs = Paragraphs::get_single_paragraph(control_file_path);
Checks::check_exit(VCPKG_LINE_INFO, pghs.get() != nullptr, "Invalid control file %s for package", control_file_path.generic_string());
StatusParagraph spgh;
spgh.package = BinaryParagraph(*pghs.get());
auto& control_file_data = spgh.package;
do_import(paths, include_directory, project_directory, control_file_data);
Checks::exit_success(VCPKG_LINE_INFO);
}
}