Module: Thor::Util
- Defined in:
- lib/thor/util.rb
Overview
This module holds several utilities:
1) Methods to convert thor namespaces to constants and vice-versa.
Thor::Utils.constant_to_namespace(Foo::Bar::Baz) #=> "foo:bar:baz"
Thor::Utils.namespace_to_constant("foo:bar:baz") #=> Foo::Bar::Baz
2) Loading thor files and sandboxing:
Thor::Utils.load_thorfile("~/.thor/foo")
Class Method Summary collapse
-
.constant_to_namespace(constant, remove_default = true) ⇒ Object
Receives a constant and converts it to a Thor namespace.
-
.convert_constants_to_namespaces(yaml) ⇒ Object
Receives a yaml (hash) and updates all constants entries to namespace.
-
.find_by_namespace(namespace) ⇒ Object
Receives a namespace and search for it in the Thor::Base subclasses.
-
.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_to_thor_class(namespace, raise_if_nil = true) ⇒ Object
Receives a namespace and tries to retrieve a Thor or Thor::Group class from it.
-
.namespaces_in_contents(contents, file = __FILE__) ⇒ Object
Given the contents, evaluate it inside the sandbox and returns the thor classes 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_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.
Class Method Details
.constant_to_namespace(constant, remove_default = true) ⇒ 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”
46 47 48 49 50 51 |
# File 'lib/thor/util.rb', line 46 def self.constant_to_namespace(constant, remove_default=true) constant = constant.to_s.gsub(/^Thor::Sandbox::/, "") constant = snake_case(constant).squeeze(":") constant.gsub!(/^default/, '') if remove_default constant end |
.convert_constants_to_namespaces(yaml) ⇒ Object
Receives a yaml (hash) and updates all constants entries to namespace. This was added to deal with deprecated versions of Thor.
TODO Deprecate this method in the future.
Returns
- TrueClass|FalseClass
-
Returns true if any change to the yaml file was made.
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/thor/util.rb', line 153 def self.convert_constants_to_namespaces(yaml) yaml_changed = false yaml.each do |k, v| next unless v[:constants] && v[:namespaces].nil? yaml_changed = true yaml[k][:namespaces] = v[:constants].map{|c| Thor::Util.constant_to_namespace(c)} end yaml_changed 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 28 29 30 |
# File 'lib/thor/util.rb', line 24 def self.find_by_namespace(namespace) namespace = 'default' if namespace.empty? Thor::Base.subclasses.find do |klass| klass.namespace == namespace end end |
.globs_for(path) ⇒ Object
Where to look for Thor files.
204 205 206 |
# File 'lib/thor/util.rb', line 204 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.
135 136 137 138 139 140 141 142 143 |
# File 'lib/thor/util.rb', line 135 def self.load_thorfile(path, content=nil) content ||= File.read(path) begin Thor::Sandbox.class_eval(content, path) rescue Exception => e $stderr.puts "WARNING: unable to load thorfile #{path.inspect}: #{e.}" end end |
.namespace_to_thor_class(namespace, raise_if_nil = 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>
Errors
- Thor::Error
-
raised if the namespace cannot be found.
- Thor::Error
-
raised if the namespace evals to a class which does not inherit from Thor or Thor::Group.
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/thor/util.rb', line 118 def self.namespace_to_thor_class(namespace, raise_if_nil=true) klass, task_name = Thor::Util.find_by_namespace(namespace), nil if klass.nil? && namespace.include?(?:) namespace = namespace.split(":") task_name = namespace.pop klass = Thor::Util.find_by_namespace(namespace.join(":")) end raise Error, "could not find Thor class or task '#{namespace}'" if raise_if_nil && klass.nil? return klass, task_name end |
.namespaces_in_contents(contents, file = __FILE__) ⇒ Object
Given the contents, evaluate it inside the sandbox and returns the thor classes defined in the sandbox.
Parameters
contents<String>
Returns
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/thor/util.rb', line 62 def self.namespaces_in_contents(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.
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/thor/util.rb', line 211 def self.ruby_command #:nodoc: @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
84 85 86 87 88 |
# File 'lib/thor/util.rb', line 84 def self.snake_case(str) return str.downcase if str =~ /^[A-Z_]+$/ str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/ return $+.downcase end |
.thor_root ⇒ Object
Returns the root where thor files are located, dependending on the OS.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/thor/util.rb', line 167 def self.thor_root return File.join(ENV["HOME"], '.thor') if ENV["HOME"] if ENV["HOMEDRIVE"] && ENV["HOMEPATH"] return File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"], '.thor') end return File.join(ENV["APPDATA"], '.thor') if ENV["APPDATA"] begin File.("~") rescue if File::ALT_SEPARATOR "C:/" else "/" end end 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.
194 195 196 197 198 199 200 |
# File 'lib/thor/util.rb', line 194 def self.thor_root_glob files = Dir["#{thor_root.gsub(/\\/, '/')}/*"] files.map! do |file| File.directory?(file) ? File.join(file, "main.thor") : file end end |