-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from klaascuvelier/improvements
Improvements
- Loading branch information
Showing
2 changed files
with
42 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,47 @@ | ||
import os | ||
import sublime | ||
import sublime_plugin | ||
import subprocess | ||
|
||
|
||
class CommandOnSave(sublime_plugin.EventListener): | ||
def on_post_save(self, view): | ||
_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) | ||
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 not settings == None: | ||
for path in settings.keys(): | ||
commands = settings.get(path) | ||
if file.startswith(path) and len(commands) > 0: | ||
print("Command on Save:") | ||
for command in commands: | ||
p = subprocess.Popen([command], shell=True, stdout=subprocess.PIPE) | ||
out, err = p.communicate() | ||
print (out.decode('utf-8')) | ||
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters