-
Notifications
You must be signed in to change notification settings - Fork 13
/
Rails.py
311 lines (219 loc) · 9.94 KB
/
Rails.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import sublime, sublime_plugin, os, glob, re
try:
from .vendor.inflector import *
except ValueError:
from vendor.inflector import *
# @author Luqman Amjad http://luqmanamjad.com
# Taken from Git Plugin (Changed to detected Rails root)
def rails_root(directory):
while directory:
if os.path.exists(os.path.join(directory, 'Rakefile')):
return directory
parent = os.path.realpath(os.path.join(directory, os.path.pardir))
if parent == directory:
# /.. == /
return False
directory = parent
return False
class RailsRelatedFilesHelper:
@staticmethod
def get_directory_listing_without_folders(path):
files = []
result = glob.glob(path)
for _file in result:
if not os.path.isdir(_file):
files.append(_file)
return files
@staticmethod
def for_controllers(app_folder, working_directory, base_file_name):
controller = base_file_name.replace('_controller', '')
model = Inflector(English).singularize(controller).lower()
namespace_directory = RailsRelatedFilesHelper.get_namespace_directory(working_directory)
working_directory_base = os.path.basename(working_directory)
if namespace_directory:
controller = os.path.join(working_directory_base, controller)
walkers = [
'app/models/' + model + '*',
'app/models/**/' + model + '*',
'app/helpers/' + controller + '**',
'app/helpers/**/' + controller + '**',
'app/views/' + controller + '/**',
'app/views/**/' + controller + '/**',
'test/' + controller + '**',
'test/**/' + controller + '**',
'spec/' + controller + '**',
'spec/**/' + controller + '**'
]
return RailsRelatedFilesHelper.get_files_while_walking(app_folder, walkers)
@staticmethod
def for_helpers(app_folder, working_directory, base_file_name):
helper = base_file_name.replace('_helper', '')
model = Inflector(English).singularize(helper).lower()
namespace_directory = RailsRelatedFilesHelper.get_namespace_directory(working_directory)
working_directory_base = os.path.basename(working_directory)
if namespace_directory:
helper = os.path.join(working_directory_base, helper)
walkers = [
'app/models/' + model + '*',
'app/models/**/' + model + '*',
'app/controllers/' + helper + '**',
'app/controllers/**/' + helper + '**',
'app/views/' + helper + '/**',
'app/views/**/' + helper + '/**',
'test/' + helper + '**',
'test/**/' + helper + '**',
'spec/' + helper + '**',
'spec/**/' + helper + '**'
]
return RailsRelatedFilesHelper.get_files_while_walking(app_folder, walkers)
@staticmethod
def for_views(app_folder, working_directory):
working_directory_base = os.path.basename(working_directory) #if app/views/posts it should return "posts"
model = Inflector(English).singularize(os.path.basename(working_directory_base)).lower() # should return "post"
namespace_directory = RailsRelatedFilesHelper.get_namespace_directory(working_directory) #should return none
controller = model
if namespace_directory:
working_directory_base = namespace_directory
controller = os.path.join(os.path.split(working_directory_base)[0], controller)
walkers = [
'app/models/' + model + '**',
'app/models/**/' + model + '**',
'app/views/' + working_directory_base + '/**',
'app/helpers/' + controller + '**',
'app/helpers/**/' + controller + '**',
'app/assets/javascripts/' + model + '**',
'app/assets/stylesheets/' + model + '**',
'app/controllers/' + controller + '**',
'app/controllers/**/' + controller + '**',
'test/' + controller + '**',
'test/**/' + controller + '**',
'spec/' + controller + '**',
'spec/**/' + controller + '**'
]
return RailsRelatedFilesHelper.get_files_while_walking(app_folder, walkers)
@staticmethod
def for_models(app_folder, working_directory, file_name_base_no_ext):
model = Inflector(English).singularize(file_name_base_no_ext).lower()
controller = Inflector(English).pluralize(file_name_base_no_ext).lower()
walkers = [
'app/models/' + model + '**',
'app/models/**/' + model + '**',
'app/helpers/' + controller + '**',
'app/helpers/**/' + controller + '**',
'app/views/' + controller + '/**',
'app/views/**/' + controller + '/**',
'app/controllers/' + controller + '**',
'app/controllers/**/' + controller + '**',
'test/' + model + '**',
'test/**/' + model + '**',
'spec/' + model + '**',
'spec/**/' + model + '**'
]
return RailsRelatedFilesHelper.get_files_while_walking(app_folder, walkers)
@staticmethod
def for_tests(app_folder, working_directory, base_file_name):
if '_controller' in base_file_name:
controller = base_file_name.replace('_controller', '').replace('_spec', '').replace('_test', '').replace('test_', '')
model = Inflector(English).singularize(controller).lower()
else:
model = base_file_name.replace('_spec', '').replace('test_', '')
controller = Inflector(English).pluralize(model).lower()
walkers = [
'app/controllers/' + controller + '**',
'app/controllers/**/' + controller + '**',
'app/models/' + model + '**',
'app/models/**/' + model + '**',
'app/helpers/' + controller + '**',
'app/helpers/**/' + controller + '**',
'app/views/' + controller + '/**',
'app/views/**/' + controller + '/**'
]
return RailsRelatedFilesHelper.get_files_while_walking(app_folder, walkers)
@staticmethod
def get_app_sub_directory(filename):
regex = re.compile('(app\/views|app\/controllers|app\/helpers|app\/models|app\/assets|test|spec)')
match = regex.findall(filename)
if match:
return match[0]
else:
return
@staticmethod
def get_namespace_directory(directory):
regex = re.compile('(\/app\/views|controllers|helpers|test|spec)\/(.*)') #amazing regex skills...
match = regex.findall(directory)
if match:
return match[0][1]
else:
return
@staticmethod
def get_files_while_walking(app_folder, walkers):
files = []
for walker in walkers:
files += (
RailsRelatedFilesHelper().get_directory_listing_without_folders(app_folder + '/' + walker)
)
files_without_full_path = []
for _file in files:
files_without_full_path += [_file.replace(app_folder + '/', '')]
return files_without_full_path
class RailsRelatedFilesCommand(sublime_plugin.TextCommand):
APP_FOLDERS = ['app/controllers', 'app/helpers', 'app/models', 'app/views', 'test', 'spec'] #assets
def run(self, edit, index):
if index >= 0:
self.open_file(index)
else:
try:
self.build_files()
sublime.active_window().show_quick_panel(self.files, self.open_file)
except:
return False
def is_visible(self, index):
#return True
try:
return self.files[index] and self.show_context_menu
except: # This should catch all exceptions and return false
return False
def open_file(self, index):
if index >= 0:
sublime.active_window().open_file(os.path.join(self.rails_root_directory, self.files[index]))
def build_files(self):
self.files = []
self.rails_root_directory = rails_root(self.get_working_dir())
if self.rails_root_directory:
self.show_context_menu = sublime.load_settings("Rails.sublime-settings").get('show_context_menu')
current_file_name = self._active_file_name()
working_directory = self.get_working_dir().replace("\\",'/')
working_directory_base = os.path.basename(working_directory)
file_name_base = os.path.basename(current_file_name)
file_name_base_no_ext = os.path.splitext(file_name_base)[0]
app_sub_directory = RailsRelatedFilesHelper.get_app_sub_directory(working_directory)
if app_sub_directory in self.APP_FOLDERS:
func, args = {
'app/controllers': (RailsRelatedFilesHelper.for_controllers, (self.rails_root_directory, working_directory, file_name_base_no_ext,)),
'app/helpers' : (RailsRelatedFilesHelper.for_helpers, (self.rails_root_directory, working_directory, file_name_base_no_ext,)),
'app/views' : (RailsRelatedFilesHelper.for_views, (self.rails_root_directory, working_directory,)),
'app/models' : (RailsRelatedFilesHelper.for_models, (self.rails_root_directory, working_directory, file_name_base_no_ext,)),
'test' : (RailsRelatedFilesHelper.for_tests, (self.rails_root_directory, working_directory, file_name_base_no_ext,)),
'spec' : (RailsRelatedFilesHelper.for_tests, (self.rails_root_directory, working_directory, file_name_base_no_ext,))
}.get(app_sub_directory)
self.files = func(*args)
if not self.files:
self.files = ['Rails Related Files: Nothing found...']
def description(self, index):
self.build_files()
try:
return self.files[index]
except IndexError as e:
return
# Taken from Git Plugin (Changed .active_view() to .view)
def _active_file_name(self):
view = self.view;
if view and view.file_name() and len(view.file_name()) > 0:
return view.file_name()
# Taken from Git Plugin
def get_working_dir(self):
file_name = self._active_file_name()
if file_name:
return os.path.dirname(file_name)
else:
return self.window.folders()[0]