forked from nicosantangelo/sublime-gulp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulp.py
411 lines (328 loc) · 14 KB
/
gulp.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import sys
import sublime
import datetime
import codecs
import os, os.path
from threading import Thread
import signal, subprocess
import json
import webbrowser
from hashlib import sha1
is_sublime_text_3 = int(sublime.version()) >= 3000
if is_sublime_text_3:
from .base_command import BaseCommand
from .progress_notifier import ProgressNotifier
import urllib.request as urllib2
else:
from base_command import BaseCommand
from progress_notifier import ProgressNotifier
import urllib2
class GulpCommand(BaseCommand):
cache_file_name = ".sublime-gulp.cache"
log_file_name = 'sublime-gulp.log'
def work(self):
self.set_instance_variables()
self.list_gulp_files()
def set_instance_variables(self):
self.gulp_files = []
self.env = Env(self.settings)
def list_gulp_files(self):
self.append_paths()
if len(self.gulp_files) > 0:
self.choose_file()
else:
self.error_message("gulpfile.js not found!")
def append_paths(self):
self.folders = []
for folder_path in self.window.folders():
self.append_to_gulp_files(folder_path)
for inner_folder in self.settings.get("gulpfile_paths", []):
self.append_to_gulp_files(os.path.join(folder_path, inner_folder))
def append_to_gulp_files(self, folder_path):
gulpfile_path = os.path.join(folder_path, "gulpfile.js")
self.folders.append(folder_path)
if os.path.exists(gulpfile_path):
self.gulp_files.append(gulpfile_path)
def choose_file(self):
if len(self.gulp_files) == 1:
self.show_tasks_from_gulp_file(0)
else:
self.show_quick_panel(self.gulp_files, self.show_tasks_from_gulp_file)
def show_tasks_from_gulp_file(self, file_index):
if file_index > -1:
self.working_dir = os.path.dirname(self.gulp_files[file_index])
if self.task_name:
self.run_gulp_task()
else:
self.defer(self.show_tasks)
def show_tasks(self):
self.tasks = self.list_tasks()
if self.tasks is not None:
self.show_quick_panel(self.tasks, self.task_list_callback)
def list_tasks(self):
try:
self.callcount = 0
json_result = self.fetch_json()
except TypeError as e:
self.error_message("Could not read available tasks.\nMaybe the JSON cache (.sublime-gulp.cache) is malformed?")
except Exception as e:
self.error_message(str(e))
else:
tasks = [[name, self.dependencies_text(task)] for name, task in json_result.items()]
return sorted(tasks, key = lambda task: task)
def dependencies_text(self, task):
return "Dependencies: " + task['dependencies'] if task['dependencies'] else ""
# Refactor
def fetch_json(self):
jsonfilename = os.path.join(self.working_dir, self.cache_file_name)
gulpfile = os.path.join(self.working_dir, "gulpfile.js") # .coffee ?
data = None
if os.path.exists(jsonfilename):
filesha1 = Security.hashfile(gulpfile)
json_data = open(jsonfilename)
try:
data = json.load(json_data)
if gulpfile in data and data[gulpfile]["sha1"] == filesha1:
return data[gulpfile]["tasks"]
finally:
json_data.close()
self.callcount += 1
if self.callcount == 1:
return self.write_to_cache()
if data is None:
raise Exception("Could not write to cache gulpfile.")
raise Exception("Sha1 from gulp cache ({0}) is not equal to calculated ({1}).\nTry erasing the cache and running Gulp again.".format(data[gulpfile]["sha1"], filesha1))
def write_to_cache(self):
package_path = os.path.join(sublime.packages_path(), self.package_name)
args = r'node "%s/write_tasks_to_cache.js"' % package_path
process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=self.env.get_path_with_exec_args(), cwd=self.working_dir, shell=True)
(stdout, stderr) = process.communicate()
if 127 == process.returncode:
raise Exception("\"node\" command not found.\nPlease be sure to have nodejs installed on your system and in your PATH (more info in the README).")
elif stderr:
self.log_errors(stderr)
raise Exception("There was an error running gulp, make sure gulp is running correctly in your project.\nFor more info check the sublime-gulp.log file")
return self.fetch_json()
def log_errors(self, text):
if not self.settings.get("log_errors", True):
return
log_path = self.working_dir + "/" + self.log_file_name
header = "Remember that you can report errors and get help in https://github.com/NicoSantangelo/sublime-gulp" if not os.path.isfile(log_path) else ""
with codecs.open(log_path, 'a', "utf-8") as log_file:
log_file.write(header + "\n\n" + str(datetime.datetime.now().strftime("%m-%d-%Y %H:%M")) + ":\n" + text.decode('utf-8'))
def task_list_callback(self, task_index):
if task_index > -1:
self.task_name = self.tasks[task_index][0]
self.run_gulp_task()
def run_gulp_task(self):
task = self.construct_gulp_task()
Thread(target = self.run_process, args = (task, )).start() # Option to kill on timeout?
def construct_gulp_task(self):
self.show_output_panel("Running '%s'...\n" % self.task_name)
return r"gulp %s" % self.task_name
def run_process(self, task):
process = CrossPlatformProcess(self, self.nonblocking)
process.run(task)
stdout, stderr = process.communicate(self.append_to_output_view_in_main_thread)
self.defer_sync(lambda: self.finish(stdout, stderr))
def finish(self, stdout, stderr):
finish_message = "gulp %s finished %s" % (self.task_name, "with some errors." if stderr else "!")
self.status_message(finish_message)
if not self.silent:
self.set_output_close_on_timeout()
elif stderr and self.settings.get("show_silent_errors", False):
self.silent = False
self.show_output_panel("Running '%s'...\n" % self.task_name)
self.append_to_output_view(stdout)
self.append_to_output_view(stderr)
self.silent = True
class GulpKillCommand(BaseCommand):
def work(self):
if ProcessCache.empty():
self.status_message("There are no running tasks")
else:
self.show_output_panel("\nFinishing the following running tasks:\n")
ProcessCache.each(lambda process: self.append_to_output_view("$ %s\n" % process.last_command))
ProcessCache.kill_all()
self.append_to_output_view("\nAll running tasks killed!\n")
class GulpShowPanelCommand(BaseCommand):
def work(self):
self.show_panel()
class GulpPluginsCommand(BaseCommand):
def work(self):
self.plugins = None
self.request_plugin_list()
def request_plugin_list(self):
progress = ProgressNotifier("Gulp: Working")
thread = PluginRegistryCall()
thread.start()
self.handle_thread(thread, progress)
def handle_thread(self, thread, progress):
if thread.is_alive() or thread.result == False:
sublime.set_timeout(lambda: self.handle_thread(thread, progress), 100)
else:
progress.stop()
plugin_response = json.loads(thread.result.decode('utf-8'))
if plugin_response["timed_out"]:
self.error_message("Sadly the request timed out, try again later.")
else:
self.plugins = PluginList(plugin_response)
self.show_quick_panel(self.plugins.quick_panel_list(), self.open_in_browser, font = 0)
def open_in_browser(self, index = -1):
if index >= 0 and index < self.plugins.length:
webbrowser.open_new(self.plugins.get(index).get('homepage'))
class GulpDeleteCacheCommand(BaseCommand):
def work(self):
try:
jsonfilename = os.path.join(self.working_dir, GulpCommand.cache_file_name)
if os.path.exists(jsonfilename):
os.remove(jsonfilename)
self.status_message('Cache removed successfully')
except Exception as e:
self.status_message("Could not remove cache: %s" % str(e))
class CrossPlatformProcess():
def __init__(self, command, nonblocking=True):
self.path = command.env.get_path_with_exec_args()
self.working_dir = command.working_dir
self.last_command = ""
self.nonblocking = nonblocking
def run(self, command):
self.process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=self.path, cwd=self.working_dir, shell=True, preexec_fn=self._preexec_val())
self.last_command = command
ProcessCache.add(self)
return self
def _preexec_val(self):
return os.setsid if sublime.platform() != "windows" else None
def communicate(self, fn = lambda x:None):
stdout, stderr = self.pipe(fn)
self.process.communicate()
self.terminate()
return (stdout, stderr)
def pipe(self, fn):
streams = [self.process.stdout, self.process.stderr]
streams_text = []
if self.nonblocking:
threads = [ThreadWithResult(target=self._pipe_stream, args=(stream, fn)) for stream in streams]
[t.join() for t in threads]
streams_text = [t.result for t in threads]
else:
streams_text = [self._pipe_stream(stream, fn) for stream in streams]
return streams_text
def _pipe_stream(self, stream, fn):
output_text = ""
while True:
line = stream.readline()
if not line: break
output_line = self.decode_line(line)
output_text += output_line
fn(output_line)
return output_text
def decode_line(self, line):
line = line.rstrip()
return str(line.decode('utf-8') if sys.version_info >= (3, 0) else line) + "\n"
def read(self, stream):
return stream.read().decode('utf-8')
def terminate(self):
if self.is_alive():
self.process.terminate()
ProcessCache.remove(self)
def is_alive(self):
return self.process.poll() is None
def kill(self):
pid = self.process.pid
if sublime.platform() == "windows":
kill_process = subprocess.Popen(['taskkill', '/F', '/T', '/PID', str(pid)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
kill_process.communicate()
else:
os.killpg(pid, signal.SIGTERM)
ProcessCache.remove(self)
class ProcessCache():
_procs = []
@classmethod
def add(cls, process):
cls._procs.append(process)
@classmethod
def remove(cls, process):
if process in cls._procs:
cls._procs.remove(process)
@classmethod
def kill_all(cls):
cls.each(lambda process: process.kill())
cls.clear()
@classmethod
def each(cls, fn):
for process in cls._procs:
fn(process)
@classmethod
def empty(cls):
return len(cls._procs) == 0
@classmethod
def clear(cls):
del cls._procs[:]
class Env():
def __init__(self, settings):
self.exec_args = settings.get('exec_args', False)
def get_path(self):
path = os.environ['PATH']
if self.exec_args:
path = self.exec_args.get('path', os.environ['PATH'])
return str(path)
def get_path_with_exec_args(self):
env = os.environ.copy()
if self.exec_args:
path = str(self.exec_args.get('path', ''))
if path:
env['PATH'] = path
return env
class Security():
@classmethod
def hashfile(cls, filename):
with open(filename, mode='rb') as f:
filehash = sha1()
content = f.read();
filehash.update(str("blob " + str(len(content)) + "\0").encode('UTF-8'))
filehash.update(content)
return filehash.hexdigest()
class PluginList():
def __init__(self, plugins_response):
self.plugins = [Plugin(plugin_json) for plugin_json in plugins_response["hits"]["hits"]]
self.length = len(self.plugins)
def get(self, index):
if index >= 0 and index < self.length:
return self.plugins[index]
def quick_panel_list(self):
return [ [plugin.get('name') + ' (v' + plugin.get('version') + ')', plugin.get('description')] for plugin in self.plugins ]
class Plugin():
def __init__(self, plugin_json):
self.plugin = plugin_json
def get(self, property):
return self.plugin['fields'][property][0] if self.has(property) else ''
def has(self, property):
return 'fields' in self.plugin and property in self.plugin['fields']
class PluginRegistryCall(Thread):
url = "http://registry.gulpjs.com/_search?fields=name,description,author,homepage,version&from=20&q=keywords:gulpplugin,gulpfriendly&size=750"
def __init__(self, timeout = 5):
self.timeout = timeout
self.result = None
Thread.__init__(self)
def run(self):
try:
request = urllib2.Request(self.url, None, headers = { "User-Agent": "Sublime Text" })
http_file = urllib2.urlopen(request, timeout = self.timeout)
self.result = http_file.read()
return
except urllib2.HTTPError as e:
err = 'Gulp: HTTP error %s contacting gulpjs registry' % (str(e.code))
except urllib2.URLError as e:
err = 'Gulp: URL error %s contacting gulpjs registry' % (str(e.reason))
sublime.error_message(err)
self.result = False
class ThreadWithResult(Thread):
def __init__(self, target, args):
self.result = None
self.target = target
self.args = args
Thread.__init__(self)
self.start()
def run(self):
self.result = self.target(*self.args)