module Ergo::ShellUtils

File system utility methods.

Public Instance Methods

directory?(path) click to toggle source
# File lib/ergo/shellutils.rb, line 21
def directory?(path)
  File.directory?(path)
end
method_missing(s, *a, &b) click to toggle source

If FileUtils responds to a missing method, then call it.

Calls superclass method
# File lib/ergo/shellutils.rb, line 66
def method_missing(s, *a, &b)
  if FileUtils.respond_to?(s)
    if $DRYRUN
      FileUtils::DryRun.__send__(s, *a, &b)
    else
      FileUtils::Verbose.__send__(s, *a, &b)
    end
  else
    super(s, *a, &b)
  end
end
sh(*args) click to toggle source

Shell out via system call.

Arguments

args - Argument vector. [Array]

Returns success of shell invocation.

# File lib/ergo/shellutils.rb, line 14
def sh(*args)
  env = (Hash === args.last ? args.pop : {})
  puts args.join(' ')
  system(env, *args)
end
sync(src, dst, options={}) click to toggle source

Synchronize a destination directory with a source directory.

TODO: Augment FileUtils instead. TODO: Not every action needs to be verbose.

# File lib/ergo/shellutils.rb, line 31
def sync(src, dst, options={})
  src_files = Dir[File.join(src, '**', '*')].map{ |f| f.sub(src+'/', '') }
  dst_files = Dir[File.join(dst, '**', '*')].map{ |f| f.sub(dst+'/', '') }

  removal = dst_files - src_files

  rm_dirs, rm_files = [], []
  removal.each do |f|
    path = File.join(dst, f)
    if File.directory?(path)
      rm_dirs << path
    else
      rm_files << path
    end
  end

  rm_files.each { |f| rm(f) }
  rm_dirs.each  { |d| rmdir(d) }

  src_files.each do |f|
    src_path = File.join(src, f)
    dst_path = File.join(dst, f)
    if File.directory?(src_path)
      mkdir_p(dst_path)
    else
      parent = File.dirname(dst_path) 
      mkdir_p(parent) unless File.directory?(parent)
      install(src_path, dst_path)
    end
  end
end