-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_script.js
More file actions
368 lines (315 loc) · 11.5 KB
/
Copy pathcreate_script.js
File metadata and controls
368 lines (315 loc) · 11.5 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
366
367
368
class Library {
constructor(name, git_url_ssh, git_url_https, description, projectPage, dependencies, addons) {
this.name = name;
this.git_url_ssh = git_url_ssh;
this.git_url_https = git_url_https;
this.dependencies = dependencies;
this.selectedCheckbox = null;
this.useSSHCheckbox = null;
this.projectPage = projectPage;
this.description = description;
this.addons = addons;
}
}
var getJSON = function (url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function () {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};
function load_libraries(callback) {
var url = "libraries.json";
var output = [];
getJSON(url, function (err, libs) {
if (err !== null) {
console.log("something went wrong " + err);
}
else {
for (const lib of libs.libraries) {
output.push(new Library(
lib.name,
lib.git_url_ssh,
lib.git_url_https,
lib.description,
lib.project_url,
lib.dependencies,
lib.addons
));
}
callback(output);
}
});
}
var libraries = [];
function get_lib(name) {
for (const lib of libraries) {
if (lib.name == name) {
return lib;
}
}
return null;
}
function disable_lib(name) {
for (lib of libraries) {
if (lib.dependencies.includes(name)) {
lib.selectedCheckbox.checked = false;
disable_lib(lib.name);
}
}
}
function enable_lib(name) {
lib = get_lib(name);
for (const depend of lib.dependencies) {
get_lib(depend).selectedCheckbox.checked = true;
enable_lib(depend);
}
}
function onSelectedCheckboxClicked(cb) {
var name = cb.value;
if (cb.checked) {
enable_lib(name);
}
else {
disable_lib(name);
}
}
function onAllSSHCheckboxClicked(cb) {
for (lib of libraries) {
lib.useSSHCheckbox.checked = cb.checked;
}
}
function onSelectAllCheckboxClicked(cb) {
for (lib of libraries) {
lib.selectedCheckbox.checked = cb.checked;
}
}
function init() {
load_libraries(
function (libs) {
libraries = libs;
var container = document.getElementById("libraries_div");
var table = document.createElement("table");
var header = document.createElement("tr");
var header_selectCheckbox = document.createElement("th");
var selectAllCheckbox = document.createElement("input");
selectAllCheckbox.id = "selectAllCheckbox";
selectAllCheckbox.type = "checkbox";
selectAllCheckbox.onclick = function () { onSelectAllCheckboxClicked(this); };
header_selectCheckbox.appendChild(selectAllCheckbox);
header.appendChild(header_selectCheckbox); // checkbox
var header_name = document.createElement("th");
header_name.appendChild(document.createTextNode("Name"));
header.appendChild(header_name);
var header_description = document.createElement("th");
header_description.appendChild(document.createTextNode("Description"));
header.appendChild(header_description);
var header_ssh = document.createElement("th");
header_ssh.setAttribute("title", "The default method for cloning submodules is https. Check to use your ssh key instead.")
header_ssh.appendChild(document.createTextNode("ssh"));
var selectAllSSHCheckbox = document.createElement("input");
selectAllSSHCheckbox.type = "checkbox";
selectAllSSHCheckbox.id = "selectAllSSHCheckbox";
selectAllSSHCheckbox.onclick = function () { onAllSSHCheckboxClicked(this); };
header_ssh.appendChild(selectAllSSHCheckbox);
header.appendChild(header_ssh);
table.appendChild(header);
function make_checkbox_toggle(cb) {
return function () {
cb.checked = !cb.checked;
onSelectedCheckboxClicked(cb);
};
}
for (lib of libraries) {
var row = document.createElement("tr");
// select checkbox
var checkbox_container = document.createElement("td");
var selectedCheckbox = document.createElement("input");
var cbId = lib.name + "Checkbox";
selectedCheckbox.id = cbId;
selectedCheckbox.type = "checkbox";
selectedCheckbox.textContent = lib.name;
selectedCheckbox.value = lib.name;
selectedCheckbox.onclick = function () { onSelectedCheckboxClicked(this); };
lib.selectedCheckbox = selectedCheckbox;
checkbox_container.appendChild(selectedCheckbox);
row.appendChild(checkbox_container);
// name
var name_container = document.createElement("td");
name_container.onclick = make_checkbox_toggle(selectedCheckbox);
var label = document.createElement("label");
label.setAttribute("for", cbId);
var project_page = document.createElement("a");
project_page.appendChild(document.createTextNode(lib.name))
project_page.title = lib.projectPage;
project_page.href = lib.projectPage;
project_page.setAttribute("target", "_blank");
label.appendChild(project_page);
name_container.appendChild(label);
row.appendChild(name_container);
// description
var description_container = document.createElement("td");
description_container.onclick = make_checkbox_toggle(selectedCheckbox);
var description = document.createTextNode(lib.description);
description_container.appendChild(description);
row.appendChild(description_container);
// ssh checkbox
var useSSHContainer = document.createElement("td");
useSSHContainer.setAttribute("title", "The default method for cloning submodules is https. Check to use your ssh key instead.")
var useSSHCheckbox = document.createElement("input");
useSSHCheckbox.id = lib.name + "useSSHCheckbox";
useSSHCheckbox.type = "checkbox";
lib.useSSHCheckbox = useSSHCheckbox;
useSSHContainer.appendChild(useSSHCheckbox);
row.appendChild(useSSHContainer);
table.appendChild(row);
}
container.appendChild(table);
}
);
}
function setupCheckboxes() {
container = document.getElementById("libraries_div");
for (lib of libraries) {
var checkbox = document.createElement("input");
var cbId = lib.name + "Checkbox";
checkbox.id = cbId;
checkbox.type = "checkbox";
checkbox.textContent = lib.name;
checkbox.value = lib.name;
checkbox.onclick = function () { onCheckboxClicked(this); };
var label = document.createElement("label");
label.setAttribute("for", cbId);
label.innerText = lib.name;
container.appendChild(checkbox);
container.appendChild(label);
container.appendChild(document.createElement("br"));
lib.checkbox = checkbox;
}
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function onGenerateCommandClicked() {
document.getElementById("generated_command").value = generate_command();
}
function depends_on(lib, dependee) {
return lib.dependencies.includes(dependee.name);
}
function has_addon(lib, addon) {
return lib.addons.includes(addon.name);
}
function getEnabledLibraries() {
var libs = new Array();
for (lib of libraries) {
if (lib.selectedCheckbox && lib.selectedCheckbox.checked) {
libs.push(lib);
}
}
return libs;
}
function checkValidGitUrl(url) {
// extremely simple test for now:
return (url.startsWith("git://") || url.startsWith("git@") || url.startsWith("https://")) && url.endsWith(".git");
}
function checkValidName(name) {
if (!name) {
window.alert("Project name is empty!");
return false;
}
if (name.split(" ").length != 1) {
window.alert("Project name must be a single word!");
return false;
}
return true;
}
function onGenerateButtonClick() {
if (checkValidName(document.getElementById("project_name_text_field").value)) {
var script = generate_script();
var output = document.getElementById("output_textarea");
output.value = script;
output.select();
output.setSelectionRange(0, 99999);
document.execCommand("copy");
}
}
function decodeHtml(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
function contains(libraries, name) {
for (const lib of libraries) {
if (lib.name == name)
return true;
}
return false;
}
function nameFromGitUrl(url) {
if (!checkValidGitUrl(url)) {
window.alert("Not a valid git url!");
return null;
}
var name = "";
var n = url.lastIndexOf("/");
if (n >= 0) {
var name = url.substring(n + 1, url.length - 4); // remove .git
}
return name;
}
function onRepositoryChanged() {
var textfield = document.getElementById("git_repository_text_field");
var url = textfield.value;
console.log(url);
if (url) {
var projectName = nameFromGitUrl(url);
console.log(projectName);
if (projectName) {
var projectNameText = document.getElementById("project_name_text_field");
projectNameText.value = projectName;
}
}
}
function generate_script() {
const projectName = document.getElementById("project_name_text_field").value.replace(/\s/g, '');
const currentUrl = document.location.href.replace("index.html", "");
let script = "curl " + currentUrl + "generate.py | python3 - -n " + projectName + " -o " + currentUrl;
const projectUrl = document.getElementById("git_repository_text_field").value;
if (projectUrl) {
script += " -u " + projectUrl;
}
const gccClangFlags = document.getElementById("flags_linux").value;
if (gccClangFlags) {
script += " -l \"" + gccClangFlags + "\"";
}
const msvcFlags = document.getElementById("flags_msvc").value;
if (msvcFlags) {
script += " -m \"" + msvcFlags + "\"";
}
const enabledLibs = getEnabledLibraries();
for (const lib of enabledLibs) {
if (lib.useSSHCheckbox && lib.useSSHCheckbox.checked) {
script += ' -libssh "' + lib.name + '"';
}
else {
script += ' -lib "' + lib.name + '"';
}
}
return script;
}
window.onload = function () {
this.init();
}