PackBinary/install.cpp

68 lines
2.3 KiB
C++
Raw Normal View History

2024-10-27 15:51:46 +08:00
#include "install.h"
#include "resource.h"
2024-10-31 14:46:47 +08:00
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <fstream>
#include <iostream>
namespace fs = boost::filesystem;
2024-10-27 15:51:46 +08:00
bool CInstallBinary::startInstall(const CmdResult& result)
{
CResource res;
auto shell_template = res.getShellTemplate();
2024-10-31 14:46:47 +08:00
auto desktop_tempte = res.getDesktopTemplate();
auto file_name = fs::path(result.binary).filename().string();
// 写临时文件
char* home = std::getenv("HOME");
fs::path homep(home);
fs::path work_path = fs::path(homep).append(".config").append("PackBinary");
fs::create_directories(work_path);
fs::path temp_shell = fs::path(result.binary).parent_path().append(file_name + "_run.sh");
fs::path temp_desktop = fs::path(work_path).append(file_name + ".desktop");
// fs::path dest_dir = fs::path(result.purpose_dir).append(file_name);
// fs::create_directories(dest_dir);
auto write_file = [](const std::string& content,
const std::string& file) -> bool {
std::ofstream of(file);
if (!of.is_open()) {
std::cerr << "file not opened: " << file << std::endl;
return false;
}
of << content;
of.close();
return true;
};
boost::replace_all(shell_template, "replace_str", file_name);
boost::replace_all(desktop_tempte, "re_name", file_name);
boost::replace_all(desktop_tempte, "re_describe", "default");
boost::replace_all(desktop_tempte, "re_path", temp_shell.string());
boost::replace_all(desktop_tempte, "re_icon", result.ico);
boost::replace_all(desktop_tempte, "re_category", result.category);
if (!write_file(shell_template, temp_shell.string())) {
return false;
}
std::string cmd("chmod +x " + temp_shell.string());
2024-10-31 14:48:48 +08:00
std::cout << cmd << " Ret:" << std::system(cmd.c_str()) << std::endl;
2024-10-31 14:46:47 +08:00
if (result.only_shell) {
std::cout << "仅生成shell完成。" << std::endl;
return true;
}
2024-10-31 14:46:47 +08:00
if (!write_file(desktop_tempte, temp_desktop.string())) {
return false;
}
cmd.clear();
cmd = "pkexec cp " + temp_desktop.string() + " /usr/share/applications";
2024-10-31 14:48:48 +08:00
std::cout << cmd << " Ret:" << std::system(cmd.c_str()) << std::endl;
2024-10-31 14:46:47 +08:00
return true;
2024-10-27 15:51:46 +08:00
}