#!/usr/bin/env ruby require 'pathname' require 'optparse' require 'shellwords' module VER file = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__ ROOT = File.expand_path('../../lib', file).encode(Encoding::UTF_8) require File.join(ROOT, 'ver/vendor/pathname') module_function def require_and_run(options = {}) # TODO: remove before release $LOAD_PATH.unshift(ROOT) require '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)