millisami-thor 0.14.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. data/.autotest +8 -0
  2. data/.gemtest +0 -0
  3. data/.gitignore +9 -0
  4. data/.rspec +1 -0
  5. data/CHANGELOG.rdoc +103 -0
  6. data/Gemfile +11 -0
  7. data/LICENSE.md +20 -0
  8. data/README.md +26 -0
  9. data/Thorfile +13 -0
  10. data/bin/rake2thor +86 -0
  11. data/bin/thor +6 -0
  12. data/lib/thor/actions/create_file.rb +105 -0
  13. data/lib/thor/actions/create_link.rb +57 -0
  14. data/lib/thor/actions/directory.rb +93 -0
  15. data/lib/thor/actions/empty_directory.rb +134 -0
  16. data/lib/thor/actions/file_manipulation.rb +270 -0
  17. data/lib/thor/actions/inject_into_file.rb +109 -0
  18. data/lib/thor/actions.rb +314 -0
  19. data/lib/thor/base.rb +598 -0
  20. data/lib/thor/core_ext/file_binary_read.rb +9 -0
  21. data/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  22. data/lib/thor/core_ext/ordered_hash.rb +100 -0
  23. data/lib/thor/error.rb +30 -0
  24. data/lib/thor/group.rb +276 -0
  25. data/lib/thor/invocation.rb +168 -0
  26. data/lib/thor/parser/argument.rb +67 -0
  27. data/lib/thor/parser/arguments.rb +165 -0
  28. data/lib/thor/parser/option.rb +120 -0
  29. data/lib/thor/parser/options.rb +181 -0
  30. data/lib/thor/parser.rb +4 -0
  31. data/lib/thor/rake_compat.rb +70 -0
  32. data/lib/thor/runner.rb +309 -0
  33. data/lib/thor/shell/basic.rb +302 -0
  34. data/lib/thor/shell/color.rb +108 -0
  35. data/lib/thor/shell/html.rb +121 -0
  36. data/lib/thor/shell.rb +88 -0
  37. data/lib/thor/task.rb +129 -0
  38. data/lib/thor/util.rb +229 -0
  39. data/lib/thor/version.rb +3 -0
  40. data/lib/thor.rb +336 -0
  41. data/spec/actions/create_file_spec.rb +170 -0
  42. data/spec/actions/directory_spec.rb +136 -0
  43. data/spec/actions/empty_directory_spec.rb +98 -0
  44. data/spec/actions/file_manipulation_spec.rb +317 -0
  45. data/spec/actions/inject_into_file_spec.rb +135 -0
  46. data/spec/actions_spec.rb +322 -0
  47. data/spec/base_spec.rb +274 -0
  48. data/spec/core_ext/hash_with_indifferent_access_spec.rb +43 -0
  49. data/spec/core_ext/ordered_hash_spec.rb +115 -0
  50. data/spec/fixtures/application.rb +2 -0
  51. data/spec/fixtures/bundle/execute.rb +6 -0
  52. data/spec/fixtures/bundle/main.thor +1 -0
  53. data/spec/fixtures/doc/%file_name%.rb.tt +1 -0
  54. data/spec/fixtures/doc/README +3 -0
  55. data/spec/fixtures/doc/block_helper.rb +3 -0
  56. data/spec/fixtures/doc/components/.empty_directory +0 -0
  57. data/spec/fixtures/doc/config.rb +1 -0
  58. data/spec/fixtures/doc/config.yaml.tt +1 -0
  59. data/spec/fixtures/group.thor +114 -0
  60. data/spec/fixtures/invoke.thor +112 -0
  61. data/spec/fixtures/path with spaces +0 -0
  62. data/spec/fixtures/script.thor +184 -0
  63. data/spec/fixtures/task.thor +10 -0
  64. data/spec/group_spec.rb +216 -0
  65. data/spec/invocation_spec.rb +100 -0
  66. data/spec/parser/argument_spec.rb +47 -0
  67. data/spec/parser/arguments_spec.rb +65 -0
  68. data/spec/parser/option_spec.rb +202 -0
  69. data/spec/parser/options_spec.rb +329 -0
  70. data/spec/rake_compat_spec.rb +72 -0
  71. data/spec/register_spec.rb +92 -0
  72. data/spec/runner_spec.rb +210 -0
  73. data/spec/shell/basic_spec.rb +223 -0
  74. data/spec/shell/color_spec.rb +41 -0
  75. data/spec/shell/html_spec.rb +27 -0
  76. data/spec/shell_spec.rb +47 -0
  77. data/spec/spec_helper.rb +54 -0
  78. data/spec/task_spec.rb +74 -0
  79. data/spec/thor_spec.rb +362 -0
  80. data/spec/util_spec.rb +163 -0
  81. data/thor.gemspec +25 -0
  82. metadata +241 -0
