133 lines
4.3 KiB
C++
133 lines
4.3 KiB
C++
#include "cmd_parse.h"
|
|
#include "install.h"
|
|
#include "pack.h"
|
|
#include <boost/filesystem.hpp>
|
|
#include <iostream>
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
CCmdParse::CCmdParse()
|
|
{
|
|
}
|
|
|
|
bool CCmdParse::cmdParse(int argc, char* argv[])
|
|
{
|
|
cmd::options_description desc("options");
|
|
desc.add_options()("help,h", "produce help message")(
|
|
"dirs,d", cmd::value<std::vector<std::string>>(),
|
|
"set search dirs")("mode,m", cmd::value<int>()->default_value(-1),
|
|
"设置执行模式,0-打包,1-安装")(
|
|
"purpose,p", cmd::value<std::string>(),
|
|
"安装目标目录")("file,f", cmd::value<std::string>(), "二进制文件")(
|
|
"ico,i", cmd::value<std::string>(), "图标文件")(
|
|
"category,c", cmd::value<std::string>()->default_value("Utility"),
|
|
"分类名称,例如Utility。")("only,o",
|
|
cmd::value<bool>()->default_value(false),
|
|
"仅生成shell启动脚本(mode=1时)");
|
|
|
|
cmd::variables_map vm;
|
|
cmd::store(cmd::parse_command_line(argc, argv, desc), vm);
|
|
cmd::notify(vm);
|
|
|
|
if (vm.count("help")) {
|
|
std::cout << desc;
|
|
return false;
|
|
}
|
|
|
|
if (vm.count("mode")) {
|
|
result_.mode = vm["mode"].as<int>();
|
|
}
|
|
if (vm.count("only")) {
|
|
result_.only_shell = vm["only"].as<bool>();
|
|
}
|
|
if (vm.count("dirs")) {
|
|
result_.lib_dirs = vm["dirs"].as<std::vector<std::string>>();
|
|
}
|
|
if (vm.count("purpose")) {
|
|
result_.purpose_dir = vm["purpose"].as<std::string>();
|
|
}
|
|
if (vm.count("file")) {
|
|
result_.binary = vm["file"].as<std::string>();
|
|
}
|
|
if (vm.count("ico")) {
|
|
result_.ico = vm["ico"].as<std::string>();
|
|
}
|
|
if (vm.count("category")) {
|
|
result_.category = vm["category"].as<std::string>();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool CCmdParse::Run()
|
|
{
|
|
std::cout << "信息 ==========================================>\n";
|
|
std::cout << "binary:" << result_.binary << "\n";
|
|
std::cout << "category:" << result_.category << "\n";
|
|
std::cout << "ico:" << result_.ico << "\n";
|
|
std::cout << "mode:" << result_.mode << "\n";
|
|
std::cout << "purpose_dir:" << result_.purpose_dir << "\n";
|
|
std::cout << "dirs:" << result_.lib_dirs.size() << "\n";
|
|
for (const auto& item : result_.lib_dirs) {
|
|
std::cout << ">>" << item << "\n";
|
|
}
|
|
|
|
auto check_file_dir = [&](const std::string& path, bool is_file) -> bool {
|
|
std::string type = is_file ? "文件:" : "文件夹:";
|
|
fs::path tp(path);
|
|
if (is_file) {
|
|
if (!fs::exists(tp) || !fs::is_regular_file(tp)) {
|
|
std::cout << type << path << ",不存在\n";
|
|
return false;
|
|
} else {
|
|
std::cout << type << path << ",检查通过!\n";
|
|
return true;
|
|
}
|
|
} else {
|
|
if (!fs::exists(tp)) {
|
|
try {
|
|
fs::create_directories(tp);
|
|
std::cout << type << path << ",不存在但自动创建成功。\n";
|
|
return true;
|
|
} catch (const std::exception& e) {
|
|
std::cerr << e.what() << '\n';
|
|
return false;
|
|
}
|
|
|
|
std::cout << type << path << ",不存在\n";
|
|
return false;
|
|
} else {
|
|
std::cout << type << path << ",检查通过!\n";
|
|
return true;
|
|
}
|
|
}
|
|
};
|
|
|
|
std::cout << "检查 ==========================================>\n";
|
|
|
|
if (!check_file_dir(result_.binary, true)) {
|
|
return false;
|
|
}
|
|
switch (result_.mode) {
|
|
case 0: {
|
|
if (!check_file_dir(result_.purpose_dir, false)) {
|
|
return false;
|
|
}
|
|
std::cout << "结果 ==========================================>\n";
|
|
CPackBinary pack;
|
|
return pack.startPack(result_);
|
|
}
|
|
case 1: {
|
|
if (!result_.only_shell && !check_file_dir(result_.ico, true)) {
|
|
return false;
|
|
}
|
|
std::cout << "结果 ==========================================>\n";
|
|
CInstallBinary install;
|
|
return install.startInstall(result_);
|
|
}
|
|
default:
|
|
std::cout << "不正确的mode模式。" << std::endl;
|
|
return false;
|
|
}
|
|
}
|