Class: Ratch::Script

Inherits:
Shell show all
Includes:
CLI
Defined in:
lib/ratch/script.rb

Overview

The Ratch Script class is used to run stand-alone Ratch scripts. Yep, this is actaully a class named exactly for what it is. How rare.

– Previous versions of this class subclassed Module and called ‘self extend` in the initialize method. Then used method_missing to route to an instance of Shell. The current design works the other way round. Both approaches seem to work just as well, though perhaps one is more robust than another? I have chosen this design simply b/c the shell methods should dispatch a bit faster. ++

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CLI

#arguments, extended, included

Methods inherited from Shell

#/, #==, [], #absolute?, #amass, #append, #apply_naming_policy, #batch, #batch_all, #blockdev?, #cd, #chardev?, #chmod, #chmod_r, #chown, #chown_r, #cmp, #cp, #cp_r, #dir, #directories, #directory?, #directory_entries, #dryrun?, #entries, #eql?, #executable?, #executable_real?, #exist?, #exists?, #file, #file?, #file_entries, #files, #glob, #grpowned?, #home, #identical?, #install, #ln, #ln_s, #ln_sf, #localize, #locally, #mkdir, #mkdir_p, #multiglob, #multiglob_r, #mv, #naming_policy, #noop?, #outofdate?, #owned?, #parent, #path, #pathnames, #pipe?, #pwd, #quiet?, #read, #readable?, #readable_real?, #relative?, #rm, #rm_f, #rm_r, #rm_rf, #rmdir, #root, #safe?, #setgid?, #setuid?, #sh, #size, #size?, #socket?, #stage, #sticky?, #symlink?, #system, #to_s, #touch, #trace?, #uptodate?, #work, #writable?, #writable_real?, #write, #zero?

Constructor Details

#initialize(file, *args) ⇒ Script

Returns a new instance of Script.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ratch/script.rb', line 33

def initialize(file, *args)
  @_file = file.to_s

  #extend self

  super(*args)

  #@_stdout = options[:stdout] || $stdout
  #@_stderr = options[:stderr] || $stderr
  #@_stdin  = options[:stdin]  || $stdin
end

Class Method Details

.execute(script, *args) ⇒ Object



27
28
29
30
# File 'lib/ratch/script.rb', line 27

def self.execute(script, *args)
  script = new(script, *args)
  script.execute!
end

Instance Method Details

#define_method(name, &block) ⇒ Object

Pass-thru to singleton class.



94
95
96
# File 'lib/ratch/script.rb', line 94

def define_method(name, &block)
  (class << self; self; end).__send__(:define_method, &block)
end

#execute!Object Also known as: run!

Be cautious about calling this in a script –an infinite loop could easily ensue.



52
53
54
55
56
57
58
59
60
# File 'lib/ratch/script.rb', line 52

def execute!
  old = $0
  begin
    $0 = script_file
    instance_eval(File.read(script_file), script_file, 1)
  ensure
    $0 = old
  end
end

#report(message) ⇒ Object

TODO: Deprecate one of the three #report, #status, #trace.



75
76
77
78
# File 'lib/ratch/script.rb', line 75

def report(message)
  #@_stdout.puts(message) unless quiet?
  puts(message) unless quiet?
end

#script_fileObject

Returns the file name of the script.



46
47
48
# File 'lib/ratch/script.rb', line 46

def script_file
  @_file
end

#status(message) ⇒ Object



81
82
83
84
# File 'lib/ratch/script.rb', line 81

def status(message)
  #@_stdout.puts message unless quiet?
  puts message unless quiet?  # dryrun? or trace?
end

#trace(message) ⇒ Object

Internal status report. Only output if in trace mode.



88
89
90
91
# File 'lib/ratch/script.rb', line 88

def trace(message)
  #@_stdout.puts message if trace?
  puts message if trace?
end