engineyard 1.4.21 → 1.4.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/README.rdoc +19 -0
  2. data/lib/engineyard.rb +3 -0
  3. data/lib/engineyard/cli.rb +1 -1
  4. data/lib/engineyard/model/instance.rb +4 -2
  5. data/lib/engineyard/thor.rb +10 -0
  6. data/lib/engineyard/version.rb +1 -1
  7. data/lib/vendor/thor/LICENSE.md +20 -0
  8. data/lib/vendor/thor/README.md +26 -0
  9. data/lib/vendor/thor/lib/thor.rb +379 -0
  10. data/lib/vendor/thor/lib/thor/actions.rb +318 -0
  11. data/lib/vendor/thor/lib/thor/actions/create_file.rb +105 -0
  12. data/lib/vendor/thor/lib/thor/actions/create_link.rb +57 -0
  13. data/lib/vendor/thor/lib/thor/actions/directory.rb +97 -0
  14. data/lib/vendor/thor/lib/thor/actions/empty_directory.rb +153 -0
  15. data/lib/vendor/thor/lib/thor/actions/file_manipulation.rb +308 -0
  16. data/lib/vendor/thor/lib/thor/actions/inject_into_file.rb +109 -0
  17. data/lib/vendor/thor/lib/thor/base.rb +611 -0
  18. data/lib/vendor/thor/lib/thor/core_ext/file_binary_read.rb +9 -0
  19. data/lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  20. data/lib/vendor/thor/lib/thor/core_ext/ordered_hash.rb +100 -0
  21. data/lib/vendor/thor/lib/thor/error.rb +35 -0
  22. data/lib/vendor/thor/lib/thor/group.rb +285 -0
  23. data/lib/vendor/thor/lib/thor/invocation.rb +170 -0
  24. data/lib/vendor/thor/lib/thor/parser.rb +4 -0
  25. data/lib/vendor/thor/lib/thor/parser/argument.rb +67 -0
  26. data/lib/vendor/thor/lib/thor/parser/arguments.rb +165 -0
  27. data/lib/vendor/thor/lib/thor/parser/option.rb +121 -0
  28. data/lib/vendor/thor/lib/thor/parser/options.rb +181 -0
  29. data/lib/vendor/thor/lib/thor/rake_compat.rb +71 -0
  30. data/lib/vendor/thor/lib/thor/runner.rb +321 -0
  31. data/lib/vendor/thor/lib/thor/shell.rb +88 -0
  32. data/lib/vendor/thor/lib/thor/shell/basic.rb +331 -0
  33. data/lib/vendor/thor/lib/thor/shell/color.rb +108 -0
  34. data/lib/vendor/thor/lib/thor/shell/html.rb +121 -0
  35. data/lib/vendor/thor/lib/thor/task.rb +132 -0
  36. data/lib/vendor/thor/lib/thor/util.rb +248 -0
  37. data/lib/vendor/thor/lib/thor/version.rb +3 -0
  38. metadata +67 -52
