-
Notifications
You must be signed in to change notification settings - Fork 3
/
QuickRails.py
114 lines (95 loc) · 3.1 KB
/
QuickRails.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
import sublime, sublime_plugin
import re
import os, sys
import time
try:
import QuickRails.QuickExec
except Exception:
from .QuickExec import QuickExecCommand
rails_root_cache = {}
def rails_root(directory):
global rails_root_cache
retval = False
leaf_dir = directory
if leaf_dir in rails_root_cache and rails_root_cache[leaf_dir]['expires'] > time.time():
return rails_root_cache[leaf_dir]['retval']
while directory:
if os.path.exists(os.path.join(directory, 'Gemfile')):
retval = directory
break
parent = os.path.realpath(os.path.join(directory, os.path.pardir))
if parent == directory:
# /.. == /
retval = False
break
directory = parent
rails_root_cache[leaf_dir] = {
'retval': retval,
'expires': time.time() + 5
}
return retval
# for readability code
def rails_root_exist(directory):
return rails_root(directory)
def get_idea(directory):
root = rails_root(directory)
if not os.path.exists(os.path.join(root, '.idea')):
os.makedirs(os.path.join(root, '.idea'))
return os.path.join(root, '.idea')
def command_with_ruby_env(cmd):
rvm = os.path.expanduser('~'+os.sep+'.rvm'+os.sep+'bin'+os.sep+'rvm-auto-ruby')
rbenv = os.path.expanduser('~'+os.sep+'.rbenv'+os.sep+'bin'+os.sep+'rbenv')
s = sublime.load_settings("QuickRails.sublime-settings")
if s.get("check_for_rvm"):
return rvm + ' -S ' + cmd
elif s.get("check_for_rbenv"):
return rbenv + ' exec ' + cmd
else:
return cmd
class QuickRailsWindowCommand(sublime_plugin.WindowCommand):
# def run(self, view, args):
# print('Running QuickRails')
def active_view(self):
return self.window.active_view()
def _active_file_name(self):
view = self.active_view()
if view and view.file_name() and len(view.file_name()) > 0:
return view.file_name()
# If there is a file in the active view use that file's directory to
# search for the Rails root. Otherwise, use the only folder that is
# open.
def get_working_dir(self):
file_name = self._active_file_name()
if file_name:
return os.path.realpath(os.path.dirname(file_name))
else:
try: # handle case with no open folder
return self.window.folders()[0]
except IndexError:
return ''
# If there's no active view or the active view is not a file on the
# filesystem (e.g. a search results view)
def is_enabled(self):
if self._active_file_name() or len(self.window.folders()) == 1:
return rails_root(self.get_working_dir())
def run_quick_command(self, command, working_dir, listener):
if not command:
return False
self.window.run_command("quick_exec", {
"cmd": [command_with_ruby_env(command)],
"shell": True,
"listenerid": id(listener),
"working_dir": working_dir
})
return True
def run_shell_command(self, command, working_dir):
if not command:
return False
self.window.run_command("exec", {
"cmd": [command_with_ruby_env(command)],
"shell": True,
"working_dir": working_dir,
"file_regex": r"([^ ]*\.rb):?(\d*)"
})
#self.display_results()
return True