Module: Bundler::Thor::Util (Ruby 2.7.3)
Description: Module : Bundler::Thor::Util - Ruby 2.7.3

### Home Classes Methods

### In Files

- bundler/vendor/thor/lib/thor/util.rb

### Methods

- ::camel_case
- ::escape_globs
- ::escape_html
- ::find_by_namespace
- ::find_class_and_command_by_namespace
- ::find_class_and_task_by_namespace
- ::globs_for
- ::load_thorfile
- ::namespace_from_thor_class
- ::namespaces_in_content
- ::ruby_command
- ::snake_case
- ::thor_classes_in
- ::thor_root
- ::thor_root_glob
- ::user_home

### Class/Module Index

Quicksearch

- AmbiguousCommandError
- AmbiguousTaskError
- Bundler
- Bundler::APIResponseMismatchError
- Bundler::BuildMetadata
- Bundler::BundlerError
- Bundler::CLI
- Bundler::CLI::Add
- Bundler::CLI::Binstubs
- Bundler::CLI::Cache
- Bundler::CLI::Check
- Bundler::CLI::Clean
- Bundler::CLI::Common
- Bundler::CLI::Config
- Bundler::CLI::Config::Base
- Bundler::CLI::Console
- Bundler::CLI::Doctor
- Bundler::CLI::Exec
- Bundler::CLI::Gem
- Bundler::CLI::Info
- Bundler::CLI::Init
- Bundler::CLI::Inject
- Bundler::CLI::Install
- Bundler::CLI::Issue
- Bundler::CLI::List
- Bundler::CLI::Lock
- Bundler::CLI::Open
- Bundler::CLI::Outdated
- Bundler::CLI::Package
- Bundler::CLI::Platform
- Bundler::CLI::Plugin
- Bundler::CLI::Pristine
- Bundler::CLI::Remove
- Bundler::CLI::Show
- Bundler::CLI::Update
- Bundler::CLI::Viz
- Bundler::CompactIndexClient
- Bundler::CompactIndexClient::Cache
- Bundler::CompactIndexClient::Error
- Bundler::CompactIndexClient::Updater
- Bundler::CompactIndexClient::Updater::MisMatchedChecksumError
- Bundler::ConnectionPool
- Bundler::ConnectionPool::Error
- Bundler::ConnectionPool::PoolShuttingDownError
- Bundler::ConnectionPool::TimedStack
- Bundler::ConnectionPool::Wrapper
- Bundler::CurrentRuby
- Bundler::CyclicDependencyError
- Bundler::Definition
- Bundler::DepProxy
- Bundler::Dependency
- Bundler::Deployment
- Bundler::Deprecate
- Bundler::DeprecatedError
- Bundler::Dsl
- Bundler::Dsl::DSLError
- Bundler::EndpointSpecification
- Bundler::Env
- Bundler::EnvironmentPreserver
- Bundler::FeatureFlag
- Bundler::Fetcher
- Bundler::Fetcher::AuthenticationRequiredError
- Bundler::Fetcher::BadAuthenticationError
- Bundler::Fetcher::Base
- Bundler::Fetcher::CertificateFailureError
- Bundler::Fetcher::CompactIndex
- Bundler::Fetcher::Dependency
- Bundler::Fetcher::Downloader
- Bundler::Fetcher::FallbackError
- Bundler::Fetcher::Index
- Bundler::Fetcher::NetworkDownError
- Bundler::Fetcher::SSLError
- Bundler::Fetcher::TooManyRequestsError
- Bundler::FileUtils
- Bundler::FileUtils::DryRun
- Bundler::FileUtils::NoWrite
- Bundler::FileUtils::StreamUtils_
- Bundler::FileUtils::Verbose
- Bundler::FriendlyErrors
- Bundler::GemHelper
- Bundler::GemHelpers
- Bundler::GemHelpers::PlatformMatch
- Bundler::GemInstaller
- Bundler::GemNotFound
- Bundler::GemRemoteFetcher
- Bundler::GemRequireError
- Bundler::GemVersionPromoter
- Bundler::Gemdeps
- Bundler::GemfileError
- Bundler::GemfileEvalError
- Bundler::GemfileLockNotFound
- Bundler::GemfileNotFound
- Bundler::GemspecError
- Bundler::GenericSystemCallError
- Bundler::GitError
- Bundler::Graph
- Bundler::Graph::GraphVizClient
- Bundler::HTTPError
- Bundler::Index
- Bundler::Injector
- Bundler::InstallError
- Bundler::InstallHookError
- Bundler::Installer
- Bundler::InvalidOption
- Bundler::LazySpecification
- Bundler::LazySpecification::Identifier
- Bundler::LockfileError
- Bundler::LockfileGenerator
- Bundler::LockfileParser
- Bundler::MarshalError
- Bundler::MatchPlatform
- Bundler::MirrorSockets
- Bundler::Molinillo
- Bundler::Molinillo::CircularDependencyError
- Bundler::Molinillo::Compatibility
- Bundler::Molinillo::Delegates
- Bundler::Molinillo::Delegates::ResolutionState
- Bundler::Molinillo::Delegates::SpecificationProvider
- Bundler::Molinillo::DependencyGraph
- Bundler::Molinillo::DependencyGraph::Action
- Bundler::Molinillo::DependencyGraph::AddEdgeNoCircular
- Bundler::Molinillo::DependencyGraph::DeleteEdge
- Bundler::Molinillo::DependencyGraph::DetachVertexNamed
- Bundler::Molinillo::DependencyGraph::Log
- Bundler::Molinillo::DependencyGraph::Tag
- Bundler::Molinillo::DependencyGraph::Vertex
- Bundler::Molinillo::DependencyState
- Bundler::Molinillo::NoSuchDependencyError
- Bundler::Molinillo::PossibilityState
- Bundler::Molinillo::ResolutionState
- Bundler::Molinillo::Resolver
- Bundler::Molinillo::Resolver::Resolution
- Bundler::Molinillo::Resolver::Resolution::Conflict
- Bundler::Molinillo::Resolver::Resolution::PossibilitySet
- Bundler::Molinillo::Resolver::Resolution::UnwindDetails
- Bundler::Molinillo::ResolverError
- Bundler::Molinillo::SpecificationProvider
- Bundler::Molinillo::UI
- Bundler::Molinillo::VersionConflict
- Bundler::NoSpaceOnDeviceError
- Bundler::OperationNotSupportedError
- Bundler::ParallelInstaller
- Bundler::ParallelInstaller::SpecInstallation
- Bundler::PathError
- Bundler::PermissionError
- Bundler::Persistent
- Bundler::Persistent::Net
- Bundler::Persistent::Net::HTTP
- Bundler::Persistent::Net::HTTP::Persistent
- Bundler::Persistent::Net::HTTP::Persistent::Error
- Bundler::PersistentHTTP
- Bundler::Plugin
- Bundler::Plugin::API
- Bundler::Plugin::API::Source
- Bundler::Plugin::DSL
- Bundler::Plugin::DSL::PluginGemfileError
- Bundler::Plugin::Events
- Bundler::Plugin::Index
- Bundler::Plugin::Index::CommandConflict
- Bundler::Plugin::Index::SourceConflict
- Bundler::Plugin::Installer
- Bundler::Plugin::Installer::Git
- Bundler::Plugin::Installer::Rubygems
- Bundler::Plugin::MalformattedPlugin
- Bundler::Plugin::SourceList
- Bundler::Plugin::UndefinedCommandError
- Bundler::Plugin::UnknownSourceError
- Bundler::PluginError
- Bundler::ProcessLock
- Bundler::ProductionError
- Bundler::RemoteSpecification
- Bundler::Resolver
- Bundler::Resolver::SpecGroup
- Bundler::Retry
- Bundler::RubyDsl
- Bundler::RubyGemsGemInstaller
- Bundler::RubyVersion
- Bundler::RubyVersionMismatch
- Bundler::RubygemsIntegration
- Bundler::RubygemsIntegration::Gem
- Bundler::Runtime
- Bundler::SecurityError
- Bundler::Settings
- Bundler::Settings::Mirror
- Bundler::Settings::MirrorConfig
- Bundler::Settings::Mirrors
- Bundler::Settings::TCPSocketProbe
- Bundler::Settings::Validator
- Bundler::Settings::Validator::Rule
- Bundler::SharedHelpers
- Bundler::SimilarityDetector
- Bundler::SocketAddress
- Bundler::Source
- Bundler::Source::Gemspec
- Bundler::Source::Git
- Bundler::Source::Git::GitCommandError
- Bundler::Source::Git::GitNotAllowedError
- Bundler::Source::Git::GitNotInstalledError
- Bundler::Source::Git::GitProxy
- Bundler::Source::Git::MissingGitRevisionError
- Bundler::Source::Metadata
- Bundler::Source::Path
- Bundler::Source::Path::Installer
- Bundler::Source::Rubygems
- Bundler::Source::Rubygems::Remote
- Bundler::SourceList
- Bundler::SpecSet
- Bundler::Standalone
- Bundler::StubSpecification
- Bundler::SudoNotPermittedError
- Bundler::TemporaryResourceError
- Bundler::Thor::Actions
- Bundler::Thor::Actions::CapturableERB
- Bundler::Thor::Actions::ClassMethods
- Bundler::Thor::Base
- Bundler::Thor::Base::ClassMethods
- Bundler::Thor::Command
- Bundler::Thor::CoreExt
- Bundler::Thor::CoreExt::OrderedHash
- Bundler::Thor::DynamicCommand
- Bundler::Thor::DynamicTask
- Bundler::Thor::Group
- Bundler::Thor::HiddenCommand
- Bundler::Thor::HiddenTask
- Bundler::Thor::Invocation
- Bundler::Thor::Invocation::ClassMethods
- Bundler::Thor::LineEditor
- Bundler::Thor::LineEditor::Basic
- Bundler::Thor::LineEditor::Readline
- Bundler::Thor::LineEditor::Readline::PathCompletion
- Bundler::Thor::NestedContext
- Bundler::Thor::RakeCompat
- Bundler::Thor::Shell
- Bundler::Thor::Shell::Basic
- Bundler::Thor::Shell::Color
- Bundler::Thor::Shell::HTML
- Bundler::Thor::Task
- Bundler::Thor::Util
- Bundler::ThreadCreationError
- Bundler::UI
- Bundler::UI::RGProxy
- Bundler::UI::Shell
- Bundler::UI::Silent
- Bundler::URI
- Bundler::URI::BadURIError
- Bundler::URI::Error
- Bundler::URI::Escape
- Bundler::URI::FTP
- Bundler::URI::File
- Bundler::URI::Generic
- Bundler::URI::HTTP
- Bundler::URI::HTTPS
- Bundler::URI::InvalidComponentError
- Bundler::URI::InvalidURIError
- Bundler::URI::LDAP
- Bundler::URI::LDAPS
- Bundler::URI::MailTo
- Bundler::URI::Parser
- Bundler::URI::REGEXP
- Bundler::URI::RFC2396_Parser
- Bundler::URI::RFC2396_REGEXP
- Bundler::URI::RFC2396_REGEXP::PATTERN
- Bundler::URICredentialsFilter
- Bundler::VersionConflict
- Bundler::VersionRanges
- Bundler::VersionRanges::ReqR
- Bundler::VirtualProtocolError
- Bundler::Worker
- Bundler::Worker::WrappedException
- Bundler::YAMLSerializer
- Bundler::YamlSyntaxError
- Error
- Gem
- Gem::Dependency
- Gem::Platform
- Gem::Specification
- InvocationError
- Kernel
- MalformattedArgumentError
- Object
- RequiredArgumentMissingError
- UndefinedCommandError
- UndefinedCommandError::SpellChecker
- UndefinedTaskError
- UnknownArgumentError
- UnknownArgumentError::SpellChecker

