Ratch  Ratch::Shell

[Validate]
Generated with Newfish 0.1.0

Shell Prompt class

Ratch Shell object provides a limited file system shell in code. It is similar to having a shell prompt available to you in Ruby.

NOTE: We have used the term trace in place of verbose for command line options. Even though Ruby itself uses the term verbose with respect to FileUtils, the term is commonly used for command specific needs, so we want to leave it open for such cases.

Public Class Methods

[](path) click to toggle source
     # File lib/ratch/shell.rb, line 776
776:     def self.[](path)
777:       new(path)
778:     end
new(*args) click to toggle source

New Shell object.

  Shell.new(:noop=>true)
  Shell.new('..', :quiet=>true)
    # File lib/ratch/shell.rb, line 28
28:     def initialize(*args)
29:       path, opts = parse_arguments(*args)
30: 
31:       opts.rekey!(&:to_sym)
32: 
33:       set_options(opts)
34: 
35:       if path.empty?
36:         path = Dir.pwd
37:       else
38:         path = File.join(*path)
39:       end
40: 
41:       raise FileNotFound, "#{path}" unless ::File.exist?(path)
42:       raise FileNotFound, "#{path}" unless ::File.directory?(path)
43: 
44:       @_work = Pathname.new(path).expand_path
45:     end

Public Instance Methods

/(path) click to toggle source

Join paths. TODO: Should this return a new directory object? Or should it change directories?

     # File lib/ratch/shell.rb, line 290
290:     def /(path)
291:       #@_work += dir   # did not work, why?
292:       @_work = dir(localize(path))
293:       self
294:     end
==(other) click to toggle source

Two Shell’s are equal if they have the same working path.

     # File lib/ratch/shell.rb, line 98
 98:     def ==(other)
 99:       return false unless other.is_a?(self.class)
100:       return false unless work == other.work
101:       true
102:     end
absolute?(path) click to toggle source
     # File lib/ratch/shell.rb, line 414
414:     def absolute?(path)   ; FileTest.absolute?(path)       ; end
amass(include_globs, exclude_globs=[], ignore_globs=[]) click to toggle source

An intergrated glob like method that takes a set of include globs, exclude globs and ignore globs to produce a collection of paths.

Ignore_globs differ from exclude_globs in that they match by the basename of the path rather than the whole pathname.

     # File lib/ratch/shell.rb, line 615
615:     def amass(include_globs, exclude_globs=[], ignore_globs=[])
616:       locally do
617:         fileutils.amass(include_globs, exclude_globs, ignore_globs)
618:       end
619:     end
append(path, text) click to toggle source

Append to file.

     # File lib/ratch/shell.rb, line 379
379:     def append(path, text)
380:       $stderr.puts "append #{path}" if trace?
381:       File.open(localize(path), 'a'){ |f| f << text } unless noop?
382:     end
apply_naming_policy(name, ext) click to toggle source
     # File lib/ratch/shell.rb, line 735
735:     def apply_naming_policy(name, ext)
736:       naming_policy.each do |policy|
737:         case policy.to_s
738:         when /^low/, /^down/
739:           name = name.downcase
740:         when /^up/
741:           name = name.upcase
742:         when /^cap/
743:           name = name.capitalize
744:         when /^ext/
745:           name = name + ".#{ext}"
746:         end
747:       end
748:       name
749:     end
batch(*patterns) click to toggle source

Returns a Batch of file patterns.

     # File lib/ratch/shell.rb, line 172
172:     def batch(*patterns)
173:       Batch.new patterns.map{|pattern| localize(pattern)}
174:     end
batch_all(*patterns) click to toggle source

Returns a Batch of file patterns, without any exclusions.

     # File lib/ratch/shell.rb, line 177
177:     def batch_all(*patterns)
178:       Batch.all patterns.map{|pattern| localize(pattern)}
179:     end
blockdev?(path) click to toggle source
     # File lib/ratch/shell.rb, line 402
402:     def blockdev?(path)   ; FileTest.blockdev?(localize(path))  ; end
cd(path, &block) click to toggle source

