2017-01-28 04:49:09 +08:00
|
|
|
#include "pch.h"
|
2017-04-28 09:08:52 +08:00
|
|
|
|
2016-11-08 08:06:36 +08:00
|
|
|
#include "Paragraphs.h"
|
2016-11-08 08:17:34 +08:00
|
|
|
#include "StatusParagraph.h"
|
2017-04-28 09:08:52 +08:00
|
|
|
#include "vcpkg_Commands.h"
|
2016-11-08 08:17:34 +08:00
|
|
|
#include "vcpkg_Files.h"
|
2016-09-22 16:15:09 +08:00
|
|
|
|
2017-01-13 14:03:57 +08:00
|
|
|
namespace vcpkg::Commands::Import
|
2016-09-22 16:15:09 +08:00
|
|
|
{
|
2016-11-08 08:17:34 +08:00
|
|
|
struct Binaries
|
|
|
|
{
|
|
|
|
std::vector<fs::path> dlls;
|
|
|
|
std::vector<fs::path> libs;
|
|
|
|
};
|
|
|
|
|
2017-04-09 11:19:35 +08:00
|
|
|
static void check_is_directory(const LineInfo& line_info, const Files::Filesystem& fs, const fs::path& dirpath)
|
2017-04-01 08:57:45 +08:00
|
|
|
{
|
2017-04-09 11:19:35 +08:00
|
|
|
Checks::check_exit(line_info, fs.is_directory(dirpath), "The path %s is not a directory", dirpath.string());
|
2017-04-01 08:57:45 +08:00
|
|
|
}
|
|
|
|
|
2017-04-09 11:19:35 +08:00
|
|
|
static Binaries find_binaries_in_dir(const Files::Filesystem& fs, const fs::path& path)
|
2016-11-08 08:17:34 +08:00
|
|
|
{
|
2017-04-13 14:15:02 +08:00
|
|
|
auto files = fs.get_files_recursive(path);
|
2017-04-09 11:19:35 +08:00
|
|
|
|
|
|
|
check_is_directory(VCPKG_LINE_INFO, fs, path);
|
2016-11-08 08:17:34 +08:00
|
|
|
|
|
|
|
Binaries binaries;
|
2017-04-09 11:19:35 +08:00
|
|
|
for (auto&& file : files)
|
|
|
|
{
|
2017-04-28 09:08:52 +08:00
|
|
|
if (fs.is_directory(file)) continue;
|
2017-09-01 09:27:10 +08:00
|
|
|
const auto ext = file.extension();
|
2017-04-09 11:19:35 +08:00
|
|
|
if (ext == ".dll")
|
|
|
|
binaries.dlls.push_back(std::move(file));
|
|
|
|
else if (ext == ".lib")
|
|
|
|
binaries.libs.push_back(std::move(file));
|
|
|
|
}
|
2016-11-08 08:17:34 +08:00
|
|
|
return binaries;
|
|
|
|
}
|
|
|
|
|
2017-04-28 09:08:52 +08:00
|
|
|
static void copy_files_into_directory(Files::Filesystem& fs,
|
|
|
|
const std::vector<fs::path>& files,
|
|
|
|
const fs::path& destination_folder)
|
2016-11-08 08:17:34 +08:00
|
|
|
{
|
2017-04-09 11:19:35 +08:00
|
|
|
std::error_code ec;
|
|
|
|
fs.create_directory(destination_folder, ec);
|
2016-11-08 08:17:34 +08:00
|
|
|
|
|
|
|
for (auto const& src_path : files)
|
|
|
|
{
|
2017-09-01 09:27:10 +08:00
|
|
|
const fs::path dest_path = destination_folder / src_path.filename();
|
2017-04-09 11:19:35 +08:00
|
|
|
fs.copy(src_path, dest_path, fs::copy_options::overwrite_existing);
|
2016-11-08 08:17:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 09:08:52 +08:00
|
|
|
static void place_library_files_in(Files::Filesystem& fs,
|
|
|
|
const fs::path& include_directory,
|
|
|
|
const fs::path& project_directory,
|
|
|
|
const fs::path& destination_path)
|
2016-11-08 08:17:34 +08:00
|
|
|
{
|
2017-04-09 11:19:35 +08:00
|
|
|
check_is_directory(VCPKG_LINE_INFO, fs, include_directory);
|
|
|
|
check_is_directory(VCPKG_LINE_INFO, fs, project_directory);
|
|
|
|
check_is_directory(VCPKG_LINE_INFO, fs, destination_path);
|
2017-09-01 09:27:10 +08:00
|
|
|
const Binaries debug_binaries = find_binaries_in_dir(fs, project_directory / "Debug");
|
|
|
|
const Binaries release_binaries = find_binaries_in_dir(fs, project_directory / "Release");
|
2016-11-08 08:17:34 +08:00
|
|
|
|
2017-09-01 09:27:10 +08:00
|
|
|
const fs::path destination_include_directory = destination_path / "include";
|
2017-04-28 09:08:52 +08:00
|
|
|
fs.copy(include_directory,
|
|
|
|
destination_include_directory,
|
|
|
|
fs::copy_options::recursive | fs::copy_options::overwrite_existing);
|
2016-11-08 08:17:34 +08:00
|
|
|
|
2017-04-09 11:19:35 +08:00
|
|
|
copy_files_into_directory(fs, release_binaries.dlls, destination_path / "bin");
|
|
|
|
copy_files_into_directory(fs, release_binaries.libs, destination_path / "lib");
|
2016-11-08 08:17:34 +08:00
|
|
|
|
2017-04-09 11:19:35 +08:00
|
|
|
std::error_code ec;
|
|
|
|
fs.create_directory(destination_path / "debug", ec);
|
|
|
|
copy_files_into_directory(fs, debug_binaries.dlls, destination_path / "debug" / "bin");
|
|
|
|
copy_files_into_directory(fs, debug_binaries.libs, destination_path / "debug" / "lib");
|
2016-11-08 08:17:34 +08:00
|
|
|
}
|
|
|
|
|
2017-04-28 09:08:52 +08:00
|
|
|
static void do_import(const VcpkgPaths& paths,
|
|
|
|
const fs::path& include_directory,
|
|
|
|
const fs::path& project_directory,
|
|
|
|
const BinaryParagraph& control_file_data)
|
2016-11-08 08:17:34 +08:00
|
|
|
{
|
2017-04-13 13:48:52 +08:00
|
|
|
auto& fs = paths.get_filesystem();
|
2017-09-01 09:27:10 +08:00
|
|
|
const fs::path library_destination_path = paths.package_dir(control_file_data.spec);
|
2017-04-09 11:19:35 +08:00
|
|
|
std::error_code ec;
|
2017-04-13 13:48:52 +08:00
|
|
|
fs.create_directory(library_destination_path, ec);
|
2017-04-09 11:19:35 +08:00
|
|
|
place_library_files_in(paths.get_filesystem(), include_directory, project_directory, library_destination_path);
|
2016-11-08 08:17:34 +08:00
|
|
|
|
2017-09-01 09:27:10 +08:00
|
|
|
const fs::path control_file_path = library_destination_path / "CONTROL";
|
2017-04-13 13:48:52 +08:00
|
|
|
fs.write_contents(control_file_path, Strings::serialize(control_file_data));
|
2016-11-08 08:17:34 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 07:29:11 +08:00
|
|
|
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
|
2016-09-22 16:15:09 +08:00
|
|
|
{
|
2017-09-01 09:05:58 +08:00
|
|
|
static const std::string EXAMPLE = Commands::Help::create_example_string(
|
2017-04-28 09:08:52 +08:00
|
|
|
R"(import C:\path\to\CONTROLfile C:\path\to\includedir C:\path\to\projectdir)");
|
2017-09-01 09:05:58 +08:00
|
|
|
args.check_exact_arg_count(3, EXAMPLE);
|
2017-02-18 07:38:39 +08:00
|
|
|
args.check_and_get_optional_command_arguments({});
|
2016-09-22 16:15:09 +08:00
|
|
|
|
|
|
|
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-28 09:08:52 +08:00
|
|
|
const Expected<std::unordered_map<std::string, std::string>> pghs =
|
|
|
|
Paragraphs::get_single_paragraph(paths.get_filesystem(), control_file_path);
|
|
|
|
Checks::check_exit(VCPKG_LINE_INFO,
|
|
|
|
pghs.get() != nullptr,
|
|
|
|
"Invalid control file %s for package",
|
|
|
|
control_file_path.generic_string());
|
2016-09-22 16:15:09 +08:00
|
|
|
|
|
|
|
StatusParagraph spgh;
|
2017-03-25 01:46:49 +08:00
|
|
|
spgh.package = BinaryParagraph(*pghs.get());
|
2016-09-22 16:15:09 +08:00
|
|
|
auto& control_file_data = spgh.package;
|
|
|
|
|
2016-11-08 08:17:34 +08:00
|
|
|
do_import(paths, include_directory, project_directory, control_file_data);
|
2017-03-23 08:45:39 +08:00
|
|
|
Checks::exit_success(VCPKG_LINE_INFO);
|
2016-09-22 16:15:09 +08:00
|
|
|
}
|
|
|
|
}
|