engineyard 2.0.7 → 2.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/lib/engineyard/cli.rb +3 -0
  2. data/lib/engineyard/thor.rb +1 -0
  3. data/lib/engineyard/version.rb +1 -1
  4. data/lib/vendor/thor/Gemfile +15 -0
  5. data/lib/vendor/thor/LICENSE.md +20 -0
  6. data/lib/vendor/thor/README.md +28 -0
  7. data/lib/vendor/thor/lib/thor.rb +379 -0
  8. data/lib/vendor/thor/lib/thor/actions.rb +318 -0
  9. data/lib/vendor/thor/lib/thor/actions/create_file.rb +105 -0
  10. data/lib/vendor/thor/lib/thor/actions/create_link.rb +57 -0
  11. data/lib/vendor/thor/lib/thor/actions/directory.rb +98 -0
  12. data/lib/vendor/thor/lib/thor/actions/empty_directory.rb +153 -0
  13. data/lib/vendor/thor/lib/thor/actions/file_manipulation.rb +308 -0
  14. data/lib/vendor/thor/lib/thor/actions/inject_into_file.rb +109 -0
  15. data/lib/vendor/thor/lib/thor/base.rb +641 -0
  16. data/lib/vendor/thor/lib/thor/core_ext/dir_escape.rb +0 -0
  17. data/lib/vendor/thor/lib/thor/core_ext/file_binary_read.rb +9 -0
  18. data/lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  19. data/lib/vendor/thor/lib/thor/core_ext/ordered_hash.rb +100 -0
  20. data/lib/vendor/thor/lib/thor/error.rb +35 -0
  21. data/lib/vendor/thor/lib/thor/group.rb +285 -0
  22. data/lib/vendor/thor/lib/thor/invocation.rb +170 -0
  23. data/lib/vendor/thor/lib/thor/parser.rb +4 -0
  24. data/lib/vendor/thor/lib/thor/parser/argument.rb +74 -0
  25. data/lib/vendor/thor/lib/thor/parser/arguments.rb +171 -0
  26. data/lib/vendor/thor/lib/thor/parser/option.rb +121 -0
  27. data/lib/vendor/thor/lib/thor/parser/options.rb +178 -0
  28. data/lib/vendor/thor/lib/thor/rake_compat.rb +71 -0
  29. data/lib/vendor/thor/lib/thor/runner.rb +321 -0
  30. data/lib/vendor/thor/lib/thor/shell.rb +88 -0
  31. data/lib/vendor/thor/lib/thor/shell/basic.rb +389 -0
  32. data/lib/vendor/thor/lib/thor/shell/color.rb +144 -0
  33. data/lib/vendor/thor/lib/thor/shell/html.rb +123 -0
  34. data/lib/vendor/thor/lib/thor/task.rb +132 -0
  35. data/lib/vendor/thor/lib/thor/util.rb +266 -0
  36. data/lib/vendor/thor/lib/thor/version.rb +3 -0
  37. data/lib/vendor/thor/thor.gemspec +26 -0
  38. metadata +38 -21
  39. data/bin/ey_perftools +0 -12
