bin/germ in devver-germinate-1.1.0 vs bin/germ in devver-germinate-1.2.0

- old
+ new

@@ -1,10 +1,11 @@ #!/usr/bin/env ruby require File.expand_path( File.join(File.dirname(__FILE__), %w[.. lib germinate])) +require 'English' require 'main' Main do description <<-END Germinate - A tool for writing about code. @@ -37,82 +38,97 @@ validate{|source| Pathname(source).readable?} description "Source file" end end + option(:debug, :d) do + cast :bool + end + + def run + help! + end + mode :format do description "Format an article for publishing" source_argument def run - Germinate.logger = self with_source_file do |source, path| - application = Germinate::Application.new - application.format(source, path, $stdout, $stderr) + @application.format(source, path) end end end mode :list do - description "List various article components" - source_argument - COLLECTIONS = [:sections, :samples, :processes] - COLLECTIONS.each do |collection| - option collection do - description "List all #{collection}" - cast :bool - end + description "List info about the source file" + argument :collection do + arity 1 + required + argument_required + description "One of: sections, samples, processes, publishers, variables" end + source_argument def run - things_to_list = [] - COLLECTIONS.each do |collection| - things_to_list << collection if params[collection].value - end with_source_file do |source, path| - application = Germinate::Application.new - application.list(source, path, things_to_list, $stdout) + @application.list(source, path, params[:collection].value) end end end mode :show do description "Show details about various article components" - source_argument - TYPES = [:section, :sample, :process] - TYPES.each do |type| - option type do - description "Show details about a #{type}" - argument_required - end + argument :type do + arity 1 + required + argument_required + description "One of: section, sample, process, publisher, variable" end + argument :item do + arity 1 + required + argument_required + description "The specific item to show details about" + end + source_argument def run - selection = TYPES.inject({}) {|sel, type| - sel[type] = params[type].values - sel - } with_source_file do |source, path| - application = Germinate::Application.new - application.show(source, path, selection, $stdout) + @application.show(source, path, params['type'].value, params['item'].value) end end end mode :select do description "Test out a selector" - source_argument - option(:selector, :s) do + argument :selector do arity 1 required argument_required - description "The selector to search for" + description "The selector to retrieve" end + source_argument + Germinate::TextTransforms.singleton_methods.each do |transform| + option(transform) do + description "Enable/disable the '#{transform}' text transform" + cast :bool + end + end + def run + options = Germinate::TextTransforms.singleton_methods.inject({}) do + |opts, transform| + if params[transform].given? + opts[transform] = params[transform].value + logger.info "Text transform '#{transform}' " + + (params[transform].value ? "enabled" : "disabled") + end + opts + end with_source_file do |source, path| - application = Germinate::Application.new - application.select(source, path, params[:selector].value, $stdout) + @application.select(source, path, params[:selector].value, options) end end end mode :generate do @@ -123,14 +139,122 @@ File.dirname(__FILE__)) stdout.write(File.read(example)) end end + mode :publish do + description "Publish the article using the named publisher" + argument :publisher + source_argument + option 'publish-options' do + arity 1 + end + + def run + with_source_file do |source, path| + options = YAML.load(params['publish-options'].value || "{}") + @application.publish(source, path, params[:publisher].value, options) + end + end + end + + mode :set do + description "Set a named value in the article" + argument :name do + description "Variable name" + cast :string + end + argument :value do + description "Variable value" + cast :string + end + source_argument + + def run + with_source_file do |source, path| + @application.set( + source, + path, + params[:name].value, + params[:value].value) + end + end + end + + def initialize + @command = "#{$PROGRAM_NAME} #{ARGV.join(' ')}" + Germinate.logger = logger + logger.progname = "germinate" + logger.formatter = lambda { |severity, time, progname, message| + lines = case message + when ::String then message.split("\n") + when ::Exception then + Array(message.message) + Array(message.backtrace) + else + message.inspect + end + lines.map{|l| "#{severity} -- #{progname}: #{l}"}.join("\n") + "\n" + } + @application = Germinate::Application.new(stdout, stderr) + @application.load_plugins! + end + + def pre_parse_parameters + end + + def pre_run + logger.level = params['debug'].value ? Logger::DEBUG : Logger::INFO + end + def with_source_file - path = params['source'].value - File.open(path) do |file| - yield file, path + rescue_errors do + path = params['source'].value + File.open(path) do |file| + yield file, path + end end + end + + def rescue_errors + yield + rescue RuntimeError => error + raise if params['debug'].value + log_user_error(error) + rescue Exception => error + raise if params['debug'].value + log_program_error(error) + end + + def log_user_error(error) + log_fatal_error <<-END +Germinate could not complete your command. +Please check your command and article for proper syntax. +#{command_report} +#{error_report(error)} +For more information, re-run the command with the --debug flag. +END + end + + def log_program_error(error) + log_fatal_error <<-END +Germinate encountered an error while executing your command. +#{command_report} +#{error_report(error)} +Please re-run the command with the --debug flag, and file a problem report at +http://github.com/devver/germinate/ +END + end + + def command_report + "The command was: '#{@command}'" + end + + def error_report(error) + "The error was: '#{error.message}'" + end + + def log_fatal_error(error) + logger.fatal(error) end end # EOF