fun:一个基本的打包功能完成。

This commit is contained in:
taynpg 2024-10-29 20:38:54 +08:00
parent bd18a7a907
commit df2380f5ed
6 changed files with 117 additions and 18 deletions

View File

@ -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": {

View File

@ -1,4 +1,6 @@
#include "cmd_parse.h"
#include "install.h"
#include "pack.h"
#include <boost/filesystem.hpp>
#include <iostream>
@ -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,6 +70,7 @@ 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 (is_file) {
if (!fs::exists(tp) || !fs::is_regular_file(tp)) {
std::cout << type << path << ",不存在\n";
return false;
@ -75,11 +78,50 @@ bool CCmdParse::checkArgs()
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";
check_file_dir(result_.binary, true);
check_file_dir(result_.ico, true);
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;
}
}

View File

@ -12,7 +12,7 @@ public:
CCmdParse();
public:
bool cmdParse(int argc, char* argv[]);
bool checkArgs();
bool Run();
private:
CmdResult result_;
};

View File

@ -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;

View File

@ -1,10 +1,16 @@
#include "pack.h"
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <iostream>
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<std::string>
@ -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<std::string> 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<std::string>
CPackBinary::parseResult(const std::vector<std::string>& result)
{
std::list<std::string> 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<std::string> 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<std::string>& libs)
bool CPackBinary::handleAndCopy(const std::list<std::string>& libs,
const std::string& des)
{
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;
}
}

5
pack.h
View File

@ -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<std::string> getDepends(const std::string& path,
const std::vector<std::string>& dirs);
std::list<std::string> parseResult(const std::vector<std::string>& result);
bool handleAndCopy(const std::list<std::string>& libs);
bool handleAndCopy(const std::list<std::string>& libs, const std::string& des);
};
#endif