-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
224 lines (166 loc) · 5.99 KB
/
__main__.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""A lightweight Javascript build service.
BuildJS is the all-in-one Javascript build service. BuildJS uses the
latest version of Google's Closure compiler to compile your javascript
files every time a file in the target directory has been modified.
"""
import os
import sys
import colorama
import re
import zipfile
import urllib.request
import watchdog.events
import watchdog.observers
import time
colorama.init()
ROOT = os.path.dirname(os.path.abspath(__file__))
TARGET = os.path.abspath(os.getcwd())
sys.path.append(ROOT)
import closure
CLOSURE_URL = "https://dl.google.com/closure-compiler/compiler-latest.zip"
CLOSURE_PATTERN = r"closure-compiler-v.+\.jar"
CLOSURE_ZIP = os.path.join(ROOT, "closure.zip")
CLOSURE_JAR = os.path.join(ROOT, "closure.jar")
DEFAULT_CONFIG = os.path.join(ROOT, "default.yaml")
BUILD_CONFIG = os.path.join(TARGET, "closure.yaml")
def download_closure():
"""Download the latest version of the Closure compiler."""
# Download the zip file
source = urllib.request.urlopen(CLOSURE_URL)
with open(CLOSURE_ZIP, "wb") as file:
file.write(source.read())
source.close()
compressed = zipfile.ZipFile(CLOSURE_ZIP)
# Guess which is the right file
for name in compressed.namelist():
if re.match(CLOSURE_PATTERN, name):
target = name
break
else:
print(colorama.Fore.RED + "Could not locate Closure jar." + colorama.Fore.RESET)
return
# Extract and delete the zip file
compressed.extract(name, ".")
compressed.close()
os.remove(CLOSURE_ZIP)
# Rename the compiler
if os.path.isfile(CLOSURE_JAR):
os.remove(CLOSURE_JAR)
os.rename(target, CLOSURE_JAR)
def check_closure():
"""Check if the compiler is downloaded."""
if not os.path.isfile(CLOSURE_JAR):
print(colorama.Fore.GREEN + "Downloading closure..." + colorama.Fore.RESET)
download_closure()
def write_configuration():
"""Create the custom configuration."""
with open(BUILD_CONFIG, "w") as write:
with open(DEFAULT_CONFIG) as read:
write.write(read.read())
def check_configuration():
"""Check if the configuration is created."""
if not os.path.isfile(BUILD_CONFIG):
print("Creating config file...")
print(colorama.Fore.RED + "No configuration!" + colorama.Fore.RESET)
write_configuration()
def load_configuration():
"""Load the command line arguments."""
builds = closure.closure(BUILD_CONFIG)
if len(builds) == 0:
print(colorama.Fore.RED + "No build targets specified." + colorama.Fore.RESET)
return builds
def common_path(paths):
"""Get the common path among a list of paths."""
path = os.path.abspath(os.path.commonpath(paths))
parts = path.split(os.sep)
for i in range(len(parts)):
if "*" in parts[i]:
return os.path.sep.join(parts[:i])
return path
def execute_and_print(build):
"""Run a build and print the results."""
name = os.path.split(build.output_path)[1]
print("Building {}...".format(name))
result = build.execute()
if result[0]:
print(colorama.Fore.GREEN + result[0].decode() + colorama.Fore.RESET)
if result[1]:
print(colorama.Fore.RED + result[1].decode() + colorama.Fore.RESET)
print("Done at {}!\n".format(time.strftime("%H:%M:%S %p")))
class BuildHandler(watchdog.events.FileSystemEventHandler):
"""An automatic build tool that monitors the file system."""
def __init__(self, build: closure.ClosureBuild):
"""Initialize the build handler."""
super().__init__()
self.build = build
self.cache = {}
def on_any_event(self, event: watchdog.events.FileSystemMovedEvent):
"""Called when a file in the system is modified."""
path = os.path.realpath(event.src_path)
if not self.build.includes_file(path):
return
try:
modify_time = os.path.getmtime(path)
except FileNotFoundError:
modify_time = -1
if modify_time > 0 and (path not in self.cache or modify_time > self.cache[path]):
self.cache[path] = modify_time
else:
#print("Already updated.\n")
return
print("Detected modified file:")
print(colorama.Fore.GREEN + os.path.abspath(path) + colorama.Fore.RESET)
execute_and_print(self.build)
def main_watch():
"""Loop recompilation as needed."""
check_closure()
check_configuration()
handles = []
observer = watchdog.observers.Observer()
builds = load_configuration()
for build in builds:
execute_and_print(build)
handler = BuildHandler(build)
path = common_path(list(build.source_patterns))
handles.append(observer.schedule(handler, path, recursive=True))
observer.start()
print("Waiting for changes...\n")
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
break
def main_manual():
"""Loop wait for user input to rebuild."""
check_closure()
check_configuration()
builds = load_configuration()
names = []
while True:
try:
print("Builds: " + ", ".join(map(lambda x: x.name, builds)))
previous = "({})".format(" ".join(names)) if names else ""
names = input(previous + ": ").strip().split() or names
for build in builds:
if build.name in names:
execute_and_print(build)
except KeyboardInterrupt:
print()
break
def main():
"""Build everything once."""
check_closure()
check_configuration()
builds = load_configuration()
print("Builds: " + ", ".join(map(lambda x: x.name, builds)))
for build in builds:
execute_and_print(build)
if __name__ == "__main__":
if len(sys.argv) == 1:
main()
elif len(sys.argv) > 1:
if sys.argv[1] == "manual":
main_manual()
elif sys.argv[1] == "watch":
main_watch()