lib/webby/apps/main.rb in webby-0.9.0 vs lib/webby/apps/main.rb in webby-0.9.1

- old
+ new

@@ -35,15 +35,17 @@ def parse( args ) opts = OptionParser.new opts.banner = 'Usage: webby [options] target [target args]' opts.separator '' - opts.on('-D', '--describe [PATTERN]', 'describe the tasks (matching optional PATTERN), then exit') {|pattern| app.do_option('--describe', pattern)} - opts.on('-P', '--prereqs', 'display the tasks and dependencies, then exit') {app.do_option('--prereqs', nil)} - opts.on('-T', '--tasks [PATTERN]', 'display the tasks (matching optional PATTERN) with descriptions, then exit') {|pattern| app.do_option('--tasks', pattern)} - opts.on('-t', '--trace', 'turn on invoke/execute tracing, enable full backtrace') {app.do_option('--trace', nil)} + desired_opts = %[--describe --prereqs --tasks --trace] + app.standard_rake_options.each do |options| + next unless desired_opts.include?(options.first) + opts.on(*options) + end + opts.separator '' opts.separator 'common options:' opts.on_tail( '-h', '--help', 'show this message' ) do @stdout.puts opts @@ -73,13 +75,14 @@ # command line arguments are converted into a page name and directory that # might get created (depending upon the task invoked). # def init( args ) # Make sure we're in a folder with a Sitefile - app.do_option('--rakefile', 'Sitefile') - app.do_option('--nosearch', nil) - app.do_option('--silent', nil) + options = app.standard_rake_options + [['--rakefile', 'Sitefile'], + ['--no-search', nil], + ['--silent', nil]].each {|opt, value| options.assoc(opt).last.call(value)} unless app.have_rakefile raise RuntimeError, "Sitefile not found" end @@ -102,10 +105,16 @@ # def app Rake.application end + # Returns the options hash from the Rake application object. + # + def options + app.options + end + # Search for the "Sitefile" starting in the current directory and working # upwards through the filesystem until the root of the filesystem is # reached. If a "Sitefile" is not found, a RuntimeError is raised. # def find_sitefile @@ -142,16 +151,19 @@ dashed = args.raw.join('-').downcase spaced = args.raw.join(' ') dir = ::File.dirname(dashed) args.dir = ('.' == dir ? '' : dir) - args.slug = ::Webby::Resources::File.basename(dashed).to_url - args.title = ::Webby::Resources::File.basename(spaced).titlecase + args.slug = ::Webby::Resources.basename(dashed).to_url + args.title = ::Webby::Resources.basename(spaced).titlecase # page should be dir/slug without leading / args.page = ::File.join(args.dir, args.slug).gsub(/^\//, '') + ext = ::File.extname(dashed) + args.page << ext unless ext.empty? + ::Webby.site.args = args Object.const_set(:SITE, Webby.site) args end @@ -182,14 +194,14 @@ end puts end else width = displayable_tasks.collect { |t| t.name_with_args.length }.max || 10 - max_column = 80 - name.size - width - 7 + max_column = truncate_output? ? terminal_width - name.size - width - 7 : nil displayable_tasks.each do |t| printf "#{name} %-#{width}s # %s\n", - t.name_with_args, truncate(t.comment, max_column) + t.name_with_args, max_column ? truncate(t.comment, max_column) : t.comment end end end # Provide standard execption handling for the given block. @@ -197,15 +209,15 @@ begin yield rescue SystemExit => ex # Exit silently with current status exit(ex.status) - rescue SystemExit, GetoptLong::InvalidOption => ex + rescue SystemExit, OptionParser::InvalidOption => ex # Exit silently exit(1) rescue Exception => ex # Exit with error message - $stderr.puts "webby aborted!" + $stderr.puts "#{name} aborted!" $stderr.puts ex.message if options.trace $stderr.puts ex.backtrace.join("\n") else $stderr.puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""