-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLIHelperFunctions.cpp
More file actions
209 lines (177 loc) · 7.64 KB
/
Copy pathCLIHelperFunctions.cpp
File metadata and controls
209 lines (177 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "CLIHelperFunctions.hpp"
#include "CLI.hpp"
#include "Libraries/Asker/Asker.hpp"
#include "Libraries/Color/Color.hpp"
#include "Libraries/Localization/Localization.hpp"
// ======== Helping functions ========
// Lists authors by comma. If author is only mentioned once, just author name is inputted
std::string listAuthors(const std::vector<std::string>& authors) {
std::string authorList;
bool first = true;
for (const std::string& raw : authors) {
std::string name = trim(raw);
if (name.empty()) continue;
if (!first) authorList += ", ";
authorList += name;
first = false;
}
return authorList;
}
// Formats the input string to letters and underscores.
std::string formatNameToSnakeCase(const std::string& input) {
std::string result = input;
for (char& c : result) {
if (c == ' ') c = '_';
else c = std::tolower(static_cast<unsigned char>(c));
}
return result;
}
// Returns progress bar for CLI
void showProgressBar(const std::string& stepName, int step, int total) {
int percentage = (step * 100) / total;
int hashes = percentage / 5;
if (percentage != 100) std::println("{} [ {} ({}{}) {}% ] {}", Color::TextHex("#f6ff75"), stepName, std::string(hashes, '#'), std::string(20 - hashes, '_'), percentage, Color::TextHex("#01e0d4"));
else std::println("{} [ {} ({}{}) {}%! ] {}", Color::TextHex("#75ff87"), stepName, std::string(hashes, '#'), std::string(20 - hashes, '_'), percentage, Color::TextHex("#01e0d4"));
}
// Clears terminal screen
void clearScreen() {
std::cout << "\033[0m\033[2J\033[H";
}
// -------- stuff required for parseProjectFile
std::string getString(const Toml::TomlTable& table, const std::string& key, const std::string& def) {
for (const auto& [k, v] : table)
if (k == key && v.type == Toml::TomlType::String)
return std::get<std::string>(v.value);
return def;
}
std::vector<std::string> getStringArray(const Toml::TomlTable& table, const std::string& key) {
for (const auto& [k, v] : table) {
if (k == key && v.type == Toml::TomlType::Array) {
std::vector<std::string> result;
const auto& arr = std::get<Toml::TomlArray>(v.value);
for (const auto& item : arr)
if (item.type == Toml::TomlType::String)
result.push_back(std::get<std::string>(item.value));
return result;
}
}
return {};
}
std::map<std::string, std::string> extractMap(const Toml::TomlTable& root, const std::string& key) {
std::map<std::string, std::string> result;
for (const auto& [k, v] : root) {
if (k == key && v.type == Toml::TomlType::Table) {
const auto& tbl = std::get<Toml::TomlTable>(v.value);
for (const auto& [tk, tv] : tbl)
if (tv.type == Toml::TomlType::String)
result[tk] = std::get<std::string>(tv.value);
}
}
return result;
}
// --------
// Argument parsing
CLIArgs parseArgs(int argc, char** argv) {
CLIArgs args;
if (argc > 1) args.command = argv[1];
for (int i = 2; i < argc; i++) {
std::string token = argv[i];
if (token.rfind("--", 0) == 0) {
std::string key = token.substr(2);
std::string value;
while (i + 1 < argc && argv[i + 1][0] != '-') {
if (!value.empty()) value += " ";
value += argv[++i];
}
args.options[key] = value;
} else args.positional.push_back(token);
}
return args;
}
ProjectConfig parseProjectFile(const std::string& file) {
std::string contents = readFile(file);
std::istringstream ss(contents);
Toml::TomlTable root = Toml::parseToml(ss);
ProjectConfig config;
// project table parsing
auto it = std::find_if(root.begin(), root.end(), [](const auto& kv){ return kv.first == "project"; });
if (it != root.end() && it->second.type == Toml::TomlType::Table) {
const auto& project = std::get<Toml::TomlTable>(it->second.value);
config.name = getString(project, "name", config.name);
config.version = getString(project, "version", config.version);
config.author = getStringArray(project, "authors");
config.license = parseLicense(getString(project, "license", ""));
config.sourceFolder = getString(project, "sourceFolder", config.sourceFolder);
config.output = parseOutput(getString(project, "output", ""));
config.buildFolder = getString(project, "buildFolder", config.buildFolder);
}
// get configured tasks, dependencies, tests and languagePacks
config.tasks = extractMap(root, "tasks");
config.dependencies = extractMap(root, "dependencies");
config.tests = extractMap(root, "tests");
config.languagePacks = extractMap(root, "languagePacks");
// configure source path
config.sourcePath = std::filesystem::path(file).parent_path().string();
return config;
}
CompilerSettings parseCompilerSettings(const std::map<std::string, ProjectSettingValue>& map) {
CompilerSettings config;
config.verbose = map.contains("verbose") ? std::get<bool>(map.at("verbose")) : config.verbose;
config.baremetal = map.contains("baremetal") ? std::get<bool>(map.at("baremetal")) : config.baremetal;
return config;
}
std::string licenseID(License license) {
switch (license) {
case License::AGPL: return "agpl";
case License::Apache: return "apache";
case License::Boost: return "boost";
case License::BSD2: return "bsd2";
case License::BSD3: return "bsd3";
case License::CC0: return "cc0";
case License::Eclipse: return "eclipse";
case License::GPL2: return "gpl2";
case License::GPL3: return "gpl3";
case License::LGPL: return "lgpl";
case License::MIT: return "mit";
case License::Mozilla: return "mozilla";
case License::Unlicense: return "unlicense";
default: return "custom";
}
}
std::string outputID(OutputType type) {
switch (type) {
case OutputType::Executable: return "exe";
case OutputType::IR: return "ir";
case OutputType::LLVM_IR: return "llvm_ir";
case OutputType::Object: return "obj";
case OutputType::SharedLibrary: return "sharedlib";
case OutputType::StaticLibrary: return "staticlib";
default: return "";
}
}
License parseLicense(std::string license) {
if (license == "mit") return License::MIT;
if (license == "apache") return License::Apache;
if (license == "gpl3") return License::GPL3;
if (license == "bsd2") return License::BSD2;
if (license == "bsd3") return License::BSD3;
if (license == "boost") return License::Boost;
if (license == "cc0") return License::CC0;
if (license == "eclipse") return License::Eclipse;
if (license == "agpl") return License::AGPL;
if (license == "gpl2") return License::GPL2;
if (license == "lgpl") return License::LGPL;
if (license == "mozilla") return License::Mozilla;
if (license == "unlicense") return License::Unlicense;
return License::Custom;
}
OutputType parseOutput(std::string outputType) {
if (outputType == "exe") return OutputType::Executable;
if (outputType == "ir") return OutputType::IR;
if (outputType == "llvm_ir") return OutputType::LLVM_IR;
if (outputType == "obj") return OutputType::Object;
if (outputType == "sharedlib") return OutputType::SharedLibrary;
if (outputType == "staticlib") return OutputType::StaticLibrary;
std::println(std::cerr, "{}[NeolumaCLI/IDtoOutput] {}", Color::TextHex("#ff5050"), Localization::translate("CLI.parseProjectFile.parseOutputError"));
return OutputType::None;
}