Module: Sprockets::PathUtils

### Home Classes Methods

### In Files

- lib/sprockets/path_utils.rb

### Methods

- #absolute_path?
- #atomic_write
- #directory?
- #entries
- #file?
- #find_upwards
- #match_path_extname
- #path_extnames
- #path_parents
- #paths_split
- #relative_path?
- #split_subpath
- #stat
- #stat_directory
- #stat_sorted_tree
- #stat_tree

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::Autoload
- Sprockets::Base
- Sprockets::Bower
- Sprockets::Bundle
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Cache::GetWrapper
- Sprockets::Cache::HashWrapper
- Sprockets::Cache::MemoryStore
- Sprockets::Cache::NullStore
- Sprockets::Cache::ReadWriteWrapper
- Sprockets::Cache::Wrapper
- Sprockets::CachedEnvironment
- Sprockets::ClosureCompressor
- Sprockets::CoffeeScriptProcessor
- Sprockets::CoffeeScriptTemplate
- Sprockets::Compressing
- Sprockets::Configuration
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::ConversionError
- Sprockets::Dependencies
- Sprockets::DigestUtils
- Sprockets::DirectiveProcessor
- Sprockets::ERBProcessor
- Sprockets::ERBTemplate
- Sprockets::EcoProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsProcessor
- Sprockets::EjsTemplate
- Sprockets::EncodingUtils
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::FileReader
- Sprockets::HTTPUtils
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::LegacyProcProcessor
- Sprockets::LegacyTiltProcessor
- Sprockets::Loader
- Sprockets::Manifest
- Sprockets::ManifestUtils
- Sprockets::Mime
- Sprockets::NotFound
- Sprockets::NotImplementedError
- Sprockets::PathDependencyUtils
- Sprockets::PathDigestUtils
- Sprockets::PathUtils
- Sprockets::Paths
- Sprockets::Processing
- Sprockets::ProcessorUtils
- Sprockets::Resolve
- Sprockets::SassCompressor
- Sprockets::SassProcessor
- Sprockets::SassProcessor::CacheStore
- Sprockets::SassProcessor::Functions
- Sprockets::SassTemplate
- Sprockets::ScssProcessor
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::Transformers
- Sprockets::URITar
- Sprockets::URIUtils
- Sprockets::UglifierCompressor
- Sprockets::UnloadedAsset
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::PathUtils

Internal: File and path related utilities. Mixed into Environment.

Probably would be called FileUtils, but that causes namespace annoyances when code actually wants to reference ::FileUtils.

### Constants

SEPARATOR_PATTERN

### Public Instance Methods

absolute_path?(path) click to toggle source

On Windows, ALT_SEPARATOR is \ Delegate to Pathname since the logic gets complex.

    # File lib/sprockets/path_utils.rb, line 76
    def absolute_path?(path)
      Pathname.new(path).absolute?
    end

atomic_write(filename) click to toggle source

Public: Write to a file atomically. Useful for situations where you don't want other processes or threads to see half-written files.

    Utils.atomic_write('important.file') do |file|
      file.write('hello')
    end

Returns nothing.

    # File lib/sprockets/path_utils.rb, line 263
    def atomic_write(filename)
      dirname, basename = File.split(filename)
      basename = [
        basename,
        Thread.current.object_id,
        Process.pid,
        rand(1000000)
      ].join('.')
      tmpname = File.join(dirname, basename)

      File.open(tmpname, 'wb+') do |f|
        yield f
      end

      FileUtils.mv(tmpname, filename)
    ensure
      FileUtils.rm(tmpname) if File.exist?(tmpname)
    end

directory?(path) click to toggle source

Public: Like `File.directory?`.

path - String file path.

Returns true path exists and is a directory.

    # File lib/sprockets/path_utils.rb, line 42
    def directory?(path)
      if stat = self.stat(path)
        stat.directory?
      else
        false
      end
    end

entries(path) click to toggle source

Public: A version of `Dir.entries` that filters out `.` files and `~` swap files.

path - String directory path

Returns an empty `Array` if the directory does not exist.

    # File lib/sprockets/path_utils.rb, line 56
    def entries(path)
      if File.directory?(path)
        Dir.entries(path, :encoding => Encoding.default_internal).reject! { |entry|
          entry =~ /^\.|~$|^\#.*\#$/
        }.sort!
      else
        []
      end
    end

file?(path) click to toggle source

Public: Like `File.file?`.

path - String file path.

Returns true path exists and is a file.

    # File lib/sprockets/path_utils.rb, line 29
    def file?(path)
      if stat = self.stat(path)
        stat.file?
      else
        false
      end
    end

find_upwards(basename, path, root = nil) click to toggle source

Internal: Find target basename checking upwards from path.

