-
Notifications
You must be signed in to change notification settings - Fork 26
/
build.py
75 lines (69 loc) · 2.59 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
#!/usr/bin/env python
# coding:UTF-8
import os
import sys
import base64
import time
import shutil
import subprocess
import platform
# javac路径 如果 javac 不在 PATH 中, 请自己添加
javapath = r'javac'
# javac编译版本
version = '1.5'
pathsep = os.pathsep
distDir = "./dist/"
classpath = os.getcwd()+"/lib" # apache lib路径
classpath = classpath + "/servlet-api.jar" + pathsep + classpath+"/jsp-api-2.1.jar" # 拼接classpath
if os.path.exists(distDir):
shutil.rmtree(distDir)
shutil.copytree("./template/", distDir)
for root, dirs, files in os.walk('.'):
for f in files:
if f.endswith(".java"):
path = os.path.join(root, f)
print('------------------------------------------------------------')
print(path)
cmd = '"{javapath}" -cp {classpath} -source {version} -target {version} {path} '.format(
javapath=javapath,
classpath=classpath,
path=path,
version=version
)
print(cmd)
p=subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True
)
out, err = p.communicate()
try:
print(err.decode())
except:
print(str(err))
targetclass = path.replace('.java', '.class')
if os.path.exists(targetclass):
with open(targetclass, 'rb') as f:
content = f.read()
res = str(base64.b64encode(content))
if len(path.split(os.path.sep)) == 4:
dispath = os.path.join(
distDir,
path.split(os.path.sep)[2]+".js"
)
with open(dispath) as disp:
result = disp.read().replace('###'+path.split(os.path.sep)[3].split('.')[0]+'###', res)
with open(dispath, mode="w") as disp:
disp.write(result)
elif len(path.split(os.path.sep)) == 5:
dispath = os.path.join(
distDir,
path.split(os.path.sep)[2] + os.path.sep + path.split(os.path.sep)[3]+".js"
)
with open(dispath) as disp:
result = disp.read().replace('###'+path.split(os.path.sep)[4].split('.')[0]+'###', res)
with open(dispath, mode="w") as disp:
disp.write(result)
else:
exit("error")