Change working directory.

TODO: Make thread safe.

     # File lib/ratch/shell.rb, line 339
339:     def cd(path, &block)
340:       if block
341:         work_old = @_work
342:         begin
343:           @_work = dir(localize(path))
344:           locally(&block)
345:           #mutex.synchronize do
346:           #  Dir.chdir(@_work){ block.call }
347:           #end
348:         ensure
349:           @_work = work_old
350:         end
351:       else
352:         @_work = dir(localize(path))
353:       end
354:     end
Also aliased as: chdir
chardev?(path) click to toggle source
     # File lib/ratch/shell.rb, line 395
395:     def chardev?(path)    ; FileTest.chardev?(localize(path))   ; end
chdir(path, &block) click to toggle source
chmod(mode, list, options={}) click to toggle source
     # File lib/ratch/shell.rb, line 570
570:     def chmod(mode, list, options={})
571:       list = localize(list)
572:       fileutils.chmod(mode, list, options)
573:     end
chmod_r(mode, list, options={}) click to toggle source
     # File lib/ratch/shell.rb, line 575
575:     def chmod_r(mode, list, options={})
576:       list = localize(list)
577:       fileutils.chmod_r(mode, list, options)
578:     end
chown(user, group, list, options={}) click to toggle source

alias_method :chmod_R, :chmod_r

     # File lib/ratch/shell.rb, line 581
581:     def chown(user, group, list, options={})
582:       list = localize(list)
583:       fileutils.chown(user, group, list, options)
584:     end
chown_r(user, group, list, options={}) click to toggle source
     # File lib/ratch/shell.rb, line 586
586:     def chown_r(user, group, list, options={})
587:       list = localize(list)
588:       fileutils.chown_r(user, group, list, options)
589:     end
cmp(a,b) click to toggle source

Same as identical?

     # File lib/ratch/shell.rb, line 475
475:     def cmp(a,b)
476:       fileutils.compare_file(a,b)
477:     end
compare_file(path, other) click to toggle source
copy(src, dest, options={}) click to toggle source
cp(src, dest, options={}) click to toggle source

cp(list, dir, options={})

     # File lib/ratch/shell.rb, line 521
521:     def cp(src, dest, options={})
522:       src  = localize(src)
523:       dest = localize(dest)
524:       fileutils.cp(src, dest, options)
525:     end
Also aliased as: copy
cp_r(src, dest, options={}) click to toggle source

cp_r(list, dir, options={})

     # File lib/ratch/shell.rb, line 529
529:     def cp_r(src, dest, options={})
530:       src  = localize(src)
531:       dest = localize(dest)
532:       fileutils.cp_r(src, dest, options)
533:     end
dir(path) click to toggle source
     # File lib/ratch/shell.rb, line 200
200:     def dir(path)
201:       #Directory.new(name)
202:       path = localize(path)
203:       raise FileNotFound unless File.directory?(path)
204:       Pathname.new(path)
205:     end
dir_entries() click to toggle source
directories() click to toggle source

Returns list of directories.

     # File lib/ratch/shell.rb, line 233
233:     def directories
234:       pathnames.select{ |f| f.directory? }
235:     end
Also aliased as: dirs, folders
directory?(path) click to toggle source
     # File lib/ratch/shell.rb, line 392
392:     def directory?(path)  ; FileTest.directory?(localize(path)) ; end
directory_entries() click to toggle source

Lists directory entries.

     # File lib/ratch/shell.rb, line 215
215:     def directory_entries
216:       entries.select{ |d| d.directory? }
217:     end
Also aliased as: dir_entries
dirs() click to toggle source
dryrun?() click to toggle source

def force? ; @_force ; end

    # File lib/ratch/shell.rb, line 92
92:     def dryrun?  ; noop? && trace? ; end
entries() click to toggle source

Lists all entries.

     # File lib/ratch/shell.rb, line 208
208:     def entries
209:       work.entries
210:     end
eql?(other) click to toggle source

Same as #== except that noop? must also be the same.

     # File lib/ratch/shell.rb, line 105
