Class: Thor::Requirement
- Inherits:
-
Object
- Object
- Thor::Requirement
- Defined in:
- lib/thor/requirement.rb
Overview
A declared dependency of one command on another, created by the
requires class macro:
desc "publish", "Publish the built package"
method_option :target, type: :string, required: true
method_option :env, type: :string, default: "development"
requires :build, args: ->(args, ) { [[:target]] }
def publish
# runs after :build has run
end
name is the command to run first: a command in the same class, or a
"namespace:command" string naming a command of another Thor class.
args and options map the current invocation into the required
command; each can be a static Array/Hash or a callable receiving the
current invocation's (args, options). Options declared by both commands
auto-pass by name; an explicit options mapping merges over them.
The class also owns the resolution of requirement names and the walk of the requirement graph used by Thor.validate_requirements!.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.label(klass, command_name) ⇒ Object
The label of a command in requirement error messages.
-
.problems(klass) ⇒ Object
Walks every requirement reachable from the commands of
klass, through other classes too, and returns one message per unresolved name and per cycle. -
.satisfy_all(instance, command, command_args, stack) ⇒ Object
Runs requirements before the command body, unless its arguments are invalid.
Instance Method Summary collapse
-
#initialize(name, args: nil, options: nil) ⇒ Requirement
constructor
A new instance of Requirement.
-
#resolve(klass) ⇒ Object
Returns the target class, command name, and command, if it exists.
-
#satisfy(instance, command, command_args, stack) ⇒ Object
Runs the requirement through
invoke, preserving Thor's once-per-run rule.
Constructor Details
#initialize(name, args: nil, options: nil) ⇒ Requirement
Returns a new instance of Requirement.
82 83 84 85 86 87 88 89 90 |
# File 'lib/thor/requirement.rb', line 82 def initialize(name, args: nil, options: nil) validate_command_name!(name) validate_args_mapping!(args) () @name = name.to_s @args = args @options = end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
80 81 82 |
# File 'lib/thor/requirement.rb', line 80 def name @name end |
Class Method Details
.label(klass, command_name) ⇒ Object
The label of a command in requirement error messages.
40 41 42 |
# File 'lib/thor/requirement.rb', line 40 def label(klass, command_name) #:nodoc: klass.namespace.to_s.empty? ? command_name : "#{klass.namespace}:#{command_name}" end |
.problems(klass) ⇒ Object
Walks every requirement reachable from the commands of klass,
through other classes too, and returns one message per unresolved
name and per cycle. Runs no command.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/thor/requirement.rb', line 47 def problems(klass) #:nodoc: problems = [] finished = [] path = [] visit = lambda do |node| if (index = path.index(node)) chain = (path[index..-1] + [node]).map { |node_klass, name| label(node_klass, name) } problems << "Circular requirement: #{chain.join(' -> ')}" next end next if finished.include?(node) path.push(node) node_klass, command_name = node node_klass.all_commands[command_name].requirements.each do |requirement| target_klass, target_name, target_command = requirement.resolve(node_klass) if target_command visit.call([target_klass, target_name]) else owner = node_klass == klass ? command_name : label(node_klass, command_name) problems << "Requirement #{requirement.name.inspect} of command #{owner.inspect} names a command that does not exist" end end path.pop finished << node end klass.all_commands.each_key { |command_name| visit.call([klass, command_name]) } problems end |
.satisfy_all(instance, command, command_args, stack) ⇒ Object
Runs requirements before the command body, unless its arguments are invalid. The invocation stack is shared for cycle detection.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/thor/requirement.rb', line 26 def satisfy_all(instance, command, command_args, stack) #:nodoc: requirements = command.requirements return if requirements.nil? || requirements.empty? return unless command.accepts_arguments?(instance, command_args) stack.push([instance.class, command.name]) begin requirements.each { |requirement| requirement.satisfy(instance, command, command_args, stack) } ensure stack.pop end end |
Instance Method Details
#resolve(klass) ⇒ Object
Returns the target class, command name, and command, if it exists.
93 94 95 96 97 |
# File 'lib/thor/requirement.rb', line 93 def resolve(klass) #:nodoc: target_klass, command_name = target(klass) command = target_klass && target_klass.all_commands[command_name] [target_klass, command_name, command] end |
#satisfy(instance, command, command_args, stack) ⇒ Object
Runs the requirement through invoke, preserving Thor's once-per-run rule.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/thor/requirement.rb', line 100 def satisfy(instance, command, command_args, stack) #:nodoc: klass, command_name, target = resolve(instance.class) raise undefined_error(instance.class, klass) unless target raise_if_circular_requirement!(klass, command_name, stack) source_option_names = option_keys(instance.class, command).map(&:to_s) target_option_keys = option_keys(klass, target) = instance. instance.invoke( klass, command_name, args_for(command_args.dup, ), (command_args.dup, , source_option_names, target_option_keys) ) rescue RequirementError raise rescue Thor::Error => e = "Requirement #{name.inspect} of command #{command.name.inspect} failed: #{e.}" if stack.length > 1 last = target ? self.class.label(klass, command_name) : name chain = command_labels(stack) + [last] = "#{}\nRequirements: #{chain.join(' -> ')}" end raise RequirementError, end |