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 =
(
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
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
58
59
60
|
# File 'lib/detroit/shell_utils.rb', line 58
def debug=(value)
@debug = value
end
|
#force=(value) ⇒ Object
54
55
56
|
# File 'lib/detroit/shell_utils.rb', line 54
def force=(value)
@force = value
end
|
#quiet=(value) ⇒ Object
55
56
57
|
# File 'lib/detroit/shell_utils.rb', line 55
def quiet=(value)
@quiet = value
end
|
89
90
91
|
# File 'lib/detroit/shell_utils.rb', line 89
def stderr
@stdout ||= $stderr
end
|
85
86
87
|
# File 'lib/detroit/shell_utils.rb', line 85
def stdin
@stdin ||= $stdin
end
|
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
57
58
59
|
# File 'lib/detroit/shell_utils.rb', line 57
def trial=(value)
@trial = value
end
|
#verbose=(value) ⇒ Object
Sets the attribute verbose
59
60
61
|
# File 'lib/detroit/shell_utils.rb', line 59
def verbose=(value)
@verbose = value
end
|
Instance Method Details
#absolute?(path) ⇒ Boolean
226
|
# File 'lib/detroit/shell_utils.rb', line 226
def absolute?(path) ; FileTest.absolute?(path) ; end
|
#append(path, text) ⇒ Object
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 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
214
|
# File 'lib/detroit/shell_utils.rb', line 214
def blockdev?(path) ; FileTest.blockdev?(path) ; end
|
#chardev?(path) ⇒ 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
67
|
# File 'lib/detroit/shell_utils.rb', line 67
def debug? ; @debug ; end
|
#directory?(path) ⇒ Boolean
204
|
# File 'lib/detroit/shell_utils.rb', line 204
def directory?(path) ; FileTest.directory?(path) ; end
|
#dryrun? ⇒ Boolean
71
|
# File 'lib/detroit/shell_utils.rb', line 71
def dryrun? ; verbose? && noop? ; end
|
#executable?(path) ⇒ Boolean
221
|
# File 'lib/detroit/shell_utils.rb', line 221
def executable?(path) ; FileTest.executable?(path) ; end
|
#executable_real?(path) ⇒ Boolean
229
|
# File 'lib/detroit/shell_utils.rb', line 229
def executable_real?(path) ; FileTest.executable_real?(path) ; end
|
#exist?(path) ⇒ Boolean
208
|
# File 'lib/detroit/shell_utils.rb', line 208
def exist?(path) ; FileTest.exist?(path) ; end
|
#exists?(path) ⇒ Boolean
209
|
# File 'lib/detroit/shell_utils.rb', line 209
def exists?(path) ; FileTest.exists?(path) ; end
|
#file?(path) ⇒ Boolean
212
|
# File 'lib/detroit/shell_utils.rb', line 212
def file?(path) ; FileTest.file?(path) ; end
|
#force? ⇒ Boolean
61
|
# File 'lib/detroit/shell_utils.rb', line 61
def force? ; @force ; end
|
#grpowned?(path) ⇒ 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
232
233
234
|
# File 'lib/detroit/shell_utils.rb', line 232
def identical?(path, other)
FileTest.identical?(path, other)
end
|
#initialize_extension_defaults ⇒ Object
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_extensions ⇒ Object
29
30
31
32
|
# File 'lib/detroit/shell_utils.rb', line 29
def initialize_extensions
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
70
|
# File 'lib/detroit/shell_utils.rb', line 70
def noop? ; @trial ; end
|
#owned?(path) ⇒ 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
|
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
50
51
52
|
# File 'lib/detroit/shell_utils.rb', line 50
def path=(dir)
@path = dir
end
|
#pipe?(path) ⇒ Boolean
211
|
# File 'lib/detroit/shell_utils.rb', line 211
def pipe?(path) ; FileTest.pipe?(path) ; end
|
#prerequisite ⇒ Object
12
13
14
15
16
|
# File 'lib/detroit/shell_utils.rb', line 12
def prerequisite
require 'rbconfig'
require 'ansi/core'
end
|
#print(str = nil) ⇒ Object
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
63
|
# File 'lib/detroit/shell_utils.rb', line 63
def quiet? ; @quiet ; end
|
#read(path) ⇒ Object
248
249
250
|
# File 'lib/detroit/shell_utils.rb', line 248
def read(path)
File.read(path)
end
|
#readable?(path) ⇒ Boolean
206
|
# File 'lib/detroit/shell_utils.rb', line 206
def readable?(path) ; FileTest.readable?(path) ; end
|
#readable_real?(path) ⇒ Boolean
230
|
# File 'lib/detroit/shell_utils.rb', line 230
def readable_real?(path) ; FileTest.readable_real?(path) ; end
|
#relative?(path) ⇒ Boolean
225
|
# File 'lib/detroit/shell_utils.rb', line 225
def relative?(path) ; FileTest.relative?(path) ; end
|
#ruby(cmd) ⇒ Object
184
185
186
|
# File 'lib/detroit/shell_utils.rb', line 184
def ruby(cmd)
sh RUBY + " " + cmd
end
|
#safe?(path) ⇒ Boolean
223
|
# File 'lib/detroit/shell_utils.rb', line 223
def safe?(path) ; FileTest.safe?(path) ; end
|
#setgid?(path) ⇒ Boolean
216
|
# File 'lib/detroit/shell_utils.rb', line 216
def setgid?(path) ; FileTest.setgid?(path) ; end
|
#setuid?(path) ⇒ Boolean
217
|
# File 'lib/detroit/shell_utils.rb', line 217
def setuid?(path) ; FileTest.setuid?(path) ; end
|
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
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
203
|
# File 'lib/detroit/shell_utils.rb', line 203
def size?(path) ; FileTest.size?(path) ; end
|
#socket?(path) ⇒ 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
213
|
# File 'lib/detroit/shell_utils.rb', line 213
def sticky?(path) ; FileTest.sticky?(path) ; end
|
#symlink?(path) ⇒ Boolean
205
|
# File 'lib/detroit/shell_utils.rb', line 205
def symlink?(path) ; FileTest.symlink?(path) ; end
|
#trace? ⇒ Boolean
66
|
# File 'lib/detroit/shell_utils.rb', line 66
def trace? ; @trace ; end
|
#trial? ⇒ 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
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
220
|
# File 'lib/detroit/shell_utils.rb', line 220
def writable?(path) ; FileTest.writable?(path) ; end
|
#writable_real?(path) ⇒ Boolean
228
|
# File 'lib/detroit/shell_utils.rb', line 228
def writable_real?(path) ; FileTest.writable_real?(path) ; end
|
#write(path, text) ⇒ Object
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
210
|
# File 'lib/detroit/shell_utils.rb', line 210
def zero?(path) ; FileTest.zero?(path) ; end
|