forked from ukyo/jsziptools
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·177 lines (145 loc) · 4.49 KB
/
build.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
#!/usr/bin/env python
import os
import argparse
import json
REPOSITORY_NAME = "jsziptools"
REPOSITORY_URL = "http://github.com/ukyo/jsziptools"
CLOSURE_COMPILER_PATH = os.environ['CLOSURE_COMPILER_PATH']
OUTPUT_PATH = 'build/jsziptools.min.js'
parser = argparse.ArgumentParser()
parser.add_argument('-C',
metavar='COMPILER_PATH',
dest='compiler',
help='Set a Closure Compiler path.',
default=CLOSURE_COMPILER_PATH)
parser.add_argument('-m',
metavar='MODULES',
dest='module',
nargs='*',
help='Set module names you want to use.',
default=['zlib', 'gz', 'zip'])
parser.add_argument('-o',
metavar='OUTPUT_PATH',
dest='output',
help='Set a output file path.',
default=OUTPUT_PATH)
parser.add_argument('-c',
metavar='CONF_FILE_PATH',
dest='conf',
help='Set a configuration file path.')
ALL_FILES = (
"lib/DataViewUtils/build/dataview.utils.min.js",
"src/jsziptools.js",
"src/utils.js",
"src/algorithms/adler32.js",
"src/algorithms/crc32.js",
"src/algorithms/deflate.js",
"src/algorithms/inflate.js",
"src/zlib.compress.js",
"src/zlib.decompress.js",
"src/gz.compress.js",
"src/gz.decompress.js",
"src/zip.pack.js",
"src/zip.unpack.js",
)
selected_modules = {
"lib/DataViewUtils/build/dataview.utils.min.js": True,
"src/jsziptools.js": True,
"src/utils.js": True,
"src/algorithms/adler32.js": False,
"src/algorithms/crc32.js": False,
"src/algorithms/deflate.js": False,
"src/algorithms/inflate.js": False,
"src/zlib.compress.js": False,
"src/zlib.decompress.js": False,
"src/gz.compress.js": False,
"src/gz.decompress.js": False,
"src/zip.pack.js": False,
"src/zip.unpack.js": False
}
require = {}
require['gz'] = [
"src/algorithms/crc32.js",
"src/algorithms/deflate.js",
"src/algorithms/inflate.js",
"src/gz.compress.js",
"src/gz.decompress.js"
]
require['gz.compress'] = [
"src/algorithms/crc32.js",
"src/algorithms/deflate.js",
"src/gz.compress.js",
]
require['gz.decompress'] = [
"src/algorithms/crc32.js",
"src/algorithms/inflate.js",
"src/gz.decompress.js"
]
require['zlib'] = [
"src/algorithms/adler32.js",
"src/algorithms/deflate.js",
"src/algorithms/inflate.js",
"src/zlib.compress.js",
"src/zlib.decompress.js"
]
require['zlib.compress'] = [
"src/algorithms/adler32.js",
"src/algorithms/deflate.js",
"src/zlib.compress.js"
]
require['zlib.decompress'] = [
"src/algorithms/adler32.js",
"src/algorithms/inflate.js",
"src/zlib.decompress.js"
]
require['zip'] = [
"src/algorithms/crc32.js",
"src/algorithms/deflate.js",
"src/algorithms/inflate.js",
"src/zip.pack.js",
"src/zip.unpack.js"
]
require['zip.pack'] = [
"src/algorithms/crc32.js",
"src/algorithms/deflate.js",
"src/zip.pack.js"
]
require['zip.unpack'] = [
"src/algorithms/crc32.js",
"src/algorithms/inflate.js",
"src/zip.unpack.js"
]
def select_modules(modules):
for module in modules:
for filename in require[module]:
selected_modules[filename] = True
selected = []
for filename in ALL_FILES:
if selected_modules[filename]:
selected.append(filename)
return selected
def main():
tmp_file = "tmp.js"
option = parser.parse_args()
if option.conf:
conf = json.load(open(option.conf, 'r'))
compiler = conf['compiler'] if 'compiler' in conf else CLOSURE_COMPILER_PATH
output = conf['output'] if 'output' in conf else OUTPUT_PATH
files = conf['files']
else:
compiler = option.compiler
output = option.output
option.module = [option.module] if type(option.module) == str else option.module
files = select_modules(option.module)
command = "java -jar %s --js %s --js_output_file %s" % (compiler, ' '.join(os.path.abspath(file) for file in files), tmp_file)
os.system(command)
directory = '/'.join(os.path.abspath(output).split('/')[:-1])
if not os.path.isdir(directory):
os.makedirs(directory)
f = open(output, "w")
f.write("// %s %s\n" % (REPOSITORY_NAME, REPOSITORY_URL))
f.write(open(tmp_file, "r").read())
f.close()
os.remove(tmp_file)
if __name__ == "__main__":
main()