-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathevent_task.py
54 lines (43 loc) · 1.77 KB
/
event_task.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
import sublime
import sublime_plugin
import os
from fnmatch import fnmatch
is_sublime_text_3 = int(sublime.version()) >= 3000
if is_sublime_text_3:
from .settings import Settings
else:
from settings import Settings
class EventTask(sublime_plugin.EventListener):
# these methods should be run synchronously to check if the
# view is transient.
def on_new(self, view):
view.run_command("gulp_update_status_bar")
def on_load(self, view):
view.run_command("gulp_update_status_bar")
def on_activated(self, view):
view.run_command("gulp_update_status_bar")
def on_post_save(self, view):
settings = Settings()
self.view = view
self.run_kill = settings.get('kill_before_save_tasks', False)
self.run_tasks(settings.get("tasks_on_save", {}))
self.run_tasks(settings.get("silent_tasks_on_save", {}), silent=True)
self.view.run_command("gulp_update_status_bar")
def run_tasks(self, tasks_on_save, silent=False):
if tasks_on_save:
for key in tasks_on_save:
value = tasks_on_save[key]
if isinstance(value, list):
for pattern in value:
self.run(key, pattern, silent)
else:
self.run(key, value, silent)
def run(self, task, pattern, silent):
folders = self.view.window().folders() or []
if any(fnmatch(self.view.file_name(), os.path.join(folder, pattern)) for folder in folders):
self.kill_once()
self.view.window().run_command("gulp", { "task_name": task, "silent": silent })
def kill_once(self):
if self.run_kill:
self.view.window().run_command("gulp_kill", { "silent": True })
self.run_kill = False