Last active
August 29, 2015 14:06
-
-
Save allizon/9de3a875be503feeb45f to your computer and use it in GitHub Desktop.
Sublime Text 3 EventListener to run Ruby files through Rubocop on save. Save to your Packages/User directory.
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
import sublime, sublime_plugin | |
import os | |
import subprocess | |
class RubocopCommand(sublime_plugin.EventListener): | |
# Replace this with the path to your Rubocop bin (obviously) | |
RUBOCOP_BIN = '/Users/alholt/.rbenv/shims/rubocop' | |
def on_post_save_async(self, view): | |
if view.file_name().endswith('rb'): | |
self.set_status('Running through Rubocop...') | |
cmd_a = [ self.RUBOCOP_BIN + ' --format simple ' + view.file_name() ] | |
p = subprocess.Popen(cmd_a, stdout = subprocess.PIPE, | |
stderr = subprocess.STDOUT, shell = True) | |
if p.stdout is not None: | |
self.process_results(p.stdout.readlines()) | |
else: | |
self.set_status('No Rubocop issues found.') | |
def process_results(self, data): | |
sublime.status_message(data[-1].decode('utf-8')) | |
for row in data: | |
row = row.decode('utf-8') | |
if len(row) > 1: | |
print(row) | |
def set_status(self, message): | |
print(message) | |
sublime.status_message(message) |
OK, cleaned up a bit. Still open to enhancements from others!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seriously, it's quick and dirty. I'm going to clean it up later and make it more easily reusable, though if anyone else wants to do so, please feel free!