Class: CommandMapper::Gen::Task

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/command_mapper/gen/task.rb

Overview

Defines a command_mapper:gen task which automatically generates a command class file.

require 'command_mapper/gen/task'
CommandMapper::Gen::Task.new('grep','lib/path/to/grep.rb')

$ rake command_mapper:gen

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_name, output, parser: nil) {|task| ... } ⇒ Task

Initializes the task.

Parameters:

  • command_name (String)

    The command name or path to the command.

  • output (String)

    The output file path.

  • parser (:help, :man, nil) (defaults to: nil)

    The optional parser to target.

Yields:

  • (task)

    If a block is given, it will be yielded to before the rake task has been defined.

Yield Parameters:

  • task (Task)

    The newly created task.



50
51
52
53
54
55
56
57
58
# File 'lib/command_mapper/gen/task.rb', line 50

def initialize(command_name,output, parser: nil)
  @command_name = command_name
  @output       = output

  @parser = parser

  yield self if block_given?
  define
end

Instance Attribute Details

#command_nameString

The command name or path to the command.

Returns:

  • (String)


19
20
21
# File 'lib/command_mapper/gen/task.rb', line 19

def command_name
  @command_name
end

#outputString

The output file path.

Returns:

  • (String)


24
25
26
# File 'lib/command_mapper/gen/task.rb', line 24

def output
  @output
end

#parser:help, ...

The parser to invoke.

Returns:

  • (:help, :man, nil)


29
30
31
# File 'lib/command_mapper/gen/task.rb', line 29

def parser
  @parser
end

Instance Method Details

#defineObject

Defines the command_mapper:gen task and output file's task.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/command_mapper/gen/task.rb', line 63

def define
  output_dir = File.dirname(@output)

  directory(output_dir)
  file(@output => output_dir) do
    generate
  end

  desc "Generates the #{@output} file"
  task 'command_mapper:gen' => @output
end

#generateObject

Generates the #output file.



78
79
80
81
82
83
84
85
86
# File 'lib/command_mapper/gen/task.rb', line 78

def generate
  args = ["--output", @output]

  if @parser
    args << '--parser' << @parser.to_s
  end

  sh "command_mapper-gen", *args, @command_name
end