-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCommandOnSave.py
47 lines (40 loc) · 1.76 KB
/
CommandOnSave.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
import os
import sublime
import sublime_plugin
import subprocess
import re
_STATUS_KEY = 'CommandOnSave'
class CommandOnSave(sublime_plugin.EventListener):
def on_post_save_async(self, view):
settings = sublime.load_settings('CommandOnSave.sublime-settings').get('commands')
if settings is None:
return
file = view.file_name()
before_stat = None
view.erase_status(_STATUS_KEY)
for path, commands in settings.items():
if file.startswith(path):
for command in commands:
# record the mtime so we can check if we need to reload the file
if before_stat is None:
before_stat = os.stat(file)
command = re.sub(
r"\b_file_\b",
file,
command
)
process = subprocess.Popen(command,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
output = process.stdout.read()
code = process.wait()
if code != 0:
view.set_status(_STATUS_KEY, 'ERROR: Command failed: %s' % (
' '.join(command)))
print('CommandOnSave %s failed code %d; output: %s' % (
' '.join(command), code, output))
# attempt to execute any other commands
if before_stat is not None and not view.is_dirty():
after_stat = os.stat(file)
if before_stat.st_mtime != after_stat.st_mtime:
# it seems like the file changed: reload the view
view.run_command('revert')