forked from dchelimsky/rspec-tmbundle
/
switch_command.rb
139 lines (126 loc) · 4.66 KB
/
switch_command.rb
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
module Spec
module Mate
# This is based on Ruy Asan's initial code:
# http://ruy.ca/posts/6-A-simple-switch-between-source-and-spec-file-command-for-textmate-with-auto-creation-
class SwitchCommand
def go_to_twin(project_directory, filepath)
other = twin(filepath)
if File.file?(other)
%x{ "$TM_SUPPORT_PATH/bin/mate" "#{other}" }
else
relative = other[project_directory.length+1..-1]
file_type = file_type(other)
if create?(relative, file_type)
content = content_for(file_type, relative)
write_and_open(other, content)
end
end
end
def twin(path)
if path =~ /^(.*?)\/(lib|app|spec)\/(.*?)$/
prefix, parent, rest = $1, $2, $3
case parent
when 'lib', 'app' then
if webapp?(prefix)
path = path.gsub(/\/app\//, "/spec/")
path = path.gsub(/\/lib\//, "/spec/lib/")
path = path.gsub(/application\.rb/, 'application_controller.rb')
else
path = path.gsub(/\/lib\//, "/spec/")
end
path = path.gsub(/\.rb$/, "_spec.rb")
path = path.gsub(/\.erb$/, ".erb_spec.rb")
path = path.gsub(/\.rhtml$/, ".rhtml_spec.rb")
when 'spec' then
path = path.gsub(/\.rhtml_spec\.rb$/, ".rhtml")
path = path.gsub(/\.erb_spec\.rb$/, ".erb")
path = path.gsub(/_spec\.rb$/, ".rb")
if webapp?(prefix)
path = path.gsub(/\/spec\/lib\//, "/lib/")
path = path.gsub(/\/spec\//, "/app/")
path = path.gsub(/application_controller\.rb/, 'application.rb')
else
path = path.gsub(/\/spec\//, "/lib/")
end
end
return path
end
end
def file_type(path)
if path =~ /^(.*?)\/(spec)\/(controllers|helpers|models|views)\/(.*?)$/
return "#{$3[0..-2]} spec"
end
if path =~ /^(.*?)\/(app)\/(controllers|helpers|models|views)\/(.*?)$/
return $3[0..-2]
end
if path =~ /_spec\.rb$/
return "spec"
end
"file"
end
def webapp?(prefix)
rails?(prefix) || merb?(prefix)
end
def rails?(prefix)
File.exist?(File.join(prefix, 'config', 'boot.rb'))
end
def merb?(prefix)
File.exist?(File.join(prefix, 'config', 'init.rb'))
end
def create?(relative_twin, file_type)
answer = `#{ ENV['TM_SUPPORT_PATH'] }/bin/CocoaDialog.app/Contents/MacOS/CocoaDialog yesno-msgbox --no-cancel --icon document --informative-text "#{relative_twin}" --text "Create missing #{file_type}?"`
answer.to_s.chomp == "1"
end
def content_for(file_type, relative_path)
case file_type
when /spec$/ then
spec(relative_path)
else
klass(relative_path)
end
end
# Extracts the snippet text
def snippet(snippet_name)
snippet_file = File.expand_path(File.dirname(__FILE__) + "/../../../../Snippets/#{snippet_name}")
xml = File.open(snippet_file).read
xml.match(/<key>content<\/key>\s*<string>([^<]*)<\/string>/m)[1]
end
def spec(path)
depth = "/.." * (path.split('/').length - 2)
header = "require File.dirname(__FILE__) + '#{depth}/spec_helper'"
snippet_name = "Describe_type.tmSnippet"
content = <<-SPEC
#{header}
#{snippet(snippet_name)}
SPEC
end
def klass(relative_path)
parts = relative_path.split('/')
lib_index = parts.index('lib')
parts = parts[lib_index+1..-1]
lines = Array.new(parts.length*2)
parts.each_with_index do |part, n|
part = part.capitalize
indent = " " * n
line = if part =~ /(.*)\.rb/
part = $1
"#{indent}class #{part}"
else
"#{indent}module #{part}"
end
lines[n] = line
lines[lines.length-(n+1)] = "#{indent}end"
end
lines.join("\n") + "\n"
end
def write_and_open(path, content)
`mkdir -p "#{File.dirname(path)}"`
`touch "#{path}"`
`osascript &>/dev/null -e 'tell app "SystemUIServer" to activate' -e 'tell app "TextMate" to activate'`
`"$TM_SUPPORT_PATH/bin/mate" "#{path}"`
escaped_content = content.gsub("\n","\\n").gsub('$','\\$').gsub('"','\\\\\\\\\\\\"')
`osascript &>/dev/null -e "tell app \\"TextMate\\" to insert \\"#{escaped_content}\\" as snippet true"`
end
end
end
end