Module: Detroit::ShellUtils

Included in:
Tool
Defined in:
lib/detroit/shell_utils.rb

Overview

ShellUtils provides the whole slew of FileUtils, FileTest and File class methods in a single module and modifies methods according to noop? and verbose? options.

Constant Summary collapse

RUBY =

Current ruby binary.

(
  bindir   = ::RbConfig::CONFIG['bindir']
  rubyname = ::RbConfig::CONFIG['ruby_install_name']
  File.join(bindir, rubyname).sub(/.*\s.*/m, '"\&"')
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *a, &b) ⇒ Object

Fallback to filutils.



35
36
37
38
39
40
41
# File 'lib/detroit/shell_utils.rb', line 35

def method_missing(s, *a, &b)
  if fileutils.respond_to?(s)
    fileutils.send(s, *a, &b)
  else
    super(s, *a, &b)
  end
end

Instance Attribute Details

#debug=(value) ⇒ Object (writeonly)

Sets the attribute debug

Parameters:

  • value

    the value to set the attribute debug to.



58
59
60
# File 'lib/detroit/shell_utils.rb', line 58

def debug=(value)
  @debug = value
end

#force=(value) ⇒ Object (writeonly)

Sets the attribute force

Parameters:

  • value

    the value to set the attribute force to.



54
55
56
# File 'lib/detroit/shell_utils.rb', line 54

def force=(value)
  @force = value
end

#quiet=(value) ⇒ Object (writeonly)

Sets the attribute quiet

Parameters:

  • value

    the value to set the attribute quiet to.



55
56
57
# File 'lib/detroit/shell_utils.rb', line 55

def quiet=(value)
  @quiet = value
end

#stderrObject



89
90
91
# File 'lib/detroit/shell_utils.rb', line 89

def stderr
  @stdout ||= $stderr
end

#stdinObject



85
86
87
# File 'lib/detroit/shell_utils.rb', line 85

def stdin
  @stdin ||= $stdin
end

#stdoutObject



81
82
83
# File 'lib/detroit/shell_utils.rb', line 81

def stdout
  @stdout ||= $stdout
end

#trace(message) ⇒ Object

Internal trace report. Only output if in trace mode.



124
125
126
127
128
129
130
# File 'lib/detroit/shell_utils.rb', line 124

def trace(message)
  return if silent?
  if trace?
    stdout.print "TRIAL RUN " if trial?
    stdout.puts message
  end
end

#trial=(value) ⇒ Object (writeonly)

Sets the attribute trial

Parameters:

  • value

    the value to set the attribute trial to.



57
58
59
# File 'lib/detroit/shell_utils.rb', line 57

def trial=(value)
  @trial = value
end

#verbose=(value) ⇒ Object (writeonly)

Sets the attribute verbose

Parameters:

  • value

    the value to set the attribute verbose to.



59
60
61
# File 'lib/detroit/shell_utils.rb', line 59

def verbose=(value)
  @verbose = value
end

Instance Method Details

#absolute?(path) ⇒ Boolean

Returns:

  • (Boolean)


226
# File 'lib/detroit/shell_utils.rb', line 226

def absolute?(path)        ; FileTest.absolute?(path)        ; end

#append(path, text) ⇒ Object

Append to file.



259
260
261
262
# File 'lib/detroit/shell_utils.rb', line 259

def append(path, text)
  $stderr.puts "append #{path}" if trace?
  File.open(path, 'a'){ |f| f << text } unless noop?
end

#ask(question) ⇒ Object

Convenient method to get simple console reply.



133
134
135
136
137
138
# File 'lib/detroit/shell_utils.rb', line 133

def ask(question)
  stdout.print "#{question} "
  stdout.flush
  input = stdin.gets #until inp = stdin.gets ; sleep 1 ; end
  input.strip
end

#atime(*args) ⇒ Object

– File Methods ———————————————————



239
# File 'lib/detroit/shell_utils.rb', line 239

def atime(*args) ; File.ctime(*args) ; end

#blockdev?(path) ⇒ Boolean

Returns:

  • (Boolean)


214
# File 'lib/detroit/shell_utils.rb', line 214

def blockdev?(path)        ; FileTest.blockdev?(path)        ; end

#chardev?(path) ⇒ Boolean

Returns:

  • (Boolean)


207
# File 'lib/detroit/shell_utils.rb', line 207

def chardev?(path)         ; FileTest.chardev?(path)         ; end

#ctime(*args) ⇒ Object



240
# File 'lib/detroit/shell_utils.rb', line 240

def ctime(*args) ; File.ctime(*args) ; end

#debug?Boolean

Returns:

  • (Boolean)


67
# File 'lib/detroit/shell_utils.rb', line 67

def debug?   ; @debug   ; end

#directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


204
# File 'lib/detroit/shell_utils.rb', line 204

def directory?(path)       ; FileTest.directory?(path)       ; end

#dryrun?Boolean

Returns:

  • (Boolean)