@@ -0,0 +1,170 @@
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 task 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_task_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 "task" or
29
+ # "namespace:task"), a Thor::Task, a Class or a Thor instance. If the task
30
+ # 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 task 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 task 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 tasks: "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 task 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 tasks:
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
+ task, args, opts, config = args
104
+
105
+ klass, task = _retrieve_class_and_task(name, task)
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, task, args, opts, config) do |instance|
110
+ instance.parent_options = options
111
+ end
112
+ end
113
+
114
+ # Invoke the given task if the given args.
115
+ def invoke_task(task, *args) #:nodoc:
116
+ current = @_invocations[self.class]
117
+
118
+ unless current.include?(task.name)
119
+ current << task.name
120
+ task.run(self, *args)
121
+ end
122
+ end
123
+
124
+ # Invoke all tasks for the current instance.
125
+ def invoke_all #:nodoc:
126
+ self.class.all_tasks.map { |_, task| invoke_task(task) }
127
+ end
128
+
129
+ # Invokes using shell padding.
130
+ def invoke_with_padding(*args)
131
+ with_padding { invoke(*args) }
132
+ end
133
+
134
+ protected
135
+
136
+ # Configuration values that are shared between invocations.
137
+ def _shared_configuration #:nodoc:
138
+ { :invocations => @_invocations }
139
+ end
140
+
141
+ # This method simply retrieves the class and task to be invoked.
142
+ # If the name is nil or the given name is a task in the current class,
143
+ # use the given name and return self as class. Otherwise, call
144
+ # prepare_for_invocation in the current class.
145
+ def _retrieve_class_and_task(name, sent_task=nil) #:nodoc:
146
+ case
147
+ when name.nil?
148
+ [self.class, nil]
149
+ when self.class.all_tasks[name.to_s]
150
+ [self.class, name.to_s]
151
+ else
152
+ klass, task = self.class.prepare_for_invocation(nil, name)
153
+ [klass, task || sent_task]
154
+ end
155
+ end
156
+
157
+ # Initialize klass using values stored in the @_initializer.
158
+ def _parse_initialization_options(args, opts, config) #:nodoc:
159
+ stored_args, stored_opts, stored_config = @_initializer
160
+
161
+ args ||= stored_args.dup
162
+ opts ||= stored_opts.dup
163
+
164
+ config ||= {}
165
+ config = stored_config.merge(_shared_configuration).merge!(config)
166
+
167
+ [ args, opts, config ]
168
+ end
169
+ end
170
+ 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,67 @@
1
+ class Thor
2
+ class Argument #:nodoc:
3
+ VALID_TYPES = [ :numeric, :hash, :array, :string ]
4
+
5
+ attr_reader :name, :description, :required, :type, :default, :banner
6
+ alias :human_name :name
7
+
8
+ def initialize(name, description=nil, required=true, type=:string, default=nil, banner=nil)
9
+ class_name = self.class.name.split("::").last
10
+
11
+ raise ArgumentError, "#{class_name} name can't be nil." if name.nil?
12
+ raise ArgumentError, "Type :#{type} is not valid for #{class_name.downcase}s." if type && !valid_type?(type)
13
+
14
+ @name = name.to_s
15
+ @description = description
16
+ @required = required || false
17
+ @type = (type || :string).to_sym
18
+ @default = default
19
+ @banner = banner || default_banner
20
+
21
+ validate! # Trigger specific validations
22
+ end
23
+
24
+ def usage
25
+ required? ? banner : "[#{banner}]"
26
+ end
27
+
28
+ def required?
29
+ required
30
+ end
31
+
32
+ def show_default?
33
+ case default
34
+ when Array, String, Hash
35
+ !default.empty?
36
+ else
37
+ default
38
+ end
39
+ end
40
+
41
+ protected
42
+
43
+ def validate!
44
+ raise ArgumentError, "An argument cannot be required and have default value." if required? && !default.nil?
45
+ end
46
+
47
+ def valid_type?(type)
48
+ self.class::VALID_TYPES.include?(type.to_sym)
49
+ end
50
+
51
+ def default_banner
52
+ case type
53
+ when :boolean
54
+ nil
55
+ when :string, :default
56
+ human_name.upcase
57
+ when :numeric
58
+ "N"
59
+ when :hash
60
+ "key:value"
61
+ when :array
62
+ "one two three"
63
+ end
64
+ end
65
+
66
+ end
67
+ end
@@ -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,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, description=nil, required=nil, type=nil, default=nil, banner=nil, lazy_default=nil, group=nil, aliases=nil, hide=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
+ @hide = hide
13
+ end
14
+
15
+ # This parse quick options given as method_options. It makes several
16
+ # assumptions, but you can be more specific using the option method.
17
+ #
18
+ # parse :foo => "bar"
19
+ # #=> Option foo with default value bar
20
+ #
21
+ # parse [:foo, :baz] => "bar"
22
+ # #=> Option foo with default value bar and alias :baz
23
+ #
24
+ # parse :foo => :required
25
+ # #=> Required option foo without default value
26
+ #
27
+ # parse :foo => 2
28
+ # #=> Option foo with default value 2 and type numeric
29
+ #
30
+ # parse :foo => :numeric
31
+ # #=> Option foo without default value and type numeric
32
+ #
33
+ # parse :foo => true
34
+ # #=> Option foo with default value true and type boolean
35
+ #
36
+ # The valid types are :boolean, :numeric, :hash, :array and :string. If none
37
+ # is given a default type is assumed. This default type accepts arguments as
38
+ # string (--foo=value) or booleans (just --foo).
39
+ #
40
+ # By default all options are optional, unless :required is given.
41
+ #
42
+ def self.parse(key, value)
43
+ if key.is_a?(Array)
44
+ name, *aliases = key
45
+ else
46
+ name, aliases = key, []
47
+ end
48
+
49
+ name = name.to_s
50
+ default = value
51
+
52
+ type = case value
53
+ when Symbol
54
+ default = nil
55
+ if VALID_TYPES.include?(value)
56
+ value
57
+ elsif required = (value == :required)
58
+ :string
59
+ end
60
+ when TrueClass, FalseClass
61
+ :boolean
62
+ when Numeric
63
+ :numeric
64
+ when Hash, Array, String
65
+ value.class.name.downcase.to_sym
66
+ end
67
+
68
+ self.new(name.to_s, nil, required, type, default, nil, nil, nil, 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