-#!/usr/bin/ruby
-# -*- coding: utf-8 -*-
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# textmate_import.rb --- import textmate snippets
require 'rubygems'
require 'plist'
-require 'choice'
+require 'trollop'
require 'fileutils'
require 'shellwords' # String#shellescape
require 'ruby-debug' if $DEBUG
-Choice.options do
- header ''
- header 'Standard Options:'
-
- option :bundle_dir do
- short '-d'
- long '--bundle-dir=PATH'
- desc 'Tells the program the directory to find the TextMate bundle directory'
- default '.'
- end
-
- option :output_dir do
- short '-o'
- long '--output-dir=PATH'
- desc 'What directory to write the new YASnippets to'
- default './textmate_import'
- end
-
- option :snippet do
- short '-f'
- long '--file=SNIPPET FILE NAME'
- desc 'A specific snippet that you want to copy or a glob for various files'
- default '*.{tmSnippet,tmCommand,plist,tmMacro}'
- end
-
- option :print_pretty do
- short '-p'
- long '--pretty-print'
- desc 'Pretty prints multiple snippets when printing to standard out'
- end
-
- option :quiet do
- short '-q'
- long '--quiet'
- desc 'Be quiet.'
- end
- option :convert_bindings do
- short '-b'
- long '--convert-bindings'
- desc "TextMate \"keyEquivalent\" keys are translated to YASnippet \"# binding :\" directives"
- end
+opts = Trollop::options do
+ opt :bundle_dir, "TextMate bundle directory", :short => '-d', :type => :string
+ opt :output_dir, "Output directory", :short => '-o', :type => :string
+ opt :glob, "Specific snippet file (or glob) inside <bundle_dir>", :short => '-g', :default => '*.{tmSnippet,tmCommand,plist,tmMacro}'
+ opt :pretty, 'Pretty prints multiple snippets when printing to standard out', :short => '-p'
+ opt :quiet, "Be quiet", :short => '-q'
+ opt :plist_file, "Use a specific plist file to derive menu information from", :type => :string
+end
+Trollop::die :bundle_dir, "must be provided" unless opts.bundle_dir
+Trollop::die :bundle_dir, "must exist" unless File.directory? opts.bundle_dir
- option :info_plist do
- short '-g'
- long '--info-plist=PLIST'
- desc "Specify a plist file derive menu information from defaults to \"bundle-dir\"/info.plist"
- end
+Trollop::die :output_dir, "must be provided" unless opts.output_dir
+Trollop::die :output_dir, "must exist" unless File.directory? opts.output_dir
- separator ''
- separator 'Common options: '
+Trollop::die :plist_file, "must exist" if opts.plist_file && File.directory?(opts.plist_file)
- option :help do
- long '--help'
- desc 'Show this message'
- end
-end
# Represents and is capable of outputting the representation of a
# TextMate menu in terms of `yas/define-menu'
if __FILE__ == $PROGRAM_NAME
# Read the the bundle's info.plist if can find it/guess it
#
- info_plist_file = Choice.choices.info_plist || File.join(Choice.choices.bundle_dir,"info.plist")
+ info_plist_file = opts.plist_file || File.join(opts.bundle_dir,"info.plist")
info_plist = TmSnippet::read_plist(info_plist_file) if info_plist_file and File.readable? info_plist_file;
# Calculate the mode name
#
- modename = File.basename Choice.choices.output_dir || "major-mode-name"
+ modename = File.basename opts.output_dir || "major-mode-name"
# Read in .yas-setup.el looking for the separator between auto-generated
#
original_dir = Dir.pwd
- yas_setup_el_file = File.join(original_dir, Choice.choices.output_dir, ".yas-setup.el")
+ yas_setup_el_file = File.join(original_dir, opts.output_dir, ".yas-setup.el")
separator = ";; --**--"
whole, head , tail = "", "", ""
if File::exists? yas_setup_el_file
# Glob snippets into snippet_files, going into subdirs
#
- Dir.chdir Choice.choices.bundle_dir
- snippet_files_glob = File.join("**", Choice.choices.snippet)
+ Dir.chdir opts.bundle_dir
+ snippet_files_glob = File.join("**", opts.glob)
snippet_files = Dir.glob(snippet_files_glob)
# Attempt to convert each snippet files in snippet_files
#
- puts "Will try to convert #{snippet_files.length} snippets...\n" unless Choice.choices.quiet
+ puts "Will try to convert #{snippet_files.length} snippets...\n" unless opts.quiet
# Iterate the globbed files
#
snippet_files.each do |file|
begin
- puts "Processing \"#{File.join(Choice.choices.bundle_dir,file)}\"\n" unless Choice.choices.quiet
+ $stdout.print "Processing \"#{File.join(opts.bundle_dir,file)}\"..." unless opts.quiet
snippet = TmSnippet.new(file,info_plist)
- if
- file_to_create = File.join(original_dir, Choice.choices.output_dir, snippet.yas_file)
- FileUtils.mkdir_p(File.dirname(file_to_create))
- File.open(file_to_create, 'w') do |f|
- f.write(snippet.to_yas)
- end
- else
- if Choice.choices.print_pretty
- puts "--------------------------------------------"
- end
- puts snippet.to_yas if Choice.choices.print_pretty or not Choice.choices.info_plist
- if Choice.choices.print_pretty
- puts "--------------------------------------------\n\n"
- end
+ file_to_create = File.join(original_dir, opts.output_dir, snippet.yas_file)
+ FileUtils.mkdir_p(File.dirname(file_to_create))
+ File.open(file_to_create, 'w') do |f|
+ f.write(snippet.to_yas)
end
+ $stdout.print "done\n"
rescue SkipSnippet => e
- $stdout.puts "Skipping \"#{file}\": #{e.message}"
+ $stdout.print "skipped! #{e.message}\n" unless opts.quiet
rescue RuntimeError => e
- $stderr.puts "Oops.... \"#{file}\": #{e.message}"
- $strerr.puts "#{e.backtrace.join("\n")}" unless Choice.choices.quiet
+ $stderr.print "failed! #{e.message}\n"
+ $strerr.print "#{e.backtrace.join("\n")}" unless opts.quiet
end
end
# Write some basic .yas-* files
#
- if Choice.choices.output_dir
- FileUtils.mkdir_p Choice.choices.output_dir
- FileUtils.touch File.join(original_dir, Choice.choices.output_dir, ".yas-make-groups") unless menustr
+ if opts.output_dir
+ FileUtils.mkdir_p opts.output_dir
+ FileUtils.touch File.join(original_dir, opts.output_dir, ".yas-make-groups") unless menustr
# Now, output head + a new tail in (possibly new) .yas-setup.el
# file
end
end
end
-# ~> -:200: undefined method `length' for /shit/:Regexp (NoMethodError)