Module: Thor::Util
- Defined in:
- lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb
Overview
Class Method Summary collapse
-
.camel_case(str) ⇒ Object
Receives a string and convert it to camel case.
-
.find_by_namespace(namespace) ⇒ Object
Receives a namespace and search for it in the Thor::Base subclasses.
-
.find_class_and_task_by_namespace(namespace, fallback = true) ⇒ Object
Receives a namespace and tries to retrieve a Thor or Thor::Group class from it.
-
.globs_for(path) ⇒ Object
Where to look for Thor files.
-
.load_thorfile(path, content = nil) ⇒ Object
Receives a path and load the thor file in the path.
-
.namespace_from_thor_class(constant) ⇒ Object
Receives a constant and converts it to a Thor namespace.
-
.namespaces_in_content(contents, file = __FILE__) ⇒ Object
Given the contents, evaluate it inside the sandbox and returns the namespaces defined in the sandbox.
-
.ruby_command ⇒ Object
Return the path to the ruby interpreter taking into account multiple installations and windows extensions.
-
.snake_case(str) ⇒ Object
Receives a string and convert it to snake case.
-
.thor_classes_in(klass) ⇒ Object
Returns the thor classes declared inside the given class.
-
.thor_root ⇒ Object
Returns the root where thor files are located, dependending on the OS.
-
.thor_root_glob ⇒ Object
Returns the files in the thor root.
- .user_home ⇒ Object
Class Method Details
.camel_case(str) ⇒ Object
Receives a string and convert it to camel case. camel_case returns CamelCase.
Parameters
String
Returns
String
104 105 106 107 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 104 def self.camel_case(str) return str if str !~ /_/ && str =~ /[A-Z]+.*/ str.split('_').map { |i| i.capitalize }.join end |
.find_by_namespace(namespace) ⇒ Object
Receives a namespace and search for it in the Thor::Base subclasses.
Parameters
- namespace<String>
-
The namespace to search for.
24 25 26 27 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 24 def self.find_by_namespace(namespace) namespace = "default#{namespace}" if namespace.empty? || namespace =~ /^:/ Thor::Base.subclasses.find { |klass| klass.namespace == namespace } end |
.find_class_and_task_by_namespace(namespace, fallback = true) ⇒ Object
Receives a namespace and tries to retrieve a Thor or Thor::Group class from it. It first searches for a class using the all the given namespace, if it’s not found, removes the highest entry and searches for the class again. If found, returns the highest entry as the class name.
Examples
class Foo::Bar < Thor
def baz
end
end
class Baz::Foo < Thor::Group
end
Thor::Util.namespace_to_thor_class("foo:bar") #=> Foo::Bar, nil # will invoke default task
Thor::Util.namespace_to_thor_class("baz:foo") #=> Baz::Foo, nil
Thor::Util.namespace_to_thor_class("foo:bar:baz") #=> Foo::Bar, "baz"
Parameters
namespace<String>
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 131 def self.find_class_and_task_by_namespace(namespace, fallback = true) if namespace.include?(?:) # look for a namespaced task pieces = namespace.split(":") task = pieces.pop klass = Thor::Util.find_by_namespace(pieces.join(":")) end unless klass # look for a Thor::Group with the right name klass, task = Thor::Util.find_by_namespace(namespace), nil end if !klass && fallback # try a task in the default namespace task = namespace klass = Thor::Util.find_by_namespace('') end return klass, task end |
.globs_for(path) ⇒ Object
Where to look for Thor files.
205 206 207 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 205 def self.globs_for(path) ["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/*.thor"] end |
.load_thorfile(path, content = nil) ⇒ Object
Receives a path and load the thor file in the path. The file is evaluated inside the sandbox to avoid namespacing conflicts.
150 151 152 153 154 155 156 157 158 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 150 def self.load_thorfile(path, content=nil) content ||= File.binread(path) begin Thor::Sandbox.class_eval(content, path) rescue Exception => e $stderr.puts "WARNING: unable to load thorfile #{path.inspect}: #{e.}" end end |
.namespace_from_thor_class(constant) ⇒ Object
Receives a constant and converts it to a Thor namespace. Since Thor tasks can be added to a sandbox, this method is also responsable for removing the sandbox namespace.
This method should not be used in general because it’s used to deal with older versions of Thor. On current versions, if you need to get the namespace from a class, just call namespace on it.
Parameters
- constant<Object>
-
The constant to be converted to the thor path.
Returns
- String
-
If we receive Foo::Bar::Baz it returns “foo:bar:baz”
43 44 45 46 47 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 43 def self.namespace_from_thor_class(constant) constant = constant.to_s.gsub(/^Thor::Sandbox::/, "") constant = snake_case(constant).squeeze(":") constant end |
.namespaces_in_content(contents, file = __FILE__) ⇒ Object
Given the contents, evaluate it inside the sandbox and returns the namespaces defined in the sandbox.
Parameters
contents<String>
Returns
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 58 def self.namespaces_in_content(contents, file=__FILE__) old_constants = Thor::Base.subclasses.dup Thor::Base.subclasses.clear load_thorfile(file, contents) new_constants = Thor::Base.subclasses.dup Thor::Base.subclasses.replace(old_constants) new_constants.map!{ |c| c.namespace } new_constants.compact! new_constants end |
.ruby_command ⇒ Object
Return the path to the ruby interpreter taking into account multiple installations and windows extensions.
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 212 def self.ruby_command @ruby_command ||= begin ruby = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']) ruby << Config::CONFIG['EXEEXT'] # escape string in case path to ruby executable contain spaces. ruby.sub!(/.*\s.*/m, '"\&"') ruby end end |
.snake_case(str) ⇒ Object
Receives a string and convert it to snake case. SnakeCase returns snake_case.
Parameters
String
Returns
String
90 91 92 93 94 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 90 def self.snake_case(str) return str.downcase if str =~ /^[A-Z_]+$/ str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/ return $+.downcase end |
.thor_classes_in(klass) ⇒ Object
Returns the thor classes declared inside the given class.
74 75 76 77 78 79 80 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 74 def self.thor_classes_in(klass) stringfied_constants = klass.constants.map { |c| c.to_s } Thor::Base.subclasses.select do |subclass| next unless subclass.name stringfied_constants.include?(subclass.name.gsub("#{klass.name}::", '')) end end |
.thor_root ⇒ Object
Returns the root where thor files are located, dependending on the OS.
184 185 186 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 184 def self.thor_root File.join(user_home, ".thor").gsub(/\\/, '/') end |
.thor_root_glob ⇒ Object
Returns the files in the thor root. On Windows thor_root will be something like this:
C:\Documents and Settings\james\.thor
If we don’t #gsub the \ character, Dir.glob will fail.
195 196 197 198 199 200 201 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 195 def self.thor_root_glob files = Dir["#{thor_root}/*"] files.map! do |file| File.directory?(file) ? File.join(file, "main.thor") : file end end |
.user_home ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb', line 160 def self.user_home @@user_home ||= if ENV["HOME"] ENV["HOME"] elsif ENV["USERPROFILE"] ENV["USERPROFILE"] elsif ENV["HOMEDRIVE"] && ENV["HOMEPATH"] File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"]) elsif ENV["APPDATA"] ENV["APPDATA"] else begin File.("~") rescue if File::ALT_SEPARATOR "C:/" else "/" end end end end |