No matching classes.

# Bundler::Thor::Util

This module holds several utilities:

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

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

2) Loading thor files and sandboxing:

    Bundler::Thor::Util.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 bundler/vendor/thor/lib/thor/util.rb, line 104
    def camel_case(str)
      return str if str !~ /_/ && str =~ /[A-Z]+.*/
      str.split("_").map(&:capitalize).join
    end

escape_globs(path) click to toggle source

Returns a string that has had any glob characters escaped. The glob characters are `* ? { } [ ]`.

#### Examples¶ ↑

    Bundler::Thor::Util.escape_globs('[apps]')   # => '\[apps\]'

#### Parameters¶ ↑

String

#### Returns¶ ↑

String

                   # File bundler/vendor/thor/lib/thor/util.rb, line 263
    def escape_globs(path)
      path.to_s.gsub(/[*?{}\[\]]/, '\\\\\\&')
    end

escape_html(string) click to toggle source

Returns a string that has had any HTML characters escaped.

#### Examples¶ ↑

    Bundler::Thor::Util.escape_html('<div>')   # => "&lt;div&gt;"

#### Parameters¶ ↑

String

#### Returns¶ ↑

String

                   # File bundler/vendor/thor/lib/thor/util.rb, line 279
    def escape_html(string)
      CGI.escapeHTML(string)
    end

