-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRubyFileToggle.py
More file actions
197 lines (140 loc) · 5.27 KB
/
Copy pathRubyFileToggle.py
File metadata and controls
197 lines (140 loc) · 5.27 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
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
import sublime
import sublime_plugin
import re
import os
settings = None
def plugin_loaded():
global settings
settings = sublime.load_settings("Ruby File Toggle.sublime-settings")
def is_debug():
global settings
return settings.get("debug")
def debug(*args):
global settings
if is_debug():
print("[Ruby File Toggle]", *args)
CREATE_IMPLEMENTATION_FILE_MESSAGE = """
The implementation file doesn't exist.
Do you want to create it?
Path: %s
"""
CREATE_TEST_FILE_MESSAGE = """
The test file doesn't exist.
Do you want to create it?
Path: %s
"""
TEST_TEMPLATE = """\
require "%s_helper"\
"""
class RubyFileToggleCommand(sublime_plugin.WindowCommand):
has_app_dir = False
is_rspec = False
is_zee = False
is_minitest = False
gemfile_lock_cache = None
def run(self):
folders = self.window.folders()
if len(folders) == 0:
return
current_file = self.window.active_view().file_name()
current_folder = None
for folder in folders:
if current_file.startswith(folder):
current_folder = folder
break
if not current_folder:
return
self.has_app_dir = os.path.isdir(os.path.join(current_folder, "app"))
self.is_minitest = self.depend_on(current_folder, "minitest")
self.is_rspec = self.depend_on(current_folder, "rspec")
debug("Has app directory?", self.has_app_dir)
debug("Minitest?", self.is_minitest)
debug("RSpec?", self.is_rspec)
if not self.is_minitest and not self.is_rspec:
debug("Project must depend on either `minitest` or `rspec`, skipping.")
return
relative_path = current_file.replace("%s/" % current_folder, "")
if relative_path.endswith(
"_spec.rb") or relative_path.endswith("_test.rb"):
debug("opening implementation file")
self.open_implementation_file(current_folder, relative_path)
else:
debug("opening spec file")
self.open_test_file(current_folder, relative_path)
def open_implementation_file(self, folder, file):
base_path = re.sub(
r"^(spec|test)\/(.*?)_(spec|test)\.rb$",
"\\2.rb",
file)
debug("base path:", base_path)
candidates = [base_path, "lib/%s" % base_path, "app/%s" % base_path]
debug("candidates:", candidates)
for path in candidates:
full_path = os.path.join(folder, path)
if os.path.isfile(full_path):
return self.window.open_file(full_path)
debug("implementation file doesn't exist")
candidates = [
os.path.join(folder, "app"),
os.path.join(folder, "lib")
]
target_dir = os.path.join(folder, "lib")
for candidate in candidates:
if os.path.isdir(candidate):
target_dir = candidate
break
full_path = os.path.join(target_dir, base_path)
relative_path = full_path.replace("%s/" % folder, "")
debug("full path", full_path)
debug("relative path:", relative_path)
if not sublime.ok_cancel_dialog(
CREATE_IMPLEMENTATION_FILE_MESSAGE % relative_path):
return
basedir = os.path.dirname(full_path)
if not os.path.isdir(basedir):
self.make_dir_for_path(full_path)
with open(full_path, "w+") as io:
print("", file=io)
self.window.open_file(full_path)
def open_test_file(self, folder, file):
framework_suffix = "test"
if self.is_rspec:
framework_suffix = "spec"
if self.has_app_dir:
regex = r"^(?:app\/)?(.*?)\.rb$"
else:
regex = r"^lib\/(.*?)\.rb$"
replacement = "{suffix}/\\1_{suffix}.rb".format(
suffix=framework_suffix)
base_path = re.sub(regex, replacement, file)
full_path = os.path.join(folder, base_path)
relative_path = full_path.replace("%s/" % folder, "")
debug("base path:", base_path)
debug("full path:", full_path)
if os.path.isfile(full_path):
debug("opening existing file")
self.window.open_file(full_path)
return
if not sublime.ok_cancel_dialog(
CREATE_TEST_FILE_MESSAGE % relative_path):
debug("user doesn't want to create file")
return
debug("user wants to create a new file")
self.make_dir_for_path(full_path)
with open(full_path, "w+") as io:
print(TEST_TEMPLATE % framework_suffix, file=io)
self.window.open_file(full_path)
def make_dir_for_path(self, filepath):
basedir = os.path.dirname(filepath)
if not os.path.isdir(basedir):
os.makedirs(basedir)
def depend_on(self, folder, gem):
gemfile_lock = os.path.join(folder, "Gemfile.lock")
debug("Gemfile.lock:", gemfile_lock)
if not os.path.isfile(gemfile_lock):
debug("Gemfile.lock doesn't exist")
return False
if not self.gemfile_lock_cache:
with open(gemfile_lock) as file:
self.gemfile_lock_cache = file.read()
return " {gem} ".format(gem=gem) in self.gemfile_lock_cache