forked from uwrit/leaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·161 lines (126 loc) · 4.83 KB
/
build.sh
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
#!/usr/local/bin/python3
# Produces a production build of Leaf
# v2:
# /bin/{runtime} for publish artifacts and tar
import os
import shutil
from subprocess import Popen, PIPE
from argparse import ArgumentParser, Namespace
from collections import namedtuple
from typing import *
class FSEnv:
def __init__(self):
self.proj_dir = os.path.dirname(os.path.realpath(__file__))
@property
def bin_dir(self):
return os.path.join(self.proj_dir, 'bin')
@property
def api_proj(self):
return os.path.join(self.proj_dir, 'src', 'server', 'API', 'API.csproj')
class Runtime:
def __init__(self, name, output_folder, args = []):
self.name = name
self.output_folder = output_folder
self.tar_file = 'leaf_{}.tar.gz'.format(self.name)
self.args = args
@staticmethod
def rhel7(env: FSEnv):
return Runtime('rhel7', os.path.join(env.bin_dir, 'rhel7'), [
'-r', 'rhel.7-x64', '--self-contained', 'false', '/p:MicrosoftNETPlatformLibrary=Microsoft.NETCore.App'])
@staticmethod
def win(env: FSEnv):
return Runtime('win', os.path.join(env.bin_dir, 'win'))
class LeafBuilder:
def __init__(self, args: Namespace):
self.env: FSEnv = FSEnv()
self.runtimes: Iterable[Runtime] = LeafBuilder.get_runtimes(
self.env, args)
@staticmethod
def get_runtimes(env: FSEnv, args: Namespace) -> Iterable[Runtime]:
rt = []
if args.rhel7:
rt.append(Runtime.rhel7(env))
if args.win:
rt.append(Runtime.win(env))
return rt
def build_single(self, runtime: Runtime):
ensure_clean_runtime(runtime)
publish(self.env, runtime)
tar(self.env, runtime)
clean_runtime(runtime, remove_tar=False)
def build(self):
if not self.runtimes:
print('No runtimes selected.')
return
print('Building Leaf for production...')
ensure_bin(self.env)
for runtime in self.runtimes:
try:
self.build_single(runtime)
except Exception as e:
print('{}'.format(e))
def report_error(err, m):
msg = str(err, 'utf-8').strip() + '\n'
if m:
msg += ' ' + m
return msg
def clean_runtime(runtime: Runtime, remove_tar: bool = True):
directory = runtime.output_folder
for f in os.listdir(directory):
path = os.path.join(directory, f)
if os.path.isfile(path):
if not remove_tar and f.endswith(runtime.tar_file):
continue
os.unlink(path)
elif os.path.isdir(path):
shutil.rmtree(path)
def ensure_bin(env: FSEnv):
print('Ensuring bin directory is present...')
if not os.path.isdir(env.bin_dir):
os.mkdir(env.bin_dir)
def ensure_clean_runtime(runtime: Runtime):
print('Ensuring {} runtime directory is clean...'.format(runtime.output_folder))
if not os.path.isdir(runtime.output_folder):
os.mkdir(runtime.output_folder)
return
clean_runtime(runtime)
def get_publish_args(env: FSEnv, runtime: Runtime) -> Iterable[str]:
args = ['dotnet', 'publish', '-c', 'Release',
'-o', runtime.output_folder]
for a in runtime.args:
args.append(a)
args.append(env.api_proj)
return args
def publish(env: FSEnv, runtime: Runtime):
print('Building API for {}...'.format(runtime.name))
args = get_publish_args(env, runtime)
p = Popen(args)
if p.wait() != 0:
raise Exception(report_error(p.stdout.read(),
'Error building Leaf API for {}...'.format(runtime.name)))
print('Built API for {}...'.format(runtime.name))
def tar(env: FSEnv, runtime: Runtime):
print('Compressing Leaf API artifacts for {}...'.format(runtime.name))
os.chdir(runtime.output_folder)
p = Popen(['tar', '--exclude', runtime.tar_file,
'-czf', runtime.tar_file, '.'])
if p.wait() != 0:
raise Exception(report_error(p.stdout.read(),
'Error compressing Leaf for {}...'.format(runtime.name)))
os.chdir(env.proj_dir)
print('Compressed Leaf to {}...'.format(
os.path.join(runtime.output_folder, runtime.tar_file)))
def get_args() -> Namespace:
parser = ArgumentParser(
prog='build.sh', description="This script builds Leaf's backend for various runtime targets.")
parser.add_argument('--rhel7', action='store_true',
help='Targets RHEL7 and Cent7 (not self-contained), outputs to ./bin/rhel7/leaf_rhel7.tar.gz')
parser.add_argument('--win', action='store_true',
help='Targets Windows (not self-contained), outputs to ./bin/win/leaf_win.tar.gz')
return parser.parse_args()
def main():
args = get_args()
builder = LeafBuilder(args)
builder.build()
print('Done...')
main()