71
# File 'lib/detroit/shell_utils.rb', line 71

def dryrun?  ; verbose? && noop? ; end

#executable?(path) ⇒ Boolean

Returns:

  • (Boolean)


221
# File 'lib/detroit/shell_utils.rb', line 221

def executable?(path)      ; FileTest.executable?(path)      ; end

#executable_real?(path) ⇒ Boolean

Returns:

  • (Boolean)


229
# File 'lib/detroit/shell_utils.rb', line 229

def executable_real?(path) ; FileTest.executable_real?(path) ; end

#exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


208
# File 'lib/detroit/shell_utils.rb', line 208

def exist?(path)           ; FileTest.exist?(path)           ; end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


209
# File 'lib/detroit/shell_utils.rb', line 209

def exists?(path)          ; FileTest.exists?(path)          ; end

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


212
# File 'lib/detroit/shell_utils.rb', line 212

def file?(path)            ; FileTest.file?(path)            ; end

#force?Boolean

Returns:

  • (Boolean)


61
# File 'lib/detroit/shell_utils.rb', line 61

def force?   ; @force   ; end

#grpowned?(path) ⇒ Boolean

Returns:

  • (Boolean)


215
# File 'lib/detroit/shell_utils.rb', line 215

def grpowned?(path)        ; FileTest.grpowned?(path)        ; end

#identical?(path, other) ⇒ Boolean Also known as: compare_file

Returns:

  • (Boolean)


232
233
234
# File 'lib/detroit/shell_utils.rb', line 232

def identical?(path, other)
  FileTest.identical?(path, other)
end

#initialize_extension_defaultsObject



19
20
21
22
23
24
25
26
# File 'lib/detroit/shell_utils.rb', line 19

def initialize_extension_defaults
  @quiet = false
  @trial = false
  @noop  = false
  @force = false     

  super() if defined?(super)
end

#initialize_extensionsObject



29
30
31
32
# File 'lib/detroit/shell_utils.rb', line 29

def initialize_extensions
  #extend(fileutils)
  super() if defined?(super)
end

#mtime(*args) ⇒ Object



241
# File 'lib/detroit/shell_utils.rb', line 241

def mtime(*args) ; File.mtime(*args) ; end

#multiglob(*args, &blk) ⇒ Object

TODO: Ultimately merge #glob and #multiglob.



191
192
193
# File 'lib/detroit/shell_utils.rb', line 191

def multiglob(*args, &blk)
  Dir.multiglob(*args, &blk)
end

#multiglob_r(*args, &blk) ⇒ Object



196
197
198
# File 'lib/detroit/shell_utils.rb', line 196

def multiglob_r(*args, &blk)
  Dir.multiglob_r(*args, &blk)
end

#noop?Boolean

Returns:

  • (Boolean)


70
# File 'lib/detroit/shell_utils.rb', line 70

def noop?    ; @trial   ; end

#owned?(path) ⇒ Boolean

Returns:

  • (Boolean)


219
# File 'lib/detroit/shell_utils.rb', line 219

def owned?(path)           ; FileTest.owned?(path)           ; end

#password(prompt = nil) ⇒ Object

TODO: Until we have better support for getting input across platforms, we are using #ask for passwords too.



142
143
144
145
# File 'lib/detroit/shell_utils.rb', line 142

def password(prompt=nil)
  prompt ||= "Enter Password: "
  ask(prompt)
end

#pathObject

A path is required for shell methods to operate. If no path is set than the current working path is used.



45
46
47
# File 'lib/detroit/shell_utils.rb', line 45

def path
  @path ||= Dir.pwd
end

#path=(dir) ⇒ Object

Set shell path.



50
51
52
# File 'lib/detroit/shell_utils.rb', line 50

def path=(dir)
  @path = dir
end

#pipe?(path) ⇒ Boolean

Returns:

  • (Boolean)


211
# File 'lib/detroit/shell_utils.rb', line 211

def pipe?(path)            ; FileTest.pipe?(path)            ; end

#prerequisiteObject



12
13
14
15
16
# File 'lib/detroit/shell_utils.rb', line 12

def prerequisite
  require 'rbconfig'
  require 'ansi/core'
  #require 'detroit/core_ext/shell_extensions'
end


94
95
96
97
# File 'lib/detroit/shell_utils.rb', line 94

def print(str=nil)
  return if silent?
  stdout.print(str.to_s)
end

#puts(str = nil) ⇒ Object



100
101
102
103
# File 'lib/detroit/shell_utils.rb', line 100

def puts(str=nil)
  return if silent?
  stdout.puts(str.to_s)
end

#quiet?Boolean

Returns:

  • (Boolean)


63
# File 'lib/detroit/shell_utils.rb', line 63

def quiet?   ; @quiet   ; end

#read(path) ⇒ Object

Read file.



248
249
250
# File 'lib/detroit/shell_utils.rb', line 248

def read(path)
  File.read(path)
end

#readable?(path) ⇒ Boolean