find_by_namespace(namespace) click to toggle source

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

#### Parameters¶ ↑

namespace<String>

  The namespace to search for.

                   # File bundler/vendor/thor/lib/thor/util.rb, line 24
    def find_by_namespace(namespace)
      namespace = "default#{namespace}" if namespace.empty? || namespace =~ /^:/
      Bundler::Thor::Base.subclasses.detect { |klass| klass.namespace == namespace }
    end

find_class_and_command_by_namespace(namespace, fallback = true) click to toggle source

Receives a namespace and tries to retrieve a Bundler::Thor or `Bundler::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 < Bundler::Thor
      def baz
      end
    end

    class Baz::Foo < Bundler::Thor::Group
    end

    Bundler::Thor::Util.namespace_to_thor_class("foo:bar")     #=> Foo::Bar, nil # will invoke default command
    Bundler::Thor::Util.namespace_to_thor_class("baz:foo")     #=> Baz::Foo, nil
    Bundler::Thor::Util.namespace_to_thor_class("foo:bar:baz") #=> Foo::Bar, "baz"

#### Parameters¶ ↑

namespace<String>

                   # File bundler/vendor/thor/lib/thor/util.rb, line 131
    def find_class_and_command_by_namespace(namespace, fallback = true)
      if namespace.include?(":") # look for a namespaced command
        pieces  = namespace.split(":")
        command = pieces.pop
        klass   = Bundler::Thor::Util.find_by_namespace(pieces.join(":"))
      end
      unless klass # look for a Bundler::Thor::Group with the right name
        klass = Bundler::Thor::Util.find_by_namespace(namespace)
        command = nil
      end
      if !klass && fallback # try a command in the default namespace
        command = namespace
        klass   = Bundler::Thor::Util.find_by_namespace("")
      end
      [klass, command]
    end

Also aliased as: find_class_and_task_by_namespace
find_class_and_task_by_namespace(namespace, fallback = true) click to toggle source
Alias for: find_class_and_command_by_namespace
globs_for(path) click to toggle source

Where to look for Bundler::Thor files.

                   # File bundler/vendor/thor/lib/thor/util.rb, line 212
    def globs_for(path)
      path = escape_globs(path)
      ["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/*.thor"]
    end

load_thorfile(path, content = nil, debug = false) 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 bundler/vendor/thor/lib/thor/util.rb, line 152
    def load_thorfile(path, content = nil, debug = false)
      content ||= File.binread(path)

      begin
        Bundler::Thor::Sandbox.class_eval(content, path)
      rescue StandardError => e
        $stderr.puts("WARNING: unable to load thorfile #{path.inspect}: #{e.message}")
        if debug
          $stderr.puts(*e.backtrace)
        else
          $stderr.puts(e.backtrace.first)
        end
      end
    end

namespace_from_thor_class(constant) click to toggle source

Receives a constant and converts it to a Bundler::Thor namespace. Since Bundler::Thor commands can be added to a sandbox, this method is also responsible for removing the sandbox namespace.

This method should not be used in general because it's used to deal with older versions of Bundler::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”

                   # File bundler/vendor/thor/lib/thor/util.rb, line 43
    def namespace_from_thor_class(constant)
      constant = constant.to_s.gsub(/^Bundler::Thor::Sandbox::/, "")
      constant = snake_case(constant).squeeze(":")
      constant
    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<String>

#### Returns¶ ↑

Array

                   # File bundler/vendor/thor/lib/thor/util.rb, line 58
    def namespaces_in_content(contents, file = __FILE__)
      old_constants = Bundler::Thor::Base.subclasses.dup
      Bundler::Thor::Base.subclasses.clear

      load_thorfile(file, contents)

      new_constants = Bundler::Thor::Base.subclasses.dup
      Bundler::Thor::Base.subclasses.replace(old_constants)

      new_constants.map!(&:namespace)
      new_constants.compact!
      new_constants
    end

ruby_command() click to toggle source

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

                   # File bundler/vendor/thor/lib/thor/util.rb, line 220
    def ruby_command
      @ruby_command ||= begin
        ruby_name = RbConfig::CONFIG["ruby_install_name"]
        ruby = File.join(RbConfig::CONFIG["bindir"], ruby_name)
        ruby << RbConfig::CONFIG["EXEEXT"]

        # avoid using different name than ruby (on platforms supporting links)
        if ruby_name != "ruby" && File.respond_to?(:readlink)
          begin
            alternate_ruby = File.join(RbConfig::CONFIG["bindir"], "ruby")
            alternate_ruby << RbConfig::CONFIG["EXEEXT"]

            # ruby is a symlink
            if File.symlink? alternate_ruby
              linked_ruby = File.readlink alternate_ruby

              # symlink points to 'ruby_install_name'
              ruby = alternate_ruby if linked_ruby == ruby_name || linked_ruby == ruby
            end
          rescue NotImplementedError # rubocop:disable HandleExceptions
            # just ignore on windows
          end
        end

        # escape string in case path to ruby executable contain spaces.
        ruby.sub!(/.*\s.*/m, '"\&"')
        ruby
      end
    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 bundler/vendor/thor/lib/thor/util.rb, line 90
    def snake_case(str)
      return str.downcase if str =~ /^[A-Z_]+$/
      str.gsub(/\B[A-Z]/, '_\&').squeeze("_") =~ /_*(.*)/
      $+.downcase
    end

thor_classes_in(klass) click to toggle source

Returns the thor classes declared inside the given class.

                   # File bundler/vendor/thor/lib/thor/util.rb, line 74
    def thor_classes_in(klass)
      stringfied_constants = klass.constants.map(&:to_s)
      Bundler::Thor::Base.subclasses.select do |subclass|
        next unless subclass.name
        stringfied_constants.include?(subclass.name.gsub("#{klass.name}::", ""))
      end
    end

thor_root() click to toggle source

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

                   # File bundler/vendor/thor/lib/thor/util.rb, line 191
    def thor_root
      File.join(user_home, ".thor").tr('\\', "/")
    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 bundler/vendor/thor/lib/thor/util.rb, line 202
    def thor_root_glob
      files = Dir["#{escape_globs(thor_root)}/*"]

      files.map! do |file|
        File.directory?(file) ? File.join(file, "main.thor") : file
      end
    end

user_home() click to toggle source

                   # File bundler/vendor/thor/lib/thor/util.rb, line 167
    def 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.expand_path("~")
        rescue
          if File::ALT_SEPARATOR
            "C:/"
          else
            "/"
          end
        end
      end
    end