|
1 | 1 | // system includes
|
| 2 | +#include <cassert> |
2 | 3 | #include <cstring>
|
3 |
| -#include <iostream> |
4 | 4 | #include <dirent.h>
|
| 5 | +#include <functional> |
| 6 | +#include <iostream> |
5 | 7 | #include <libgen.h>
|
| 8 | +#include <optional> |
| 9 | +#include <string> |
6 | 10 | #include <unistd.h>
|
7 | 11 | #include <vector>
|
8 | 12 |
|
@@ -50,6 +54,41 @@ std::string findAppimagetool() {
|
50 | 54 | return "";
|
51 | 55 | }
|
52 | 56 |
|
| 57 | +/** |
| 58 | + * Fetch variable from environment by name(s). |
| 59 | + * The first name is treated as the currently accepted one, whereas the others are still supported but will trigger a |
| 60 | + * warning. |
| 61 | + * @param names One or more names |
| 62 | + * @return value if available, std::nullopt otherwise |
| 63 | + */ |
| 64 | +std::optional<std::string> getEnvVar(std::initializer_list<std::string> names) { |
| 65 | + assert(names.size() >= 1); |
| 66 | + |
| 67 | + for (auto it = names.begin(); it != names.end(); ++it) { |
| 68 | + const auto* value = getenv(it->c_str()); |
| 69 | + |
| 70 | + if (value == nullptr) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + if (it != names.begin()) { |
| 75 | + std::cerr << "Warning: please use $" << *names.begin() << " instead of $" << *it << std::endl; |
| 76 | + } |
| 77 | + |
| 78 | + return value; |
| 79 | + } |
| 80 | + |
| 81 | + return std::nullopt; |
| 82 | +} |
| 83 | + |
| 84 | +void doSomethingWithEnvVar(std::initializer_list<std::string> names, const std::function<void(const std::string&)>& todo) { |
| 85 | + const auto value = getEnvVar(names); |
| 86 | + |
| 87 | + if (value.has_value()) { |
| 88 | + todo(value.value()); |
| 89 | + } |
| 90 | +} |
| 91 | + |
53 | 92 | int main(const int argc, const char* const* const argv) {
|
54 | 93 | args::ArgumentParser parser("linuxdeploy-plugin-appimage");
|
55 | 94 |
|
@@ -127,23 +166,42 @@ int main(const int argc, const char* const* const argv) {
|
127 | 166 | }
|
128 | 167 | }
|
129 | 168 |
|
130 |
| - if (getenv("VERBOSE") != nullptr) { |
131 |
| - args.push_back(strdup("-v")); |
132 |
| - } |
| 169 | + constexpr auto appimagetool_verbose_arg = "-v"; |
133 | 170 |
|
134 |
| - if (getenv("OUTPUT") != nullptr) { |
| 171 | + doSomethingWithEnvVar({"LDAI_VERBOSE", "VERBOSE"}, [&](const auto& value) { |
| 172 | + (void) value; |
| 173 | + args.push_back(strdup(appimagetool_verbose_arg)); |
| 174 | + }); |
| 175 | + |
| 176 | + // the only exception to prefixing is $DEBUG, which is used across multiple linuxdeploy plugins |
| 177 | + doSomethingWithEnvVar({"DEBUG"}, [&](const auto& value) { |
| 178 | + (void) value; |
| 179 | + |
| 180 | + // avoid duplicates |
| 181 | + if (std::find(args.begin(), args.end(), appimagetool_verbose_arg) == args.end()) { |
| 182 | + args.push_back(strdup(appimagetool_verbose_arg)); |
| 183 | + } |
| 184 | + }); |
| 185 | + |
| 186 | + doSomethingWithEnvVar({"LDAI_OUTPUT", "OUTPUT"}, [&](const auto& value) { |
| 187 | + (void) value; |
135 | 188 | args.push_back(strdup(getenv("OUTPUT")));
|
136 |
| - } |
| 189 | + }); |
137 | 190 |
|
138 |
| - if (getenv("NO_APPSTREAM") != nullptr) { |
| 191 | + doSomethingWithEnvVar({"LDAI_NO_APPSTREAM", "NO_APPSTREAM"}, [&](const auto& value) { |
| 192 | + (void) value; |
139 | 193 | args.push_back(strdup("--no-appstream"));
|
140 |
| - } |
| 194 | + }); |
141 | 195 |
|
142 |
| - auto comp = getenv("APPIMAGE_COMP"); |
143 |
| - if (comp != nullptr) { |
| 196 | + doSomethingWithEnvVar({"LDAI_COMP", "APPIMAGE_COMP"}, [&](const auto& value) { |
144 | 197 | args.push_back(strdup("--comp"));
|
145 |
| - args.push_back(strdup(comp)); |
146 |
| - } |
| 198 | + args.push_back(strdup(value.c_str())); |
| 199 | + }); |
| 200 | + |
| 201 | + // $VERSION is already picked up by appimagetool |
| 202 | + doSomethingWithEnvVar({"LDAI_VERSION"}, [&](const auto& value) { |
| 203 | + setenv("VERSION", value.c_str(), true); |
| 204 | + }); |
147 | 205 |
|
148 | 206 | args.push_back(nullptr);
|
149 | 207 |
|
|
0 commit comments