class Geny::Command

Constants

FILENAME

The filename for a generator, relative to the root

SEPARATOR

Nested commands are joined with colon

TEMPLATES

The directory where templates are stored, relative to the root

Attributes

name[R]

The name of the command @return [String]

registry[R]

The registry that was used to find this command @return [Registry]

root[R]

The root directory for the command @return [String]

Public Class Methods

new(name:, root:, registry:) click to toggle source

Create a new command @param registry [Registry] registry used to find this command @param name [String] name of the command @param root [String] name of the command

# File lib/geny/command.rb, line 31
def initialize(name:, root:, registry:)
  @name = name
  @root = root
  @registry = registry
end

Public Instance Methods

define(&block) click to toggle source

Defines the behavior of a command. The block is evaluated within the context of a DSL.

@example

command = Commmand.new(name: "foo", root: "path/to/root")
command.define do
  parse {}
  invoke {}
end
# File lib/geny/command.rb, line 102
def define(&block)
  @dsl = DSL.new
  @dsl.instance_eval(&block)
  self
end
description() click to toggle source

The description for a command @return [String]

# File lib/geny/command.rb, line 63
def description
  parser.description
end
file() click to toggle source

The path where the command is located @return [String]

# File lib/geny/command.rb, line 39
def file
  File.join(root, FILENAME)
end
helpers() click to toggle source

The command's helper modules @return [Array<Module>]

# File lib/geny/command.rb, line 57
def helpers
  dsl.helpers
end
invoke(**options) click to toggle source

Invoke a command with options @raise [Argy::ValidationError] when required arguments are missing @param options [Hash{Symbol => Object}]

# File lib/geny/command.rb, line 87
def invoke(**options)
  options = parser.default_values.merge(options)
  parser.validate!(options)
  invoke!(options)
end
parse(argv) click to toggle source

Parse command-line options @param argv [Array<String>] @raise [Argy::ParseError] when the arguments are invalid @raise [Argy::CoersionError] when the arguments cannot be coerced @raise [Argy::ValidationError] when required arguments are missing @return [Argy::Options]

# File lib/geny/command.rb, line 73
def parse(argv)
  parser.parse(argv)
end
parser() click to toggle source

The command's option parser @return [Argy::Parser]

# File lib/geny/command.rb, line 51
def parser
  dsl.parser
end
run(argv) click to toggle source

Parse command-line options and run the command @param argv [Array<String>]

# File lib/geny/command.rb, line 79
def run(argv)
  options = parse(argv)
  invoke!(options.to_h)
end
templates_path() click to toggle source

The path where templates are located @return [String]

# File lib/geny/command.rb, line 45
def templates_path
  File.join(root, TEMPLATES)
end

Private Instance Methods

dsl() click to toggle source
# File lib/geny/command.rb, line 124
def dsl
  return @dsl if @dsl

  @dsl = DSL.new
  @dsl.instance_eval File.read(file), file
  @dsl
end
extend_with_queries(options) click to toggle source
# File lib/geny/command.rb, line 119
def extend_with_queries(options)
  queries = options.map { |k, v| [:"#{k}?", !!v] }
  options.merge(queries.to_h)
end
invoke!(options) click to toggle source
# File lib/geny/command.rb, line 110
def invoke!(options)
  context = Context::Invoke.new(
    command: self,
    locals: extend_with_queries(options)
  )

  context.instance_eval(&dsl.invoke)
end