-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMigraine.py
More file actions
59 lines (49 loc) · 1.75 KB
/
Migraine.py
File metadata and controls
59 lines (49 loc) · 1.75 KB
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
import sublime, sublime_plugin
import os
import re
class MigraineCommand(sublime_plugin.WindowCommand):
def run(self):
try:
cur_path = self.window.active_view().file_name()
except AttributeError:
if self.window.folders():
cur_path = self.window.folders()[0]
if cur_path:
if os.path.isfile(cur_path):
cur_path = os.path.dirname(cur_path)
root = self.find_ror_root(cur_path)
else:
raise NothingOpen("Open folder or file")
if root:
self.migrations_dir = os.path.join(root, 'db', 'migrate')
migrations = os.listdir(self.migrations_dir)
pattern = re.compile('^\d+_\w+.rb$')
migrations = sorted([m for m in migrations if pattern.match(m)])
latest_migration = os.path.join(self.migrations_dir, migrations[-1])
self.panel_items = migrations
self.window.show_quick_panel(self.panel_items, self.open_selected)
def open_selected(self,selected_file_index):
if selected_file_index != -1:
full_path = os.path.join(self.migrations_dir, self.panel_items[selected_file_index])
sublime.active_window().open_file(full_path)
def find_ror_root(self, path):
expected_items = ['Gemfile', 'app', 'config', 'db']
files = os.listdir(path)
if path == '/':
raise NotRailsApp("Can't find Rails project")
if len([x for x in expected_items if x in files]) == len(expected_items):
return path
else:
return self.find_ror_root(self.parent_path(path))
def parent_path(self, path):
return os.path.abspath(os.path.join(path, '..'))
def on_done(self,list):
return path
class Error(Exception):
def __init__(self, msg):
self.msg = msg
sublime.error_message(self.msg)
class NotRailsApp(Error):
pass
class NothingOpen(Error):
pass