#!/usr/bin/env ruby require 'pathname' require 'optparse' # annoying fixes class Pathname alias / join def cp(dest) FileUtils.copy_file(expand_path.to_s, dest.to_s, preserve = true) end def =~(regexp) to_s =~ regexp end end module VER file = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__ ROOT = File.expand_path('../../lib', file).encode(Encoding::UTF_8) module_function def require_and_run(options = {}) # TODO: remove before release $LOAD_PATH.unshift(ROOT) if options[:fork] fork do trap(:HUP){ 'terminal disconnected' } require 'ver' run(options) end else require 'ver' run(options) end end def install_tm_bundle(path) require File.join(ROOT, 'ver/plist') require 'json' require 'fileutils' glob = File.expand_path('Syntaxes/*.{plist,tmLanguage}', path) Dir.glob(glob, File::FNM_CASEFOLD) do |syntax| plist = VER::Plist.parse_xml(syntax) name = plist['name'].to_s.gsub(/[\/\u2014]/, '-') puts "Installing #{name}" target = File.expand_path("../config/syntax/#{name}.json", ROOT) json = JSON.pretty_unparse(plist) json.gsub!(/\\x\{(\h+)\}/, '\\\\x\1') FileUtils.mkdir_p(File.dirname(target)) File.open(target, 'w+'){|io| io.write(json) } end info = Plist.parse_xml(File.join(path, 'info.plist')) info_name = info['name'] glob = File.expand_path('Preferences/*.{plist,tmPreferences}', path) preferences = {} Dir.glob(glob, File::FNM_CASEFOLD) do |preference| plist = Plist.parse_xml(preference) name = plist['name'].to_s.gsub(/[\/\u2014]/, '-') puts "Adding #{name}" preferences[plist['uuid']] = plist end pref = File.expand_path("../config/preferences/#{info_name}.json", ROOT) pref.gsub!(/[\/\u2014]/, '-') FileUtils.mkdir_p(File.dirname(pref)) json = JSON.pretty_unparse(preferences) puts "Writing to #{pref}" File.open(pref, 'w+'){|io| io.write(json) } exit end end options = { fork: true } op = OptionParser.new{|o| o.on('-Eex[:in]', '--encoding EXTERNAL[:INTERNAL]', 'used encoding'){|enc| options[:encoding] = enc } o.on('--font-family NAME', 'font family'){|fn| options[:font_family] = fn } o.on('--font-size INT', 'font size'){|fs| options[:font_size] = fs } o.on('-f', '--nofork', 'stay attached to terminal'){ options[:fork] = false } o.on('-k', '--keymap NAME', 'vim or emacs'){|k| options[:keymap] = k } o.separator ' ' o.on('--install-tm-bundle PATH', 'Install textmate bundle for use with VER', &VER.method(:install_tm_bundle)) o.separator ' ' o.on('-h', '--help', 'Show this help'){ puts o; exit } o.on('--debug', 'turn $DEBUG on'){ $DEBUG = true } } op.parse!(ARGV) VER.require_and_run(options)