require 'optparse' require 'rubygems' require 'haml' module Compass module Exec class ExecError < StandardError end def report_error(e, options) $stderr.puts "#{e.class} on line #{get_line e} of #{get_file e}: #{e.message}" if options[:trace] e.backtrace[1..-1].each { |t| $stderr.puts " #{t}" } else $stderr.puts "Run with --trace to see the full backtrace" end end def get_file(exception) exception.backtrace[0].split(/:/, 2)[0] end def get_line(exception) # SyntaxErrors have weird line reporting # when there's trailing whitespace, # which there is for Haml documents. return exception.message.scan(/:(\d+)/)[0] if exception.is_a?(::Haml::SyntaxError) exception.backtrace[0].scan(/:(\d+)/)[0] end module_function :report_error, :get_file, :get_line class Compass attr_accessor :args, :options, :opts def initialize(args) self.args = args self.options = {} end def run! begin parse! perform! rescue Exception => e raise e if e.is_a? SystemExit if e.is_a?(ExecError) || e.is_a?(OptionParser::ParseError) $stderr.puts e.message else ::Compass::Exec.report_error(e, @options) end exit 1 end exit 0 end protected def perform! if options[:command] do_command(options[:command]) else puts self.opts end end def parse! self.opts = OptionParser.new(&method(:set_opts)) self.opts.parse!(self.args) if ARGV.size > 0 self.options[:project_name] = ARGV.shift end self.options[:command] ||= self.options[:project_name] ? :create_project : :update_project self.options[:environment] ||= :production self.options[:framework] ||= :compass end def set_opts(opts) opts.banner = <