From df2380f5ed61161bd5fd6d67f0996ffb1f657d6e Mon Sep 17 00:00:00 2001 From: taynpg Date: Tue, 29 Oct 2024 20:38:54 +0800 Subject: [PATCH] =?UTF-8?q?fun=EF=BC=9A=E4=B8=80=E4=B8=AA=E5=9F=BA?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E6=89=93=E5=8C=85=E5=8A=9F=E8=83=BD=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 1 + cmd_parse.cpp | 60 +++++++++++++++++++++++++++++++++++------- cmd_parse.h | 2 +- main.cpp | 6 ++++- pack.cpp | 61 ++++++++++++++++++++++++++++++++++++++----- pack.h | 5 +++- 6 files changed, 117 insertions(+), 18 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 74831d9..1ec6b1a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -17,6 +17,7 @@ } ], "args": [ + "-m", "0", "-f", "/home/yun/Code/PackBinary/build/PackBinary", "-p", "/home/yun/文档", "-d", "/home/yun/mlib/boost/lib" ] }, // "cmake.configureSettings": { diff --git a/cmd_parse.cpp b/cmd_parse.cpp index 8c9be99..cac36c1 100644 --- a/cmd_parse.cpp +++ b/cmd_parse.cpp @@ -1,4 +1,6 @@ #include "cmd_parse.h" +#include "install.h" +#include "pack.h" #include #include @@ -52,7 +54,7 @@ bool CCmdParse::cmdParse(int argc, char* argv[]) return true; } -bool CCmdParse::checkArgs() +bool CCmdParse::Run() { std::cout << "信息 ==========================================>\n"; std::cout << "binary:" << result_.binary << "\n"; @@ -68,18 +70,58 @@ bool CCmdParse::checkArgs() auto check_file_dir = [&](const std::string& path, bool is_file) -> bool { std::string type = is_file ? "文件:" : "文件夹:"; fs::path tp(path); - if (!fs::exists(tp) || !fs::is_regular_file(tp)) { - std::cout << type << path << ",不存在\n"; - return false; + 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 { - std::cout << type << path << ",检查通过!\n"; - return true; + 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"; - check_file_dir(result_.binary, true); - check_file_dir(result_.ico, true); - return false; + 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 (!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; + } } diff --git a/cmd_parse.h b/cmd_parse.h index f7fa991..a660d52 100644 --- a/cmd_parse.h +++ b/cmd_parse.h @@ -12,7 +12,7 @@ public: CCmdParse(); public: bool cmdParse(int argc, char* argv[]); - bool checkArgs(); + bool Run(); private: CmdResult result_; }; diff --git a/main.cpp b/main.cpp index 7dcaa97..bfa2b18 100644 --- a/main.cpp +++ b/main.cpp @@ -4,11 +4,15 @@ int main(int argc, char** argv) { + if (argc == 1) { + std::cout << "请使用 -h 或者 --help 查看帮助。" << std::endl; + return 0; + } CCmdParse parse; if (!parse.cmdParse(argc, argv)) { return 0; } - if (!parse.checkArgs()) { + if (!parse.Run()) { return 0; } return 0; diff --git a/pack.cpp b/pack.cpp index 0a0e4ea..d7a2c5d 100644 --- a/pack.cpp +++ b/pack.cpp @@ -1,10 +1,16 @@ #include "pack.h" -#include #include +#include +#include + +namespace fs = boost::filesystem; bool CPackBinary::startPack(const CmdResult& result) { - return false; + result_ = result; + auto dpends = getDepends(result_.binary, result.lib_dirs); + auto should_copy = parseResult(dpends); + return handleAndCopy(should_copy, result_.purpose_dir); } std::vector @@ -20,7 +26,7 @@ CPackBinary::getDepends(const std::string& path, cmds.append(" && ldd " + path); auto* pf = popen(cmds.c_str(), "r"); - if (pf != nullptr) { + if (pf == nullptr) { return result; } char buffer[1024]{}; @@ -33,7 +39,7 @@ CPackBinary::getDepends(const std::string& path, std::vector split; boost::split(split, output, boost::is_any_of("\t")); for (const auto& item : split) { - std::cout << item << "\n"; + result.push_back(item); } return result; } @@ -42,11 +48,54 @@ std::list CPackBinary::parseResult(const std::vector& result) { std::list ret; + auto backup = result; + for (auto& item : backup) { + if (item.empty()) { + continue; + } + if (boost::contains(item, "not found")) { + std::cout << "未找到依赖:" << item << std::endl; + continue; + } + boost::replace_all(item, "=>", ""); + std::vector split; + boost::split(split, item, boost::is_any_of(" ")); + std::string h; + if (split.size() == 4) { + h = split[2]; + } + if (split.size() == 3) { + h = split[1]; + } + if (boost::starts_with(h, "/lib")) { + continue; + } + if (!h.empty()) { + ret.push_back(h); + std::cout << "依赖:" << h << std::endl; + } + } + ret.push_back(result_.binary); return ret; } -bool CPackBinary::handleAndCopy(const std::list& libs) +bool CPackBinary::handleAndCopy(const std::list& libs, + const std::string& des) { - return false; + auto filename = fs::path(result_.binary).filename().string(); + auto dest_directory = fs::path(des).append(filename); + + try { + fs::create_directories(dest_directory); + for (const auto& item : libs) { + auto item_name = fs::path(item).filename().string(); + auto newpath = fs::path(dest_directory).append(item_name); + fs::copy_file(item, newpath, fs::copy_options::overwrite_existing); + } + return true; + } catch (const std::exception& e) { + std::cerr << e.what() << '\n'; + return false; + } } diff --git a/pack.h b/pack.h index 79aa7d6..97182d9 100644 --- a/pack.h +++ b/pack.h @@ -10,6 +10,9 @@ public: CPackBinary() = default; ~CPackBinary() = default; +private: + CmdResult result_; + public: bool startPack(const CmdResult& result); @@ -17,7 +20,7 @@ private: std::vector getDepends(const std::string& path, const std::vector& dirs); std::list parseResult(const std::vector& result); - bool handleAndCopy(const std::list& libs); + bool handleAndCopy(const std::list& libs, const std::string& des); }; #endif \ No newline at end of file