@@ -0,0 +1,165 @@
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
+ shift
148
+ end
149
+ end
150
+
151
+ # Raises an error if @non_assigned_required array is not empty.
152
+ #
153
+ def check_requirement!
154
+ unless @non_assigned_required.empty?
155
+ names = @non_assigned_required.map do |o|
156
+ o.respond_to?(:switch_name) ? o.switch_name : o.human_name
157
+ end.join("', '")
158
+
159
+ class_name = self.class.name.split('::').last.downcase
160
+ raise RequiredArgumentMissingError, "No value provided for required #{class_name} '#{names}'"
161
+ end
162
+ end
163
+
164
+ end
165
+ end
@@ -0,0 +1,120 @@
1
+ class Thor
2
+ class Option < Argument #:nodoc:
3
+ attr_reader :aliases, :group, :lazy_default
4
+
5
+ VALID_TYPES = [:boolean, :numeric, :hash, :array, :string]
6
+
7
+ def initialize(name, description=nil, required=nil, type=nil, default=nil, banner=nil, lazy_default=nil, group=nil, aliases=nil)
8
+ super(name, description, required, type, default, banner)
9
+ @lazy_default = lazy_default
10
+ @group = group.to_s.capitalize if group
11
+ @aliases = [*aliases].compact
12
+ end
13
+
14
+ # This parse quick options given as method_options. It makes several
15
+ # assumptions, but you can be more specific using the option method.
16
+ #
17
+ # parse :foo => "bar"
18
+ # #=> Option foo with default value bar
19
+ #
20
+ # parse [:foo, :baz] => "bar"
21
+ # #=> Option foo with default value bar and alias :baz
22
+ #
23
+ # parse :foo => :required
24
+ # #=> Required option foo without default value
25
+ #
26
+ # parse :foo => 2
27
+ # #=> Option foo with default value 2 and type numeric
28
+ #
29
+ # parse :foo => :numeric
30
+ # #=> Option foo without default value and type numeric
31
+ #
32
+ # parse :foo => true
33
+ # #=> Option foo with default value true and type boolean
34
+ #
35
+ # The valid types are :boolean, :numeric, :hash, :array and :string. If none
36
+ # is given a default type is assumed. This default type accepts arguments as
37
+ # string (--foo=value) or booleans (just --foo).
38
+ #
39
+ # By default all options are optional, unless :required is given.
40
+ #
41
+ def self.parse(key, value)
42
+ if key.is_a?(Array)
43
+ name, *aliases = key
44
+ else
45
+ name, aliases = key, []
46
+ end
47
+
48
+ name = name.to_s
49
+ default = value
50
+
51
+ type = case value
52
+ when Symbol
53
+ default = nil
54
+ if VALID_TYPES.include?(value)
55
+ value
56
+ elsif required = (value == :required)
57
+ :string
58
+ end
59
+ when TrueClass, FalseClass
60
+ :boolean
61
+ when Numeric
62
+ :numeric
63
+ when Hash, Array, String
64
+ value.class.name.downcase.to_sym
65
+ end
66
+
67
+ self.new(name.to_s, nil, required, type, default, nil, nil, nil, aliases)
68
+ end
69
+
70
+ def switch_name
71
+ @switch_name ||= dasherized? ? name : dasherize(name)
72
+ end
73
+
74
+ def human_name
75
+ @human_name ||= dasherized? ? undasherize(name) : name
76
+ end
77
+
78
+ def usage(padding=0)
79
+ sample = if banner && !banner.to_s.empty?
80
+ "#{switch_name}=#{banner}"
81
+ else
82
+ switch_name
83
+ end
84
+
85
+ sample = "[#{sample}]" unless required?
86
+
87
+ if aliases.empty?
88
+ (" " * padding) << sample
89
+ else
90
+ "#{aliases.join(', ')}, #{sample}"
91
+ end
92
+ end
93
+
94
+ VALID_TYPES.each do |type|
95
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
96
+ def #{type}?
97
+ self.type == #{type.inspect}
98
+ end
99
+ RUBY
100
+ end
101
+
102
+ protected
103
+
104
+ def validate!
105
+ raise ArgumentError, "An option cannot be boolean and required." if boolean? && required?
106
+ end
107
+
108
+ def dasherized?
109
+ name.index('-') == 0
110
+ end
111
+
112
+ def undasherize(str)
113
+ str.sub(/^-{1,2}/, '')
114
+ end
115
+
116
+ def dasherize(str)
117
+ (str.length > 1 ? "--" : "-") + str.gsub('_', '-')
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,181 @@
1
+ class Thor
2
+ # This is a modified version of Daniel Berger's Getopt::Long class, licensed
3
+ # under Ruby's license.
4
+ #
5
+ class Options < Arguments #:nodoc:
6
+ LONG_RE = /^(--\w+(?:-\w+)*)$/
7
+ SHORT_RE = /^(-[a-z])$/i
8
+ EQ_RE = /^(--\w+(?:-\w+)*|-[a-z])=(.*)$/i
9
+ SHORT_SQ_RE = /^-([a-z]{2,})$/i # Allow either -x -v or -xv style for single char args
10
+ SHORT_NUM = /^(-[a-z])#{NUMERIC}$/i
11
+
12
+ # Receives a hash and makes it switches.
13
+ def self.to_switches(options)
14
+ options.map do |key, value|
15
+ case value
16
+ when true
17
+ "--#{key}"
18
+ when Array
19
+ "--#{key} #{value.map{ |v| v.inspect }.join(' ')}"
20
+ when Hash
21
+ "--#{key} #{value.map{ |k,v| "#{k}:#{v}" }.join(' ')}"
22
+ when nil, false
23
+ ""
24
+ else
25
+ "--#{key} #{value.inspect}"
26
+ end
27
+ end.join(" ")
28
+ end
29
+
30
+ # Takes a hash of Thor::Option and a hash with defaults.
31
+ def initialize(hash_options={}, defaults={})
32
+ options = hash_options.values
33
+ super(options)
34
+
35
+ # Add defaults
36
+ defaults.each do |key, value|
37
+ @assigns[key.to_s] = value
38
+ @non_assigned_required.delete(hash_options[key])
39
+ end
40
+
41
+ @shorts, @switches, @extra = {}, {}, []
42
+
43
+ options.each do |option|
44
+ @switches[option.switch_name] = option
45
+
46
+ option.aliases.each do |short|
47
+ @shorts[short.to_s] ||= option.switch_name
48
+ end
49
+ end
50
+ end
51
+
52
+ def remaining
53
+ @extra
54
+ end
55
+
56
+ def parse(args)
57
+ @pile = args.dup
58
+
59
+ while peek
60
+ match, is_switch = current_is_switch?
61
+ shifted = shift
62
+
63
+ if is_switch
64
+ case shifted
65
+ when SHORT_SQ_RE
66
+ unshift($1.split('').map { |f| "-#{f}" })
67
+ next
68
+ when EQ_RE, SHORT_NUM
69
+ unshift($2)
70
+ switch = $1
71
+ when LONG_RE, SHORT_RE
72
+ switch = $1
73
+ end
74
+
75
+ switch = normalize_switch(switch)
76
+ option = switch_option(switch)
77
+ @assigns[option.human_name] = parse_peek(switch, option)
78
+ elsif match
79
+ @extra << shifted
80
+ @extra << shift while peek && peek !~ /^-/
81
+ else
82
+ @extra << shifted
83
+ end
84
+ end
85
+
86
+ check_requirement!
87
+
88
+ assigns = Thor::CoreExt::HashWithIndifferentAccess.new(@assigns)
89
+ assigns.freeze
90
+ assigns
91
+ end
92
+
93
+ def check_unknown!
94
+ # an unknown option starts with - or -- and has no more -'s afterward.
95
+ unknown = @extra.select { |str| str =~ /^--?[^-]*$/ }
96
+ raise UnknownArgumentError, "Unknown switches '#{unknown.join(', ')}'" unless unknown.empty?
97
+ end
98
+
99
+ protected
100
+
101
+ # Returns true if the current value in peek is a registered switch.
102
+ #
103
+ def current_is_switch?
104
+ case peek
105
+ when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM
106
+ [true, switch?($1)]
107
+ when SHORT_SQ_RE
108
+ [true, $1.split('').any? { |f| switch?("-#{f}") }]
109
+ else
110
+ [false, false]
111
+ end
112
+ end
113
+
114
+ def current_is_switch_formatted?
115
+ case peek
116
+ when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM, SHORT_SQ_RE
117
+ true
118
+ else
119
+ false
120
+ end
121
+ end
122
+
123
+ def switch?(arg)
124
+ switch_option(normalize_switch(arg))
125
+ end
126
+
127
+ def switch_option(arg)
128
+ if match = no_or_skip?(arg)
129
+ @switches[arg] || @switches["--#{match}"]
130
+ else
131
+ @switches[arg]
132
+ end
133
+ end
134
+
135
+ # Check if the given argument is actually a shortcut.
136
+ #
137
+ def normalize_switch(arg)
138
+ (@shorts[arg] || arg).tr('_', '-')
139
+ end
140
+
141
+ # Parse boolean values which can be given as --foo=true, --foo or --no-foo.
142
+ #
143
+ def parse_boolean(switch)
144
+ if current_is_value?
145
+ if ["true", "TRUE", "t", "T", true].include?(peek)
146
+ shift
147
+ true
148
+ elsif ["false", "FALSE", "f", "F", false].include?(peek)
149
+ shift
150
+ false
151
+ else
152
+ true
153
+ end
154
+ else
155
+ @switches.key?(switch) || !no_or_skip?(switch)
156
+ end
157
+ end
158
+
159
+ # Parse the value at the peek analyzing if it requires an input or not.
160
+ #
161
+ def parse_peek(switch, option)
162
+ if current_is_switch_formatted? || last?
163
+ if option.boolean?
164
+ # No problem for boolean types
165
+ elsif no_or_skip?(switch)
166
+ return nil # User set value to nil
167
+ elsif option.string? && !option.required?
168
+ # Return the default if there is one, else the human name
169
+ return option.lazy_default || option.default || option.human_name
170
+ elsif option.lazy_default
171
+ return option.lazy_default
172
+ else
173
+ raise MalformattedArgumentError, "No value provided for option '#{switch}'"
174
+ end
175
+ end
176
+
177
+ @non_assigned_required.delete(option)
178
+ send(:"parse_#{option.type}", switch)
179
+ end
180
+ end
181
+ 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,70 @@
1
+ require 'rake'
2
+ require 'rake/dsl_definition'
3
+
4
+ class Thor
5
+ # Adds a compatibility layer to your Thor classes which allows you to use
6
+ # rake package tasks. For example, to use rspec rake tasks, one can do:
7
+ #
8
+ # require 'thor/rake_compat'
9
+ #
10
+ # class Default < Thor
11
+ # include Thor::RakeCompat
12
+ #
13
+ # Spec::Rake::SpecTask.new(:spec) do |t|
14
+ # t.spec_opts = ['--options', "spec/spec.opts"]
15
+ # t.spec_files = FileList['spec/**/*_spec.rb']
16
+ # end
17
+ # end
18
+ #
19
+ module RakeCompat
20
+ include Rake::DSL if defined?(Rake::DSL)
21
+
22
+ def self.rake_classes
23
+ @rake_classes ||= []
24
+ end
25
+
26
+ def self.included(base)
27
+ # Hack. Make rakefile point to invoker, so rdoc task is generated properly.
28
+ rakefile = File.basename(caller[0].match(/(.*):\d+/)[1])
29
+ Rake.application.instance_variable_set(:@rakefile, rakefile)
30
+ self.rake_classes << base
31
+ end
32
+ end
33
+ end
34
+
35
+ # override task on (main), for compatibility with Rake 0.9
36
+ self.instance_eval do
37
+ alias rake_namespace namespace
38
+
39
+ def task(*)
40
+ task = super
41
+
42
+ if klass = Thor::RakeCompat.rake_classes.last
43
+ non_namespaced_name = task.name.split(':').last
44
+
45
+ description = non_namespaced_name
46
+ description << task.arg_names.map{ |n| n.to_s.upcase }.join(' ')
47
+ description.strip!
48
+
49
+ klass.desc description, task.comment || non_namespaced_name
50
+ klass.send :define_method, non_namespaced_name do |*args|
51
+ Rake::Task[task.name.to_sym].invoke(*args)
52
+ end
53
+ end
54
+
55
+ task
56
+ end
57
+
58
+ def namespace(name)
59
+ if klass = Thor::RakeCompat.rake_classes.last
60
+ const_name = Thor::Util.camel_case(name.to_s).to_sym
61
+ klass.const_set(const_name, Class.new(Thor))
62
+ new_klass = klass.const_get(const_name)
63
+ Thor::RakeCompat.rake_classes << new_klass
64
+ end
65
+
66
+ super
67
+ Thor::RakeCompat.rake_classes.pop
68
+ end
69
+ end
70
+