This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
class Pathname
|
||||
|
||||
# Change current working directory of the process
|
||||
# to the given path
|
||||
#
|
||||
# See Dir.chdir
|
||||
#
|
||||
# CREDIT: Ryan Duryea
|
||||
|
||||
def chdir(&block)
|
||||
Dir.chdir(self, &block)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
class Pathname
|
||||
|
||||
# Is a directory path empty?
|
||||
#
|
||||
# Returns [Boolean]
|
||||
def empty?
|
||||
Dir.glob(::File.join(to_s, '*')).empty?
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
class Pathname
|
||||
|
||||
# Pluralized alias for `#exist?` method.
|
||||
alias :exists? :exist?
|
||||
|
||||
end
|
||||
@@ -0,0 +1,57 @@
|
||||
class Pathname
|
||||
|
||||
# Glob pathnames.
|
||||
def glob(match, *opts)
|
||||
flags = glob_flags(opts)
|
||||
Dir.glob(::File.join(self.to_s, match), flags).collect{ |m| self.class.new(m) }
|
||||
end
|
||||
|
||||
# Return the first glob match.
|
||||
#
|
||||
# DEPRECATE: While slightly faster then glob().first, not really worth it
|
||||
# unless this can be rewritten to shortcut on first match (using fnmatch?).
|
||||
# In wich case, is there a better name for this method?
|
||||
def glob_first(match, *opts)
|
||||
flags = glob_flags(opts)
|
||||
file = ::Dir.glob(::File.join(self.to_s, match), flags).first
|
||||
file ? self.class.new(file) : nil
|
||||
end
|
||||
|
||||
# Return globbed matches with pathnames relative to the current pathname.
|
||||
def glob_relative(match, *opts)
|
||||
flags = glob_flags(opts)
|
||||
files = Dir.glob(::File.join(self.to_s, match), flags)
|
||||
files = files.map{ |f| f.sub(self.to_s.chomp('/') + '/', '') }
|
||||
files.collect{ |m| self.class.new(m) }
|
||||
end
|
||||
|
||||
# Does a directory contain a matching entry?
|
||||
# Or if the pathname is a file, same as #fnmatch.
|
||||
#
|
||||
# TODO: Move to own file? Better name?
|
||||
#
|
||||
# Returns [Pathname]
|
||||
|
||||
def include?(pattern,*opts)
|
||||
if directory?
|
||||
glob_first(pattern,*opts)
|
||||
else
|
||||
fnmatch(pattern,*opts)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def glob_flags(opts)
|
||||
flags = 0
|
||||
opts.each do |opt|
|
||||
case opt when Symbol, String
|
||||
flags += ::File.const_get("FNM_#{opt}".upcase)
|
||||
else
|
||||
flags += opt
|
||||
end
|
||||
end
|
||||
flags
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,12 @@
|
||||
class Pathname
|
||||
|
||||
# Home constant for building paths from root directory onward.
|
||||
#
|
||||
# TODO: Pathname#home needs to be more robust.
|
||||
#
|
||||
# Returns [Pathname]
|
||||
def self.home
|
||||
Pathname.new('~')
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
class Pathname
|
||||
|
||||
# Platform dependent null device.
|
||||
#
|
||||
# CREDIT: Daniel Burger
|
||||
|
||||
def self.null
|
||||
case RUBY_PLATFORM
|
||||
when /mswin/i
|
||||
'NUL'
|
||||
when /amiga/i
|
||||
'NIL:'
|
||||
when /openvms/i
|
||||
'NL:'
|
||||
else
|
||||
'/dev/null'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
class Pathname
|
||||
|
||||
# Start a path. Another alias for #new.
|
||||
#
|
||||
# Pathname / 'usr'
|
||||
#
|
||||
def self./(path)
|
||||
new(path)
|
||||
end
|
||||
|
||||
# Try to get this into standard Pathname class.
|
||||
alias_method :/, :+
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
class Pathname
|
||||
|
||||
# Alternate to Pathname#new.
|
||||
#
|
||||
# Pathname['/usr/share']
|
||||
#
|
||||
# Returns [Pathname]
|
||||
|
||||
def self.[](path)
|
||||
new(path)
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
require 'facets/fileutils/outofdate'
|
||||
|
||||
class Pathname
|
||||
|
||||
# Is a path out of date relative a set of source files.
|
||||
#
|
||||
# Returns [Boolean]
|
||||
|
||||
def outofdate?(*sources)
|
||||
::FileUtils.outofdate?(to_s, sources.flatten)
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
class Pathname
|
||||
# Reads the first line of the file
|
||||
#
|
||||
# Captures the best practice from this post at stack overflow:
|
||||
# https://stackoverflow.com/questions/1490138/reading-the-first-line-of-a-file-in-ruby
|
||||
#
|
||||
# Credit: Ryan Duryea
|
||||
def readline(*args)
|
||||
open { |f| f.readline(*args) }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
class Pathname
|
||||
|
||||
# Root constant for building paths from root directory onward.
|
||||
#
|
||||
# Returns [Pathname]
|
||||
def self.root
|
||||
Pathname.new('/')
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
require 'facets/file/rootname'
|
||||
|
||||
class Pathname
|
||||
|
||||
#
|
||||
def rootname
|
||||
self.class.new(File.rootname(to_s))
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
require 'facets/filetest/safe'
|
||||
|
||||
class Pathname
|
||||
|
||||
# Is a path reasonably safe?
|
||||
#
|
||||
# Do not mistake this for a perfect solution!
|
||||
#
|
||||
# Returns [Boolean]
|
||||
|
||||
def safe?
|
||||
FileTest.safe?(to_s)
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,9 @@
|
||||
class Pathname
|
||||
|
||||
#
|
||||
def split_root
|
||||
head, tail = *::File.split_root(to_s)
|
||||
[self.class.new(head), self.class.new(tail)]
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,28 @@
|
||||
# Unfortunately NilClass#to_path causes FileUtils to bomb when testing
|
||||
# for symlinks. See issue #77. It would be better if Ruby could be changed
|
||||
# so this would work. But until then we have no choice but to deprecate
|
||||
# this method.
|
||||
|
||||
#class NilClass
|
||||
# # Provide platform dependent null path.
|
||||
# #
|
||||
# # @standard
|
||||
# # require 'facets/pathname'
|
||||
# #
|
||||
# # @author Daniel Burger
|
||||
# def to_path
|
||||
# Pathname.null
|
||||
# end
|
||||
#end
|
||||
|
||||
class Array
|
||||
# Convert array to Pathname instance.
|
||||
#
|
||||
# @standard
|
||||
# require 'facets/pathname'
|
||||
#
|
||||
def to_path
|
||||
Pathname.new(join('/'))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
class Pathname
|
||||
|
||||
# Alias #to_s to #to_str when #to_str is not defined.
|
||||
#
|
||||
# Returns [String]
|
||||
alias_method(:to_str, :to_s) unless method_defined?(:to_str)
|
||||
|
||||
end
|
||||
@@ -0,0 +1,11 @@
|
||||
class Pathname
|
||||
|
||||
# Is a path up to date relative to a set of source files?
|
||||
#
|
||||
# Returns [Boolean]
|
||||
|
||||
def uptodate?(*sources)
|
||||
::FileUtils.uptodate?(to_s, sources.flatten)
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,38 @@
|
||||
class Pathname
|
||||
|
||||
# Recursively visit a directory located by its path, yielding each resource
|
||||
# as its full matching pathname object. If called on a file, yield the file.
|
||||
#
|
||||
# call-seq:
|
||||
# visit => yield each file
|
||||
# visit(all: true) => yield visited directories as well
|
||||
# visit(hidden: true) => yield hidden files and directories as well
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# # Locate any file but *.haml within app/**/*
|
||||
# Pathname.new("app").visit do |f|
|
||||
# next unless f.to_s =~ /\.haml$/
|
||||
# f
|
||||
# end
|
||||
#
|
||||
# TODO: Use #map instead of #each?
|
||||
#
|
||||
# CREDIT: Jean-Denis Vauguet
|
||||
|
||||
def visit(options = {:all => false, :hidden => false})
|
||||
if self.directory?
|
||||
children.each do |entry|
|
||||
next if entry.basename.to_s[0] == "." && !options[:hidden]
|
||||
yield(entry) unless entry.directory? && !options[:all]
|
||||
##entry.visit(:all => options[:all]) { |sub_entry| yield sub_entry } if entry.directory?
|
||||
entry.visit(:all => options[:all], :hidden => options[:hidden]) do |sub_entry|
|
||||
yield(sub_entry)
|
||||
end if entry.directory?
|
||||
end
|
||||
else
|
||||
yield self
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
class Pathname
|
||||
|
||||
# Work constant for building paths from root directory onward.
|
||||
#
|
||||
# Returns [Pathname]
|
||||
def self.work
|
||||
Pathname.new('.')
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user