105:     def eql?(other)
106:       return false unless other.is_a?(self.class)
107:       return false unless work == other.work
108:       return false unless noop?  == other.noop?
109:       true
110:     end
executable?(path) click to toggle source
     # File lib/ratch/shell.rb, line 409
409:     def executable?(path) ; FileTest.executable?(localize(path))  ; end
executable_real?(path) click to toggle source
     # File lib/ratch/shell.rb, line 417
417:     def executable_real?(path) ; FileTest.executable_real?(localize(path)) ; end
exist?(path) click to toggle source
     # File lib/ratch/shell.rb, line 396
396:     def exist?(path)      ; FileTest.exist?(localize(path))     ; end
exists?(path) click to toggle source
     # File lib/ratch/shell.rb, line 397
397:     def exists?(path)     ; FileTest.exists?(localize(path))    ; end
file(path) click to toggle source
     # File lib/ratch/shell.rb, line 188
188:     def file(path)
189:       #FileObject[name]
190:       path = localize(path)
191:       raise FileNotFound unless File.file?(path)
192:       Pathname.new(path)
193:     end
file?(path) click to toggle source
     # File lib/ratch/shell.rb, line 400
400:     def file?(path)       ; FileTest.file?(localize(path))      ; end
file_entries() click to toggle source

Lists file entries.

     # File lib/ratch/shell.rb, line 223
223:     def file_entries
224:       entries.select{ |f| f.file? }
225:     end
files() click to toggle source

Returns list of files.

     # File lib/ratch/shell.rb, line 240
240:     def files
241:       pathnames.select{ |f| f.file? }
242:     end
folders() click to toggle source
glob(*patterns, &block) click to toggle source

Glob pattern. Returns matches as strings.

     # File lib/ratch/shell.rb, line 245
245:     def glob(*patterns, &block)
246:       opts = (::Integer===patterns.last ? patterns.pop : 0)
247:       matches = []
248:       locally do
249:         matches = patterns.map{ |pattern| ::Dir.glob(pattern, opts) }.flatten
250:       end
251:       if block_given?
252:         matches.each(&block)
253:       else
254:         matches
255:       end
256:     end
grpowned?(path) click to toggle source
     # File lib/ratch/shell.rb, line 403
403:     def grpowned?(path)   ; FileTest.grpowned?(localize(path))  ; end
home(*args) click to toggle source

Current home path.

     # File lib/ratch/shell.rb, line 143
143:     def home(*args)
144:       dir(File.expand_path('~'), *args)
145:     end
identical?(path, other) click to toggle source
     # File lib/ratch/shell.rb, line 420
420:     def identical?(path, other)
421:       FileTest.identical?(localize(path), localize(other))
422:     end
Also aliased as: compare_file
install(src, dest, mode, options={}) click to toggle source
     # File lib/ratch/shell.rb, line 564
564:     def install(src, dest, mode, options={})
565:       src  = localize(src)
566:       dest = localize(dest)
567:       fileutils.install(src, dest, mode, options)
568:     end
ln(old, new, options={}) click to toggle source

ln(list, destdir, options={})

     # File lib/ratch/shell.rb, line 499
499:     def ln(old, new, options={})
500:       old = localize(old)
501:       new = localize(new)
502:       fileutils.ln(old, new, options)
503:     end
Also aliased as: link
ln_s(old, new, options={}) click to toggle source

ln_s(list, destdir, options={})

     # File lib/ratch/shell.rb, line 507
507:     def ln_s(old, new, options={})
508:       old = localize(old)
509:       new = localize(new)
510:       fileutils.ln_s(old, new, options)
511:     end
Also aliased as: symlink
ln_sf(old, new, options={}) click to toggle source
     # File lib/ratch/shell.rb, line 514
514:     def ln_sf(old, new, options={})
515:       old = localize(old)
516:       new = localize(new)
517:       fileutils.ln_sf(old, new, options)
518:     end
localize(local_path) click to toggle source

