-
Notifications
You must be signed in to change notification settings - Fork 1
/
dist.py
executable file
·65 lines (55 loc) · 1.75 KB
/
dist.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
#!/usr/bin/env python3
import os
import shutil
import shlex
import subprocess
import tempfile
import zipfile
import requests
closure_compiler_url = "https://dl.google.com/closure-compiler/compiler-latest.zip"
def setup():
r = requests.get(closure_compiler_url, allow_redirects=True)
with tempfile.NamedTemporaryFile(suffix=".zip", mode="wb") as tf:
tf.write(r.content)
ref = zipfile.ZipFile(tf.name, "r")
tmpdir = tempfile.mkdtemp()
ref.extractall(tmpdir)
compiler_filename = None
for name in os.listdir(path=tmpdir):
if "closure-compiler" in name:
compiler_filename = os.path.join(tmpdir, name)
dist = "dist"
try:
os.mkdir(dist)
except Exception:
pass
for name in os.listdir(path="src"):
source = os.path.join("src", name)
if os.path.isdir(source) and name != "node_modules":
shutil.copytree(source, os.path.join(dist, name))
elif os.path.isfile(source):
filename, ext = os.path.splitext(name)
if ext == ".js":
cmd = "java -jar {jarfile} --js {name} \
--js_output_file {dest}".format(
jarfile=compiler_filename,
name=source,
dest=os.path.join(dist, name)
)
p = subprocess.Popen(shlex.split(cmd))
out, err = p.communicate()
if err:
print(err)
else:
shutil.copy(source, dist)
os.chdir(dist)
p = subprocess.Popen(["yarn", "install"])
out, err = p.communicate()
if err:
print(err)
else:
print(out)
shutil.rmtree(tmpdir)
print('Done!')
if __name__ == "__main__":
setup()