In Files

Thor::Util

This module holds several utilities:

1) Methods to convert thor namespaces to constants and vice-versa.

  Thor::Utils.namespace_from_thor_class(Foo::Bar::Baz) #=> "foo:bar:baz"

2) Loading thor files and sandboxing:

  Thor::Utils.load_thorfile("~/.thor/foo")

Public Class Methods

camel_case(str) click to toggle source

Receives a string and convert it to camel case. camel_case returns CamelCase.

Parameters

String

Returns

String

     # File lib/thor/util.rb, line 106
106:     def self.camel_case(str)
107:       return str if str !~ /_/ && str =~ /[A-Z]+.*/
108:       str.split('_').map { |i| i.capitalize }.join
109:     end
convert_constants_to_namespaces(yaml) click to toggle source

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.
     # File lib/thor/util.rb, line 175
175:     def self.convert_constants_to_namespaces(yaml)
176:       yaml_changed = false
177: 
178:       yaml.each do |k, v|
179:         next unless v[:constants] && v[:namespaces].nil?
180:         yaml_changed = true
181:         yaml[k][:namespaces] = v[:constants].map{|c| Thor::Util.namespace_from_thor_class(c)}
182:       end
183: 
184:       yaml_changed
185:     end
find_by_namespace(namespace) click to toggle source

Receives a namespace and search for it in the Thor::Base subclasses.

Parameters

namespace:The namespace to search for.
    # File lib/thor/util.rb, line 24
24:     def self.find_by_namespace(namespace)
25:       namespace = "default#{namespace}" if namespace.empty? || namespace =~ /^:/
26: 
27:       Thor::Base.subclasses.find do |klass|
28:         klass.namespace == namespace
29:       end
30:     end
globs_for(path) click to toggle source

Where to look for Thor files.

     # File lib/thor/util.rb, line 232
232:     def self.globs_for(path)
233:       ["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/*.thor"]
234:     end
load_thorfile(path, content=nil) click to toggle source

Receives a path and load the thor file in the path. The file is evaluated inside the sandbox to avoid namespacing conflicts.

     # File lib/thor/util.rb, line 157
157:     def self.load_thorfile(path, content=nil)
158:       content ||= File.read(path)
159: 
160:       begin
161:         Thor::Sandbox.class_eval(content, path)
162:       rescue Exception => e
163:         $stderr.puts "WARNING: unable to load thorfile #{path.inspect}: #{e.message}"
164:       end
165:     end
namespace_from_thor_class(constant, remove_default=true) click to toggle source

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:The constant to be converted to the thor path.

Returns

String:If we receive Foo::Bar::Baz it returns “foo:bar:baz“
    # File lib/thor/util.rb, line 46
46:     def self.namespace_from_thor_class(constant, remove_default=true)
47:       constant = constant.to_s.gsub(/^Thor::Sandbox::/, "")
48:       constant = snake_case(constant).squeeze(":")
49:       constant.gsub!(/^default/, '') if remove_default
50:       constant
51:     end
namespace_to_thor_class_and_task(namespace, raise_if_nil=true) click to toggle source

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

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.
     # File lib/thor/util.rb, line 139
139:     def self.namespace_to_thor_class_and_task(namespace, raise_if_nil=true)
140:       if namespace.include?(?:)
141:         pieces = namespace.split(":")
142:         task   = pieces.pop
143:         klass  = Thor::Util.find_by_namespace(pieces.join(":"))
144:       end
145: 
146:       unless klass
147:         klass, task = Thor::Util.find_by_namespace(namespace), nil
148:       end
149: 
150:       raise Error, "could not find Thor class or task '#{namespace}'" if raise_if_nil && klass.nil?
151:       return klass, task
152:     end
namespaces_in_content(contents, file=__FILE__) click to toggle source

Given the contents, evaluate it inside the sandbox and returns the namespaces defined in the sandbox.

Parameters

contents

Returns

Array[Object]

    # File lib/thor/util.rb, line 62
62:     def self.namespaces_in_content(contents, file=__FILE__)
63:       old_constants = Thor::Base.subclasses.dup
64:       Thor::Base.subclasses.clear
65: 
66:       load_thorfile(file, contents)
67: 
68:       new_constants = Thor::Base.subclasses.dup
69:       Thor::Base.subclasses.replace(old_constants)
70: 
71:       new_constants.map!{ |c| c.namespace }
72:       new_constants.compact!
73:       new_constants
74:     end
ruby_command() click to toggle source

Return the path to the ruby interpreter taking into account multiple installations and windows extensions.

     # File lib/thor/util.rb, line 239
239:     def self.ruby_command
240:       @ruby_command ||= begin
241:         ruby = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
242:         ruby << Config::CONFIG['EXEEXT']
243: 
244:         # escape string in case path to ruby executable contain spaces.
245:         ruby.sub!(/.*\s.*/m, '"\&"')
246:         ruby
247:       end
248:     end
snake_case(str) click to toggle source

Receives a string and convert it to snake case. SnakeCase returns snake_case.

Parameters

String

Returns

String

    # File lib/thor/util.rb, line 92
92:     def self.snake_case(str)
93:       return str.downcase if str =~ /^[A-Z_]+$/
94:       str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/
95:       return $+.downcase
96:     end
thor_classes_in(klass) click to toggle source

Returns the thor classes declared inside the given class.

    # File lib/thor/util.rb, line 78
78:     def self.thor_classes_in(klass)
79:       Thor::Base.subclasses.select do |subclass|
80:         klass.constants.include?(subclass.name.gsub("#{klass.name}::", ''))
81:       end
82:     end
thor_root() click to toggle source

Returns the root where thor files are located, dependending on the OS.

     # File lib/thor/util.rb, line 211
211:     def self.thor_root
212:       File.join(user_home, ".thor")
213:     end
thor_root_glob() click to toggle source

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.

     # File lib/thor/util.rb, line 222
222:     def self.thor_root_glob
223:       files = Dir["#{thor_root.gsub(/\\/, '/')}/*"]
224: 
225:       files.map! do |file|
226:         File.directory?(file) ? File.join(file, "main.thor") : file
227:       end
228:     end
user_home() click to toggle source

(Not documented)

     # File lib/thor/util.rb, line 187
187:     def self.user_home
188:       @@user_home ||= if ENV["HOME"]
189:         ENV["HOME"]
190:       elsif ENV["USERPROFILE"]
191:         ENV["USERPROFILE"]
192:       elsif ENV["HOMEDRIVE"] && ENV["HOMEPATH"]
193:         File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"])
194:       elsif ENV["APPDATA"]
195:         ENV["APPDATA"]
196:       else
197:         begin
198:           File.expand_path("~")
199:         rescue
200:           if File::ALT_SEPARATOR
201:             "C:/"
202:           else
203:             "/"
204:           end
205:         end
206:       end
207:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.