forked from CDrummond/lms-material
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkrel.py
executable file
·288 lines (238 loc) · 8.22 KB
/
mkrel.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
#!/usr/bin/env python
#
# LMS-Material
#
# Copyright (c) 2018-2019 Craig Drummond <craig.p.drummond@gmail.com>
# MIT license.
#
import hashlib
import os
import re
import requests
import shutil
import subprocess
import sys
PUBLIC_XML = "public.xml"
BUILD_FOLDER = "build"
HTML_FOLDER = BUILD_FOLDER + "/MaterialSkin/HTML/material/html"
INSTALL_XML = BUILD_FOLDER + "/MaterialSkin/install.xml"
MINIFY = True
JS_COMPILER = "tools/closure-compiler/closure-compiler-v20181008.jar"
def info(s):
print("INFO: %s" %s)
def error(s):
print("ERROR: %s" % s)
exit(-1)
def usage():
print("Usage: %s <major>.<minor>.<patch>" % sys.argv[0])
exit(-1)
def checkVersion(version):
try:
parts=version.split('.')
major=int(parts[0])
minor=int(parts[1])
patch=int(parts[2])
except:
error("Invalid version number")
def releaseUrl(version):
return "/~https://github.com/CDrummond/lms-material/releases/download/%s/lms-material-%s.zip" % (version, version)
def checkVersionExists(version):
url = releaseUrl(version)
info("Checking %s" % url)
request = requests.head(url)
if request.status_code == 200 or request.status_code == 302:
error("Version already exists")
def updateLine(line, startStr, endStr, updateStr):
start=line.find(startStr)
if start!=-1:
start+=len(startStr)
end=line.find(endStr, start)
if end!=-1:
return "%s%s%s" % (line[:start], updateStr, line[end:])
return None
def updateInstallXml(version):
lines=[]
updated=False
info("Updating %s" % INSTALL_XML)
with open(INSTALL_XML, "r") as f:
lines=f.readlines()
for i in xrange(len(lines)):
updated = updateLine(lines[i], "<version>", "</version>", version)
if updated:
lines[i]=updated
updated=True
break
if not updated:
error("Failed to update version in %s" % INSTALL_XML)
with open(INSTALL_XML, "w") as f:
for line in lines:
f.write(line)
def prepare():
info("Preparing code")
if os.path.exists(BUILD_FOLDER):
shutil.rmtree(BUILD_FOLDER)
os.mkdir(BUILD_FOLDER)
try:
shutil.copytree("MaterialSkin", "%s/MaterialSkin" % BUILD_FOLDER)
except Exception as e:
error("Failed to copy files to %s folder - %s" % (BUILD_FOLDER, str(e)))
# Remove unminified versions of JS files, if we have the minified version
for js in os.listdir("%s/lib" % HTML_FOLDER):
if js.endswith(".min.js"):
orig = js.replace(".min.js", ".js")
if os.path.exists("%s/lib/%s" % (HTML_FOLDER, orig)):
os.remove("%s/lib/%s" % (HTML_FOLDER, orig))
orig = js.replace(".min.js", ".orig.js")
if os.path.exists("%s/lib/%s" % (HTML_FOLDER, orig)):
os.remove("%s/lib/%s" % (HTML_FOLDER, orig))
def cleanup():
info("Removing build folder")
if os.path.exists(BUILD_FOLDER):
shutil.rmtree(BUILD_FOLDER)
def fixUtils():
text=""
with open("%s/js/utils.js" % HTML_FOLDER, "r") as f:
for line in f.readlines():
text+=line.replace(".css?", ".min.css?")
with open("%s/js/utils.js" % HTML_FOLDER, "w") as f:
f.write(text)
def minifyJs():
info("...JS")
for js in sorted(os.listdir("%s/js" % HTML_FOLDER)):
if js.endswith(".js"):
info("......%s" % js)
jsCommand = ["java", "-jar", JS_COMPILER, "--js_output_file=%s/js/%s" % (HTML_FOLDER, js.replace(".js", ".min.js")), "%s/js/%s" % (HTML_FOLDER, js)]
subprocess.call(jsCommand, shell=False)
def minifyCss():
info("...CSS")
for css in sorted(os.listdir("%s/css" % HTML_FOLDER)):
if css.endswith(".css"):
info("......%s" % css)
origCss = "%s/css/%s" % ( HTML_FOLDER, css)
cssStr=""
with open(origCss, "r") as f:
for line in f.readlines():
cssStr+=line.replace("\n", "").replace(" {", "{")
while (" " in cssStr):
cssStr=cssStr.replace(" ", " ")
cssStr=cssStr.replace("} ", "}").replace("{ ", "{").replace("; ", ";").replace(": ", ":").replace(" :", ":").replace(" !", "!");
while True:
start = cssStr.find("/*")
if start<0:
break
end = cssStr.find("*/")
if end<start:
break
cssStr=cssStr[:start]+cssStr[end+2:]
minCss = origCss.replace(".css", ".min.css")
with open(minCss, "w") as f:
f.write(cssStr)
def removeUnminified():
info("...removing non-minified files")
if MINIFY:
for entry in os.listdir("%s/js" % HTML_FOLDER):
if entry.endswith(".js") and not entry.endswith(".min.js"):
os.remove("%s/js/%s" % (HTML_FOLDER, entry))
for entry in os.listdir("%s/css" % HTML_FOLDER):
if entry.endswith(".css") and not entry.endswith(".min.css"):
os.remove("%s/css/%s" % (HTML_FOLDER, entry))
def fixHtml():
info("...updating HTML files")
for entry in os.listdir("%s/../" % HTML_FOLDER):
if entry.endswith(".html"):
fixedLines = []
path = "%s/../%s" % (HTML_FOLDER, entry)
with open(path, "r") as f:
lines=f.readlines()
for line in lines:
matchedJs = False
matchedCss = False
matches = re.findall('src\\s*\\=\\"html/js/[^\\"]+\\.js', line)
if matches and 1==len(matches):
matchedJs = True
else:
matches = re.findall('href\\s*\\=\\"html/css/[^\\"]+\\.css', line)
if matches and 1==len(matches):
matchedCss = True
if matchedJs:
fixedLines.append(line.replace(".js", ".min.js"))
elif matchedCss:
fixedLines.append(line.replace(".css", ".min.css"))
else:
fixedLines.append(line)
with open(path, "w") as f:
for line in fixedLines:
f.write(line)
def minify():
info("Minifying")
fixUtils()
minifyJs()
minifyCss()
removeUnminified()
fixHtml()
def createZip(version):
info("Creating ZIP")
zipFile="lms-material-%s" % version
os.chdir('build')
shutil.make_archive(zipFile, 'zip', 'MaterialSkin')
zipFile+=".zip"
os.rename(zipFile, "../%s" % zipFile)
os.chdir('..')
return zipFile
def getSha1Sum(zipFile):
info("Generating SHA1")
sha1 = hashlib.sha1()
with open(zipFile, 'rb') as f:
while True:
data = f.read(65535)
if not data:
break
sha1.update(data)
return sha1.hexdigest()
def updatePublicXml(version, zipFile, sha1):
lines=[]
updatedVersion=False
updatedUrl=False
updatedSha=False
info("Updating %s" % PUBLIC_XML)
with open(PUBLIC_XML, "r") as f:
lines=f.readlines()
for i in xrange(len(lines)):
updated = updateLine(lines[i], 'version="', '"', version)
if updated:
lines[i]=updated
updatedVersion=True
updated = updateLine(lines[i], '<url>', '</url>', releaseUrl(version))
if updated:
lines[i]=updated
updatedUrl=True
updated = updateLine(lines[i], '<sha>', '</sha>', sha1)
if updated:
lines[i]=updated
updatedSha=True
if updatedVersion and updatedUrl and updatedSha:
break
if not updatedVersion:
error("Failed to update version in %s" % PUBLIC_XML)
if not updatedUrl:
error("Failed to url version in %s" % PUBLIC_XML)
if not updatedSha:
error("Failed to sha version in %s" % PUBLIC_XML)
with open(PUBLIC_XML, "w") as f:
for line in lines:
f.write(line)
if 1==len(sys.argv):
usage()
version=sys.argv[1]
if version!="test":
checkVersion(version)
checkVersionExists(version)
prepare()
updateInstallXml(version)
if MINIFY:
minify()
zipFile = createZip(version)
sha1 = getSha1Sum(zipFile)
if version!="test":
updatePublicXml(version, zipFile, sha1)
cleanup()