diff --git a/CMakeLists.txt b/CMakeLists.txt index faef521..441d600 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,4 +27,4 @@ find_package(Boost REQUIRED program_options filesystem) include_directories(${Boost_INCLUDE_DIR}) add_executable(PackBinary ${SOURCES_FILE}) -target_link_libraries(PackBinary PRIVATE ${Boost_LIBRARIES}) \ No newline at end of file +target_link_libraries(PackBinary PRIVATE ${Boost_LIBRARIES} dl) \ No newline at end of file diff --git a/install.cpp b/install.cpp index 79e5b0d..43eda92 100644 --- a/install.cpp +++ b/install.cpp @@ -1,9 +1,63 @@ #include "install.h" #include "resource.h" +#include +#include +#include +#include + +namespace fs = boost::filesystem; bool CInstallBinary::startInstall(const CmdResult& result) { CResource res; auto shell_template = res.getShellTemplate(); - return false; + 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()); + std::system(cmd.c_str()); + + if (!write_file(desktop_tempte, temp_desktop.string())) { + return false; + } + + cmd.clear(); + cmd = "pkexec cp " + temp_desktop.string() + " /usr/share/applications"; + std::system(cmd.c_str()); + + return true; } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 6678137..cd3383b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,11 @@ #include #include "cmd_parse.h" +#include int main(int argc, char** argv) { + std::cout << boost::dll::program_location().string() << std::endl; if (argc == 1) { std::cout << "请使用 -h 或者 --help 查看帮助。" << std::endl; return 0;