module Sys

Sys provides a number of file manipulation tools for the convenience of writing Rakefiles. All commands in this module will announce their activity on standard output if the $verbose flag is set ($verbose = true is the default). You can control this by globally setting $verbose or by using the verbose and quiet methods.

Sys has been deprecated in favor of the FileUtils module available in Ruby 1.8.

Constants

RUBY

Public Instance Methods

copy(file_name, dest_file) click to toggle source

Copy a single file from file_name to dest_file.

# File lib/rake/contrib/sys.rb, line 48
def copy(file_name, dest_file)
  log "Copying file #{file_name} to #{dest_file}"
  File.copy(file_name, dest_file)
end
copy_files(wildcard, dest_dir) click to toggle source

Copy all files matching wildcard into the directory dest_dir.

# File lib/rake/contrib/sys.rb, line 54
def copy_files(wildcard, dest_dir)
  for_matching_files(wildcard, dest_dir) { |from, to| copy(from, to) }
end
delete(*wildcards) click to toggle source

Remove all files matching wildcard. If a matching file is a directory, it must be empty to be removed. used delete_all to recursively delete directories.

# File lib/rake/contrib/sys.rb, line 83
def delete(*wildcards)
  wildcards.each do |wildcard|
    FileList.glob(wildcard).each do |fn|
      if File.directory?(fn)
        log "Deleting directory #{fn}"
        Dir.delete(fn)
      else
        log "Deleting file #{fn}"
        File.delete(fn)
      end
    end
  end
end
delete_all(*wildcards) click to toggle source

Recursively delete all files and directories matching wildcard.

# File lib/rake/contrib/sys.rb, line 98
def delete_all(*wildcards)
  wildcards.each do |wildcard|
    FileList.glob(wildcard).each do |fn|
      next if ! File.exist?(fn)
      if File.directory?(fn)
        FileList.glob("#{fn}/*").each do |subfn|
          next if subfn=='.' || subfn=='..'
          delete_all(subfn)
        end
        log "Deleting directory #{fn}"
        Dir.delete(fn)
      else
        log "Deleting file #{fn}"
        File.delete(fn)
      end
    end
  end
end
for_files(*wildcards) { |fn| ... } click to toggle source

Perform a block with each file matching a set of wildcards.

# File lib/rake/contrib/sys.rb, line 163
def for_files(*wildcards)
  wildcards.each do |wildcard|
    FileList.glob(wildcard).each do |fn|
      yield(fn)
    end
  end
end
indir(dir) { || ... } click to toggle source

Make dir the current working directory for the duration of executing the given block.

# File lib/rake/contrib/sys.rb, line 127
def indir(dir)
  olddir = Dir.pwd
  Dir.chdir(dir)
  yield
ensure
  Dir.chdir(olddir)
end
install(wildcard, dest_dir, mode) click to toggle source

Install all the files matching wildcard into the dest_dir directory. The permission mode is set to mode.

# File lib/rake/contrib/sys.rb, line 30
def install(wildcard, dest_dir, mode)
  FileList.glob(wildcard).each do |fn|
    File.install(fn, dest_dir, mode, $verbose)
  end
end
log(msg) click to toggle source

Write a message to standard error if $verbose is enabled.

# File lib/rake/contrib/sys.rb, line 147
def log(msg)
  print "  " if $trace && $verbose
  $stderr.puts msg if $verbose
end
makedirs(*dirs) click to toggle source

Make the directories given in dirs.

# File lib/rake/contrib/sys.rb, line 118
def makedirs(*dirs)
  dirs.each do |fn|
    log "Making directory #{fn}"
    File.makedirs(fn)
  end
end
quiet(&block) click to toggle source

Perform a block with $verbose disabled.

# File lib/rake/contrib/sys.rb, line 153
def quiet(&block)
  with_verbose(false, &block)
end
ruby(*args) click to toggle source

Run a Ruby interpreter with the given arguments.

# File lib/rake/contrib/sys.rb, line 43
def ruby(*args)
  run "#{RUBY} #{args.join(' ')}"
end
run(cmd) click to toggle source

Run the system command cmd.

# File lib/rake/contrib/sys.rb, line 37
def run(cmd)
  log cmd
  system(cmd) or fail "Command Failed: [#{cmd}]"
end
split_all(path) click to toggle source

Split a file path into individual directory names.

For example:

split_all("a/b/c") =>  ['a', 'b', 'c']
# File lib/rake/contrib/sys.rb, line 139
def split_all(path)
  head, tail = File.split(path)
  return [tail] if head == '.' || tail == '/'
  return [head, tail] if head == '/'
  return split_all(head) + [tail]
end
verbose(&block) click to toggle source

Perform a block with $verbose enabled.

# File lib/rake/contrib/sys.rb, line 158
def verbose(&block)
  with_verbose(true, &block)
end

Private Instance Methods

for_matching_files(wildcard, dest_dir) { |fn, dest_file| ... } click to toggle source
# File lib/rake/contrib/sys.rb, line 175
def for_matching_files(wildcard, dest_dir)
  FileList.glob(wildcard).each do |fn|
    dest_file = File.join(dest_dir, fn)
    parent = File.dirname(dest_file)
    makedirs(parent) if ! File.directory?(parent)
    yield(fn, dest_file)
  end
end
with_verbose(v) { || ... } click to toggle source
# File lib/rake/contrib/sys.rb, line 184
def with_verbose(v)
  oldverbose = $verbose
  $verbose = v
  yield
ensure
  $verbose = oldverbose
end