This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.py
90 lines (84 loc) · 2.82 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
import os, re
jsLibs=["jquery.1.11.1.min.js","bootstrap.min.js","underscore-min.js","backbone-min.js","nprogress.js"]
cssLibs=["nprogress.css","bootstrap.min.css","style.css"]
jsLibFolder="./client/js/"
appFolder="./client/app/"
appSubFolders=["models","collections","views","routes"]
cssLibFolder="./client/css/"
templateFolder="./client/templates/"
ignoreAppFile=["sw.$"]
sourceFile="./client/index-tmpl.html"
targetFile="./client/index.html"
def concatJsLibs ():
prefix="/*This script is automatically generated by builder. Do not change it manually. */ \n"
return concatFileList(jsLibFolder,jsLibs,prefix)
def concatCssLibs ():
prefix ="/*This CSS file is generated. Do not change it manually.*/\n"
return concatFileList(cssLibFolder,cssLibs,prefix)
def concatFileList(folder,fileArr,prefix):
content=prefix
for fn in fileArr:
path=folder+fn
with open(path,'r') as f:
content+=f.read()
content+="\n"
return content
def concatAppFiles ():
allAppFiles=[]
allAppFiles+=scanFolder(appFolder)
for fold in appSubFolders:
folder=appFolder+fold+"/"
allAppFiles+=scanFolder(folder)
prefix= "/*This script file is generated automatically. Do not change the content. */ \n"
return concatFileList("",allAppFiles,prefix)
def scanFolder(folder):
files=os.listdir(folder)
l=[]
for f in files:
if isFileIgnored(f):
continue
path=folder+f
if os.path.isfile(path):
l.append(path)
return l
def isFileIgnored(fileName):
ignore=False
for pattern in ignoreAppFile:
m=re.search(pattern,fileName)
if m:
ignore=True
break
return ignore
def loadTemplates():
tmpls=os.listdir(templateFolder)
tmplScripts=[];
for tmpl in tmpls:
if isFileIgnored(tmpl):
continue
tmplName=tmpl.split(".html")[0]
path=templateFolder+tmpl
content=open(path,'r').read()
tmplScripts.append('<script type="text/template" id="'+tmplName+'">'+content+"</script>");
return "\n".join(tmplScripts)
def streamConvert():
outf=open(targetFile,"w")
inf=open(sourceFile,"r")
data=inf.readline()
while data:
if data =="{csslib}\n":
print "Argument CSS libraries"
outf.write(concatCssLibs()+"\n")
elif data =="{jslib}\n":
print "Argument JS libraries"
outf.write(concatJsLibs()+"\n")
elif data =="{appfiles}\n":
print "Argument App Scripts"
outf.write(concatAppFiles()+"\n")
elif data =="{templates}\n":
print "Argument HTML Templates"
outf.write(loadTemplates()+"\n")
else:
outf.write(data)
data=inf.readline()
print "All Done. Target File: "+targetFile
streamConvert();