Returns:

  • (Boolean)


206
# File 'lib/detroit/shell_utils.rb', line 206

def readable?(path)        ; FileTest.readable?(path)        ; end

#readable_real?(path) ⇒ Boolean

Returns:

  • (Boolean)


230
# File 'lib/detroit/shell_utils.rb', line 230

def readable_real?(path)   ; FileTest.readable_real?(path)   ; end

#relative?(path) ⇒ Boolean

Returns:

  • (Boolean)


225
# File 'lib/detroit/shell_utils.rb', line 225

def relative?(path)        ; FileTest.relative?(path)        ; end

#ruby(cmd) ⇒ Object

Shell-out to ruby.



184
185
186
# File 'lib/detroit/shell_utils.rb', line 184

def ruby(cmd)
  sh RUBY + " " + cmd
end

#safe?(path) ⇒ Boolean

Returns:

  • (Boolean)


223
# File 'lib/detroit/shell_utils.rb', line 223

def safe?(path)            ; FileTest.safe?(path)            ; end

#setgid?(path) ⇒ Boolean

Returns:

  • (Boolean)


216
# File 'lib/detroit/shell_utils.rb', line 216

def setgid?(path)          ; FileTest.setgid?(path)          ; end

#setuid?(path) ⇒ Boolean

Returns:

  • (Boolean)


217
# File 'lib/detroit/shell_utils.rb', line 217

def setuid?(path)          ; FileTest.setuid?(path)          ; end

#sh(cmd) ⇒ Object

Shell runner.



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/detroit/shell_utils.rb', line 163

def sh(cmd)
  trace cmd
  return true if noop?

  success = nil
  if quiet?
    silently{ success = system(cmd) }
  else
    success = system(cmd)
  end
  success
end

#silent?Boolean

Returns:

  • (Boolean)


73
# File 'lib/detroit/shell_utils.rb', line 73

def silent?  ; @quiet   ; end

#size(path) ⇒ Object

– File Testing ———————————————————



202
# File 'lib/detroit/shell_utils.rb', line 202

def size(path)             ; FileTest.size(path)             ; end

#size?(path) ⇒ Boolean

Returns:

  • (Boolean)


203
# File 'lib/detroit/shell_utils.rb', line 203

def size?(path)            ; FileTest.size?(path)            ; end

#socket?(path) ⇒ Boolean

Returns:

  • (Boolean)


218
# File 'lib/detroit/shell_utils.rb', line 218

def socket?(path)          ; FileTest.socket?(path)          ; end

#status(message) ⇒ Object Also known as: report



112
113
114
115
# File 'lib/detroit/shell_utils.rb', line 112

def status(message)
  return if silent?
  stdout.puts "#{message}".ansi(:bold)
end

#sticky?(path) ⇒ Boolean

Returns:

  • (Boolean)


213
# File 'lib/detroit/shell_utils.rb', line 213

def sticky?(path)          ; FileTest.sticky?(path)          ; end

#symlink?(path) ⇒ Boolean

Returns:

  • (Boolean)


205
# File 'lib/detroit/shell_utils.rb', line 205

def symlink?(path)         ; FileTest.symlink?(path)         ; end

#trace?Boolean

Returns:

  • (Boolean)


66
# File 'lib/detroit/shell_utils.rb', line 66

def trace?   ; @trace   ; end

#trial?Boolean

Returns:

  • (Boolean)


64
# File 'lib/detroit/shell_utils.rb', line 64

def trial?   ; @trial   ; end

#utime(*args) ⇒ Object



243
# File 'lib/detroit/shell_utils.rb', line 243

def utime(*args) ; File.utime(*args) unless noop? ; end

#verbose?Boolean

Returns:

  • (Boolean)


69
# File 'lib/detroit/shell_utils.rb', line 69

def verbose? ; @verbose ; end

#warn(message) ⇒ Object



106
107
108
109
# File 'lib/detroit/shell_utils.rb', line 106

def warn(message)
  return if silent?
  stderr.puts "WARNING ".ansi(:yellow) + message.to_s
end

#writable?(path) ⇒ Boolean

Returns:

  • (Boolean)


220
# File 'lib/detroit/shell_utils.rb', line 220

def writable?(path)        ; FileTest.writable?(path)        ; end

#writable_real?(path) ⇒ Boolean

Returns:

  • (Boolean)


228
# File 'lib/detroit/shell_utils.rb', line 228

def writable_real?(path)   ; FileTest.writable_real?(path)   ; end

#write(path, text) ⇒ Object

Write file.



253
254
255
256
# File 'lib/detroit/shell_utils.rb', line 253

def write(path, text)
  $stderr.puts "write #{path}" if trace?
  File.open(path, 'w'){ |f| f << text } unless noop?
end

#zero?(path) ⇒ Boolean

Returns:

  • (Boolean)


210
# File 'lib/detroit/shell_utils.rb', line 210

def zero?(path)            ; FileTest.zero?(path)            ; end