Returns a path local to the current working path.

     # File lib/ratch/shell.rb, line 687
687:     def localize(local_path)
688:       # some path arguments are optional
689:       return local_path unless local_path
690:       #
691:       case local_path
692:       when Array
693:         local_path.collect do |lp|
694:           if absolute?(lp)
695:             lp
696:           else
697:             File.expand_path(File.join(work.to_s, lp))
698:           end
699:         end
700:       else
701:         # do not localize an absolute path
702:         return local_path if absolute?(local_path)
703:         File.expand_path(File.join(work.to_s, local_path))
704:         #(work + local_path).expand_path.to_s
705:       end
706:     end
locally(&block) click to toggle source

Change directory to the shell’s work directory, process the block and then return to user directory.

     # File lib/ratch/shell.rb, line 710
710:     def locally(&block)
711:       if work.to_s == Dir.pwd
712:         block.call
713:       else
714:         mutex.synchronize do
715:           #work.chdir(&block)
716:           Dir.chdir(work, &block)
717:         end
718:       end
719:     end
mkdir(dir, options={}) click to toggle source
     # File lib/ratch/shell.rb, line 480
480:     def mkdir(dir, options={})
481:       dir = localize(dir)
482:       fileutils.mkdir(dir, options)
483:     end
mkdir_p(dir, options={}) click to toggle source
     # File lib/ratch/shell.rb, line 485
485:     def mkdir_p(dir, options={})
486:       dir = localize(dir)
487:       unless File.directory?(dir)
488:         fileutils.mkdir_p(dir, options)
489:       end
490:     end
Also aliased as: mkpath
mkpath(dir, options={}) click to toggle source
move(src, dest, options={}) click to toggle source
multiglob(*args, &blk) click to toggle source

TODO: Ultimately merge glob and multiglob.

     # File lib/ratch/shell.rb, line 264
264:     def multiglob(*args, &blk)
265:       Dir.multiglob(*args, &blk)
266:     end
multiglob_r(*args, &blk) click to toggle source
     # File lib/ratch/shell.rb, line 268
268:     def multiglob_r(*args, &blk)
269:       Dir.multiglob_r(*args, &blk)
270:     end
mv(src, dest, options={}) click to toggle source

mv(list, dir, options={})

     # File lib/ratch/shell.rb, line 536
536:     def mv(src, dest, options={})
537:       src  = localize(src)
538:       dest = localize(dest)
539:       fileutils.mv(src, dest, options)
540:     end
Also aliased as: move
naming_policy(*policies) click to toggle source
     # File lib/ratch/shell.rb, line 725
725:     def naming_policy(*policies)
726:       if policies.empty?
727:         @naming_policy ||= ['down', 'ext']
728:       else
729:         @naming_policy = policies
730:       end
731:     end
noop?() click to toggle source
    # File lib/ratch/shell.rb, line 89
89:     def noop?    ; @_noop  ; end
outofdate?(path, *sources) click to toggle source
     # File lib/ratch/shell.rb, line 622
622:     def outofdate?(path, *sources)
623:       #fileutils.outofdate?(localize(path), localize(sources))  # DIDN'T WORK, why?
624:       locally do
625:         fileutils.outofdate?(path, sources.flatten)
626:       end
627:     end
owned?(path) click to toggle source
     # File lib/ratch/shell.rb, line 407
407:     def owned?(path)      ; FileTest.owned?(localize(path))     ; end
parent() click to toggle source
 Return a new prompt with the same location.
 NOTE: Use #dup or #clone ?

def new ; Shell.new(work) ; end

     # File lib/ratch/shell.rb, line 160
160:     def parent
161:       dir('..')
162:     end
path(path) click to toggle source
     # File lib/ratch/shell.rb, line 182
182:     def path(path)
183:       Pathname.new(localize(path))
184:     end
Also aliased as: pathname
pathname(path) click to toggle source
pathnames() click to toggle source

Likes entries but omits ’.’ and ’..’ paths.

     # File lib/ratch/shell.rb, line 228
