-
Notifications
You must be signed in to change notification settings - Fork 0
/
SublimeDrush.py
88 lines (67 loc) · 2.99 KB
/
SublimeDrush.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
import sublime
import sublime_plugin
import subprocess
# TODO: Use PATH to find drush (preferably cross platform method?)
# TODO: Menu Item
# TODO: Define default settings ala ST3 readonly ones
''' SublimeDrush: Parent Class '''
class SublimeDrush():
# Init class vars
name = "SublimeDrush AutoCache"
cmd_name = ""
drush_path = ""
file_types = []
working_dir = ""
''' Retrieve and initialise settings '''
def init_settings(self, view):
# Load the settings file
global_settings = sublime.load_settings("sublimedrush.sublime-settings")
self.drush_path = global_settings.get("drush_path")
self.allowed_types = global_settings.get("allowed_types")
self.output_type = global_settings.get("output_type")
# If option is set use project settings, else if set use global settings
if sublime.active_window().active_view().settings().get("sublimedrush").get("drush_working_dir"):
self.working_dir = sublime.active_window().active_view().settings().get("sublimedrush").get("drush_working_dir")
elif global_settings.get("drush_working_dir"):
self.working_dir = global_settings.get("drush_working_dir")
''' Check if this is a supported filetype '''
def check_allowed_type(self, view):
# If current file type in allowed list, proceed - is determined by syntax type e.g. Python.tmLanguage
for allowed_type in self.allowed_types:
current_type = view.settings().get('syntax').split('/')[2]
if (allowed_type + '.tmLanguage') == current_type:
return True
''' Run commands '''
def run_cmd(self, command):
try:
proc = subprocess.Popen(
command, bufsize=0,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
universal_newlines=True, shell=True, cwd=self.working_dir)
return proc
except FileNotFoundError:
sublime.error_message(self.name + ": Please check your working directory")
''' Print output '''
def trace(self, proc):
lines = []
# Read the output into a variable
while proc.poll() is None:
# Read output
line = proc.stdout.readline()
if line:
lines.append(line)
return lines
''' Drush Cache; inherits SublimeDrush '''
class SublimeDrushCC(sublime_plugin.EventListener, SublimeDrush):
# Event called after save
def on_post_save_async(self, view):
self.init_settings(view)
if (self.check_allowed_type(view)):
sublime.status_message(self.name + ": Clearing...")
proc = self.run_cmd([self.drush_path + " " + "cc all"])
status = self.trace(proc)
# Output confirmation dialog
if (self.output_type == 1):
sublime.status_message(self.name + ": Cleared")
elif (self.output_type == 2):
view.window().show_quick_panel(status, None, sublime.MONOSPACE_FONT)