-
Notifications
You must be signed in to change notification settings - Fork 7
/
ver
executable file
·177 lines (141 loc) · 5.04 KB
/
ver
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
#!/usr/bin/env ruby
require 'pathname'
require 'optparse'
require 'shellwords'
module VER
require_relative '../lib/ver/vendor/pathname'
module_function
def require_and_run(options = {})
require_relative '../lib/ver'
run(options)
end
# Convert textmate bundles into ruby for use in VER.
#
# @param [String] path
# points to the root directory, for example './Ruby.tmbundle'
def install_tm_bundle(path)
require File.join(ROOT, 'ver/plist')
require 'pp'
require 'fileutils'
require 'shellwords'
install_tm_syntax(path)
install_tm_preferences(path)
install_tm_snippets(path)
install_tm_commands(path)
exit 0
end
def install_tm_syntax(path)
glob = File.expand_path('Syntaxes/*.{plist,tmLanguage}', path)
Dir.glob(glob, File::FNM_CASEFOLD) do |syntax|
puts "Parsing #{syntax}" if $DEBUG
plist = VER::Plist.parse_xml(syntax)
name = plist[:name]
install_rb_source('Syntax', '../config/syntax', name, plist)
end
end
def install_tm_preferences(path)
info = Plist.parse_xml(File.join(path, 'info.plist'))
name = info[:name]
glob = File.expand_path('Preferences/*.{plist,tmPreferences}', path)
preferences = []
Dir.glob(glob, File::FNM_CASEFOLD) do |preference|
puts "Parsing #{preference}" if $DEBUG
plist = Plist.parse_xml(preference)
puts "Adding Preference: #{plist[:name]}" if $DEBUG
preferences << plist
end
preferences = preferences.sort_by{|plist| plist[:name] }
install_rb_source('Preferences', '../config/preferences', name, preferences)
end
def install_tm_snippets(path)
info = Plist.parse_xml(File.join(path, 'info.plist'))
name = info[:name]
glob = File.expand_path('Snippets/*.{plist,tmSnippet}', path)
snippets = []
Dir.glob(glob, File::FNM_CASEFOLD) do |snippet|
puts "Parsing #{snippet}" if $DEBUG
plist = Plist.parse_xml(snippet)
puts "Adding Snippet: #{plist[:name]}" if $DEBUG
snippets << plist
end
snippets = snippets.sort_by{|plist| plist[:name] }
install_rb_source('Snippets', '../config/snippets', name, snippets)
end
def install_tm_commands(path)
info = Plist.parse_xml(File.join(path, 'info.plist'))
name = info[:name]
glob = File.expand_path('Commands/*.{plist,tmCommand}', path)
commands = []
Dir.glob(glob, File::FNM_CASEFOLD) do |command|
puts "Parsing #{command}" if $DEBUG
plist = Plist.parse_xml(command)
puts "Adding Command: #{plist[:name]}" if $DEBUG
commands << plist
end
commands = commands.sort_by{|plist| plist[:name] }
install_rb_source('Commands', '../config/commands', name, commands)
end
def install_tm_theme(path)
require File.join(ROOT, 'ver/plist')
require 'pp'
require 'fileutils'
require 'shellwords'
path = File.expand_path(path)
puts "Parsing #{path}" if $DEBUG
plist = Plist.parse_xml(path)
name = plist[:name]
install_rb_source('Theme', '../config/theme', name, plist)
ensure
exit
end
def install_rb_source(type, path, name, plist)
if plist.empty?
puts "#{type} - #{path} - #{name} -- not installing because of empty plist" if $DEBUG
return
end
filename = name.gsub(/[:\/\u2014]/, '-').squeeze('-')
target = File.expand_path("#{path}/#{filename}.rb", ROOT)
dirname = File.dirname(target)
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
content = plist.pretty_inspect
puts "Writing #{type} #{name} to #{target}" if $DEBUG
File.open(target, 'w+') do |io|
io.puts "# Encoding: UTF-8"
io.puts
io.puts(content)
end
escaped_target = target.shellescape
check = `ruby -c #{escaped_target}`
fail "#{target}: #{check}" unless $?.exitstatus == 0
end
end
options = {}
$0 = "#{__FILE__} #{ARGV.join(' ')}"
op = OptionParser.new{|o|
o.on('-Eex[:in]', '--encoding EXTERNAL[:INTERNAL]', 'used encoding'){|enc|
options[:encoding] = enc }
o.on('--font FONT', 'font family'){|fn| options[:font] = fn }
o.on('--fork', 'detach from terminal on startup'){ options[:fork] = true }
o.on('-f', '--no-fork', 'stay attached to terminal'){ options[:fork] = false }
o.on('--eventmachine', "Use EventMachine"){ options[:eventmachine] = true }
o.on('--no-eventmachine', "Dont' use EventMachine"){ options[:eventmachine] = false }
o.on('-k', '--keymap NAME', 'vim or emacs'){|k| options[:keymap] = k }
o.on('-s', '--session NAME', 'Keep session called NAME'){|s| options[:session] = s }
o.separator ' '
o.on('--install-tm-bundle PATH',
'Install textmate bundle for use with VER',
&VER.method(:install_tm_bundle))
o.on('--install-tm-theme PATH',
'Install textmate theme for use with VER',
&VER.method(:install_tm_theme))
o.separator ' '
o.on('-h', '--help', 'Show this help'){ puts o; exit }
o.on('--debug', 'turn $DEBUG on'){ $DEBUG = true }
o.on('--trace', 'turn tracer on (extremely verbose)'){
# require 'tracer'
options[:tracer] = true
}
}
$0 = ['ver', *ARGV].join(' ')
op.parse!(ARGV)
VER.require_and_run(options)