228:     def pathnames
229:       work.entries - %{. ..}.map{|f|Pathname.new(f)}
230:     end
pipe?(path) click to toggle source
     # File lib/ratch/shell.rb, line 399
399:     def pipe?(path)       ; FileTest.pipe?(localize(path))      ; end
pwd() click to toggle source

Present working directory.

     # File lib/ratch/shell.rb, line 470
470:     def pwd
471:       work.to_s
472:     end
quiet?() click to toggle source
 Opertaton mode. This can be :noop, :verbose or :dryrun.
 The later is the same as the first two combined.

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

    # File lib/ratch/shell.rb, line 87
87:     def quiet?   ; @_quiet ; end
read(path) click to toggle source

Read file.

     # File lib/ratch/shell.rb, line 368
368:     def read(path)
369:       File.read(localize(path))
370:     end
readable?(path) click to toggle source
     # File lib/ratch/shell.rb, line 394
394:     def readable?(path)   ; FileTest.readable?(localize(path))  ; end
readable_real?(path) click to toggle source
     # File lib/ratch/shell.rb, line 418
418:     def readable_real?(path)   ; FileTest.readable_real?(localize(path))   ; end
relative?(path) click to toggle source
     # File lib/ratch/shell.rb, line 413
413:     def relative?(path)   ; FileTest.relative?(path)       ; end
remove(list, options={}) click to toggle source
rm(list, options={}) click to toggle source
     # File lib/ratch/shell.rb, line 543
543:     def rm(list, options={})
544:       list = localize(list)
545:       fileutils.rm(list, options)
546:     end
Also aliased as: remove
rm_f(list, options={}) click to toggle source
     # File lib/ratch/shell.rb, line 554
554:     def rm_f(list, options={})
555:       list = localize(list)
556:       fileutils.rm_f(list, options)
557:     end
rm_r(list, options={}) click to toggle source
     # File lib/ratch/shell.rb, line 549
549:     def rm_r(list, options={})
550:       list = localize(list)
551:       fileutils.rm_r(list, options)
552:     end
rm_rf(list, options={}) click to toggle source
     # File lib/ratch/shell.rb, line 559
559:     def rm_rf(list, options={})
560:       list = localize(list)
561:       fileutils.rm_rf(list, options)
562:     end
rmdir(dir, options={}) click to toggle source
     # File lib/ratch/shell.rb, line 493
493:     def rmdir(dir, options={})
494:       dir = localize(dir)
495:       fileutils.rmdir(dir, options)
496:     end
root(*args) click to toggle source

Root location.

     # File lib/ratch/shell.rb, line 138
138:     def root(*args)
139:       dir('/', *args)
140:     end
safe?(path) click to toggle source
     # File lib/ratch/shell.rb, line 411
411:     def safe?(path)       ; FileTest.safe?(localize(path)) ; end
setgid?(path) click to toggle source
     # File lib/ratch/shell.rb, line 404
404:     def setgid?(path)     ; FileTest.setgid?(localize(path))    ; end
setuid?(path) click to toggle source
     # File lib/ratch/shell.rb, line 405
405:     def setuid?(path)     ; FileTest.setuid?(localize(path))    ; end
sh(cmd) click to toggle source

Shell runner.

     # File lib/ratch/shell.rb, line 307
307:     def sh(cmd)
308:       #puts "--> system call: #{cmd}" if trace?
309:       puts cmd if trace?
310:       return true if noop?
311:       #locally do
312:         if quiet?
313:           silently{ system(cmd) }
314:         else
315:           system(cmd)
316:         end
317:       #end
318:     end
size(path) click to toggle source
     # File lib/ratch/shell.rb, line 390
390:     def size(path)        ; FileTest.size(localize(path))       ; end
size?(path) click to toggle source
     # File lib/ratch/shell.rb, line 391
391:     def size?(path)       ; FileTest.size?(localize(path))      ; end
socket?(path) click to toggle source
     # File lib/ratch/shell.rb, line 406
406:     def socket?(path)     ; FileTest.socket?(localize(path))    ; end
stage(stage_dir, files) click to toggle source

