thor_dleavitt 0.18.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/CHANGELOG.md +139 -0
  4. data/LICENSE.md +20 -0
  5. data/README.md +35 -0
  6. data/Thorfile +30 -0
  7. data/bin/thor_dleavitt +6 -0
  8. data/lib/thor/actions.rb +318 -0
  9. data/lib/thor/actions/create_file.rb +105 -0
  10. data/lib/thor/actions/create_link.rb +60 -0
  11. data/lib/thor/actions/directory.rb +119 -0
  12. data/lib/thor/actions/empty_directory.rb +137 -0
  13. data/lib/thor/actions/file_manipulation.rb +317 -0
  14. data/lib/thor/actions/inject_into_file.rb +109 -0
  15. data/lib/thor/base.rb +654 -0
  16. data/lib/thor/command.rb +136 -0
  17. data/lib/thor/core_ext/hash_with_indifferent_access.rb +80 -0
  18. data/lib/thor/core_ext/io_binary_read.rb +12 -0
  19. data/lib/thor/core_ext/ordered_hash.rb +100 -0
  20. data/lib/thor/error.rb +32 -0
  21. data/lib/thor/group.rb +282 -0
  22. data/lib/thor/invocation.rb +172 -0
  23. data/lib/thor/parser.rb +4 -0
  24. data/lib/thor/parser/argument.rb +74 -0
  25. data/lib/thor/parser/arguments.rb +171 -0
  26. data/lib/thor/parser/option.rb +121 -0
  27. data/lib/thor/parser/options.rb +218 -0
  28. data/lib/thor/rake_compat.rb +72 -0
  29. data/lib/thor/runner.rb +322 -0
  30. data/lib/thor/shell.rb +88 -0
  31. data/lib/thor/shell/basic.rb +422 -0
  32. data/lib/thor/shell/color.rb +148 -0
  33. data/lib/thor/shell/html.rb +127 -0
  34. data/lib/thor/util.rb +270 -0
  35. data/lib/thor/version.rb +3 -0
  36. data/lib/thor_dleavitt.rb +473 -0
  37. data/spec/actions/create_file_spec.rb +170 -0
  38. data/spec/actions/create_link_spec.rb +95 -0
  39. data/spec/actions/directory_spec.rb +169 -0
  40. data/spec/actions/empty_directory_spec.rb +129 -0
  41. data/spec/actions/file_manipulation_spec.rb +382 -0
  42. data/spec/actions/inject_into_file_spec.rb +135 -0
  43. data/spec/actions_spec.rb +331 -0
  44. data/spec/base_spec.rb +291 -0
  45. data/spec/command_spec.rb +80 -0
  46. data/spec/core_ext/hash_with_indifferent_access_spec.rb +48 -0
  47. data/spec/core_ext/ordered_hash_spec.rb +115 -0
  48. data/spec/exit_condition_spec.rb +19 -0
  49. data/spec/fixtures/application.rb +2 -0
  50. data/spec/fixtures/app{1}/README +3 -0
  51. data/spec/fixtures/bundle/execute.rb +6 -0
  52. data/spec/fixtures/bundle/main.thor +1 -0
  53. data/spec/fixtures/command.thor +10 -0
  54. data/spec/fixtures/doc/%file_name%.rb.tt +1 -0
  55. data/spec/fixtures/doc/COMMENTER +11 -0
  56. data/spec/fixtures/doc/README +3 -0
  57. data/spec/fixtures/doc/block_helper.rb +3 -0
  58. data/spec/fixtures/doc/config.rb +1 -0
  59. data/spec/fixtures/doc/config.yaml.tt +1 -0
  60. data/spec/fixtures/doc/excluding/%file_name%.rb.tt +1 -0
  61. data/spec/fixtures/enum.thor +10 -0
  62. data/spec/fixtures/group.thor +128 -0
  63. data/spec/fixtures/invoke.thor +118 -0
  64. data/spec/fixtures/path with spaces b/data/spec/fixtures/path with → spaces +0 -0
  65. data/spec/fixtures/preserve/script.sh +3 -0
  66. data/spec/fixtures/script.thor +220 -0
  67. data/spec/fixtures/subcommand.thor +17 -0
  68. data/spec/group_spec.rb +222 -0
  69. data/spec/helper.rb +67 -0
  70. data/spec/invocation_spec.rb +108 -0
  71. data/spec/parser/argument_spec.rb +53 -0
  72. data/spec/parser/arguments_spec.rb +66 -0
  73. data/spec/parser/option_spec.rb +202 -0
  74. data/spec/parser/options_spec.rb +400 -0
  75. data/spec/rake_compat_spec.rb +72 -0
  76. data/spec/register_spec.rb +197 -0
  77. data/spec/runner_spec.rb +241 -0
  78. data/spec/shell/basic_spec.rb +330 -0
  79. data/spec/shell/color_spec.rb +95 -0
  80. data/spec/shell/html_spec.rb +31 -0
  81. data/spec/shell_spec.rb +47 -0
  82. data/spec/subcommand_spec.rb +30 -0
  83. data/spec/thor_spec.rb +499 -0
  84. data/spec/util_spec.rb +196 -0
  85. data/thor.gemspec +24 -0
  86. metadata +191 -0
