Sha256: ae08103861c00224dddc6463eaad8b28559b30cd70b49bf7c69e38bb236e46db

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

require_relative 'libs/shell_helpers'
require_relative '02_a_chainer_options_set'

class RakeCommander::Custom::Chainer < RakeCommander
  include Examples::Libs::ShellHelpers
  TARGET_TASK = 'examples:chained'.freeze

  namespace :examples

  task :chainer
  desc "Uses rake (or raked) to invoke #{TARGET_TASK}"

  # When an option as a default value defined, it is added to `options` result
  # even when the option was not invoked
  options_with_defaults true

  # Loads the otions from a pre-defined options set
  options_use RakeCommander::Custom::ChainerOptionsSet
  # Redefines the description of the option `:chain`
  option_reopen :chain, desc: "Calls: '< rake|raked > #{TARGET_TASK} task'"
  # Adds some option of its own
  str_desc  = "The method used to shell the call to examples:chained."
  str_desc << " Options: #{SHELL_METHODS.join(', ')}"
  option '-m', 'method [METHOD]', default: 'system', desc: str_desc

  def task(*_args)
    if options[:c]
      cmd = "#{subcommand_base} -- #{subcommand_arguments.join(' ')}"
      puts "Calling --> '#{cmd}'"
      shell(cmd, method: options[:m])
    else
      puts "Nothing to do :|"
    end
  end

  def subcommand_base
    with = options[:w] == 'raked' ? 'bin\raked' : 'rake'
    "#{with} #{self.class::TARGET_TASK}"
  end

  def subcommand_arguments
    [].tap do |args|
      if options.key?(:s)
        str_opt  = "--say"
        str_opt << " \"#{options[:s]}\"" if options[:s]
        args << str_opt
      end
      args << "--debug" if options[:b]
    end
  end

  private

  def puts(str)
    return super unless options[:b]
    super "#{app_id}   #{str}   #{thread_id}"
  end

  def app_id
    "(#{self.class.task_fullname}: #{Rake.application.object_id})"
  end

  def thread_id
    "(PID: #{Process.pid} ++ Thread: #{Thread.current.object_id})"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rake-commander-0.2.2 examples/02_a_chainer_example.rb
rake-commander-0.2.0 examples/02_a_chainer_example.rb