-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.py
executable file
·795 lines (675 loc) · 24.6 KB
/
build.py
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
#!/usr/bin/env python
# ****************************************************
# * Copyright (C) 2023 - Jordan Irwin (AntumDeluge) *
# ****************************************************
# * This software is licensed under the MIT license. *
# * See: LICENSE.txt for details. *
# ****************************************************
import codecs, errno, math, os, re, shutil, subprocess, sys, time
from urllib.error import HTTPError
from zipfile import ZipFile
modules = {}
def installModule(mod, pkg=None):
if mod in modules:
print("\nWARNING: module '{}' already imported".format(mod))
return
pkg = mod if not pkg else pkg
try:
modules[mod] = __import__(mod)
except ModuleNotFoundError:
print("\ninstalling {} module ...".format(mod))
subprocess.run((sys.executable, "-m", "pip", "install", pkg))
try:
modules[mod] = __import__(mod)
except ModuleNotFoundError:
print("\nWARNING: failed to install '{}' module".format(mod))
# set up environment
dir_root = os.path.dirname(__file__)
os.chdir(dir_root)
dir_root = os.getcwd()
file_conf = os.path.join(dir_root, "build.conf")
class Targets:
names = []
actions = {}
completed = []
def getNames(self):
return self.names
def add(self, name, action):
if not callable(action):
exitWithError("action parameter of Targets.add must be a function")
if name in self.actions:
exitWithError("cannot re-define target: {}".format(name))
self.names.append(name)
self.actions[name] = action
def run(self, name, _dir, verbose=False):
if name not in self.actions:
exitWithError("target not defined: {}".format(name))
elif name in self.completed:
if verbose:
print("\nnot re-running target: {}".format(name))
return
if name == "clean":
# cleaning resets all targets (only first time run)
self.completed = []
self.actions[name](_dir, verbose)
self.completed.append(name)
targets = Targets()
options = {
"web-dist": False
}
templates = {}
templates["html-head"] = "<html>\n\
<head>\n\
{{head}}\n\
</head>\
\n<body>"
templates["html-tail"] = "</body>\n</html>"
templates["favicon-data"] = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAAJ1BMVEVHcEzytYYAAABhPAA2Njbkhnb/2rCYWUE9GwD6+voskrrX19cedpDJqZNvAAAAAXRSTlMAQObYZgAAAFRJREFUCNdjYIADJiUlCEPFxQnCVAIBBbCUCpThqKQkAma4CSmmgBmCQABiMEs0SjQagBjRB2W2ghmRy7KmghmlYonhIAaDeaBoMdgOZmNjAwYsAACaGQ2gK4O7gQAAAABJRU5ErkJggg=="
templates["favicon"] = "<link rel=\"icon\" href=\"{}\">".format(templates["favicon-data"])
templates["button-uplevel"] = "<a class=\"button\" href=\"../\" name=\"top\"><span class=\"button\">Back</span></a>"
templates["button-totop"] = "<a class=\"button\" href=\"#top\"><span class=\"button\">Back to Top</span></a>"
# --- UTILITY FUNCTIONS --- #
def printUsage():
file_exe = os.path.basename(__file__)
print("\nUSAGE:\n {} [-h] [-v|-q] [-w] {}".format(file_exe, "|".join(targets.getNames())))
def printWarning(msg):
print("\nWARNING: " + msg)
def printError(msg):
print("\nERROR: " + msg)
def exitWithError(msg, code=1, usage=False):
printError(msg)
if usage:
printUsage()
sys.exit(code)
def readFile(filepath):
if not os.path.exists(filepath):
exitWithError("cannot open file for reading, does not exist: {}".format(filepath), errno.ENOENT)
if os.path.isdir(filepath):
exitWithError("cannot open file for reading, directory exists: {}".format(filepath), errno.EISDIR)
fopen = codecs.open(filepath, "r", "utf-8")
# clean up line delimeters
content = fopen.read().replace("\r\n", "\n").replace("\r", "\n")
fopen.close()
return content
def writeFile(filepath, data):
if type(data) in (list, tuple):
# convert data to string
data = "\n".join(data)
fopen = codecs.open(filepath, "w", "utf-8")
fopen.write(data)
fopen.close()
def getConfig(key, default=None):
if not os.path.isfile(file_conf):
printError("config not found: {}".format(file_conf))
return None
lines = readFile(file_conf).split("\n")
lidx = 0;
for line in lines:
lidx =+ 0
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" not in line:
printWarning("malformed line in config ({}): {}".format(lidx, line))
continue
tmp = line.split("=", 1)
if key == tmp[0].strip():
return tmp[1].strip()
return default
def checkTargetNotExists(target, action=None, add_parent=False):
err = errno.EEXIST
msg = ""
if action:
msg += "cannot " + action + ", "
if os.path.exists(target):
if os.path.isdir(target):
err = errno.EISDIR
msg += "directory"
else:
msg += "file"
msg += " exists: {}".format(target)
exitWithError(msg, err)
if add_parent:
dir_parent = os.path.dirname(target)
if not os.path.exists(dir_parent):
os.makedirs(dir_parent)
elif not os.path.isdir(dir_parent):
exitWithError("cannot create directory, file exists: {}".format(dir_parent), err)
def checkTargetNotDir(target, action=None):
msg = ""
if action:
msg += "cannot " + action + ", "
if os.path.exists(target) and os.path.isdir(target):
exitWithError(msg + "directory exists: {}".format(target), errno.EISDIR)
def checkFileSourceExists(source, action=None):
msg = ""
if action:
msg += "cannot " + action + " file, "
if not os.path.exists(source):
msg += "source does not exist: {}".format(source)
exitWithError(msg, errno.ENOENT)
if os.path.isdir(source):
msg += "source is a directory: {}".format(source)
exitWithError(msg, errno.EISDIR)
def checkDirSourceExists(source, action=None):
msg = ""
if action:
msg += "cannot " + action + " directory, "
if not os.path.exists(source):
msg += "source does not exist: {}".format(source)
exitWithError(msg, errno.ENOENT)
if not os.path.isdir(source):
msg += "source is a file: {}".format(source)
exitWithError(msg, errno.EEXIST)
def makeDir(dirpath, verbose=False):
checkTargetNotExists(dirpath, "create directory")
os.makedirs(dirpath)
if not os.path.isdir(dirpath):
exitWithError("failed to create directory, an unknown error occured: {}".format(dirpath))
if verbose:
print("new directory '{}'".format(dirpath))
def deleteFile(filepath, verbose=False):
if os.path.exists(filepath):
if os.path.isdir(filepath):
exitWithError("cannot delete file, directory exists: {}".format(filepath), errno.EISDIR)
os.remove(filepath)
if os.path.exists(filepath):
exitWithError("failed to delete file, an unknown error occured: {}".format(filepath))
if verbose:
print("delete '{}'".format(filepath))
def deleteDir(dirpath, verbose=False):
if os.path.exists(dirpath):
if not os.path.isdir(dirpath):
exitWithError("cannot delete directory, file exists: {}".format(dirpath), errno.EEXIST)
for obj in os.listdir(dirpath):
objpath = os.path.join(dirpath, obj)
if not os.path.isdir(objpath):
deleteFile(objpath, verbose)
else:
deleteDir(objpath, verbose)
if len(os.listdir(dirpath)) != 0:
exitWithError("failed to delete directory, not empty: {}".format(dirpath))
os.rmdir(dirpath)
if os.path.exists(dirpath):
exitWithError("failed to delete directory, an unknown error occurred: {}".format(dirpath))
if verbose:
print("delete '{}'".format(dirpath))
def copyFile(source, target, name=None, verbose=False):
if name:
target = os.path.join(target, name)
checkFileSourceExists(source, "copy")
checkTargetNotExists(target, "copy file", True)
shutil.copyfile(source, target)
if not os.path.exists(target):
exitWithError("failed to copy file, an unknown error occurred: {}".format(target))
if verbose:
print("copy '{}' -> '{}'".format(source, target))
def copyExecutable(source, target, name=None, verbose=False):
copyFile(source, target, name, verbose)
os.chmod(target, 0o775)
def copyDir(source, target, name=None, verbose=False):
if name:
target = os.path.join(target, name)
checkDirSourceExists(source, "copy")
checkTargetNotExists(target, "copy directory")
makeDir(target, False)
if not os.path.isdir(target):
exitWithError("failed to copy directory, an unknown error occurred: {}".format(target))
if verbose:
print("copy '{}' -> '{}'".format(source, target))
for obj in os.listdir(source):
objsource = os.path.join(source, obj)
objtarget = os.path.join(target, obj)
if not os.path.isdir(objsource):
copyFile(objsource, objtarget, None, verbose)
else:
copyDir(objsource, objtarget, None, verbose)
def moveFile(source, target, name=None, verbose=False):
if name:
target = os.path.join(target, name)
checkFileSourceExists(source, "move")
checkTargetNotExists(target, "move file", True)
shutil.move(source, target)
if os.path.exists(source) or not os.path.exists(target):
exitWithError("failed to move file, an unknown error occurred: {}".format(target))
if verbose:
print("move '{}' -> '{}'".format(source, target))
def moveDir(source, target, name=None, verbose=False):
if name:
target = os.path.join(target, name)
checkDirSourceExists(source, "move")
checkTargetNotExists(target, "move directory")
makeDir(target, False)
if not os.path.isdir(target):
exitWithError("failed to move directory, an unknown error occurred: {}".format(target))
for obj in os.listdir(source):
objsource = os.path.join(source, obj)
objtarget = os.path.join(target, obj)
if not os.path.isdir(objsource):
moveFile(objsource, objtarget, None, verbose)
else:
moveDir(objsource, objtarget, None, verbose)
deleteDir(source, False)
if verbose:
print("move '{}' -> '{}'".format(source, target))
def downloadFile(url, filename, verbose=False):
if verbose:
print("\ndownloading file from {} ...".format(url))
dir_target = os.path.join(os.getcwd(), "temp")
if not os.path.exists(dir_target):
os.makedirs(dir_target)
if not os.path.isdir(dir_target):
exitWithError("cannot download to temp directory, file exists: {}".format(dir_target), errno.EEXIST)
file_target = os.path.join(dir_target, filename)
if os.path.exists(file_target):
print("{} exists, delete to re-download".format(file_target))
return
try:
modules["wget"].download(url, file_target)
except HTTPError:
exitWithError("could not download file from: {}".format(url))
def packFile(sourcefile, archive, amend=False, verbose=False):
checkFileSourceExists(sourcefile)
new_archive = type(archive) != ZipFile
zopen = archive
if new_archive:
checkTargetNotDir(archive, "create zip")
zopen = ZipFile(archive, "a" if amend else "w")
zopen.write(sourcefile)
# if ZipFile was passed, calling instruction should close the file
if new_archive:
zopen.close()
if verbose:
print("compress '{}' => '{}'".format(sourcefile, archive))
def packDir(sourcedir, archive, incroot=False, amend=False, verbose=False):
checkDirSourceExists(sourcedir)
checkTargetNotDir(archive, "create zip")
dir_start = os.getcwd()
# normalize path to archive
a_basename = os.path.basename(archive)
a_dirname = os.path.dirname(archive)
if a_dirname:
os.chdir(a_dirname)
a_dirname = os.getcwd()
archive = os.path.join(a_dirname, a_basename)
os.chdir(dir_start)
# clean up path name
os.chdir(sourcedir)
dir_abs = os.getcwd()
idx_trim = len(dir_abs) + 1
if incroot:
os.chdir(dir_start)
idx_trim = len(dir_start) + 1
zopen = ZipFile(archive, "a" if amend else "w")
z_count_start = len(zopen.namelist())
for ROOT, DIRS, FILES in os.walk(dir_abs):
for f in FILES:
f = os.path.join(ROOT, f)[idx_trim:]
packFile(f, zopen, True, verbose)
z_count_end = len(zopen.namelist())
zopen.close()
os.chdir(dir_start)
if z_count_end == 0:
printWarning("no files compressed, archive empty: {}".format(archive))
deleteFile(archive, verbose)
elif verbose:
z_count_diff = z_count_end - z_count_start
if z_count_diff == 0:
print("archive unchanged: {}".format(archive))
else:
print("added {} files into archive: {}".format(z_count_diff, archive))
def unpack(filepath, dir_target=None, verbose=False):
if not os.path.isfile(filepath):
exitWithError("cannot extract zip, file not found: {}".format(filepath), errno.ENOENT)
dir_start = os.getcwd()
dir_parent = os.path.dirname(filepath)
if dir_target == None:
dir_target = os.path.join(dir_parent, os.path.basename(filepath).lower().split(".zip")[0])
if os.path.exists(dir_target):
if not os.path.isdir(dir_target):
exitWithError("cannot extract zip, file exists: {}".format(dir_target), errno.EEXIST)
shutil.rmtree(dir_target)
os.makedirs(dir_target)
os.chdir(dir_target)
if verbose:
print("extracting contents of {} ...".format(filepath))
zopen = ZipFile(filepath, "r")
zopen.extractall()
zopen.close()
# return to original directory
os.chdir(dir_start)
def runCommand(cmd, args=[], failOnError=True, winext=None):
if sys.platform == "win32" and winext:
cmd = cmd + "." + winext
args = [cmd] + list(args)
try:
res = subprocess.run(args)
if res.returncode != 0 and failOnError:
exitWithError("called process exited with error: {}".format(" ".join(args)), res.returncode)
return res.returncode
except FileNotFoundError:
exitWithError("the system could not find file to execute: {}".format(cmd), errno.ENOENT)
return 0
# --- TARGET FUNCTIONS --- #
def clean(_dir, verbose=False):
print("\ncleaning build files ...")
dir_build = os.path.join(_dir, "build")
if os.path.exists(dir_build):
if not os.path.isdir(dir_build):
exitWithError("cannot remove build directory, file exists: {}".format(dir_build), errno.EEXIST)
deleteDir(dir_build, verbose)
return
print("no files to remove")
def updateVersion(_dir, verbose=False):
app_ver = getConfig("version")
print("\nchargen version {}".format(app_ver))
file_config_js = os.path.join(_dir, "script", "config.js")
contents = readFile(file_config_js)
changes = re.sub(
r"^config\[\"version\"\] = .*;$",
"config[\"version\"] = \"{}\";".format(app_ver),
contents, 1, re.M)
if changes != contents:
writeFile(file_config_js, changes)
if verbose:
print("updated file '{}'".format(file_config_js))
file_changelog = os.path.join(_dir, "doc", "changelog.txt")
contents = readFile(file_changelog)
changes = re.sub(r"^next$", app_ver, contents, 1, re.M)
if changes != contents:
writeFile(file_changelog, changes)
if verbose:
print("updated file '{}'".format(file_changelog))
file_config_neu = os.path.join(_dir, "neutralino.config.json")
contents = readFile(file_config_neu)
changes = re.sub(
r"\"version\": .*,$",
"\"version\": \"{}\",".format(app_ver),
contents, 1, re.M
)
if changes != contents:
writeFile(file_config_neu, changes)
if verbose:
print("updated file '{}'".format(file_config_neu))
def stageWeb(_dir, verbose=False):
installModule("markdown")
targets.run("update-version", _dir, verbose)
print("\nstaging web files ...")
dir_web = os.path.join(_dir, "build", "web")
deleteDir(dir_web, verbose)
makeDir(dir_web, verbose)
if not os.path.isdir(dir_web):
# FIXME: correct error value
exitWithError("failed to create staging directory: {}".format(dir_web), errno.ENOENT)
files_stage = getConfig("stage_files", "").split(";")
dirs_stage = getConfig("stage_dirs", "").split(";")
for f in files_stage:
file_source = os.path.join(_dir, f)
file_target = os.path.join(dir_web, f)
copyFile(file_source, file_target, None, verbose)
for d in dirs_stage:
dir_source = os.path.join(_dir, d)
dir_target = os.path.join(dir_web, d)
copyDir(dir_source, dir_target, None, verbose)
dir_assets = os.path.join(dir_web, "assets")
if not os.path.isdir(dir_assets):
exitWithError("no assets staged (missing directory: {})".format(dir_assets), errno.ENOENT)
# convert README to HTML
file_readme = os.path.join(dir_assets, "README")
html = modules["markdown"].markdown(readFile(file_readme + ".md"))
html_head = [
" <title>Assets Info</title>",
# ~ "<link rel=\"icon\" href=\"{}\">".format(templates["favicon"]),
templates["favicon"],
"<link rel=\"stylesheet\" href=\"../script/main.css\">",
"<script type=\"module\" src=\"../script/nav.js\"></script>"
]
html_head = re.sub(r"^{{head}}$", "\n ".join(html_head), templates["html-head"], 1, re.M)
html = "\n".join((html_head, templates["button-uplevel"], html, templates["button-totop"],
templates["html-tail"]))
writeFile(file_readme + ".html", html)
print("\ncleaning web files ...")
for ROOT, DIRS, FILES in os.walk(dir_assets):
for f in FILES:
file_staged = os.path.join(ROOT, f)
if ".xcf" in f or f.endswith(".md"):
deleteFile(file_staged, verbose)
file_config_js = os.path.join(dir_web, "script", "config.js")
contents = readFile(file_config_js)
changes = re.sub(
r"^config\[\"asset-info\"\] = .*$",
"config[\"asset-info\"] = \"assets/README.html\"",
contents, 1, re.M
)
writeFile(file_config_js, changes)
if options["web-dist"]:
contents = changes
changes = re.sub(
r"^config\[\"web-dist\"\] = false",
"config[\"web-dist\"] = true",
contents, 1, re.M
)
if changes != contents:
writeFile(file_config_js, changes)
if verbose:
print("configured for web distribution: {}".format(file_config_js))
def distWeb(_dir, verbose=False):
targets.run("stage-web", _dir, verbose)
print("\ncreating web distribution ...")
app_ver = getConfig("version")
dir_build = os.path.join(_dir, "build")
dir_web = os.path.join(dir_build, "web")
dir_dist = os.path.join(dir_build, "dist")
file_dist = os.path.join(dir_dist, "chargen_{}_web.zip".format(app_ver))
if not os.path.exists(dir_dist):
makeDir(dir_dist, verbose)
packDir(dir_web, file_dist, False, False, verbose)
packFile("README.md", file_dist, True, verbose)
def stageDesktop(_dir, verbose=False):
targets.run("stage-web", _dir, verbose)
print("\nstaging desktop files ...")
dir_build = os.path.join(_dir, "build")
dir_web = os.path.join(dir_build, "web")
dir_neu = os.path.join(dir_build, "neutralinojs")
dir_app = os.path.join(dir_build, "desktop")
dir_doc = os.path.join(dir_app, "doc")
dir_res = os.path.join(dir_app, "resources")
if not os.path.isdir(dir_neu):
res = runCommand("npm", ("run", "stage-desktop"), False, "cmd")
if res != 0:
printWarning("call to 'npm' returned error")
else:
print("\nskipped Neutralinojs download, directory exists: {}".format(dir_neu))
deleteDir(dir_app, verbose)
makeDir(dir_app, verbose)
copyDir(dir_web, dir_app, "resources", verbose)
copyFile(
os.path.join(dir_neu, "LICENSE"),
dir_app,
"LICENSE-neutralinojs.txt",
verbose
)
copyFile(
os.path.join(dir_res, "LICENSE.txt"),
os.path.join(dir_app, "LICENSE.txt"),
None, verbose
)
copyDir(
os.path.join(dir_neu, "bin"),
os.path.join(dir_app, "bin"),
None, verbose
)
copyFile(
os.path.join(dir_neu, "resources", "js", "neutralino.js"),
os.path.join(dir_res, "js", "neutralino.js"),
None, verbose
)
copyFile(
os.path.join(_dir, "neutralino.config.json"),
os.path.join(dir_app, "neutralino.config.json"),
None, verbose
)
# add Neutralinojs script to HTML
if verbose:
print("\nincorporating neutralino.js into index.html")
file_index = os.path.join(dir_res, "index.html")
lines_orig = readFile(file_index).split("\n")
lines = list(lines_orig)
for idx in range(len(lines)):
if lines[idx].strip() == "<head>":
lines.insert(idx+1, " <script src=\"js/neutralino.js\"></script>")
break
if lines != lines_orig:
writeFile(file_index, lines)
file_config_js = os.path.join(dir_res, "script", "config.js")
content = readFile(file_config_js)
changes = re.sub(
r"^config\[\"desktop\"\] = false",
"config[\"desktop\"] = true",
content, 1, re.M
)
if changes != content:
writeFile(file_config_js, changes)
if verbose:
print("updated file '{}'".format(file_config_js))
def runDesktop(_dir, verbose=False):
targets.run("stage-desktop", _dir, verbose)
print("\nrunning desktop app ...")
dir_start = os.getcwd()
dir_app = os.path.join(_dir, "build", "desktop")
os.chdir(dir_app)
ret = runCommand("npm", ("exec", "neu", "run"), winext="cmd")
os.chdir(dir_start)
def _packageDist(distname, ext="", verbose=False):
app_ver = getConfig("version")
dir_temp = os.path.join(os.getcwd(), "tmp")
makeDir(dir_temp, verbose)
target_exe = os.path.join(dir_temp, "chargen"+ext)
copyExecutable(
os.path.normpath("chargen/chargen-{}{}".format(distname, ext)),
target_exe, None, verbose
)
copyFile(
os.path.normpath("chargen/resources.neu"),
dir_temp, "resources.neu", verbose
)
# ~ if ext == ".exe":
# ~ copyFile(
# ~ os.path.normpath("chargen/WebView2Loader.dll"),
# ~ dir_temp, "WebView2Loader.dll", verbose
# ~ )
copyDir(
os.path.normpath("../resources/doc"),
dir_temp, "doc", verbose
)
for filename in ("LICENSE.txt", "LICENSE-neutralinojs.txt"):
copyFile(
os.path.join("..", filename),
dir_temp, filename, verbose
)
copyFile(
os.path.join(dir_root, "README.md"),
dir_temp, "README.md", verbose
)
packDir(dir_temp, "chargen_{}_{}.zip".format(app_ver, distname), False, False, verbose)
deleteDir(dir_temp, verbose)
def distDesktop(_dir, verbose=False):
targets.run("stage-desktop", _dir, verbose)
print("\ncreating desktop app distribution ...")
dir_start = os.getcwd()
dir_build = os.path.join(_dir, "build")
dir_app = os.path.join(dir_build, "desktop")
dir_dist_temp = os.path.join(dir_app, "dist")
os.chdir(dir_app)
runCommand("npm", ("exec", "neu", "build", "--release"), winext="cmd")
os.chdir(dir_dist_temp)
if verbose:
print("packaging Linux binaries ...")
for arch in ("arm64", "armhf", "x64"):
_packageDist("linux_" + arch, "", verbose)
if verbose:
print("packaging Mac OS X binaries ...")
for arch in ("arm64", "x64"):
_packageDist("mac_" + arch, "", verbose)
if verbose:
print("packaging Windows binaries ...")
_packageDist("win_x64", ".exe", verbose)
dir_dist = os.path.join(dir_build, "dist")
if not os.path.isdir(dir_dist):
makeDir(dir_dist, verbose)
for obj in os.listdir(dir_dist_temp):
objpath = os.path.join(dir_dist_temp, obj)
if os.path.isfile(objpath):
moveFile(objpath, dir_dist, obj, verbose)
os.chdir(dir_start)
deleteDir(dir_dist_temp, verbose)
def printChanges(_dir, verbose=False):
changelog = getConfig("changelog")
if not changelog:
exitWithError("cannot parse changelog, 'changelog' not configured in build.conf")
changelog = os.path.join(dir_root, os.path.normpath(changelog))
if not os.path.isfile(changelog):
exitWithError("cannot parse changelog, file not found: {}".format(changelog), errno.ENOENT)
lines = []
started = False
for li in readFile(changelog).split("\n"):
if started and not li:
break
if not started and li and not li.startswith("-"):
started = True
elif started and li:
lines.append(li)
print("\n".join(lines))
def init(_dir, verbose=False):
installModule("wget")
installModule("markdown")
targets.add("init", init)
targets.add("clean", clean)
targets.add("update-version", updateVersion)
targets.add("stage-web", stageWeb)
targets.add("dist-web", distWeb)
targets.add("stage-desktop", stageDesktop)
targets.add("run-desktop", runDesktop)
targets.add("dist-desktop", distDesktop)
targets.add("print-changes", printChanges)
def main(_dir, argv):
if "-h" in argv or "--help" in argv:
printUsage()
sys.exit(0)
verbose = "-v" in argv
if verbose:
argv.pop(argv.index("-v"))
silent = "-q" in argv
if silent:
argv.pop(argv.index("-q"))
# silent overrides verbose
verbose = False
options["web-dist"] = "-w" in argv
if options["web-dist"]:
argv.pop(argv.index("-w"))
if len(argv) == 0:
exitWithError("missing command parameter", usage=True)
# check commands before starting
command_list = targets.getNames()
for command in argv:
if command not in command_list:
exitWithError("unknown command: {}".format(command), usage=True)
time_start = time.time()
for command in argv:
targets.run(command, _dir, verbose)
time_end = time.time()
time_diff = time_end - time_start
secs = math.floor(time_diff)
ms = math.floor(round(time_diff - secs, 3) * 1000)
mins = math.floor(secs / 60)
secs = secs if mins == 0 else math.floor(secs % (mins * 60))
duration = "{}s".format(secs)
if ms > 0:
duration += ".{}ms".format(ms)
if mins > 0:
duration = "{}m:".format(mins) + duration
if not silent:
print("\nduration: {}".format(duration));
if __name__ == "__main__":
os.chdir(os.path.dirname(__file__))
main(os.getcwd(), sys.argv[1:])