TODO: should this have SOURCE diectory?

  stage(directory, source_dir, files)
     # File lib/ratch/shell.rb, line 601
601:     def stage(stage_dir, files)
602:       #dir   = localize(directory)
603:       #files = localize(files)
604:       locally do
605:         fileutils.stage(stage_dir, work, files)
606:       end
607:     end
sticky?(path) click to toggle source
     # File lib/ratch/shell.rb, line 401
401:     def sticky?(path)     ; FileTest.sticky?(localize(path))    ; end
system(cmd) click to toggle source

TODO: Tie this into the System class.

     # File lib/ratch/shell.rb, line 300
300:     def system(cmd)
301:       locally do
302:         super(cmd)
303:       end
304:     end
to_s() click to toggle source

String representation is work directory path.

    # File lib/ratch/shell.rb, line 95
95:     def to_s ; work.to_s ; end
touch(list, options={}) click to toggle source

alias_method :chown_R, :chown_r

     # File lib/ratch/shell.rb, line 592
592:     def touch(list, options={})
593:       list = localize(list)
594:       fileutils.touch(list, options)
595:     end
trace?() click to toggle source
    # File lib/ratch/shell.rb, line 88
88:     def trace?   ; @_trace ; end
uptodate?(path, *sources) click to toggle source
     # File lib/ratch/shell.rb, line 647
647:     def uptodate?(path, *sources)
648:       locally do
649:         fileutils.uptodate?(path, sources.flatten)
650:       end
651:     end
work(*args) click to toggle source

Current working path.

     # File lib/ratch/shell.rb, line 148
148:     def work(*args)
149:       return @_work if args.empty?
150:       return dir(@_work, *args)
151:     end
writable?(path) click to toggle source
     # File lib/ratch/shell.rb, line 408
408:     def writable?(path)   ; FileTest.writable?(localize(path))  ; end
writable_real?(path) click to toggle source
     # File lib/ratch/shell.rb, line 416
416:     def writable_real?(path)   ; FileTest.writable_real?(localize(path))   ; end
write(path, text) click to toggle source

Write file.

     # File lib/ratch/shell.rb, line 373
373:     def write(path, text)
374:       $stderr.puts "write #{path}" if trace?
375:       File.open(localize(path), 'w'){ |f| f << text } unless noop?
376:     end
zero?(path) click to toggle source
     # File lib/ratch/shell.rb, line 398
398:     def zero?(path)       ; FileTest.zero?(localize(path))      ; end

Private Instance Methods

fileutils() click to toggle source

Returns FileUtils module based on mode.

     # File lib/ratch/shell.rb, line 754
754:     def fileutils
755:       if dryrun?
756:         ::FileUtils::DryRun
757:       elsif noop?
758:         ::FileUtils::Noop
759:       elsif trace?
760:         ::FileUtils::Verbose
761:       else
762:         ::FileUtils
763:       end
764:     end
mutex() click to toggle source
    # File lib/ratch/shell.rb, line 64
64:     def mutex
65:       @mutex ||= Mutex.new
66:     end
parse_arguments(*args) click to toggle source
    # File lib/ratch/shell.rb, line 50
50:     def parse_arguments(*args)
51:       opts = (Hash===args.last ? args.pop : {})
52:       return args, opts
53:     end
set_options(opts) click to toggle source
    # File lib/ratch/shell.rb, line 56
56:     def set_options(opts)
57:       @_quiet = opts[:quiet]
58:       @_noop  = opts[:noop]  || opts[:dryrun]
59:       @_trace = opts[:trace] || opts[:dryrun]
60:       #@_force = opts[:force]
61:     end
util_options(options) click to toggle source

This may be used by script commands to allow for per command noop and trace options. Global options have precedence.

     # File lib/ratch/shell.rb, line 768
768:     def util_options(options)
769:       noop  = noop?  || options[:noop]  || options[:dryrun]
770:       trace = trace? || options[:trace] || options[:dryrun]
771:       return noop, trace
772:     end

Disabled; run with --debug to generate this.