-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
1144 lines (1013 loc) · 31.9 KB
/
index.js
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable max-lines */
// MIT Licensed (see LICENSE.md).
const execa = require("execa");
const path = require("path");
const mkdirp = require("mkdirp");
const fs = require("fs");
const glob = require("glob");
const os = require("os");
const rimraf = require("rimraf");
const commandExists = require("command-exists").sync;
const yargs = require("yargs");
const findUp = require("find-up");
const puppeteer = require("puppeteer");
const express = require("express");
let hostos = "";
let executableExtension = "";
const initialize = () => {
switch (os.platform()) {
case "win32":
hostos = "Windows";
executableExtension = ".exe";
break;
case "darwin":
hostos = "Mac";
break;
default:
hostos = "Linux";
break;
}
};
initialize();
const repoRootFile = ".welder";
const dirs = (() => {
const repo = path.dirname(findUp.sync(repoRootFile));
const libraries = path.join(repo, "Code", "Libraries");
const resources = path.join(repo, "Resources");
const build = path.join(repo, "Build");
const prebuiltContent = path.join(build, "PrebuiltContent");
const includedBuilds = path.join(build, "IncludedBuilds");
const packages = path.join(build, "Packages");
const page = path.join(build, "Page");
const downloads = path.join(build, "Downloads");
return {
build,
downloads,
includedBuilds,
libraries,
packages,
page,
prebuiltContent,
repo,
resources
};
})();
const executables = [
{
copyToIncludedBuilds: true,
directory: "Projects",
name: "WelderEditor",
nonResourceDependencies: [
"Data",
"LauncherTemplates",
repoRootFile
],
prebuild: true,
resourceLibraries: [
"FragmentCore",
"Loading",
"Core",
"UiWidget",
"EditorUi",
"Editor",
"Fallback"
],
vfsOnlyPackage: ["LauncherTemplates"]
},
{
// Since the launcher includes the editor build, it must come afterwards.
copyToIncludedBuilds: false,
directory: "Projects",
name: "WelderLauncher",
nonResourceDependencies: [
"Data",
path.join("Build", "IncludedBuilds"),
repoRootFile
],
prebuild: true,
resourceLibraries: [
"FragmentCore",
"Loading",
"Core",
"Launcher"
],
vfsOnlyPackage: []
}
];
const printSizes = (dir) => {
let list = [];
try {
list = fs.readdirSync(dir);
} catch (err) {
return 0;
}
let size = 0;
list.forEach((fileName) => {
const file = path.join(dir, fileName);
let stat = null;
try {
stat = fs.statSync(file);
} catch (err) {
return;
}
if (stat.isDirectory() && !stat.isSymbolicLink() && !fs.lstatSync(file).isSymbolicLink()) {
size += printSizes(file);
} else {
size += stat.size;
}
});
if (size > 1024 * 1024) {
const number = `${size}`;
const leading = "0".repeat(16 - number.length) + number;
console.log(leading, dir);
}
return size;
};
const tryUnlinkSync = (fullPath) => {
try {
fs.unlinkSync(fullPath);
return true;
} catch (err) {
return false;
}
};
const printIndentedLine = (line, symbol) => {
const indent = " ".repeat(4);
console.log(indent + symbol + line);
};
const printErrorLine = (line) => {
printIndentedLine(line, "- ");
process.exitCode = 1;
};
const printLogLine = (line) => {
printIndentedLine(line, "+ ");
};
const parseLines = (str, lineCallback) => {
let text = str;
if (text.stack) {
text = `${text.stack}`;
} else if (typeof text === "object") {
text = JSON.stringify(text, null, 2);
} else {
text = `${text}`;
}
const matches = text.match(/[^\r\n]+/gu);
if (matches) {
for (const line of matches) {
lineCallback(line);
}
} else if (text) {
lineCallback(text);
}
};
const ensureCommandExists = (command) => {
if (!commandExists(command)) {
printErrorLine(`Command '${command}' does not exist`);
return false;
}
return true;
};
const exec = async (executable, args, options) => {
const result = execa(executable, args, options);
const readData = (optionsFunc, name) => {
const strName = `${name}Str`;
result[strName] = "";
if (result[name]) {
result[name].on("data", (data) => {
const str = data.toString();
if (optionsFunc) {
parseLines(str, optionsFunc);
}
result[strName] += str;
});
}
};
readData(options.out, "stdout");
readData(options.err, "stderr");
const final = await result;
return {
failed: final.failed,
stderr: result.stderrStr,
stdout: result.stdoutStr
};
};
const clearCreateDirectory = (directory) => {
rimraf.sync(directory);
mkdirp.sync(directory);
};
// If this fails, it returns an empty string, otherwise it returns trimmed stdout.
const execSimple = async (...args) => {
const result = await exec(...args);
if (result.failed) {
return "";
}
return result.stdout.trim();
};
/*
* Add files to an existing zip. If the file paths are absolute, only the file name will be added to the root,
* otherwise if the files are relative the entire relative path will be added.
*/
const zipAdd = async (cwd, outputZip, files) => {
if (files.length === 0) {
return;
}
const options = {
cwd,
err: printErrorLine,
out: printLogLine,
reject: false,
stdio: [
"ignore",
"pipe",
"pipe"
]
};
await exec("7z", [
"a",
"-tzip",
"-mx=9",
"-mfb=128",
"-mpass=10",
outputZip,
...files
], options);
};
const zipExtract = async (zipFile, outDir) => {
const options = {
err: printErrorLine,
out: printLogLine,
reject: false,
stdio: [
"ignore",
"pipe",
"pipe"
]
};
await exec("7z", [
"x",
zipFile,
`-o${outDir}`,
"-y"
], options);
};
const gatherSourceFiles = (directory, extensions) => {
console.log("Gathering Source Files");
const files = glob.sync(`**/*.@(${extensions})`, {
absolute: true,
cwd: directory
});
const filteredFiles = files.filter((filePath) => {
const code = fs.readFileSync(filePath, "utf8");
return !code.startsWith("// External.");
});
return filteredFiles;
};
const runEslint = async (options) => {
console.log("Running Eslint");
const eslintOptions = {
cwd: dirs.repo,
err: options.validate ? printErrorLine : printLogLine,
out: options.validate ? printErrorLine : printLogLine,
reject: false,
stdio: [
"ignore",
"pipe",
"pipe"
]
};
const eslintPath = path.normalize(path.join(require.resolve("eslint"), "..", "..", "bin", "eslint.js"));
const args = [
eslintPath,
"."
];
if (!options.validate) {
args.push("--fix");
}
await exec("node", args, eslintOptions);
};
const runClangTidy = async (options, sourceFiles) => {
console.log("Running Clang Tidy");
if (!ensureCommandExists("clang-tidy")) {
return;
}
// Run clang-tidy.
const clangTidyOptions = {
cwd: dirs.libraries,
reject: false,
// We only ignore stderr because it prints 'unable to find compile_commands.json'.
stdio: [
"ignore",
"pipe",
"inherit"
]
};
for (const filePath of sourceFiles) {
const oldCode = fs.readFileSync(filePath, "utf8");
// We always tell it to fix the file, and we compare it afterward to see if it changed.
const args = [
"-extra-arg=-Weverything",
"-fix",
"-header-filter=.*",
filePath
];
// Clang-tidy emits all the errors to the stdout (redirect to stderr).
const result = await exec("clang-tidy", args, clangTidyOptions);
if (!options.validate) {
continue;
}
const newCode = fs.readFileSync(filePath, "utf8");
if (oldCode !== newCode) {
printErrorLine(`File '${filePath}' was not clang-tidy'd`);
parseLines(result.stdout, printErrorLine);
// Rewrite the original code back.
fs.writeFileSync(filePath, oldCode, "utf8");
}
}
};
const runClangFormat = async (options, sourceFiles) => {
console.log("Running Clang Format");
if (!ensureCommandExists("clang-format")) {
return;
}
const clangFormatOptions = {
cwd: dirs.libraries,
reject: false,
stdio: [
"ignore",
"pipe",
"ignore"
],
stripEof: false,
stripFinalNewline: false
};
await Promise.all(sourceFiles.map(async (filePath) => {
const result = await exec("clang-format", [filePath], clangFormatOptions);
const oldCode = fs.readFileSync(filePath, "utf8");
const newCode = result.stdout;
if (oldCode !== newCode) {
if (options.validate) {
printErrorLine(`File '${filePath}' was not clang-formatted`);
} else {
fs.writeFileSync(filePath, newCode, "utf8");
}
}
}));
};
const runWelderFormat = async (options, sourceFiles) => {
console.log("Running Welder Format");
await Promise.all(sourceFiles.map(async (filePath) => {
const oldCode = fs.readFileSync(filePath, "utf8");
// Split our code into lines (detect Windows newline too so we can remove it).
const lines = oldCode.split(/\r?\n/u);
const commentRegex = /^[ \t]*[/*-=\\]+.*/u;
// Remove any comments from the first lines.
while (lines.length !== 0) {
const [line] = lines;
if (commentRegex.test(line)) {
lines.shift();
} else {
break;
}
}
/*
* Remove any comments that are long bar comments.
* Technically this could remove the beginning of a multi-line comment, but our
* style says it's invalid to have one that has a ton of stars in it anyways.
*/
const barCommentRegex = /^[ \t]*[/*-=\\]{40}.*/u;
// These comments may have text after them, but we delete that too intentionally.
for (let lineIndex = 0; lineIndex < lines.length;) {
const line = lines[lineIndex];
if (barCommentRegex.test(line)) {
lines.splice(lineIndex, 1);
} else {
++lineIndex;
}
}
// Add back in the standard file header (would have been removed above) with a newline after it.
lines.unshift("// MIT Licensed (see LICENSE.md).");
// Join all lines together with a standard UNIX newline.
const newCodeWithoutEnding = lines.join("\n");
const newCode = newCodeWithoutEnding.endsWith("\n") ? newCodeWithoutEnding : `${newCodeWithoutEnding}\n`;
if (oldCode !== newCode) {
if (options.validate) {
printErrorLine(`File '${filePath}' must be welder-formatted`);
} else {
fs.writeFileSync(filePath, newCode, "utf8");
}
}
}));
};
const determineCmakeCombo = (options) => {
const aliases = {
Empty: {
builder: "Ninja",
config: "Release",
platform: "Stub",
targetos: hostos,
toolchain: "Clang",
vfs: true
},
Emscripten: {
architecture: "wasm",
builder: "Ninja",
config: "Release",
platform: "Emscripten",
targetos: "Emscripten",
toolchain: "Emscripten",
vfs: true
},
Linux: {
builder: "Ninja",
config: "Release",
platform: "Linux",
targetos: "Linux",
toolchain: "Clang",
vfs: false
},
Windows: {
builder: "Visual Studio 17 2022",
config: "Release",
platform: "Windows",
targetos: "Windows",
toolchain: "MSVC",
vfs: false
}
};
const alias = options.alias ? options.alias : hostos;
let combo = aliases[alias];
if (!combo) {
printErrorLine(`Undefined alias ${alias}, choosing platform empty`);
combo = aliases.empty;
}
/*
* Allow options to override builder, toolchian, etc.
* It is the user's responsibility to ensure this is a valid combination.
*/
combo = Object.assign(combo, options);
combo.alias = alias;
combo.architecture = combo.architecture || os.arch();
combo.config = combo.config || "Release";
combo.vfs = combo.vfs || false;
return combo;
};
const activateBuildDir = (combo) => {
const comboStr =
`${hostos}_${combo.targetos}_${combo.builder}_${combo.toolchain}_${combo.platform}_${combo.architecture}_${combo.config}`.
replace(/ /gu, "-");
const comboDir = path.join(dirs.build, comboStr);
mkdirp.sync(comboDir);
/*
* This will always be set to the last build directory the user created (when calling cmake/build).
* This is used for finding compile_commands.json, cmake artefacts, etc.
*/
const activeLink = path.join(dirs.build, "Active");
tryUnlinkSync(activeLink);
fs.symlinkSync(`./${comboStr}`, activeLink, "junction");
printLogLine(`Activated ${comboStr}`);
return comboDir;
};
const readCmakeVariables = (buildDir) => {
const cmakeCachePath = path.join(buildDir, "CMakeCache.txt");
const contents = fs.readFileSync(cmakeCachePath, "utf8");
const regex = /(?<name>[a-zA-Z0-9_-]+):UNINITIALIZED=(?<value>.*)/gu;
const result = {};
for (;;) {
const array = regex.exec(contents);
if (!array) {
break;
}
result[array.groups.name] = array.groups.value;
}
return result;
};
const getVersionedPrebuiltContentDir = (cmakeVariables) => {
// This must match the revisionChangesetName in ContentLogic.cpp:
//const revisionChangesetName = `Version-${cmakeVariables.WELDER_REVISION}-${cmakeVariables.WELDER_CHANGESET}`;
const revisionChangesetName = `Version-${cmakeVariables.WELDER_REVISION}`;
return path.join(dirs.prebuiltContent, revisionChangesetName);
};
const makeExecutableZip = async (cmakeVariablesOptional, executable, fileSystemZip) => {
console.log(`Building zip for ${executable.name}`);
tryUnlinkSync(fileSystemZip);
const files = [...executable.nonResourceDependencies];
for (const resourceLibrary of executable.resourceLibraries) {
const resourceLibraryPath = path.join(dirs.resources, resourceLibrary);
if (fs.existsSync(resourceLibraryPath)) {
files.push(resourceLibraryPath);
} else {
printLogLine(`Skipping resource library for ${resourceLibrary}`);
}
if (cmakeVariablesOptional) {
const prebuiltPath = path.join(getVersionedPrebuiltContentDir(cmakeVariablesOptional), resourceLibrary);
if (fs.existsSync(prebuiltPath)) {
files.push(prebuiltPath);
} else {
printLogLine(`Skipping prebuilt content for ${resourceLibrary}`);
}
}
}
const relativeFiles = files.map((file) => path.relative(dirs.repo, path.normalize(file)));
await zipAdd(dirs.repo, fileSystemZip, relativeFiles);
};
const generateBinaryCArray = (id, buffer) => `unsigned char ${id}Data[] = {${buffer.join(",")}};\nunsigned int ${id}Size = ${buffer.length};\n`;
const buildvfs = async (cmakeVariablesOptional, buildDir, combo) => {
for (const executable of executables) {
console.log(`Building virtual file system for ${executable.name}`);
const libraryDir = path.join(buildDir, "Code", "Libraries", executable.directory, executable.name);
mkdirp.sync(libraryDir);
const makeFsBuffer = async () => {
if (combo.vfs) {
const fileSystemZip = path.join(libraryDir, "FileSystem.zip");
await makeExecutableZip(cmakeVariablesOptional, executable, fileSystemZip);
return fs.readFileSync(fileSystemZip);
}
return Buffer.alloc(1);
};
const vfsCppContents = generateBinaryCArray("VirtualFileSystem", await makeFsBuffer());
const vfsCppFile = path.join(libraryDir, "VirtualFileSystem.cpp");
if (!fs.existsSync(vfsCppFile) || fs.readFileSync(vfsCppFile, "utf8") !== vfsCppContents) {
fs.writeFileSync(vfsCppFile, vfsCppContents, "utf8");
}
}
};
const cmake = async (options) => {
console.log("Running Cmake", options);
if (!ensureCommandExists("cmake") || !ensureCommandExists("git")) {
return null;
}
const gitOptions = {
cwd: dirs.repo,
err: printErrorLine,
reject: false,
stdio: [
"ignore",
"pipe",
"pipe"
]
};
const branch = await execSimple("git", [
"rev-parse",
"--abbrev-ref",
"HEAD"
], gitOptions);
const revision = await execSimple("git", [
"rev-list",
"--count",
"HEAD"
], gitOptions);
const shortChangeset = await execSimple("git", [
"log",
"-1",
"--pretty=%h",
"--abbrev=12"
], gitOptions);
const changeset = await execSimple("git", [
"log",
"-1",
"--pretty=%H"
], gitOptions);
const changesetDate = `"${await execSimple("git", [
"log",
"-1",
"--pretty=%cd",
"--date=format:%Y-%m-%d"
], gitOptions)}"`;
const tag = await execSimple("git", [
"describe",
"--tags"
], gitOptions);
const versionResult = (/v(?<major>[0-9]+)\.(?<minor>[0-9]+)\.(?<patch>[0-9]+)/u).exec(tag);
const version = versionResult ? {
major: parseInt(versionResult.groups.major, 10),
minor: parseInt(versionResult.groups.minor, 10),
patch: parseInt(versionResult.groups.patch, 10)
} : {major: 0, minor: 0, patch: 0};
const builderArgs = [];
const toolchainArgs = [];
const architectureArgs = [];
const configArgs = [];
const combo = determineCmakeCombo(options);
if (combo.builder === "Ninja") {
builderArgs.push("-DCMAKE_MAKE_PROGRAM=ninja");
}
if (combo.toolchain === "Emscripten") {
if (!process.env.EMSCRIPTEN) {
printErrorLine("Cannot find EMSCRIPTEN environment variable");
}
const toolchainFile = path.join(process.env.EMSCRIPTEN, "cmake/Modules/Platform/Emscripten.cmake");
toolchainArgs.push(`-DCMAKE_TOOLCHAIN_FILE=${toolchainFile}`);
toolchainArgs.push("-DEMSCRIPTEN_GENERATE_BITCODE_STATIC_LIBRARIES=0");
}
if (combo.toolchain === "Clang") {
if (hostos === "Windows") {
// CMake on Windows tries to do a bunch of detection thinking that it will be using MSVC.
toolchainArgs.push("-DCMAKE_SYSTEM_NAME=Generic");
}
toolchainArgs.push("-DCMAKE_C_COMPILER:PATH=clang");
toolchainArgs.push("-DCMAKE_CXX_COMPILER:PATH=clang++");
toolchainArgs.push("-DCMAKE_C_COMPILER_ID=Clang");
toolchainArgs.push("-DCMAKE_CXX_COMPILER_ID=Clang");
toolchainArgs.push("-DCMAKE_LINKER=lld");
toolchainArgs.push("-DCMAKE_AR=/usr/bin/llvm-ar");
}
if (combo.toolchain === "MSVC" && combo.architecture === "x64") {
architectureArgs.push("-DCMAKE_GENERATOR_PLATFORM=x64");
architectureArgs.push("-T");
architectureArgs.push("host=x64");
}
if (combo.toolchain !== "MSVC") {
configArgs.push(`-DCMAKE_BUILD_TYPE=${combo.config}`);
configArgs.push("-DCMAKE_EXPORT_COMPILE_COMMANDS=1");
}
const cmakeArgs = [
`-DWELDER_MS_SINCE_EPOCH=${Date.now()}`,
`-DWELDER_BRANCH=${branch}`,
`-DWELDER_REVISION=${revision}`,
`-DWELDER_SHORT_CHANGESET=${shortChangeset}`,
`-DWELDER_CHANGESET=${changeset}`,
`-DWELDER_CHANGESET_DATE=${changesetDate}`,
`-DWELDER_MAJOR_VERSION=${version.major}`,
`-DWELDER_MINOR_VERSION=${version.minor}`,
`-DWELDER_PATCH_VERSION=${version.patch}`,
`-DWELDER_CONFIG=${combo.config}`,
"-G",
combo.builder,
...builderArgs,
`-DWELDER_TOOLCHAIN=${combo.toolchain}`,
...toolchainArgs,
`-DWELDER_PLATFORM=${combo.platform}`,
`-DWELDER_ARCHITECTURE=${combo.architecture}`,
...architectureArgs,
...configArgs,
`-DWELDER_HOSTOS=${hostos}`,
`-DWELDER_TARGETOS=${combo.targetos}`,
dirs.repo
];
parseLines(cmakeArgs, printLogLine);
parseLines(combo, printLogLine);
const buildDir = activateBuildDir(combo);
clearCreateDirectory(buildDir);
await buildvfs(null, buildDir, combo);
const cmakeOptions = {
cwd: buildDir,
err: printErrorLine,
out: printLogLine,
reject: false,
stdio: [
"ignore",
"pipe",
"pipe"
]
};
await exec("cmake", cmakeArgs, cmakeOptions);
return buildDir;
};
const preventNoOutputTimeout = () => {
const start = Date.now();
const interval = setInterval(() => {
printLogLine(`Working... (${Math.floor((Date.now() - start) / 1000)} seconds)`);
}, 1000 * 10);
return () => clearInterval(interval);
};
const findExecutableDir = (buildDir, config, directory, library) => [
path.join(buildDir, "Code", "Libraries", directory, library, config),
path.join(buildDir, "Code", "Libraries", directory, library)
].filter((filePath) => fs.existsSync(filePath))[0];
const findExecutable = (buildDir, config, directory, library) =>
{
const executableDir = findExecutableDir(buildDir, config, directory, library);
return path.join(executableDir, `${library}${executableExtension}`);
}
const format = async (options) => {
console.log("Formatting");
await runEslint(options);
const sourceFiles = gatherSourceFiles(dirs.libraries, "c|cc|cxx|cpp|h|hxx|hpp|inl");
if (options.tidy) {
await runClangTidy(options, sourceFiles);
}
await runClangFormat(options, sourceFiles);
const scriptFiles = gatherSourceFiles(dirs.resources, "zilchscript|z|zilchfrag|zilchFrag");
const allFiles = sourceFiles.concat(scriptFiles);
await runWelderFormat(options, allFiles);
console.log("Formatted");
};
const build = async (options) => {
console.log("Building");
if (!ensureCommandExists("cmake")) {
return;
}
const combo = determineCmakeCombo(options);
const buildDir = activateBuildDir(combo);
const cmakeVariables = readCmakeVariables(buildDir);
await buildvfs(cmakeVariables, buildDir, combo);
const opts = {
cwd: buildDir,
err: printErrorLine,
out: (line) => {
if (line.search(/\b(?:FAILED|failed|ERROR| error )\b/u) === -1) {
printLogLine(line);
} else {
printErrorLine(line);
}
},
reject: false,
stdio: [
"ignore",
"pipe",
"pipe"
]
};
const makeArgArray = (optsName) => options[optsName] ? [
`--${optsName}`,
options[optsName]
] : [];
const target = makeArgArray("target");
const parallel = makeArgArray("parallel");
const endPnot = preventNoOutputTimeout();
await exec("cmake", [
"--build",
".",
"--config",
combo.config,
...target,
...parallel
], opts);
endPnot();
console.log("Built");
};
const executeBuiltProcess = async (buildDir, combo, directory, library, args) => {
if (combo.toolchain === "Emscripten") {
const pageDirectory = path.join(buildDir, "Code", "Libraries", directory, library);
if (!fs.existsSync(pageDirectory)) {
printErrorLine(`Directory does not exist ${pageDirectory}`);
return [];
}
const app = express();
app.use("/", express.static(pageDirectory));
const port = 3000;
const server = app.listen(port);
const argString = args.map((arg) => JSON.stringify(arg)).join(" ");
const url = `http://localhost:${port}/${library}.html?${argString}`;
const browser = await puppeteer.launch({
args: [
"--no-sandbox",
"--disable-setuid-sandbox"
],
headless: false,
timeout: 0
});
const page = await browser.newPage();
const downloadDir = path.join(dirs.downloads, Math.random().toString(36).
substr(2, 8));
clearCreateDirectory(downloadDir);
// eslint-disable-next-line no-underscore-dangle
await page._client.send("Page.setDownloadBehavior", {
behavior: "allow",
downloadPath: downloadDir
});
let pageResolver = null;
const finishedPromise = new Promise((resolve) => {
pageResolver = resolve;
});
page.on("console", (event) => {
if (event.text() === "Stopping main loop") {
pageResolver();
}
printLogLine(event.text());
});
page.on("error", (event) => parseLines(event.stack, printErrorLine));
page.on("pageerror", (event) => parseLines(event.stack, printErrorLine));
await page.goto(url);
await finishedPromise;
for (;;) {
if (!fs.readdirSync(downloadDir).find((fileName) => fileName.endsWith(".crdownload"))) {
break;
}
await new Promise((resolve) => setTimeout(resolve, 100));
}
server.close();
await browser.close();
const downloadPaths = fs.readdirSync(downloadDir).map((fileName) => path.join(downloadDir, fileName));
return downloadPaths;
}
const executablePath = findExecutable(buildDir, combo.config, directory, library);
if (!fs.existsSync(executablePath)) {
printErrorLine(`Executable does not exist ${executablePath}`);
return [];
}
const opts = {
cwd: buildDir,
err: printLogLine,
out: printLogLine,
reject: false,
stdio: [
"ignore",
"pipe",
"pipe"
]
};
await exec(executablePath, args, opts);
return [];
};
const prebuilt = async (options) => {
console.log("Copying Prebuilt Content");
const endPnot = preventNoOutputTimeout();
const combo = determineCmakeCombo(options);
rimraf.sync(dirs.prebuiltContent);
const buildDir = activateBuildDir(combo);
for (const executable of executables) {
if (!executable.prebuild) {
continue;
}
const downloadPaths = await executeBuiltProcess(buildDir, combo, executable.directory, executable.name, [
"-CopyPrebuiltContent",
"-Exit"
]);
for (const downloadPath of downloadPaths) {
console.log("Extracting download", downloadPath);
await zipExtract(downloadPath, dirs.prebuiltContent);
}
}
rimraf.sync(dirs.downloads);
if (!fs.existsSync(dirs.prebuiltContent) || fs.readdirSync(dirs.prebuiltContent).length === 0) {
printLogLine("Prebuilt content directory did not exist or was empty");
}
console.log("Copied Prebuilt Content");
endPnot();
};
const pack = async (options) => {
console.log("Packing");
const combo = determineCmakeCombo(options);
const buildDir = activateBuildDir(combo);
const filter = [
".pdb",
".ilk",
".exp",
".lib",
".wast",
".cmake",
"CMakeFiles",
"FileSystem.zip",
"VirtualFileSystem.cpp"
];
if (combo.toolchain === "Emscripten") {
clearCreateDirectory(dirs.page);
// This prevents GitHub from processing our files with Jekyll.
fs.writeFileSync(path.join(dirs.page, ".nojekyll"), "", "utf8");
}
mkdirp.sync(dirs.packages);
const cmakeVariables = readCmakeVariables(buildDir);
rimraf.sync(dirs.includedBuilds);
for (const executable of executables) {
const library = executable.name;
console.log(`Packaging library ${library}`);
const executableDir = findExecutableDir(buildDir, combo.config, executable.directory, library);
if (!fs.existsSync(executableDir)) {
printErrorLine(`Library directory does not exist ${executableDir}`);
continue;
}
const files = fs.readdirSync(executableDir).filter((file) => !filter.includes(path.extname(file)) && !filter.includes(file)).
map((file) => path.join(executableDir, file));
///*
// * This needs to match index.js:pack/Standalone.cpp:BuildId::Parse/BuildId::GetFullId/BuildVersion.cpp:GetBuildVersionName
// * Application.Branch.Major.Minor.Patch.Revision.ShortChangeset.MsSinceEpoch.TargetOs.Architecture.Config.Extension
// * Example: WelderEditor.master.1.5.0.1501.fb02756c46a4.1574702096290.Windows.x86.Release.zip
// */
//const name =
// `${library}.` +
// `${cmakeVariables.WELDER_BRANCH}.` +
// `${cmakeVariables.WELDER_MAJOR_VERSION}.` +
// `${cmakeVariables.WELDER_MINOR_VERSION}.` +
// `${cmakeVariables.WELDER_PATCH_VERSION}.` +
// `${cmakeVariables.WELDER_REVISION}.` +
// `${cmakeVariables.WELDER_SHORT_CHANGESET}.` +