-
Notifications
You must be signed in to change notification settings - Fork 4
/
publish.py
142 lines (114 loc) · 4.12 KB
/
publish.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
import os
import sys
import subprocess
import re
def usage():
banner("publish [-dry] [-update-commit] -to <path> [<project>|all]",
"Build and publish win-x64 self-contained deployment files",
"-dry Show commands but don't run them",
"-to <path> Copy win-x64 files to the specified path",
"<project> The project to publish: import, CMS, or barcode",
"all Publish all projects",
"-update-commit Just update gitcommit.txt in the target directory")
def have_arg(name):
return name in sys.argv
def get_arg(name, default_value=None):
if have_arg(name):
ix = sys.argv.index(name)
if ix < (len(sys.argv) - 1):
return sys.argv[ix+1]
return default_value
def banner(*argv):
b1 = '#' * 72
b2 = '# '
print("\n")
print(b1)
print(b2)
for arg in argv:
print(b2 + arg)
print(b2)
print(b1)
print("\n")
def run_and_read(cmd, verbose=True):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
response = proc.stdout.read()
return "".join(chr(x) for x in response)
def run(cmd, verbose=True, dryrun=False):
print("Run: " + cmd)
os.system(cmd)
def get_git_commit():
regex = re.compile("^commit\s+([a-z0-9]+).*Date:\\s+(.+\\d+:\\d+:\\d+)")
response = run_and_read('git log -n 1').replace('\r', ' ').replace('\n', ' ')
print(response)
m = regex.match(response)
if m:
id = m.group(1)[0:7]
dt = m.group(2)
return(id + ': ' + dt)
else:
return "n/a"
def get_subdirs(parent):
subdirs = [f for f in os.listdir(parent) if os.path.isdir(os.path.join(parent, f))]
print(subdirs)
return subdirs
def find_file(rootdir, filename):
filepath = os.path.join(rootdir, filename)
if os.path.isfile(filepath):
return filepath
subdirs = get_subdirs(rootdir)
for subdir in subdirs:
result = find_file(os.path.join(rootdir, subdir), filename)
if result:
return result
return None
def publish(project, dryrun = False):
print(" ")
banner('Publishing {0} for win-x64'.format(project))
run('dotnet build {0}'.format(project), verbose=True, dryrun=dryrun)
run('dotnet publish {0} -c Debug -r win-x64 --self-contained'.format(project), verbose=True, dryrun=dryrun)
def copyfiles(source, destination, dryrun=False):
run('robocopy "{0}" "{1}" /E >> robocopy.log'.format(source, destination), dryrun=dryrun, verbose=True)
def deploy(project, destination, dryrun = False):
subdir = os.path.join(destination,project)
print(f"Copying files to {subdir}")
if not os.path.isdir(subdir):
os.mkdir(subdir)
copyfiles("./{0}/bin/Debug/netcoreapp2.2/win-x64/publish".format(project), subdir, dryrun)
def update_commit_id(deploy_dir):
path = os.path.join(deploy_dir, 'wwwroot', 'assets')
if not os.path.isdir(path):
print(f"{path} does not exist")
return
path = os.path.join(path, 'gitcommit.txt')
commit_id = get_git_commit()
banner(f'Updating {path}', f'with "{commit_id}"')
with open(path, 'w') as fp:
fp.writelines([commit_id])
fp.write('\n')
# print("Args", args.m_args)
if have_arg('-help'):
usage()
quit()
dryrun = have_arg('-dry')
deploy_dir = get_arg('-to', None)
if deploy_dir and not os.path.isdir(deploy_dir):
print('The deployment directory, "{0}", does not exist.'.format(deploy_dir))
quit()
if have_arg('-update-commit'):
update_commit_id(deploy_dir)
exit(0)
if deploy_dir:
if os.path.isfile("robocopy.log"):
os.unlink("robocopy.log")
if have_arg('import') or have_arg('all'):
publish('import', dryrun)
if deploy_dir:
deploy('import', deploy_dir, dryrun=dryrun)
if have_arg('cms') or have_arg('all'):
publish('CMS', dryrun)
if deploy_dir:
deploy('CMS', deploy_dir, dryrun=dryrun)
if have_arg('barcode') or have_arg('all'):
publish('BarcodeReader', dryrun)
if deploy_dir:
deploy('BarcodeReader', deploy_dir, dryrun=dryrun)