basename - String filename: ".sprocketsrc" path - String path to start search: "app/assets/javascripts/app.js" root - String path to stop at (default: system root)

Returns String filename or nil.

    # File lib/sprockets/path_utils.rb, line 188
    def find_upwards(basename, path, root = nil)
      path_parents(path, root).each do |dir|
        filename = File.join(dir, basename)
        return filename if file?(filename)
      end
      nil
    end

match_path_extname(path, extensions) click to toggle source

Internal: Match path extnames against available extensions.

path - String extensions - Hash of String extnames to values

Returns [String extname, Object value] or nil nothing matched.

    # File lib/sprockets/path_utils.rb, line 148
    def match_path_extname(path, extensions)
      match, key = nil, ""
      path_extnames(path).reverse_each do |extname|
        key.prepend(extname)
        if value = extensions[key]
          match = [key.dup, value]
        elsif match
          break
        end
      end
      match
    end

path_extnames(path) click to toggle source

Internal: Get path's extensions.

path - String

Returns an Array of String extnames.

    # File lib/sprockets/path_utils.rb, line 138
    def path_extnames(path)
      File.basename(path).scan(/\.[^.]+/)
    end

path_parents(path, root = nil) click to toggle source

Internal: Returns all parents for path

path - String absolute filename or directory root - String path to stop at (default: system root)

Returns an Array of String paths.

    # File lib/sprockets/path_utils.rb, line 167
    def path_parents(path, root = nil)
      root = "#{root}#{File::SEPARATOR}" if root
      parents = []

      loop do
        parent = File.dirname(path)
        break if parent == path
        break if root && !path.start_with?(root)
        parents << path = parent
      end

      parents
    end

paths_split(paths, filename) click to toggle source

Internal: Detect root path and base for file in a set of paths.

paths - Array of String paths filename - String path of file expected to be in one of the paths.

Returns [String root, String path]

    # File lib/sprockets/path_utils.rb, line 124
    def paths_split(paths, filename)
      paths.each do |path|
        if subpath = split_subpath(path, filename)
          return path, subpath
        end
      end
      nil
    end

relative_path?(path) click to toggle source

Public: Check if path is explicitly relative. Starts with "./" or "../".

path - String path.

Returns true if path is relative, otherwise false.

    # File lib/sprockets/path_utils.rb, line 97
    def relative_path?(path)
      path =~ /^\.\.?($|#{SEPARATOR_PATTERN})/ ? true : false
    end

split_subpath(path, subpath) click to toggle source

Internal: Get relative path for root path and subpath.

path - String path subpath - String subpath of path

Returns relative String path if subpath is a subpath of path, or nil if subpath is outside of path.

    # File lib/sprockets/path_utils.rb, line 108
    def split_subpath(path, subpath)
      return "" if path == subpath
      path = File.join(path, '')
      if subpath.start_with?(path)
        subpath[path.length..-1]
      else
        nil
      end
    end

stat(path) click to toggle source

Public: Like `File.stat`.

path - String file or directory path

Returns nil if the file does not exist.

    # File lib/sprockets/path_utils.rb, line 16
    def stat(path)
      if File.exist?(path)
        File.stat(path.to_s)
      else
        nil
      end
    end

stat_directory(dir) click to toggle source

Public: Stat all the files under a directory.

dir - A String directory

Returns an Enumerator of [path, stat].

    # File lib/sprockets/path_utils.rb, line 201
    def stat_directory(dir)
      return to_enum(__method__, dir) unless block_given?

      self.entries(dir).each do |entry|
        path = File.join(dir, entry)
        if stat = self.stat(path)
          yield path, stat
        end
      end

      nil
    end

stat_sorted_tree(dir, &block) click to toggle source

Public: Recursive stat all the files under a directory in alphabetical order.

dir - A String directory

Returns an Enumerator of [path, stat].

    # File lib/sprockets/path_utils.rb, line 239
    def stat_sorted_tree(dir, &block)
      return to_enum(__method__, dir) unless block_given?

      self.stat_directory(dir).sort_by { |path, stat|
        stat.directory? ? "#{path}/" : path
      }.each do |path, stat|
        yield path, stat

        if stat.directory?
          stat_sorted_tree(path, &block)
        end
      end

      nil
    end

stat_tree(dir, &block) click to toggle source

Public: Recursive stat all the files under a directory.

dir - A String directory

Returns an Enumerator of [path, stat].

    # File lib/sprockets/path_utils.rb, line 219
    def stat_tree(dir, &block)
      return to_enum(__method__, dir) unless block_given?

      self.stat_directory(dir) do |path, stat|
        yield path, stat

        if stat.directory?
          stat_tree(path, &block)
        end
      end

      nil
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.