@@ -0,0 +1,172 @@
1
+ class Thor
2
+ module Invocation
3
+ def self.included(base) #:nodoc:
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ # This method is responsible for receiving a name and find the proper
9
+ # class and command for it. The key is an optional parameter which is
10
+ # available only in class methods invocations (i.e. in Thor::Group).
11
+ def prepare_for_invocation(key, name) #:nodoc:
12
+ case name
13
+ when Symbol, String
14
+ Thor::Util.find_class_and_command_by_namespace(name.to_s, !key)
15
+ else
16
+ name
17
+ end
18
+ end
19
+ end
20
+
21
+ # Make initializer aware of invocations and the initialization args.
22
+ def initialize(args=[], options={}, config={}, &block) #:nodoc:
23
+ @_invocations = config[:invocations] || Hash.new { |h,k| h[k] = [] }
24
+ @_initializer = [ args, options, config ]
25
+ super
26
+ end
27
+
28
+ # Receives a name and invokes it. The name can be a string (either "command" or
29
+ # "namespace:command"), a Thor::Command, a Class or a Thor instance. If the
30
+ # command cannot be guessed by name, it can also be supplied as second argument.
31
+ #
32
+ # You can also supply the arguments, options and configuration values for
33
+ # the command to be invoked, if none is given, the same values used to
34
+ # initialize the invoker are used to initialize the invoked.
35
+ #
36
+ # When no name is given, it will invoke the default command of the current class.
37
+ #
38
+ # ==== Examples
39
+ #
40
+ # class A < Thor
41
+ # def foo
42
+ # invoke :bar
43
+ # invoke "b:hello", ["José"]
44
+ # end
45
+ #
46
+ # def bar
47
+ # invoke "b:hello", ["José"]
48
+ # end
49
+ # end
50
+ #
51
+ # class B < Thor
52
+ # def hello(name)
53
+ # puts "hello #{name}"
54
+ # end
55
+ # end
56
+ #
57
+ # You can notice that the method "foo" above invokes two commands: "bar",
58
+ # which belongs to the same class and "hello" which belongs to the class B.
59
+ #
60
+ # By using an invocation system you ensure that a command is invoked only once.
61
+ # In the example above, invoking "foo" will invoke "b:hello" just once, even
62
+ # if it's invoked later by "bar" method.
63
+ #
64
+ # When class A invokes class B, all arguments used on A initialization are
65
+ # supplied to B. This allows lazy parse of options. Let's suppose you have
66
+ # some rspec commands:
67
+ #
68
+ # class Rspec < Thor::Group
69
+ # class_option :mock_framework, :type => :string, :default => :rr
70
+ #
71
+ # def invoke_mock_framework
72
+ # invoke "rspec:#{options[:mock_framework]}"
73
+ # end
74
+ # end
75
+ #
76
+ # As you noticed, it invokes the given mock framework, which might have its
77
+ # own options:
78
+ #
79
+ # class Rspec::RR < Thor::Group
80
+ # class_option :style, :type => :string, :default => :mock
81
+ # end
82
+ #
83
+ # Since it's not rspec concern to parse mock framework options, when RR
84
+ # is invoked all options are parsed again, so RR can extract only the options
85
+ # that it's going to use.
86
+ #
87
+ # If you want Rspec::RR to be initialized with its own set of options, you
88
+ # have to do that explicitly:
89
+ #
90
+ # invoke "rspec:rr", [], :style => :foo
91
+ #
92
+ # Besides giving an instance, you can also give a class to invoke:
93
+ #
94
+ # invoke Rspec::RR, [], :style => :foo
95
+ #
96
+ def invoke(name=nil, *args)
97
+ if name.nil?
98
+ warn "[Thor] Calling invoke() without argument is deprecated. Please use invoke_all instead.\n#{caller.join("\n")}"
99
+ return invoke_all
100
+ end
101
+
102
+ args.unshift(nil) if Array === args.first || NilClass === args.first
103
+ command, args, opts, config = args
104
+
105
+ klass, command = _retrieve_class_and_command(name, command)
106
+ raise "Expected Thor class, got #{klass}" unless klass <= Thor::Base
107
+
108
+ args, opts, config = _parse_initialization_options(args, opts, config)
109
+ klass.send(:dispatch, command, args, opts, config) do |instance|
110
+ instance.parent_options = options
111
+ end
112
+ end
113
+
114
+ # Invoke the given command if the given args.
115
+ def invoke_command(command, *args) #:nodoc:
116
+ current = @_invocations[self.class]
117
+
118
+ unless current.include?(command.name)
119
+ current << command.name
120
+ command.run(self, *args)
121
+ end
122
+ end
123
+ alias invoke_task invoke_command
124
+
125
+ # Invoke all commands for the current instance.
126
+ def invoke_all #:nodoc:
127
+ self.class.all_commands.map { |_, command| invoke_command(command) }
128
+ end
129
+
130
+ # Invokes using shell padding.
131
+ def invoke_with_padding(*args)
132
+ with_padding { invoke(*args) }
133
+ end
134
+
135
+ protected
136
+
137
+ # Configuration values that are shared between invocations.
138
+ def _shared_configuration #:nodoc:
139
+ { :invocations => @_invocations }
140
+ end
141
+
142
+ # This method simply retrieves the class and command to be invoked.
143
+ # If the name is nil or the given name is a command in the current class,
144
+ # use the given name and return self as class. Otherwise, call
145
+ # prepare_for_invocation in the current class.
146
+ def _retrieve_class_and_command(name, sent_command=nil) #:nodoc:
147
+ case
148
+ when name.nil?
149
+ [self.class, nil]
150
+ when self.class.all_commands[name.to_s]
151
+ [self.class, name.to_s]
152
+ else
153
+ klass, command = self.class.prepare_for_invocation(nil, name)
154
+ [klass, command || sent_command]
155
+ end
156
+ end
157
+ alias _retrieve_class_and_task _retrieve_class_and_command
158
+
159
+ # Initialize klass using values stored in the @_initializer.
160
+ def _parse_initialization_options(args, opts, config) #:nodoc:
161
+ stored_args, stored_opts, stored_config = @_initializer
162
+
163
+ args ||= stored_args.dup
164
+ opts ||= stored_opts.dup
165
+
166
+ config ||= {}
167
+ config = stored_config.merge(_shared_configuration).merge!(config)
168
+
169
+ [ args, opts, config ]
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,4 @@
1
+ require 'thor/parser/argument'
2
+ require 'thor/parser/arguments'
3
+ require 'thor/parser/option'
4
+ require 'thor/parser/options'
@@ -0,0 +1,74 @@
1
+ class Thor
2
+ class Argument #:nodoc:
3
+ VALID_TYPES = [ :numeric, :hash, :array, :string ]
4
+
5
+ attr_reader :name, :description, :enum, :required, :type, :default, :banner
6
+ alias :human_name :name
7
+
8
+ def initialize(name, options={})
9
+ class_name = self.class.name.split("::").last
10
+
11
+ type = options[:type]
12
+
13
+ raise ArgumentError, "#{class_name} name can't be nil." if name.nil?
14
+ raise ArgumentError, "Type :#{type} is not valid for #{class_name.downcase}s." if type && !valid_type?(type)
15
+
16
+ @name = name.to_s
17
+ @description = options[:desc]
18
+ @required = options.key?(:required) ? options[:required] : true
19
+ @type = (type || :string).to_sym
20
+ @default = options[:default]
21
+ @banner = options[:banner] || default_banner
22
+ @enum = options[:enum]
23
+
24
+ validate! # Trigger specific validations
25
+ end
26
+
27
+ def usage
28
+ required? ? banner : "[#{banner}]"
29
+ end
30
+
31
+ def required?
32
+ required
33
+ end
34
+
35
+ def show_default?
36
+ case default
37
+ when Array, String, Hash
38
+ !default.empty?
39
+ else
40
+ default
41
+ end
42
+ end
43
+
44
+ protected
45
+
46
+ def validate!
47
+ if required? && !default.nil?
48
+ raise ArgumentError, "An argument cannot be required and have default value."
49
+ elsif @enum && !@enum.is_a?(Array)
50
+ raise ArgumentError, "An argument cannot have an enum other than an array."
51
+ end
52
+ end
53
+
54
+ def valid_type?(type)
55
+ self.class::VALID_TYPES.include?(type.to_sym)
56
+ end
57
+
58
+ def default_banner
59
+ case type
60
+ when :boolean
61
+ nil
62
+ when :string, :default
63
+ human_name.upcase
64
+ when :numeric
65
+ "N"
66
+ when :hash
67
+ "key:value"
68
+ when :array
69
+ "one two three"
70
+ end
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,171 @@
1
+ class Thor
2
+ class Arguments #:nodoc:
3
+ NUMERIC = /(\d*\.\d+|\d+)/
4
+
5
+ # Receives an array of args and returns two arrays, one with arguments
6
+ # and one with switches.
7
+ #
8
+ def self.split(args)
9
+ arguments = []
10
+
11
+ args.each do |item|
12
+ break if item =~ /^-/
13
+ arguments << item
14
+ end
15
+
16
+ return arguments, args[Range.new(arguments.size, -1)]
17
+ end
18
+
19
+ def self.parse(*args)
20
+ to_parse = args.pop
21
+ new(*args).parse(to_parse)
22
+ end
23
+
24
+ # Takes an array of Thor::Argument objects.
25
+ #
26
+ def initialize(arguments=[])
27
+ @assigns, @non_assigned_required = {}, []
28
+ @switches = arguments
29
+
30
+ arguments.each do |argument|
31
+ if argument.default != nil
32
+ @assigns[argument.human_name] = argument.default
33
+ elsif argument.required?
34
+ @non_assigned_required << argument
35
+ end
36
+ end
37
+ end
38
+
39
+ def parse(args)
40
+ @pile = args.dup
41
+
42
+ @switches.each do |argument|
43
+ break unless peek
44
+ @non_assigned_required.delete(argument)
45
+ @assigns[argument.human_name] = send(:"parse_#{argument.type}", argument.human_name)
46
+ end
47
+
48
+ check_requirement!
49
+ @assigns
50
+ end
51
+
52
+ def remaining
53
+ @pile
54
+ end
55
+
56
+ private
57
+
58
+ def no_or_skip?(arg)
59
+ arg =~ /^--(no|skip)-([-\w]+)$/
60
+ $2
61
+ end
62
+
63
+ def last?
64
+ @pile.empty?
65
+ end
66
+
67
+ def peek
68
+ @pile.first
69
+ end
70
+
71
+ def shift
72
+ @pile.shift
73
+ end
74
+
75
+ def unshift(arg)
76
+ unless arg.kind_of?(Array)
77
+ @pile.unshift(arg)
78
+ else
79
+ @pile = arg + @pile
80
+ end
81
+ end
82
+
83
+ def current_is_value?
84
+ peek && peek.to_s !~ /^-/
85
+ end
86
+
87
+ # Runs through the argument array getting strings that contains ":" and
88
+ # mark it as a hash:
89
+ #
90
+ # [ "name:string", "age:integer" ]
91
+ #
92
+ # Becomes:
93
+ #
94
+ # { "name" => "string", "age" => "integer" }
95
+ #
96
+ def parse_hash(name)
97
+ return shift if peek.is_a?(Hash)
98
+ hash = {}
99
+
100
+ while current_is_value? && peek.include?(?:)
101
+ key, value = shift.split(':',2)
102
+ hash[key] = value
103
+ end
104
+ hash
105
+ end
106
+
107
+ # Runs through the argument array getting all strings until no string is
108
+ # found or a switch is found.
109
+ #
110
+ # ["a", "b", "c"]
111
+ #
112
+ # And returns it as an array:
113
+ #
114
+ # ["a", "b", "c"]
115
+ #
116
+ def parse_array(name)
117
+ return shift if peek.is_a?(Array)
118
+ array = []
119
+
120
+ while current_is_value?
121
+ array << shift
122
+ end
123
+ array
124
+ end
125
+
126
+ # Check if the peek is numeric format and return a Float or Integer.
127
+ # Otherwise raises an error.
128
+ #
129
+ def parse_numeric(name)
130
+ return shift if peek.is_a?(Numeric)
131
+
132
+ unless peek =~ NUMERIC && $& == peek
133
+ raise MalformattedArgumentError, "Expected numeric value for '#{name}'; got #{peek.inspect}"
134
+ end
135
+
136
+ $&.index('.') ? shift.to_f : shift.to_i
137
+ end
138
+
139
+ # Parse string:
140
+ # for --string-arg, just return the current value in the pile
141
+ # for --no-string-arg, nil
142
+ #
143
+ def parse_string(name)
144
+ if no_or_skip?(name)
145
+ nil
146
+ else
147
+ value = shift
148
+ if @switches.is_a?(Hash) && switch = @switches[name]
149
+ if switch.enum && !switch.enum.include?(value)
150
+ raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
151
+ end
152
+ end
153
+ value
154
+ end
155
+ end
156
+
157
+ # Raises an error if @non_assigned_required array is not empty.
158
+ #
159
+ def check_requirement!
160
+ unless @non_assigned_required.empty?
161
+ names = @non_assigned_required.map do |o|
162
+ o.respond_to?(:switch_name) ? o.switch_name : o.human_name
163
+ end.join("', '")
164
+
165
+ class_name = self.class.name.split('::').last.downcase
166
+ raise RequiredArgumentMissingError, "No value provided for required #{class_name} '#{names}'"
167
+ end
168
+ end
169
+
170
+ end
171
+ end
@@ -0,0 +1,121 @@
1
+ class Thor
2
+ class Option < Argument #:nodoc:
3
+ attr_reader :aliases, :group, :lazy_default, :hide
4
+
5
+ VALID_TYPES = [:boolean, :numeric, :hash, :array, :string]
6
+
7
+ def initialize(name, options={})
8
+ options[:required] = false unless options.key?(:required)
9
+ super
10
+ @lazy_default = options[:lazy_default]
11
+ @group = options[:group].to_s.capitalize if options[:group]
12
+ @aliases = Array(options[:aliases])
13
+ @hide = options[:hide]
14
+ end
15
+
16
+ # This parse quick options given as method_options. It makes several
17
+ # assumptions, but you can be more specific using the option method.
18
+ #
19
+ # parse :foo => "bar"
20
+ # #=> Option foo with default value bar
21
+ #
22
+ # parse [:foo, :baz] => "bar"
23
+ # #=> Option foo with default value bar and alias :baz
24
+ #
25
+ # parse :foo => :required
26
+ # #=> Required option foo without default value
27
+ #
28
+ # parse :foo => 2
29
+ # #=> Option foo with default value 2 and type numeric
30
+ #
31
+ # parse :foo => :numeric
32
+ # #=> Option foo without default value and type numeric
33
+ #
34
+ # parse :foo => true
35
+ # #=> Option foo with default value true and type boolean
36
+ #
37
+ # The valid types are :boolean, :numeric, :hash, :array and :string. If none
38
+ # is given a default type is assumed. This default type accepts arguments as
39
+ # string (--foo=value) or booleans (just --foo).
40
+ #
41
+ # By default all options are optional, unless :required is given.
42
+ #
43
+ def self.parse(key, value)
44
+ if key.is_a?(Array)
45
+ name, *aliases = key
46
+ else
47
+ name, aliases = key, []
48
+ end
49
+
50
+ name = name.to_s
51
+ default = value
52
+
53
+ type = case value
54
+ when Symbol
55
+ default = nil
56
+ if VALID_TYPES.include?(value)
57
+ value
58
+ elsif required = (value == :required)
59
+ :string
60
+ end
61
+ when TrueClass, FalseClass
62
+ :boolean
63
+ when Numeric
64
+ :numeric
65
+ when Hash, Array, String
66
+ value.class.name.downcase.to_sym
67
+ end
68
+ self.new(name.to_s, :required => required, :type => type, :default => default, :aliases => aliases)
69
+ end
70
+
71
+ def switch_name
72
+ @switch_name ||= dasherized? ? name : dasherize(name)
73
+ end
74
+
75
+ def human_name
76
+ @human_name ||= dasherized? ? undasherize(name) : name
77
+ end
78
+
79
+ def usage(padding=0)
80
+ sample = if banner && !banner.to_s.empty?
81
+ "#{switch_name}=#{banner}"
82
+ else
83
+ switch_name
84
+ end
85
+
86
+ sample = "[#{sample}]" unless required?
87
+
88
+ if aliases.empty?
89
+ (" " * padding) << sample
90
+ else
91
+ "#{aliases.join(', ')}, #{sample}"
92
+ end
93
+ end
94
+
95
+ VALID_TYPES.each do |type|
96
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
97
+ def #{type}?
98
+ self.type == #{type.inspect}
99
+ end
100
+ RUBY
101
+ end
102
+
103
+ protected
104
+
105
+ def validate!
106
+ raise ArgumentError, "An option cannot be boolean and required." if boolean? && required?
107
+ end
108
+
109
+ def dasherized?
110
+ name.index('-') == 0
111
+ end
112
+
113
+ def undasherize(str)
114
+ str.sub(/^-{1,2}/, '')
115
+ end
116
+
117
+ def dasherize(str)
118
+ (str.length > 1 ? "--" : "-") + str.gsub('_', '-')
119
+ end
120
+ end
121
+ end