Module: Fire::ShellUtils
- Defined in:
- lib/fire/shellutils.rb
Overview
File system utility methods.
Instance Method Summary collapse
- #directory?(path) ⇒ Boolean
-
#method_missing(s, *a, &b) ⇒ Object
If FileUtils responds to a missing method, then call it.
-
#sh(*args) ⇒ Object
Shell out via system call.
-
#sync(src, dst, options = {}) ⇒ Object
Synchronize a destination directory with a source directory.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(s, *a, &b) ⇒ Object
If FileUtils responds to a missing method, then call it.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/fire/shellutils.rb', line 64 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 |
Instance Method Details
#directory?(path) ⇒ Boolean
19 20 21 |
# File 'lib/fire/shellutils.rb', line 19 def directory?(path) File.directory?(path) end |
#sh(*args) ⇒ Object
Shell out via system call.
14 15 16 17 |
# File 'lib/fire/shellutils.rb', line 14 def sh(*args) puts args.join(' ') system(*args) end |
#sync(src, dst, options = {}) ⇒ Object
TODO:
Augment FileUtils instead.
TODO:
Not every action needs to be verbose.
Synchronize a destination directory with a source directory.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fire/shellutils.rb', line 29 def sync(src, dst, ={}) 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 |