@@ -0,0 +1,144 @@
1
+ require 'thor/shell/basic'
2
+
3
+ class Thor
4
+ module Shell
5
+ # Inherit from Thor::Shell::Basic and add set_color behavior. Check
6
+ # Thor::Shell::Basic to see all available methods.
7
+ #
8
+ class Color < Basic
9
+ # Embed in a String to clear all previous ANSI sequences.
10
+ CLEAR = "\e[0m"
11
+ # The start of an ANSI bold sequence.
12
+ BOLD = "\e[1m"
13
+
14
+ # Set the terminal's foreground ANSI color to black.
15
+ BLACK = "\e[30m"
16
+ # Set the terminal's foreground ANSI color to red.
17
+ RED = "\e[31m"
18
+ # Set the terminal's foreground ANSI color to green.
19
+ GREEN = "\e[32m"
20
+ # Set the terminal's foreground ANSI color to yellow.
21
+ YELLOW = "\e[33m"
22
+ # Set the terminal's foreground ANSI color to blue.
23
+ BLUE = "\e[34m"
24
+ # Set the terminal's foreground ANSI color to magenta.
25
+ MAGENTA = "\e[35m"
26
+ # Set the terminal's foreground ANSI color to cyan.
27
+ CYAN = "\e[36m"
28
+ # Set the terminal's foreground ANSI color to white.
29
+ WHITE = "\e[37m"
30
+
31
+ # Set the terminal's background ANSI color to black.
32
+ ON_BLACK = "\e[40m"
33
+ # Set the terminal's background ANSI color to red.
34
+ ON_RED = "\e[41m"
35
+ # Set the terminal's background ANSI color to green.
36
+ ON_GREEN = "\e[42m"
37
+ # Set the terminal's background ANSI color to yellow.
38
+ ON_YELLOW = "\e[43m"
39
+ # Set the terminal's background ANSI color to blue.
40
+ ON_BLUE = "\e[44m"
41
+ # Set the terminal's background ANSI color to magenta.
42
+ ON_MAGENTA = "\e[45m"
43
+ # Set the terminal's background ANSI color to cyan.
44
+ ON_CYAN = "\e[46m"
45
+ # Set the terminal's background ANSI color to white.
46
+ ON_WHITE = "\e[47m"
47
+
48
+ # Set color by using a string or one of the defined constants. If a third
49
+ # option is set to true, it also adds bold to the string. This is based
50
+ # on Highline implementation and it automatically appends CLEAR to the end
51
+ # of the returned String.
52
+ #
53
+ # Pass foreground, background and bold options to this method as
54
+ # symbols.
55
+ #
56
+ # Example:
57
+ #
58
+ # set_color "Hi!", :red, :on_white, :bold
59
+ #
60
+ # The available colors are:
61
+ #
62
+ # :bold
63
+ # :black
64
+ # :red
65
+ # :green
66
+ # :yellow
67
+ # :blue
68
+ # :magenta
69
+ # :cyan
70
+ # :white
71
+ # :on_black
72
+ # :on_red
73
+ # :on_green
74
+ # :on_yellow
75
+ # :on_blue
76
+ # :on_magenta
77
+ # :on_cyan
78
+ # :on_white
79
+ def set_color(string, *colors)
80
+ if colors.all? { |color| color.is_a?(Symbol) || color.is_a?(String) }
81
+ ansi_colors = colors.map { |color| lookup_color(color) }
82
+ "#{ansi_colors.join}#{string}#{CLEAR}"
83
+ else
84
+ # The old API was `set_color(color, bold=boolean)`. We
85
+ # continue to support the old API because you should never
86
+ # break old APIs unnecessarily :P
87
+ foreground, bold = colors
88
+ foreground = self.class.const_get(foreground.to_s.upcase) if foreground.is_a?(Symbol)
89
+
90
+ bold = bold ? BOLD : ""
91
+ "#{bold}#{foreground}#{string}#{CLEAR}"
92
+ end
93
+ end
94
+
95
+ protected
96
+
97
+ # Overwrite show_diff to show diff with colors if Diff::LCS is
98
+ # available.
99
+ #
100
+ def show_diff(destination, content) #:nodoc:
101
+ if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
102
+ actual = File.binread(destination).to_s.split("\n")
103
+ content = content.to_s.split("\n")
104
+
105
+ Diff::LCS.sdiff(actual, content).each do |diff|
106
+ output_diff_line(diff)
107
+ end
108
+ else
109
+ super
110
+ end
111
+ end
112
+
113
+ def output_diff_line(diff) #:nodoc:
114
+ case diff.action
115
+ when '-'
116
+ say "- #{diff.old_element.chomp}", :red, true
117
+ when '+'
118
+ say "+ #{diff.new_element.chomp}", :green, true
119
+ when '!'
120
+ say "- #{diff.old_element.chomp}", :red, true
121
+ say "+ #{diff.new_element.chomp}", :green, true
122
+ else
123
+ say " #{diff.old_element.chomp}", nil, true
124
+ end
125
+ end
126
+
127
+ # Check if Diff::LCS is loaded. If it is, use it to create pretty output
128
+ # for diff.
129
+ #
130
+ def diff_lcs_loaded? #:nodoc:
131
+ return true if defined?(Diff::LCS)
132
+ return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
133
+
134
+ @diff_lcs_loaded = begin
135
+ require 'diff/lcs'
136
+ true
137
+ rescue LoadError
138
+ false
139
+ end
140
+ end
141
+
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,123 @@
1
+ require 'thor/shell/basic'
2
+
3
+ class Thor
4
+ module Shell
5
+ # Inherit from Thor::Shell::Basic and add set_color behavior. Check
6
+ # Thor::Shell::Basic to see all available methods.
7
+ #
8
+ class HTML < Basic
9
+ # The start of an HTML bold sequence.
10
+ BOLD = "font-weight: bold"
11
+
12
+ # Set the terminal's foreground HTML color to black.
13
+ BLACK = 'color: black'
14
+ # Set the terminal's foreground HTML color to red.
15
+ RED = 'color: red'
16
+ # Set the terminal's foreground HTML color to green.
17
+ GREEN = 'color: green'
18
+ # Set the terminal's foreground HTML color to yellow.
19
+ YELLOW = 'color: yellow'
20
+ # Set the terminal's foreground HTML color to blue.
21
+ BLUE = 'color: blue'
22
+ # Set the terminal's foreground HTML color to magenta.
23
+ MAGENTA = 'color: magenta'
24
+ # Set the terminal's foreground HTML color to cyan.
25
+ CYAN = 'color: cyan'
26
+ # Set the terminal's foreground HTML color to white.
27
+ WHITE = 'color: white'
28
+
29
+ # Set the terminal's background HTML color to black.
30
+ ON_BLACK = 'background-color: black'
31
+ # Set the terminal's background HTML color to red.
32
+ ON_RED = 'background-color: red'
33
+ # Set the terminal's background HTML color to green.
34
+ ON_GREEN = 'background-color: green'
35
+ # Set the terminal's background HTML color to yellow.
36
+ ON_YELLOW = 'background-color: yellow'
37
+ # Set the terminal's background HTML color to blue.
38
+ ON_BLUE = 'background-color: blue'
39
+ # Set the terminal's background HTML color to magenta.
40
+ ON_MAGENTA = 'background-color: magenta'
41
+ # Set the terminal's background HTML color to cyan.
42
+ ON_CYAN = 'background-color: cyan'
43
+ # Set the terminal's background HTML color to white.
44
+ ON_WHITE = 'background-color: white'
45
+
46
+ # Set color by using a string or one of the defined constants. If a third
47
+ # option is set to true, it also adds bold to the string. This is based
48
+ # on Highline implementation and it automatically appends CLEAR to the end
49
+ # of the returned String.
50
+ #
51
+ def set_color(string, *colors)
52
+ if colors.all? { |color| color.is_a?(Symbol) || color.is_a?(String) }
53
+ html_colors = colors.map { |color| lookup_color(color) }
54
+ "<span style=\"#{html_colors.join("; ")};\">#{string}</span>"
55
+ else
56
+ color, bold = colors
57
+ html_color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
58
+ styles = [html_color]
59
+ styles << BOLD if bold
60
+ "<span style=\"#{styles.join("; ")};\">#{string}</span>"
61
+ end
62
+ end
63
+
64
+ # Ask something to the user and receives a response.
65
+ #
66
+ # ==== Example
67
+ # ask("What is your name?")
68
+ #
69
+ # TODO: Implement #ask for Thor::Shell::HTML
70
+ def ask(statement, color=nil)
71
+ raise NotImplementedError, "Implement #ask for Thor::Shell::HTML"
72
+ end
73
+
74
+ protected
75
+
76
+ # Overwrite show_diff to show diff with colors if Diff::LCS is
77
+ # available.
78
+ #
79
+ def show_diff(destination, content) #:nodoc:
80
+ if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
81
+ actual = File.binread(destination).to_s.split("\n")
82
+ content = content.to_s.split("\n")
83
+
84
+ Diff::LCS.sdiff(actual, content).each do |diff|
85
+ output_diff_line(diff)
86
+ end
87
+ else
88
+ super
89
+ end
90
+ end
91
+
92
+ def output_diff_line(diff) #:nodoc:
93
+ case diff.action
94
+ when '-'
95
+ say "- #{diff.old_element.chomp}", :red, true
96
+ when '+'
97
+ say "+ #{diff.new_element.chomp}", :green, true
98
+ when '!'
99
+ say "- #{diff.old_element.chomp}", :red, true
100
+ say "+ #{diff.new_element.chomp}", :green, true
101
+ else
102
+ say " #{diff.old_element.chomp}", nil, true
103
+ end
104
+ end
105
+
106
+ # Check if Diff::LCS is loaded. If it is, use it to create pretty output
107
+ # for diff.
108
+ #
109
+ def diff_lcs_loaded? #:nodoc:
110
+ return true if defined?(Diff::LCS)
111
+ return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
112
+
113
+ @diff_lcs_loaded = begin
114
+ require 'diff/lcs'
115
+ true
116
+ rescue LoadError
117
+ false
118
+ end
119
+ end
120
+
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,132 @@
1
+ class Thor
2
+ class Task < Struct.new(:name, :description, :long_description, :usage, :options)
3
+ FILE_REGEXP = /^#{Regexp.escape(File.dirname(__FILE__))}/
4
+
5
+ def initialize(name, description, long_description, usage, options=nil)
6
+ super(name.to_s, description, long_description, usage, options || {})
7
+ end
8
+
9
+ def initialize_copy(other) #:nodoc:
10
+ super(other)
11
+ self.options = other.options.dup if other.options
12
+ end
13
+
14
+ def hidden?
15
+ false
16
+ end
17
+
18
+ # By default, a task invokes a method in the thor class. You can change this
19
+ # implementation to create custom tasks.
20
+ def run(instance, args=[])
21
+ arity = nil
22
+
23
+ if private_method?(instance)
24
+ instance.class.handle_no_task_error(name)
25
+ elsif public_method?(instance)
26
+ arity = instance.method(name).arity
27
+ instance.send(name, *args)
28
+ elsif local_method?(instance, :method_missing)
29
+ instance.send(:method_missing, name.to_sym, *args)
30
+ else
31
+ instance.class.handle_no_task_error(name)
32
+ end
33
+ rescue ArgumentError => e
34
+ handle_argument_error?(instance, e, caller) ?
35
+ instance.class.handle_argument_error(self, e, arity) : (raise e)
36
+ rescue NoMethodError => e
37
+ handle_no_method_error?(instance, e, caller) ?
38
+ instance.class.handle_no_task_error(name) : (raise e)
39
+ end
40
+
41
+ # Returns the formatted usage by injecting given required arguments
42
+ # and required options into the given usage.
43
+ def formatted_usage(klass, namespace = true, subcommand = false)
44
+ if namespace
45
+ namespace = klass.namespace
46
+ formatted = "#{namespace.gsub(/^(default)/,'')}:"
47
+ end
48
+ formatted = "#{klass.namespace.split(':').last} " if subcommand
49
+
50
+ formatted ||= ""
51
+
52
+ # Add usage with required arguments
53
+ formatted << if klass && !klass.arguments.empty?
54
+ usage.to_s.gsub(/^#{name}/) do |match|
55
+ match << " " << klass.arguments.map{ |a| a.usage }.compact.join(' ')
56
+ end
57
+ else
58
+ usage.to_s
59
+ end
60
+
61
+ # Add required options
62
+ formatted << " #{required_options}"
63
+
64
+ # Strip and go!
65
+ formatted.strip
66
+ end
67
+
68
+ protected
69
+
70
+ def not_debugging?(instance)
71
+ !(instance.class.respond_to?(:debugging) && instance.class.debugging)
72
+ end
73
+
74
+ def required_options
75
+ @required_options ||= options.map{ |_, o| o.usage if o.required? }.compact.sort.join(" ")
76
+ end
77
+
78
+ # Given a target, checks if this class name is a public method.
79
+ def public_method?(instance) #:nodoc:
80
+ !(instance.public_methods & [name.to_s, name.to_sym]).empty?
81
+ end
82
+
83
+ def private_method?(instance)
84
+ !(instance.private_methods & [name.to_s, name.to_sym]).empty?
85
+ end
86
+
87
+ def local_method?(instance, name)
88
+ methods = instance.public_methods(false) + instance.private_methods(false) + instance.protected_methods(false)
89
+ !(methods & [name.to_s, name.to_sym]).empty?
90
+ end
91
+
92
+ def sans_backtrace(backtrace, caller) #:nodoc:
93
+ saned = backtrace.reject { |frame| frame =~ FILE_REGEXP || (frame =~ /\.java:/ && RUBY_PLATFORM =~ /java/) }
94
+ saned -= caller
95
+ end
96
+
97
+ def handle_argument_error?(instance, error, caller)
98
+ not_debugging?(instance) && error.message =~ /wrong number of arguments/ && begin
99
+ saned = sans_backtrace(error.backtrace, caller)
100
+ # Ruby 1.9 always include the called method in the backtrace
101
+ saned.empty? || (saned.size == 1 && RUBY_VERSION >= "1.9")
102
+ end
103
+ end
104
+
105
+ def handle_no_method_error?(instance, error, caller)
106
+ not_debugging?(instance) &&
107
+ error.message =~ /^undefined method `#{name}' for #{Regexp.escape(instance.to_s)}$/
108
+ end
109
+ end
110
+
111
+ # A task that is hidden in help messages but still invocable.
112
+ class HiddenTask < Task
113
+ def hidden?
114
+ true
115
+ end
116
+ end
117
+
118
+ # A dynamic task that handles method missing scenarios.
119
+ class DynamicTask < Task
120
+ def initialize(name, options=nil)
121
+ super(name.to_s, "A dynamically-generated task", name.to_s, name.to_s, options)
122
+ end
123
+
124
+ def run(instance, args=[])
125
+ if (instance.methods & [name.to_s, name.to_sym]).empty?
126
+ super
127
+ else
128
+ instance.class.handle_no_task_error(name)
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,266 @@
1
+ require 'rbconfig'
2
+
3
+ class Thor
4
+ module Sandbox #:nodoc:
5
+ end
6
+
7
+ # This module holds several utilities:
8
+ #
9
+ # 1) Methods to convert thor namespaces to constants and vice-versa.
10
+ #
11
+ # Thor::Util.namespace_from_thor_class(Foo::Bar::Baz) #=> "foo:bar:baz"
12
+ #
13
+ # 2) Loading thor files and sandboxing:
14
+ #
15
+ # Thor::Util.load_thorfile("~/.thor/foo")
16
+ #
17
+ module Util
18
+
19
+ # Receives a namespace and search for it in the Thor::Base subclasses.
20
+ #
21
+ # ==== Parameters
22
+ # namespace<String>:: The namespace to search for.
23
+ #
24
+ def self.find_by_namespace(namespace)
25
+ namespace = "default#{namespace}" if namespace.empty? || namespace =~ /^:/
26
+ Thor::Base.subclasses.find { |klass| klass.namespace == namespace }
27
+ end
28
+
29
+ # Receives a constant and converts it to a Thor namespace. Since Thor tasks
30
+ # can be added to a sandbox, this method is also responsable for removing
31
+ # the sandbox namespace.
32
+ #
33
+ # This method should not be used in general because it's used to deal with
34
+ # older versions of Thor. On current versions, if you need to get the
35
+ # namespace from a class, just call namespace on it.
36
+ #
37
+ # ==== Parameters
38
+ # constant<Object>:: The constant to be converted to the thor path.
39
+ #
40
+ # ==== Returns
41
+ # String:: If we receive Foo::Bar::Baz it returns "foo:bar:baz"
42
+ #
43
+ def self.namespace_from_thor_class(constant)
44
+ constant = constant.to_s.gsub(/^Thor::Sandbox::/, "")
45
+ constant = snake_case(constant).squeeze(":")
46
+ constant
47
+ end
48
+
49
+ # Given the contents, evaluate it inside the sandbox and returns the
50
+ # namespaces defined in the sandbox.
51
+ #
52
+ # ==== Parameters
53
+ # contents<String>
54
+ #
55
+ # ==== Returns
56
+ # Array[Object]
57
+ #
58
+ def self.namespaces_in_content(contents, file=__FILE__)
59
+ old_constants = Thor::Base.subclasses.dup
60
+ Thor::Base.subclasses.clear
61
+
62
+ load_thorfile(file, contents)
63
+
64
+ new_constants = Thor::Base.subclasses.dup
65
+ Thor::Base.subclasses.replace(old_constants)
66
+
67
+ new_constants.map!{ |c| c.namespace }
68
+ new_constants.compact!
69
+ new_constants
70
+ end
71
+
72
+ # Returns the thor classes declared inside the given class.
73
+ #
74
+ def self.thor_classes_in(klass)
75
+ stringfied_constants = klass.constants.map { |c| c.to_s }
76
+ Thor::Base.subclasses.select do |subclass|
77
+ next unless subclass.name
78
+ stringfied_constants.include?(subclass.name.gsub("#{klass.name}::", ''))
79
+ end
80
+ end
81
+
82
+ # Receives a string and convert it to snake case. SnakeCase returns snake_case.
83
+ #
84
+ # ==== Parameters
85
+ # String
86
+ #
87
+ # ==== Returns
88
+ # String
89
+ #
90
+ def self.snake_case(str)
91
+ return str.downcase if str =~ /^[A-Z_]+$/
92
+ str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/
93
+ return $+.downcase
94
+ end
95
+
96
+ # Receives a string and convert it to camel case. camel_case returns CamelCase.
97
+ #
98
+ # ==== Parameters
99
+ # String
100
+ #
101
+ # ==== Returns
102
+ # String
103
+ #
104
+ def self.camel_case(str)
105
+ return str if str !~ /_/ && str =~ /[A-Z]+.*/
106
+ str.split('_').map { |i| i.capitalize }.join
107
+ end
108
+
109
+ # Receives a namespace and tries to retrieve a Thor or Thor::Group class
110
+ # from it. It first searches for a class using the all the given namespace,
111
+ # if it's not found, removes the highest entry and searches for the class
112
+ # again. If found, returns the highest entry as the class name.
113
+ #
114
+ # ==== Examples
115
+ #
116
+ # class Foo::Bar < Thor
117
+ # def baz
118
+ # end
119
+ # end
120
+ #
121
+ # class Baz::Foo < Thor::Group
122
+ # end
123
+ #
124
+ # Thor::Util.namespace_to_thor_class("foo:bar") #=> Foo::Bar, nil # will invoke default task
125
+ # Thor::Util.namespace_to_thor_class("baz:foo") #=> Baz::Foo, nil
126
+ # Thor::Util.namespace_to_thor_class("foo:bar:baz") #=> Foo::Bar, "baz"
127
+ #
128
+ # ==== Parameters
129
+ # namespace<String>
130
+ #
131
+ def self.find_class_and_task_by_namespace(namespace, fallback = true)
132
+ if namespace.include?(?:) # look for a namespaced task
133
+ pieces = namespace.split(":")
134
+ task = pieces.pop
135
+ klass = Thor::Util.find_by_namespace(pieces.join(":"))
136
+ end
137
+ unless klass # look for a Thor::Group with the right name
138
+ klass, task = Thor::Util.find_by_namespace(namespace), nil
139
+ end
140
+ if !klass && fallback # try a task in the default namespace
141
+ task = namespace
142
+ klass = Thor::Util.find_by_namespace('')
143
+ end
144
+ return klass, task
145
+ end
146
+
147
+ # Receives a path and load the thor file in the path. The file is evaluated
148
+ # inside the sandbox to avoid namespacing conflicts.
149
+ #
150
+ def self.load_thorfile(path, content=nil, debug=false)
151
+ content ||= File.binread(path)
152
+
153
+ begin
154
+ Thor::Sandbox.class_eval(content, path)
155
+ rescue Exception => e
156
+ $stderr.puts("WARNING: unable to load thorfile #{path.inspect}: #{e.message}")
157
+ if debug
158
+ $stderr.puts(*e.backtrace)
159
+ else
160
+ $stderr.puts(e.backtrace.first)
161
+ end
162
+ end
163
+ end
164
+
165
+ def self.user_home
166
+ @@user_home ||= if ENV["HOME"]
167
+ ENV["HOME"]
168
+ elsif ENV["USERPROFILE"]
169
+ ENV["USERPROFILE"]
170
+ elsif ENV["HOMEDRIVE"] && ENV["HOMEPATH"]
171
+ File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"])
172
+ elsif ENV["APPDATA"]
173
+ ENV["APPDATA"]
174
+ else
175
+ begin
176
+ File.expand_path("~")
177
+ rescue
178
+ if File::ALT_SEPARATOR
179
+ "C:/"
180
+ else
181
+ "/"
182
+ end
183
+ end
184
+ end
185
+ end
186
+
187
+ # Returns the root where thor files are located, depending on the OS.
188
+ #
189
+ def self.thor_root
190
+ File.join(user_home, ".thor").gsub(/\\/, '/')
191
+ end
192
+
193
+ # Returns the files in the thor root. On Windows thor_root will be something
194
+ # like this:
195
+ #
196
+ # C:\Documents and Settings\james\.thor
197
+ #
198
+ # If we don't #gsub the \ character, Dir.glob will fail.
199
+ #
200
+ def self.thor_root_glob
201
+ files = Dir["#{escape_globs(thor_root)}/*"]
202
+
203
+ files.map! do |file|
204
+ File.directory?(file) ? File.join(file, "main.thor") : file
205
+ end
206
+ end
207
+
208
+ # Where to look for Thor files.
209
+ #
210
+ def self.globs_for(path)
211
+ path = escape_globs(path)
212
+ ["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/*.thor"]
213
+ end
214
+
215
+ # Return the path to the ruby interpreter taking into account multiple
216
+ # installations and windows extensions.
217
+ #
218
+ def self.ruby_command
219
+ @ruby_command ||= begin
220
+ ruby_name = RbConfig::CONFIG['ruby_install_name']
221
+ ruby = File.join(RbConfig::CONFIG['bindir'], ruby_name)
222
+ ruby << RbConfig::CONFIG['EXEEXT']
223
+
224
+ # avoid using different name than ruby (on platforms supporting links)
225
+ if ruby_name != 'ruby' && File.respond_to?(:readlink)
226
+ begin
227
+ alternate_ruby = File.join(RbConfig::CONFIG['bindir'], 'ruby')
228
+ alternate_ruby << RbConfig::CONFIG['EXEEXT']
229
+
230
+ # ruby is a symlink
231
+ if File.symlink? alternate_ruby
232
+ linked_ruby = File.readlink alternate_ruby
233
+
234
+ # symlink points to 'ruby_install_name'
235
+ ruby = alternate_ruby if linked_ruby == ruby_name || linked_ruby == ruby
236
+ end
237
+ rescue NotImplementedError
238
+ # just ignore on windows
239
+ end
240
+ end
241
+
242
+ # escape string in case path to ruby executable contain spaces.
243
+ ruby.sub!(/.*\s.*/m, '"\&"')
244
+ ruby
245
+ end
246
+ end
247
+
248
+ # Returns a string that has had any glob characters escaped.
249
+ # The glob characters are `* ? { } [ ]`.
250
+ #
251
+ # ==== Examples
252
+ #
253
+ # Thor::Util.escape_globs('[apps]') # => '\[apps\]'
254
+ #
255
+ # ==== Parameters
256
+ # String
257
+ #
258
+ # ==== Returns
259
+ # String
260
+ #
261
+ def self.escape_globs(path)
262
+ path.to_s.gsub(/[*?{}\[\]]/, '\\\\\\&')
263
+ end
264
+
265
+ end
266
+ end