|
| 1 | +// system includes |
| 2 | +#include <cstring> |
| 3 | +#include <iostream> |
| 4 | +#include <dirent.h> |
| 5 | +#include <libgen.h> |
| 6 | +#include <unistd.h> |
| 7 | + |
| 8 | +// library includes |
| 9 | +#include <args.hxx> |
| 10 | + |
| 11 | +bool isFile(const std::string& path) { |
| 12 | + return access(path.c_str(), F_OK) == 0; |
| 13 | +} |
| 14 | + |
| 15 | +std::string findAppimagetool(const char* const argv0) { |
| 16 | + std::vector<char> argv0Buf(strlen(argv0) + 1); |
| 17 | + strcpy(argv0Buf.data(), argv0); |
| 18 | + |
| 19 | + auto currentDirPath = std::string(dirname(argv0Buf.data())); |
| 20 | + |
| 21 | + // search next to current binary |
| 22 | + std::vector<std::string> knownPaths = { |
| 23 | + currentDirPath + "/appimagetool", |
| 24 | + currentDirPath + "/appimagetool.AppImage", |
| 25 | + currentDirPath + "/appimagetool-x86_64.AppImage", |
| 26 | + }; |
| 27 | + |
| 28 | + std::stringstream ss; |
| 29 | + ss << getenv("PATH"); |
| 30 | + |
| 31 | + // also search in PATH |
| 32 | + std::string currentPath; |
| 33 | + while (std::getline(ss, currentPath, ':')) { |
| 34 | + knownPaths.push_back(currentPath + "/appimagetool"); |
| 35 | + knownPaths.push_back(currentPath + "/appimagetool.AppImage"); |
| 36 | + knownPaths.push_back(currentPath + "/appimagetool-x86_64.AppImage"); |
| 37 | + } |
| 38 | + |
| 39 | + for (const auto& path : knownPaths) { |
| 40 | + if (isFile(path)) |
| 41 | + return path; |
| 42 | + } |
| 43 | + |
| 44 | + throw std::runtime_error("Could not find appimagetool in PATH"); |
| 45 | +} |
| 46 | + |
| 47 | +int main(const int argc, const char* const* const argv) { |
| 48 | + args::ArgumentParser parser("linuxdeploy-plugin-appimage"); |
| 49 | + |
| 50 | + args::ValueFlag<std::string> appdir(parser, "AppDir", "AppDir to package as AppImage", {"appdir"}); |
| 51 | + args::Flag pluginType(parser, "", "Return plugin type and exit", {"plugin-type"}); |
| 52 | + |
| 53 | + try { |
| 54 | + parser.ParseCLI(argc, argv); |
| 55 | + } catch (const args::UsageError&) { |
| 56 | + std::cout << parser; |
| 57 | + } catch (const args::ParseError& e) { |
| 58 | + std::cerr << "Failed to parse arguments: " << e.what() << std::endl << std::endl; |
| 59 | + std::cout << parser; |
| 60 | + return 1; |
| 61 | + } |
| 62 | + |
| 63 | + if (!appdir) { |
| 64 | + std::cerr << "--appdir parameter required" << std::endl << std::endl; |
| 65 | + std::cout << parser; |
| 66 | + return 1; |
| 67 | + } |
| 68 | + |
| 69 | + if (pluginType) { |
| 70 | + std::cout << "output" << std::endl; |
| 71 | + return 0; |
| 72 | + } |
| 73 | + |
| 74 | + auto pathToAppimagetool = findAppimagetool(argv[0]); |
| 75 | + |
| 76 | + execl(pathToAppimagetool.c_str(), "appimagetool", appdir.Get().c_str(), nullptr); |
| 77 | + |
| 78 | + auto error = errno; |
| 79 | + std::cerr << "execl() failed: " << strerror(error) << std::endl; |
| 80 | +} |
0 commit comments