module Folio # = Prompt class # class Prompt attr :path # def initialize(*path) opts = (Hash===path.last ? path.pop : {}) mode(opts) if path.empty? path = Dir.pwd else path = File.join(*path) end raise FileNotFound unless ::File.exist?(path) raise FileNotFound unless ::File.directory?(path) @path = Directory.new(path) end # opertaton mode def mode(opts=nil) return @mode unless opts opts.each do |key, val| next unless val case key when :noop @mode = (@mode == :verbose ? :dryrun : :noop) when :verbose @mode = (@mode == :noop ? :dryrun : :verbose) when :dryrun @mode = :dryrun end end end def to_s ; path.to_s ; end def pwd ; path ; end # Get the FileObject. def file(name) FileObject(name) end #-- # ::Dir #++ # Join paths. def +(fname) @path =+ fname end alias_method :/, :+ # Directory entries. def paths path.paths end # Directory entries. def entries path.entries end alias_method :ls, :entries # Directory entries. def directories path.paths.select{ |f| f.directory? } end # Directory entries. def files path.paths.select{ |f| f.file? } end # Glob pattern. def glob(*patterns) opts = (::Integer===patterns.last ? patterns.pop : 0) pattern = localize(pattern) ::Dir.glob(pattern, opts).each{ |f| FileObject[f] } end def cd(dir, &block) #dir = localize(dir) if block @path.cd(dir, &block) else @path += dir end end #-- # fileutils #++ def mkdir(dir, options) dir = localize(dir) fileutils.mkdir(dir, options) end def mkdir_p(dir, options) dir = localize(dir) fileutils.mkdir_p(dir, options) end def rmdir(dir, options) dir = localize(dir) fileutils.rmdir(dir, options) end # ln(list, destdir, options) def ln(old, new, options) old = localize(old) new = localize(new) fileutils.ln(old, new, options) end # ln_s(list, destdir, options) def ln_s(old, new, options) old = localize(old) new = localize(new) fileutils.ln_s(old, new, options) end def ln_sf(old, new, options) old = localize(old) new = localize(new) fileutils.ln_sf(old, new, options) end # cp(list, dir, options) def cp(src, dest, options) src = localize(src) dest = localize(dest) fileutils.cp(src, dest, options) end # cp_r(list, dir, options) def cp_r(src, dest, options) src = localize(src) dest = localize(dest) fileutils.cp_r(src, dest, options) end # mv(list, dir, options) def mv(src, dest, options) src = localize(src) dest = localize(dest) fileutils.mv(src, dest, options) end def rm(list, options) list = localize(list) fileutils.rm(list, options) end def rm_r(list, options) list = localize(list) fileutils.rm_f(list, options) end def rm_rf(list, options) list = localize(list) fileutils.rm_rf(list, options) end def install(src, dest, mode, options) src = localize(src) dest = localize(dest) fileutils.install(src, dest, mode, options) end def chmod(mode, list, options) list = localize(list) fileutils.chmod(mode, list, options) end def chmod_r(mode, list, options) list = localize(list) fileutils.chmod_r(mode, list, options) end #alias_method :chmod_R, :chmod_r def chown(user, group, list, options) list = localize(list) fileutils.chown(user, group, list, options) end def chown_r(user, group, list, options) list = localize(list) fileutils.chown_r(user, group, list, options) end #alias_method :chown_R, :chown_r def touch(list, options) list = localize(list) fileutils.touch(list, options) end private def localize(local_path) case local_path when Array local_path.collect{ |lp| File.expand_path(File.join(path, lp)) } else File.expand_path(File.join(path, local_path)) end end def fileutils return ::FileUtils unless @mode case @mode when :dryrun ::FileUtils::DryRun when :noop ::FileUtils::Noop when :verbose ::FileUtils::Verbose else ::FileUtils end end public#class def self.[](path) new(path) end end end