#!/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. With Germinate, your source code is also your article text. Special directives tell Germinate which parts to format as article text and where to insert source code excerpts and program output. To get started, execute: germ generate > my_article.rb to have Germinate generate a basic example article. For more information, see the project homepage at http://github.com/devver/germinate/ Development of Germinate is graciously sponsored by Devver, purveyor of fine cloud-based services to busy Ruby developers. If you like this tool please check them out at http://devver.net. END author "Avdi Grimm " version Germinate::VERSION def self.source_argument argument :source do arity 1 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 with_source_file do |source, path| @application.format(source, path) end end end mode :list do 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 with_source_file do |source, path| @application.list(source, path, params[:collection].value) end end end mode :show do description "Show details about various article components" 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 with_source_file do |source, path| @application.show(source, path, params['type'].value, params['item'].value) end end end mode :select do description "Test out a selector" argument :selector do arity 1 required argument_required 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.select(source, path, params[:selector].value, options) end end end mode :generate do description "Generate a sample article" def run example = File.expand_path("../doc/examples/basic.rb", 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 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