-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflakegen.cpp
More file actions
366 lines (331 loc) · 10.4 KB
/
flakegen.cpp
File metadata and controls
366 lines (331 loc) · 10.4 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <string>
#include <format>
#include <fstream>
#include <filesystem>
#include <ftxui/component/app.hpp>
#include <ftxui/component/component.hpp>
#include <ftxui/component/component_base.hpp>
#include <ftxui/dom/elements.hpp>
const std::unordered_map<std::string, std::string> lang_map = {
{"c", "clang\ngnumake\nclang-tools\nbear\npkg-config\n"},
{"cpp", "clang\ncmake\nclang-tools\nbear\npkg-config\n"},
{"rust", "rustc\nrustfmt\nrust-analyzer\ncargo\nclippy\n"},
{"zig", "zig\nzls\n"},
{"js", "nodejs\n"}
};
std::string flake = "{{\n"
"\tdescription = \"{} devshell\";\n"
"\tinputs = {{\n"
"\t\tnixpkgs.url = \"nixpkgs/nixos-{}\";\n"
"{}"
"\t}};\n"
"\toutputs = {{ self, nixpkgs, {} }}:\n"
"\tlet\n"
"\t\tsystem = \"{}\";\n"
"\t\tpkgs = import nixpkgs {{ inherit system; }};\n"
"{}"
"\tin\n"
"\t{{\n"
"\t\tdevShell.${{system}}.default = pkgs.mkShell {}{{\n"
"{}"
"\t\t\tbuildInputs= with pkgs; [\n{}\n\t\t\t];\n"
"{}"
"\t\t}};\n"
"\t}};\n"
"}}";
std::vector<std::string> split(
std::string const& str,
std::string const& delim,
std::size_t times = INT_MAX
);
std::string gen_inputs (std::string name, std::string url);
void usage (std::string program);
using namespace ftxui;
Component LabeledInput(std::string label_text, Component input_comp);
int main()
{
namespace fs = std::filesystem;
App screen = App::TerminalOutput();
std::vector<std::string> tab_values{
"Setup",
"Inputs",
"Packages",
"Shell Hook",
// "Variables and Overrides",
"Finish"
};
int tab_selected = 0;
auto tab_toggle = Toggle(&tab_values, &tab_selected);
std::string devshell_name = "";
std::string dir = ".";
std::string system = "x86_64-linux";
std::string vnixos = "unstable";
std::string language = "";
std::vector<std::string> nix_inputs;
ftxui::Box vbox_size;
std::vector<std::string> shellHook {};
std::string pkgs = "", build_inputs = "", native_build_inputs = "";
std::function<void ()> onClick_generate;
std::vector<std::string> status_messages {};
Component i_devshell = Input(&devshell_name, "");
Component i_dir = Input(&dir, " ./ ");
Component i_system = Input(&system, " x86_64-linux ");
Component i_vnix = Input(&vnixos, " unstable ");
Component i_lang = Input(&language, "");
std::string current_nix_input = "";
Component i_nix_input = Input(¤t_nix_input, "Add a nix input (i.e. name=github:user/repo)...");
Component i_nix_input_logic = CatchEvent(i_nix_input, [&](Event event) {
if (event == Event::Return)
{
if (!current_nix_input.empty())
{
nix_inputs.push_back(current_nix_input);
current_nix_input.clear();
}
return true;
}
return false;
});
std::string current_shellHook_input= "";
Component i_shellhook = Input(¤t_shellHook_input, "");
Component i_shellhook_logic = CatchEvent(i_shellhook, [&](Event event) {
if (event == Event::Return)
{
if (!current_shellHook_input.empty())
{
shellHook.emplace_back(current_shellHook_input);
current_shellHook_input.clear();
}
return true;
}
return false;
});
Component i_pkgs = Input(&pkgs, "");
Component i_buildinputs = Input(&build_inputs, "");
Component i_nbuildinputs = Input(&native_build_inputs, "");
onClick_generate = [&] ()
{
std::stringstream flake_inputs {};
std::stringstream flake_outputs {};
std::stringstream packages {};
for (std::string& in: nix_inputs)
{
std::vector<std::string> splits = split(in, "=");
if (splits.size() != 2)
{
status_messages.emplace_back("[ERROR] Invalid Nix Input!");
tab_selected = 1;
return;
}
flake_inputs << gen_inputs(splits.front(), splits.back());
flake_outputs << splits.front() + ", ";
packages << "\t\t\t\t" + splits.front() + ".packages.${system}." + splits.front() + "\n";
}
flake_outputs << "...";
std::string _nbuildInputs = "";
if (!native_build_inputs.empty())
{
_nbuildInputs = "\t\t\tnativeBuildInputs = with pkgs; [\n";
std::vector<std::string> nbI = split(native_build_inputs, " ");
for (std::string pkg : nbI)
{
_nbuildInputs += "\t\t\t\t" + pkg + "\n";
}
_nbuildInputs += "\t\t\t];\n";
}
std::string _buildInputs = "";
if (!build_inputs.empty())
{
if (!language.empty() && lang_map.contains(language))
{
std::vector<std::string> lang_pkgs = split(lang_map.at(language), "\n");
for (std::string pkg : lang_pkgs)
{
_buildInputs += "\t\t\t\t" + pkg + "\n";
}
}
std::vector<std::string> bI = split(build_inputs, " ");
for (std::string pkg: bI)
{
_buildInputs += "\t\t\t\t" + pkg + "\n";
}
std::vector<std::string> ipkgs = split(pkgs, " ");
for (std::string pkg: ipkgs)
{
_buildInputs += "\t\t\t\t" + pkg + "\n";
}
_buildInputs += packages.str();
}
std::stringstream shell_hooks {};
shell_hooks << "";
if (!shellHook.empty())
{
shell_hooks << "\t\t\tshellHook = ''\n";
for (std::string& sh: shellHook)
{
shell_hooks << "\t\t\t\t" + sh + "\n";
}
shell_hooks << "\t\t\t'';\n";
}
fs::path path {fs::current_path()};
if (!dir.empty())
{
if (fs::exists(dir)) path /= dir;
else
{
status_messages.emplace_back("[ERROR] Path doesn't exist! Please provide a valid path!");
tab_selected = 0;
return;
}
}
std::string flake_inputs_str = flake_inputs.str();
std::string flake_outputs_str = flake_outputs.str();
std::string shell_hooks_str = shell_hooks.str();
std::string variables = "", overrides = ""; //INFO: not yet impl'd
try
{
std::ofstream nix_flake (path/"flake.nix");
if (!nix_flake.is_open())
{
throw std::runtime_error("File is not open!");
}
nix_flake << std::vformat(
flake,
std::make_format_args(
devshell_name, vnixos, flake_inputs_str, flake_outputs_str, system,
variables, overrides, _nbuildInputs, _buildInputs, shell_hooks_str
)
);
nix_flake.close();
status_messages.emplace_back("[INFO] Path to generated flake: "+ path.string());
screen.Exit();
} catch (std::exception& e)
{
status_messages.emplace_back(std::format("[ERROR] {}", e.what()));
}
};
Component setup_tab = Container::Vertical({
LabeledInput("DevShell Name: ", i_devshell),
LabeledInput("Output Directory: ",i_dir),
LabeledInput("System: ",i_system),
LabeledInput("Nixpkgs Version: ",i_vnix),
LabeledInput("Language: ",i_lang),
});
Component nix_input_tab = Renderer(i_nix_input_logic, [&] {
Elements list_elements;
for (const auto& in : nix_inputs)
{
list_elements.push_back(text(" • " + in) | color(Color::Blue));
}
return vbox({
text(" Nix Flake Inputs ") | bold | center,
separator(),
vbox(std::move(list_elements)) | size(HEIGHT, LESS_THAN, 10) | reflect(vbox_size),
filler(),
separator(),
hbox({ text(" Add Input: "), i_nix_input_logic->Render() | flex }),
});
});
Component packages_tab = Container::Vertical({
LabeledInput("Native Build Inputs: ", i_nbuildinputs),
LabeledInput("Build Inputs: ", i_buildinputs),
LabeledInput("Packages: ", i_pkgs),
});
Component shellHook_tab = Renderer(i_shellhook_logic, [&] {
Elements list_elements;
for (const auto& hook : shellHook)
{
list_elements.push_back(text(" • " + hook) | color(Color::Blue));
}
return vbox({
text(" Nix Flake Shell Hook") | bold | center,
separator(),
vbox(std::move(list_elements)) | size(HEIGHT, LESS_THAN, 10) | reflect(vbox_size),
filler(),
separator(),
hbox({ i_shellhook_logic->Render() | flex }),
});
});
Component btn_generate = Button(" Generate Flake ", onClick_generate, ButtonOption::Animated());
Component finish_container = Container::Vertical({btn_generate});
Component finish_tab = Renderer(finish_container, [&] {
Elements list_messages;
for (const std::string& message : status_messages)
{
Color msg_color = message.find("[ERROR]") != std::string::npos ? Color::Red : Color::Blue;
list_messages.push_back(text(" • " + message) | color(msg_color));
}
return vbox({
text(" Status Messages ") | bold | center,
separator(),
vbox(std::move(list_messages)) | size(HEIGHT, LESS_THAN, 10) | reflect(vbox_size),
filler(),
separator(),
finish_container->Render()
});
});
// Tabs
Component tab_container = Container::Tab(
{
setup_tab,
nix_input_tab,
packages_tab,
shellHook_tab,
finish_tab
},
&tab_selected);
Component container = Container::Vertical({
tab_toggle,
tab_container,
});
Component tab_renderer = Renderer(container, [&] {
return vbox({
tab_toggle->Render(),
separator(),
tab_container->Render(),
}) |
border;
});
screen.Loop(tab_renderer);
}
std::string gen_inputs (std::string name, std::string url)
{
std::string final_str {};
final_str.append("\t\t" + name + ".url = \"{}\";\n");
if (url.contains(':'))
{
final_str = std::vformat(final_str, std::make_format_args(url));
}else
{
std::string full_url = "github:" + url;
final_str = std::vformat(final_str, std::make_format_args(full_url));
}
return final_str;
}
std::vector<std::string> split (
std::string const& str,
std::string const& delim,
std::size_t times
)
{
std::vector<std::string> vtok;
std::size_t start {0};
auto end = str.find(delim);
while ((times-- > 0) && (end != std::string::npos))
{
vtok.emplace_back(str.substr(start, end - start));
start = end + delim.length();
end = str.find(delim, start);
}
vtok.emplace_back(str.substr(start));
return vtok;
}
Component LabeledInput(std::string label_text, Component input_comp)
{
return Renderer(input_comp, [label_text, input_comp] {
return hbox({
text(label_text) | size(WIDTH, EQUAL, 20) | vcenter,
input_comp->Render()
});
});
}