-
Notifications
You must be signed in to change notification settings - Fork 15
/
guard.py
303 lines (224 loc) · 9.48 KB
/
guard.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
import sublime
import sublime_plugin
try:
import thread
st_version = 2
except ImportError:
from threading import Thread
st_version = 3
import subprocess
import os
import stat
import functools
import re
sublime_guard_controller = None
class GuardController(object):
def __init__(self):
self.proc = None
self.running = False
self.auto_show_enabled = True
self.clear_when_find_this_text = None
def set_listener(self, listener):
self.listener = listener
if st_version == 2:
self.output_view = self.listener.window.get_output_panel('guard')
else:
self.output_view = self.listener.window.create_output_panel('guard')
self.enable_word_wrap()
self.set_color_scheme()
self.load_config()
return self
def open_file_paths(self):
return [view.file_name() for view in self.listener.window.views() if view.file_name()]
def open_folder_paths(self):
return self.listener.window.folders()
def path_has_guardfile(self, path):
return os.path.exists(path + '/Guardfile')
def find_project_root_path(self):
project_root_path = None
for path in self.open_folder_paths():
print ("Checking ... " + path)
if (self.path_has_guardfile(path)):
project_root_path = path
break
return project_root_path
def enable_word_wrap(self):
self.output_view.settings().set("word_wrap", True)
def set_color_scheme(self):
self.output_view.settings().set("syntax", "Packages/Guard/GuardOutput.tmLanguage")
self.output_view.settings().set("color_scheme", "Packages/Guard/GuardOutput.tmTheme")
def enable_auto_show(self):
self.auto_show_enabled = True
def disable_auto_show(self):
self.auto_show_enabled = False
def set_permissions(self, path):
os.chmod(path, stat.S_IRWXU | stat.S_IXGRP | stat.S_IRGRP | stat.S_IXOTH | stat.S_IROTH)
def start_guard(self):
project_root_path = self.find_project_root_path()
if (project_root_path == None):
sublime.error_message("Failed to find Guardfile in any of the open folders.")
else:
package_path = sublime.packages_path()
self.set_permissions(package_path + "/Guard/guard_wrapper")
self.set_permissions(package_path + "/Guard/run_guard.sh")
cmd_array = [package_path + "/Guard/guard_wrapper", package_path + "/Guard/run_guard.sh", project_root_path]
self.proc = subprocess.Popen(cmd_array, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.running = True
self.show_guard_view_and_enable_autoshow()
if self.proc.stdout:
if st_version == 2:
thread.start_new_thread(self.read_stdout, ())
else:
Thread(target=self.read_stdout, args=()).start()
if self.proc.stderr:
if st_version == 2:
thread.start_new_thread(self.read_stderr, ())
else:
Thread(target=self.read_stderr, args=()).start()
def read_stdout(self):
while True:
data = os.read(self.proc.stdout.fileno(), 2 ** 15)
if data != "":
sublime.set_timeout(functools.partial(self.append_data, data), 0)
else:
self.proc.stdout.close()
self.running = False
break
def read_stderr(self):
while True:
data = os.read(self.proc.stderr.fileno(), 2 ** 15)
if data != "":
sublime.set_timeout(functools.partial(self.append_data, data), 0)
else:
self.proc.stderr.close()
self.running = False
break
def append_data(self, data):
if (self.auto_show_enabled):
self.show_guard_view()
clean_data = data.decode("utf-8")
clean_data = self.normalize_line_endings(clean_data)
clean_data = self.remove_terminal_color_codes(clean_data)
# actually append the data
if st_version == 2:
self.output_view.set_read_only(False)
edit = self.output_view.begin_edit()
# clear the output window when a predefined text is found.
if (self.clear_when_find_this_text and self.clear_when_find_this_text.search(clean_data)):
self.output_view.erase(edit, sublime.Region(0, self.output_view.size()))
self.output_view.insert(edit, self.output_view.size(), clean_data)
# scroll to the end of the new insert
self.scroll_to_end_of_guard_view()
self.output_view.end_edit(edit)
self.output_view.set_read_only(True)
else:
self.output_view.run_command('guard_message', {'string': clean_data})
self.scroll_to_end_of_guard_view()
def normalize_line_endings(self, data):
return data.replace('\r\n', '\n').replace('\r', '\n')
def remove_terminal_color_codes(self, data):
color_regex = re.compile("\\033\[[0-9;m]*", re.UNICODE)
return color_regex.sub("", data)
def scroll_to_end_of_guard_view(self):
(cur_row, _) = self.output_view.rowcol(self.output_view.size())
self.output_view.show(self.output_view.text_point(cur_row, 0))
def show_guard_view_and_enable_autoshow(self):
self.enable_auto_show()
self.show_guard_view()
def show_guard_view(self):
self.listener.window.run_command('show_panel', {'panel': 'output.guard'})
def hide_guard_view(self):
self.disable_auto_show()
self.listener.window.run_command('hide_panel', {'panel': 'output.guard'})
def stop_guard(self):
self.proc.stdin.write(b'e\n')
self.proc.stdin.flush()
self.running = False
def is_guard_running(self):
return self.running
def reload_guard(self):
self.proc.stdin.write(b'r\n')
self.proc.stdin.flush()
def run_all_tests(self):
self.proc.stdin.write(b'\n')
self.proc.stdin.flush()
def output_help(self):
self.proc.stdin.write(b'h\n')
self.proc.stdin.flush()
def toggle_notifications(self):
self.proc.stdin.write(b'n\n')
self.proc.stdin.flush()
def pause(self):
self.proc.stdin.write(b'p\n')
self.proc.stdin.flush()
def load_config(self):
s = sublime.load_settings("Guard.sublime-settings")
clear_text = s.get("clear_when_find_this_text")
if (clear_text):
self.clear_when_find_this_text = re.compile(clear_text)
else:
self.clear_when_find_this_text = None
def GuardControllerSingleton():
global sublime_guard_controller
if sublime_guard_controller == None:
sublime_guard_controller = GuardController()
return sublime_guard_controller
else:
return sublime_guard_controller
class StartGuardCommand(sublime_plugin.WindowCommand):
def run(self):
GuardControllerSingleton().set_listener(self).start_guard()
def is_enabled(self):
return not GuardControllerSingleton().is_guard_running()
class StopGuardCommand(sublime_plugin.WindowCommand):
def run(self):
GuardControllerSingleton().set_listener(self).stop_guard()
def is_enabled(self):
return GuardControllerSingleton().is_guard_running()
class HideGuardCommand(sublime_plugin.WindowCommand):
def run(self):
GuardControllerSingleton().set_listener(self).hide_guard_view()
def is_enabled(self):
return True
class ShowGuardCommand(sublime_plugin.WindowCommand):
def run(self):
GuardControllerSingleton().set_listener(self).show_guard_view_and_enable_autoshow()
def is_enabled(self):
return True
class ReloadGuardCommand(sublime_plugin.WindowCommand):
def run(self):
GuardControllerSingleton().set_listener(self).reload_guard()
def is_enabled(self):
return GuardControllerSingleton().is_guard_running()
class RunAllTestsGuardCommand(sublime_plugin.WindowCommand):
def run(self):
GuardControllerSingleton().set_listener(self).run_all_tests()
def is_enabled(self):
return GuardControllerSingleton().is_guard_running()
class RunAllTestsAndShowGuardCommand(sublime_plugin.WindowCommand):
def run(self):
GuardControllerSingleton().set_listener(self).show_guard_view()
GuardControllerSingleton().set_listener(self).run_all_tests()
def is_enabled(self):
return GuardControllerSingleton().is_guard_running()
class OutputHelpGuardCommand(sublime_plugin.WindowCommand):
def run(self):
GuardControllerSingleton().set_listener(self).output_help()
def is_enabled(self):
return GuardControllerSingleton().is_guard_running()
class ToggleNotificationsGuardCommand(sublime_plugin.WindowCommand):
def run(self):
GuardControllerSingleton().set_listener(self).toggle_notifications()
def is_enabled(self):
return GuardControllerSingleton().is_guard_running()
class PauseGuardCommand(sublime_plugin.WindowCommand):
def run(self):
GuardControllerSingleton().set_listener(self).pause()
def is_enabled(self):
return GuardControllerSingleton().is_guard_running()
class GuardMessageCommand(sublime_plugin.TextCommand):
"""
A command to write a message to the Guard messaging buffer
"""
def run(self, edit, string=''):
self.view.insert(edit, self.view.size(), string)