-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgobuild.py
More file actions
executable file
·159 lines (135 loc) · 4.53 KB
/
gobuild.py
File metadata and controls
executable file
·159 lines (135 loc) · 4.53 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
#!/usr/bin/python3
import os
import sys
import argparse
import subprocess
import base64
template = '''// +build !debug
package {package}
import "encoding/base64"
// {variable} is a byte representation for {fn}
var {variable}, _ = base64.StdEncoding.DecodeString("{base64str}")
'''
goosarchs = [
# ('darwin', '386'), // not supported
('darwin', 'amd64'),
# # ('darwin', 'arm'), // not compiling
# # ('darwin', 'arm64'), // not compiling
# ('dragonfly', 'amd64'),
('freebsd', '386'),
('freebsd', 'amd64'),
('freebsd', 'arm'),
('linux', '386'),
('linux', 'amd64'),
('linux', 'arm'),
('linux', 'arm64'),
# ('linux', 'ppc64'),
# ('linux', 'ppc64le'),
# ('linux', 'mips'),
# ('linux', 'mipsle'),
# ('linux', 'mips64'),
# ('linux', 'mips64le'),
# ('netbsd', '386'),
# ('netbsd', 'amd64'),
# ('netbsd', 'arm'),
# ('openbsd', '386'),
# ('openbsd', 'amd64'),
# ('openbsd', 'arm'),
# ('plan9', '386'),
# ('plan9', 'amd64'),
# # ('solaris', 'amd64'), // not compiling
('windows', '386'),
('windows', 'amd64'),
]
GOFILE = 'things-gui.go'
TARGET = 'things-gui'
def get_version(path):
version = None
with open(os.path.join(path, GOFILE), 'r') as f:
for line in f:
if line.startswith('const AppVersion ='):
version = line.split('"')[1]
if version is None:
raise Exception('Cannot find version in {}'.format(GOFILE))
return version
binfiles = [
('''./react/static/js/main-bundle-{}.min.js'''.format(
get_version(os.path.dirname(__file__))), "FileMainBundleMinJS"),
('''./react/static/js/vendors-bundle-{}.min.js'''.format(
get_version(os.path.dirname(__file__))), "FileVendorsBundleMinJS"),
("./react/static/js/editor.worker.js", "FileEditorWorkerJS"),
("./react/static/fonts/monaco-font.ttf", "FileMonacoFontTTF"),
("./react/static/img/thingsdb.gif", "FileThingsdbGIF"),
("./react/static/img/thingsgui-logo.png", "FileThingsguiLogo"),
("./react/static/img/thingsdb-logo.png", "FileThingsdbLogo"),
("./react/static/img/CesbitLogo.png", "FileCesbitLogo"),
("./react/static/img/view-edit.png", "FileViewEditLogo"),
("./react/static/favicon.ico", "FileFaviconICO"),
("./react/templates/app.html", "FileAppHTML"),
]
def build_all():
path = os.path.dirname(os.path.abspath(__file__))
version = get_version(path)
outpath = os.path.join(path, 'bin', version)
if not os.path.exists(outpath):
os.makedirs(outpath)
for goos, goarch in goosarchs:
tmp_env = os.environ.copy()
tmp_env["GOOS"] = goos
tmp_env["GOARCH"] = goarch
outfile = os.path.join(outpath, '{}_{}_{}_{}.{}'.format(
TARGET,
version,
goos,
goarch,
'exe' if goos == 'windows' else 'bin'))
with subprocess.Popen(
['go', 'build', '-o', outfile],
env=tmp_env,
cwd=path,
stdout=subprocess.PIPE) as proc:
print('Building {}/{}...'.format(goos, goarch))
def build(output=''):
path = os.path.dirname(os.path.abspath(__file__))
version = get_version(path)
outfile = output if output else os.path.join(path, '{}_{}.{}'.format(
TARGET, version, 'exe' if sys.platform.startswith('win') else 'bin'))
args = ['go', 'build', '-o', outfile]
with subprocess.Popen(
args,
cwd=path,
stdout=subprocess.PIPE) as proc:
print('Building {}...'.format(outfile))
def compile(fn, variable, empty=False):
if empty:
data = b''
else:
with open(fn, 'rb') as f:
data = f.read()
with open('{}.go'.format(variable.lower()), 'w', encoding='utf-8') as f:
f.write(template.format(
package='main',
fn=fn,
variable=variable,
base64str=base64.b64encode(data).decode('utf-8')
))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-o', '--output',
default='',
help='alternative output filename (requires -b/--build)')
parser.add_argument(
'-a', '--build-all',
action='store_true',
help='build binaries for all goos and goarchs')
args = parser.parse_args()
print('Create go files...')
for bf in binfiles:
compile(*bf)
print('Finished creating go files!')
if args.build_all:
build_all()
print('Finished building binaries!')
else:
build(output=args.output)