-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
129 lines (97 loc) 路 4.05 KB
/
Copy pathbuild.py
File metadata and controls
129 lines (97 loc) 路 4.05 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
import shutil
import os
import sys
OUTPUT_DIR = 'build/'
def get_files_except_manifest(path):
for file in os.listdir(path):
isFile = os.path.isfile(os.path.join(path, file))
isNotManifest = 'manifest.json' not in file
if isFile and isNotManifest:
yield file
def get_js_files(path):
return filter(lambda file: file.endswith('.js'),
get_files_except_manifest(path))
# Dir name should end with /
def copyover_from_extension(dir, subdir=''):
for filename in get_files_except_manifest(dir + subdir):
src = '%s%s%s' % (dir, subdir, filename)
dest = '%s%s%s' % (OUTPUT_DIR, subdir, filename)
print("[build.py] Copying %s to %s" % (src, dest))
shutil.copyfile(src, dest)
def valOfDEV(dev):
return DEV.split("DEV=")[1].lower()
def minify_js(dir, subdir, DEV):
for filename in get_js_files(dir + subdir):
src = '%s%s%s' % (dir, subdir, filename)
dest = '%s%s%s.min' % (OUTPUT_DIR, subdir, filename)
debug_opts = "--debug --formatting=PRETTY_PRINT" if (valOfDEV(DEV) == "true") else ""
cmd = ("java -jar node_modules/google-closure-compiler/compiler.jar "
"--compilation_level ADVANCED_OPTIMIZATIONS "
"--externs chrome_externs.js "
"%s "
"--define 'NOT_COMPILED=false' "
"--define '%s' "
"%s > %s" % (debug_opts, DEV, src, dest))
print("[build.py] %s" % (cmd))
os.system(cmd)
print("[build.py] Removing %s%s%s" % (OUTPUT_DIR, subdir, filename))
os.remove('%s%s%s' % (OUTPUT_DIR, subdir, filename))
# Start -- sketch command line args handling
bump = False
if (len(sys.argv) > 1) and "bump" in sys.argv:
bump = True
DEV = reduce(lambda acc, cur:
cur if "DEV=" in cur else acc, sys.argv, "DEV=false")
trueOrFalse = ("false" == valOfDEV(DEV)) or ("true" == valOfDEV(DEV))
if (not valOfDEV) or (not trueOrFalse):
print("Usage of DEV param: DEV=false or DEV=true")
sys.exit(1)
print("[build.py] Removing %s dir if it exists..." % (OUTPUT_DIR))
# Remove build folder
if os.path.exists(OUTPUT_DIR):
shutil.rmtree(OUTPUT_DIR)
# Create build folder
print("[build.py] Creating %s dir..." % (OUTPUT_DIR))
os.makedirs(OUTPUT_DIR)
print("[build.py] Creating nested assets/ src/ options/ directories...")
# Create nested assets/ src/ /options folders
for subdir in ['assets', 'src/', 'options/']:
os.makedirs("%s%s" % (OUTPUT_DIR, subdir))
print("[build.py] Copying over nested files...")
for subdir in ['', 'assets/', 'src/', 'options/']:
copyover_from_extension('extension/', subdir)
print("[build.py] Minifying .js files...")
for subdir in ['src/', 'options/']:
minify_js('extension/', subdir, DEV)
### Replace .js -> .js.min for manifest.json
print("[build.py] Editing manifest.json and copying over...")
# Read in the file
with open('%smanifest.json' % ('extension/'), 'r') as manifest:
manifest_data = manifest.read()
# Replace the target string
manifest_data = manifest_data.replace('.js', '.js.min')
# Write the file out again
with open('%smanifest.json' % (OUTPUT_DIR), 'w') as new_manifest:
new_manifest.write(manifest_data)
print("[build.py] Editing html files and copying over...")
# Replace .js -> .js.min for printpage.html
# Read in the file
with open('%sprintpage.html' % ('extension/'), 'r') as html:
html_data = html.read()
# Replace the target string
html_data = html_data.replace('.js', '.js.min')
# Write the file out again
with open('%sprintpage.html' % (OUTPUT_DIR), 'w') as new_html:
new_html.write(html_data)
# Replace .js -> .js.min for options/options.html
# Read in the file
with open('%soptions.html' % ('extension/options/'), 'r') as html:
html_data = html.read()
# Replace the target string
html_data = html_data.replace('.js', '.js.min')
# Write the file out again
with open('%s%soptions.html' % (OUTPUT_DIR, 'options/'), 'w') as new_html:
new_html.write(html_data)
print("[build.py] Packaging %s..." % (OUTPUT_DIR))
cmd = "./pack.sh bump" if bump else "./pack.sh"
os.system(cmd)