lib/tap/task.rb in bahuvrihi-tap-0.10.4 vs lib/tap/task.rb in bahuvrihi-tap-0.10.5

- old
+ new

@@ -1,8 +1,9 @@ require 'tap/support/batchable' require 'tap/support/executable' -require 'tap/support/command_line' +require 'tap/support/lazydoc/method' +autoload(:OptionParser, 'optparse') module Tap # Tasks are the basic organizational unit of Tap. Tasks provide # a standard backbone for creating the working parts of an application @@ -212,67 +213,51 @@ # subclass.define_configurations(configs) subclass.define_dependencies(dependencies) subclass.define_process(block) if block_given? - - # - # Register documentation - # - const_name = subclass.to_s - caller.each_with_index do |line, index| - case line - when /\/tap\/support\/declarations.rb/ then next - when Support::Lazydoc::CALLER_REGEXP - subclass.source_file = File.expand_path($1) - lzd = subclass.lazydoc(false) - lzd[const_name, false]['manifest'] = lzd.register($3.to_i - 1) - break - end - end - - arity = options[:arity] || (block_given? ? block.arity : -1) - comment = Support::Comment.new - comment.subject = case - when arity > 0 - Array.new(arity, "INPUT").join(' ') - when arity < 0 - array = Array.new(-1 * arity - 1, "INPUT") - array << "INPUTS..." - array.join(' ') - else "" - end - subclass.lazydoc(false)[const_name, false]['args'] ||= comment - - subclass.default_name = const_name.underscore + subclass.default_name = subclass.to_s.underscore subclass end - def instantiate(argv, app=Tap::App.instance) # => instance, argv + # Parses the argv into an instance of self and an array of arguments (implicitly + # to be enqued to the instance and run by app). Yields a help string to the + # block when the argv indicates 'help'. + # + def parse(argv=ARGV, app=Tap::App.instance, &block) # :yields: help_str + parse!(argv.dup, &block) + end + + # Same as parse, but removes switches destructively. + def parse!(argv=ARGV, app=Tap::App.instance) # :yields: help_str opts = OptionParser.new # Add configurations - config = {} + argv_config = {} unless configurations.empty? opts.separator "" opts.separator "configurations:" end - configurations.each do |receiver, key, configuration| - opts.on(*Support::CommandLine.configv(configuration)) do |value| - config[key] = value + configurations.each do |receiver, key, config| + opts.on(*config.to_optparse_argv) do |value| + argv_config[key] = value end end # Add options on_tail, giving priority to configurations opts.separator "" opts.separator "options:" opts.on_tail("-h", "--help", "Print this help") do opts.banner = "#{help}usage: tap run -- #{to_s.underscore} #{args.subject}" - puts opts - exit + if block_given? + yield(opts.to_s) + else + puts opts + exit + end end # Add option for name name = default_name opts.on_tail('--name NAME', /^[^-].*/, 'Specify a name') do |value| @@ -293,30 +278,46 @@ use_args.concat(obj) else use_args << obj end end - + + # parse the argv opts.parse!(argv) + + # build and reconfigure the instance and any associated + # batch objects as specified in the file configurations obj = new({}, name, app) - path_configs = load_config(app.config_filepath(name)) if path_configs.kind_of?(Array) path_configs.each_with_index do |path_config, i| - obj.initialize_batch_obj(path_config, "#{name}_#{i}") unless i == 0 + next if i == 0 + batch_obj = obj.initialize_batch_obj(path_config, "#{name}_#{i}") + batch_obj.reconfigure(argv_config) end path_configs = path_configs[0] end - + obj.reconfigure(path_configs).reconfigure(argv_config) + + # recollect arguments argv = (argv + use_args).collect {|str| str =~ /\A---\s*\n/ ? YAML.load(str) : str } - [obj.reconfigure(path_configs).reconfigure(config), argv] + [obj, argv] end + + def execute(argv=ARGV) + instance, args = parse(ARGV) do |help| + puts help + exit + end + instance.execute(*args) + end + def lazydoc(resolve=true) lazydoc = super(false) - lazydoc.register_method_pattern('args', :process) unless lazydoc.resolved? + lazydoc[self.to_s]['args'] ||= lazydoc.register_method(:process, Support::Lazydoc::Method) super end DEFAULT_HELP_TEMPLATE = %Q{<% manifest = task_class.manifest %> <%= task_class %><%= manifest.subject.to_s.strip.empty? ? '' : ' -- ' %><%= manifest.subject %> @@ -613,7 +614,8 @@ def config_task(name, klass=Tap::Task, &block) configs = config[name] || {} raise ArgumentError, "config '#{name}' is not a hash" unless configs.kind_of?(Hash) klass.new(configs, name, &block) end + end end \ No newline at end of file