ratch 1.2.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/HISTORY.rdoc +87 -0
  3. data/README.rdoc +86 -66
  4. data/bin/ludo +26 -10
  5. data/lib/ratch/batch.rb +115 -74
  6. data/lib/ratch/core_ext/facets.rb +2 -1
  7. data/lib/ratch/core_ext/to_console.rb +11 -11
  8. data/lib/ratch/file_list.rb +6 -15
  9. data/lib/ratch/script.rb +1 -1
  10. data/lib/ratch/shell.rb +356 -518
  11. data/lib/ratch/utils/config.rb +4 -4
  12. data/lib/ratch/utils/email.rb +20 -97
  13. data/lib/ratch/utils/rdoc.rb +4 -7
  14. data/lib/ratch/utils/tar.rb +94 -50
  15. data/lib/ratch/utils/xdg.rb +2 -3
  16. data/lib/ratch/utils/zlib.rb +5 -4
  17. data/lib/ratch/version.rb +3 -0
  18. data/lib/ratch.rb +8 -24
  19. metadata +134 -157
  20. data/.ruby +0 -99
  21. data/History.rdoc +0 -35
  22. data/License.txt +0 -204
  23. data/Version +0 -1
  24. data/lib/ratch/console.rb +0 -199
  25. data/lib/ratch/system.rb +0 -15
  26. data/lib/ratch/utils/ftp.rb +0 -134
  27. data/lib/ratch/utils/pom.rb +0 -22
  28. data/lib/ratch.yml +0 -99
  29. data/man/ratch.1 +0 -28
  30. data/spec/01_shell.rdoc +0 -198
  31. data/spec/02_script.rdoc +0 -34
  32. data/spec/03_batch.rdoc +0 -71
  33. data/spec/04_system.rdoc +0 -3
  34. data/spec/applique/array.rb +0 -8
  35. data/spec/applique/setup.rb +0 -20
  36. data/test/case_batch.rb +0 -63
  37. data/test/case_shell.rb +0 -46
  38. data/test/core_ext/case_pathname.rb +0 -361
  39. data/test/helper.rb +0 -4
  40. data/test/utils/case_cli.rb +0 -6
  41. data/test/utils/case_config.rb +0 -12
  42. data/test/utils/case_email.rb +0 -10
  43. data/test/utils/case_ftp.rb +0 -6
  44. data/test/utils/case_pom.rb +0 -17
  45. data/test/utils/case_rdoc.rb +0 -23
  46. data/test/utils/case_tar.rb +0 -6
  47. data/test/utils/case_zlib.rb +0 -11
  48. data/test/utils/fixtures/pom_sample/Profile +0 -4
  49. data/test/utils/fixtures/rdoc_sample/README.rdoc +0 -4
  50. data/test/utils/fixtures/rdoc_sample/lib/rdoc_sample/rdoc_sample.rb +0 -9
  51. /data/{COPYING → LICENSE.txt} +0 -0
data/lib/ratch/shell.rb CHANGED
@@ -1,99 +1,106 @@
1
- require 'thread'
2
- #require 'rbconfig'
1
+ require 'pathname'
2
+ require 'fileutils'
3
3
  require 'ratch/core_ext'
4
4
  require 'ratch/batch'
5
5
 
6
6
  module Ratch
7
7
 
8
- # TODO: Better base class?
8
+ # Raised when a path that has to exist doesn't.
9
9
  class FileNotFound < StandardError
10
10
  end
11
11
 
12
12
  # = Shell Prompt class
13
13
  #
14
- # Ratch Shell object provides a limited file system shell in code.
15
- # It is similar to having a shell prompt available to you in Ruby.
14
+ # A Shell object is a file system shell in code: it stands at a
15
+ # directory, and paths given to its methods are taken from there, much
16
+ # as they are at a shell prompt.
17
+ #
18
+ # shell = Ratch::Shell.new('project')
19
+ #
20
+ # shell.file?('README.md')
21
+ # shell.mkdir_p('tmp/cache')
22
+ # shell.cp('README.md', 'tmp')
23
+ # shell.sh('git status')
24
+ #
25
+ # The process's own working directory is left alone, so shells at
26
+ # different directories can be used side by side, and from different
27
+ # threads.
28
+ #
29
+ # A shell can be put in a mode in which it only pretends:
30
+ #
31
+ # shell = Ratch::Shell.new('project', :dryrun=>true)
32
+ # shell.rm_r('tmp') # prints "rm -r tmp" and removes nothing
33
+ #
34
+ # NOTE: We use the term *trace* in place of *verbose* for the option
35
+ # that reports each step. Ruby's FileUtils calls it *verbose*, but that
36
+ # word is often wanted for a command's own purposes.
16
37
  #
17
- # NOTE: We have used the term *trace* in place of *verbose* for command
18
- # line options. Even though Ruby itself uses the term *verbose* with respect
19
- # to FileUtils, the term is commonly used for command specific needs, so we
20
- # want to leave it open for such cases.
21
38
  class Shell
22
39
 
23
- # New Shell object.
40
+ #
41
+ def self.[](*path)
42
+ new(*path)
43
+ end
44
+
45
+ # New Shell object. The path can be given in parts. Without one, the
46
+ # shell stands at the present working directory.
24
47
  #
25
48
  # Shell.new(:noop=>true)
26
49
  # Shell.new('..', :quiet=>true)
27
50
  #
28
- def initialize(*args)
29
- path, opts = parse_arguments(*args)
30
-
31
- opts.rekey!(&:to_sym)
51
+ # Options:
52
+ #
53
+ # :quiet print nothing; commands run with #sh have their output dropped
54
+ # :noop change nothing on disk and run no commands (also :nowrite)
55
+ # :trace report each step (also :verbose)
56
+ # :dryrun both :noop and :trace
57
+ #
58
+ def initialize(*path)
59
+ opts = (Hash === path.last ? path.pop : {})
60
+ opts = opts.each_with_object({}){ |(k, v), h| h[k.to_sym] = v }
32
61
 
33
62
  set_options(opts)
34
63
 
35
- if path.empty?
36
- path = Dir.pwd
37
- else
38
- path = File.join(*path)
39
- end
64
+ path = path.empty? ? Dir.pwd : File.join(*path.map{ |part| part.to_s })
40
65
 
41
- raise FileNotFound, "#{path}" unless ::File.exist?(path)
42
66
  raise FileNotFound, "#{path}" unless ::File.directory?(path)
43
67
 
44
68
  @_work = Pathname.new(path).expand_path
45
69
  end
46
70
 
47
- private
71
+ private
48
72
 
49
73
  #
50
- def parse_arguments(*args)
51
- opts = (Hash===args.last ? args.pop : {})
52
- return args, opts
74
+ def set_options(opts)
75
+ @_quiet = !!(opts[:quiet] || opts[:silent])
76
+ @_nowrite = !!(opts[:nowrite] || opts[:noop] || opts[:dryrun])
77
+ @_trace = !!(opts[:trace] || opts[:verbose] || opts[:dryrun])
53
78
  end
54
79
 
55
- #
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]
80
+ # The options a new shell needs to be in the same mode as this one.
81
+ def mode_options
82
+ {:quiet=>quiet?, :noop=>noop?, :trace=>trace?}
61
83
  end
62
84
 
63
- #
64
- def mutex
65
- @mutex ||= Mutex.new
66
- end
67
-
68
- public
69
-
70
- # Opertaton mode. This can be :noop, :verbose or :dryrun.
71
- # The later is the same as the first two combined.
72
- #def mode(opts=nil)
73
- # return @mode unless opts
74
- # opts.each do |key, val|
75
- # next unless val
76
- # case key
77
- # when :noop
78
- # @mode = (@mode == :verbose ? :dryrun : :noop)
79
- # when :verbose
80
- # @mode = (@mode == :noop ? :dryrun : :verbose)
81
- # when :dryrun
82
- # @mode = :dryrun
83
- # end
84
- # end
85
- #end
86
-
87
- def quiet? ; @_quiet ; end
88
- def trace? ; @_trace ; end
89
- def noop? ; @_noop ; end
90
- #def force? ; @_force ; end
91
-
92
- def dryrun? ; noop? && trace? ; end
85
+ public
86
+
87
+ def quiet? ; @_quiet ; end
88
+ def trace? ; @_trace ; end
89
+ def nowrite? ; @_nowrite ; end
90
+
91
+ def dryrun? ; nowrite? && trace? ; end
92
+
93
+ alias_method :verbose?, :trace?
94
+ alias_method :noop?, :nowrite?
93
95
 
94
96
  # String representation is work directory path.
95
97
  def to_s ; work.to_s ; end
96
98
 
99
+ #
100
+ def inspect
101
+ "#<#{self.class} #{work}>"
102
+ end
103
+
97
104
  # Two Shell's are equal if they have the same working path.
98
105
  def ==(other)
99
106
  return false unless other.is_a?(self.class)
@@ -109,111 +116,83 @@ module Ratch
109
116
  true
110
117
  end
111
118
 
119
+ #
120
+ def hash
121
+ [self.class, work, noop?].hash
122
+ end
123
+
112
124
  # Provides convenient starting points in the file system.
113
125
  #
114
126
  # root #=> #<Pathname:/>
115
127
  # home #=> #<Pathname:/home/jimmy>
116
128
  # work #=> #<Pathname:/home/jimmy/Documents>
117
129
  #
118
- # TODO: Replace these with Folio when Folio's is as capable.
119
-
120
- # Current root path.
121
- #def root(*args)
122
- # Pathname['/', *args]
123
- #end
124
-
125
- # Current home path.
126
- #def home(*args)
127
- # Pathname['~', *args].expand_path
128
- #end
129
-
130
- # Current working path.
131
- #def work(*args)
132
- # Pathname['.', *args]
133
- #end
134
-
135
- # TODO: Should these take *args?
130
+ # Each takes path parts to add, and the result must be a directory.
136
131
 
137
132
  # Root location.
138
- def root(*args)
139
- dir('/', *args)
133
+ def root(*parts)
134
+ dir(File.join('/', *parts.map{ |part| part.to_s }))
140
135
  end
141
136
 
142
137
  # Current home path.
143
- def home(*args)
144
- dir(File.expand_path('~'), *args)
138
+ def home(*parts)
139
+ dir(File.join(File.expand_path('~'), *parts.map{ |part| part.to_s }))
145
140
  end
146
141
 
147
142
  # Current working path.
148
- def work(*args)
149
- return @_work if args.empty?
150
- return dir(@_work, *args)
143
+ def work(*parts)
144
+ return @_work if parts.empty?
145
+ dir(File.join(*parts.map{ |part| part.to_s }))
151
146
  end
152
147
 
153
- # Alias for #work.
154
- #alias_method :pwd, :work
155
-
156
- # Return a new prompt with the same location.
157
- # NOTE: Use #dup or #clone ?
158
- #def new ; Shell.new(work) ; end
148
+ # Present working directory, as a String.
149
+ def pwd
150
+ work.to_s
151
+ end
159
152
 
153
+ # The directory above the working path.
160
154
  def parent
161
- dir('..')
155
+ work.parent
162
156
  end
163
157
 
164
- #
165
- #def [](name)
166
- # #FileObject[localize(name)]
167
- # #Pathname.new(localize(path))
168
- # Pathlist.new(localize(path))
169
- #end
170
-
171
158
  # Returns a Batch of file +patterns+.
172
159
  def batch(*patterns)
173
- Batch.new patterns.map{|pattern| localize(pattern)}
160
+ Batch.new(work, *patterns, mode_options)
174
161
  end
175
162
 
176
163
  # Returns a Batch of file +patterns+, without any exclusions.
177
164
  def batch_all(*patterns)
178
- Batch.all patterns.map{|pattern| localize(pattern)}
165
+ Batch.all(work, *patterns, mode_options)
179
166
  end
180
167
 
181
- #
168
+ # A Pathname for a path, which need not exist.
182
169
  def path(path)
183
170
  Pathname.new(localize(path))
184
171
  end
185
172
  alias_method :pathname, :path
186
173
 
187
- #
174
+ # A Pathname for a file, which must exist.
188
175
  def file(path)
189
- #FileObject[name]
190
176
  path = localize(path)
191
- raise FileNotFound unless File.file?(path)
177
+ raise FileNotFound, path unless File.file?(path)
192
178
  Pathname.new(path)
193
179
  end
194
180
 
195
- #def doc(name)
196
- # Document.new(name)
197
- #end
198
-
199
- #
181
+ # A Pathname for a directory, which must exist.
200
182
  def dir(path)
201
- #Directory.new(name)
202
183
  path = localize(path)
203
- raise FileNotFound unless File.directory?(path)
184
+ raise FileNotFound, path unless File.directory?(path)
204
185
  Pathname.new(path)
205
186
  end
206
187
 
207
- # Lists all entries.
188
+ # Lists all entries, as Dir.entries does, but as Pathnames.
208
189
  def entries
209
190
  work.entries
210
191
  end
211
192
 
212
- #alias_method :ls, :entries
213
-
214
193
  # Lists directory entries.
215
194
  def directory_entries
216
- entries.select{ |d| d.directory? }
195
+ entries.select{ |d| (work + d).directory? }
217
196
  end
218
197
 
219
198
  #
@@ -221,506 +200,355 @@ module Ratch
221
200
 
222
201
  # Lists file entries.
223
202
  def file_entries
224
- entries.select{ |f| f.file? }
203
+ entries.select{ |f| (work + f).file? }
225
204
  end
226
205
 
227
- # Likes entries but omits '.' and '..' paths.
206
+ # Like #entries but omits the '.' and '..' paths.
228
207
  def pathnames
229
- work.entries - %w{. ..}.map{|f|Pathname.new(f)}
208
+ work.children(false)
230
209
  end
231
210
 
232
211
  # Returns list of directories.
233
212
  def directories
234
- pathnames.select{ |f| f.directory? }
213
+ pathnames.select{ |d| (work + d).directory? }
235
214
  end
236
215
  alias_method :dirs, :directories
237
216
  alias_method :folders, :directories
238
217
 
239
218
  # Returns list of files.
240
219
  def files
241
- pathnames.select{ |f| f.file? }
220
+ pathnames.select{ |f| (work + f).file? }
242
221
  end
243
222
 
244
- # Glob pattern. Returns matches as strings.
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
257
-
258
- # Glob files.
259
- #def glob(*args, &blk)
260
- # Dir.glob(*args, &blk)
261
- #end
262
-
263
- # TODO: Ultimately merge #glob and #multiglob.
264
- def multiglob(*args, &blk)
265
- Dir.multiglob(*args, &blk)
266
- end
267
-
268
- def multiglob_r(*args, &blk)
269
- Dir.multiglob_r(*args, &blk)
270
- end
271
-
272
- =begin
273
- # Match pattern. Like #glob but returns file objects.
274
- # TODO: There is no FileObject any more. Should there be?
275
- def match(*patterns, &block)
276
- opts = (::Integer===patterns.last ? patterns.pop : 0)
277
- patterns = localize(patterns)
278
- matches = patterns.map{ |pattern| ::Dir.glob(pattern, opts) }.flatten
279
- matches = matches.map{ |f| FileObject[f] }
280
- if block_given?
281
- matches.each(&block)
282
- else
283
- matches
284
- end
285
- end
286
- =end
287
-
288
- # Join paths.
289
- # TODO: Should this return a new directory object? Or should it change directories?
223
+ # A new shell at a directory under this one, in the same mode.
224
+ #
225
+ # lib = shell / 'lib'
226
+ #
290
227
  def /(path)
291
- #@_work += dir # did not work, why?
292
- @_work = dir(localize(path))
293
- self
228
+ self.class.new(dir(path), mode_options)
294
229
  end
295
230
 
296
231
  # Alias for #/.
297
- alias_method '+', '/'
232
+ alias_method :+, :/
298
233
 
299
- # TODO: Tie this into the System class.
300
- def system(cmd)
301
- locally do
302
- super(cmd)
303
- end
234
+ # Run a command from the working directory, as Kernel#system does,
235
+ # whatever the mode.
236
+ def system(*cmd)
237
+ opts = (Hash === cmd.last ? cmd.pop.dup : {})
238
+ opts[:chdir] ||= work.to_s
239
+ ::Kernel.system(*cmd, **opts)
304
240
  end
305
241
 
306
- # Shell runner.
307
- def sh(cmd)
308
- #puts "--> system call: #{cmd}" if trace?
309
- puts cmd if trace?
242
+ # Shell runner. Runs a command from the working directory and returns
243
+ # true if it succeeded. In trace mode the command is printed first; in
244
+ # noop mode it is not run; in quiet mode its output is dropped.
245
+ #
246
+ # shell.sh 'make install'
247
+ # shell.sh 'git', 'commit', '-m', message
248
+ #
249
+ def sh(*cmd)
250
+ $stderr.puts cmd.join(' ') if trace?
310
251
  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
319
-
320
- # Shell runner.
321
- #def sh(cmd)
322
- # if dryrun?
323
- # puts cmd
324
- # true
325
- # else
326
- # puts "--> system call: #{cmd}" if trace?
327
- # if quiet?
328
- # silently{ system(cmd) }
329
- # else
330
- # system(cmd)
331
- # end
332
- # end
333
- #end
252
+ if quiet?
253
+ system(*cmd, :out=>File::NULL, :err=>File::NULL)
254
+ else
255
+ system(*cmd)
256
+ end
257
+ end
334
258
 
335
259
  # Change working directory.
336
260
  #
337
- # TODO: Make thread safe.
261
+ # With a block, the shell stands at +path+ for the length of the block
262
+ # and then returns to where it was. The process's working directory is
263
+ # changed too for that time, so that code in the block which doesn't
264
+ # go through the shell sees the same directory. Ruby allows only one
265
+ # such block at a time across threads.
338
266
  #
339
267
  def cd(path, &block)
340
268
  if block
341
269
  work_old = @_work
342
270
  begin
343
- @_work = dir(localize(path))
344
- locally(&block)
345
- #mutex.synchronize do
346
- # Dir.chdir(@_work){ block.call }
347
- #end
271
+ @_work = dir(path)
272
+ Dir.chdir(@_work.to_s){ block.call(self) }
348
273
  ensure
349
274
  @_work = work_old
350
275
  end
351
276
  else
352
- @_work = dir(localize(path))
277
+ @_work = dir(path)
353
278
  end
354
279
  end
355
280
 
356
281
  #
357
282
  alias_method :chdir, :cd
358
283
 
359
- # Bonus FileUtils features.
360
- #def cd(*a,&b)
361
- # puts "cd #{a}" if dryrun? or trace?
362
- # fileutils.chdir(*a,&b)
363
- #end
284
+ # -- FileTest --
364
285
 
365
- # -- File IO Shortcuts -----------------------------------------------
286
+ # File tests, which take one path.
287
+ FILE_TESTS = %w{
288
+ size size? directory? symlink? readable? chardev? exist?
289
+ zero? pipe? file? sticky? blockdev? grpowned? setgid? setuid?
290
+ socket? owned? writable? executable? readable_real?
291
+ writable_real? executable_real? world_readable? world_writable?
292
+ }
366
293
 
367
- # Read file.
368
- def read(path)
369
- File.read(localize(path))
294
+ FILE_TESTS.each do |name|
295
+ module_eval <<-EOS, __FILE__, __LINE__ + 1
296
+ def #{name}(path)
297
+ ::FileTest.#{name}(localize(path))
298
+ end
299
+ EOS
370
300
  end
371
301
 
372
- # Write file.
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
302
+ alias_method :dir?, :directory?
377
303
 
378
- # Append to file.
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
304
+ # Ruby dropped +exists?+ in 3.2; it is kept here.
305
+ alias_method :exists?, :exist?
383
306
 
307
+ # Is the path safe from the meddling of others?
308
+ def safe?(path)
309
+ ::FileTest.safe?(localize(path))
310
+ end
384
311
 
385
- #############
386
- # FileTest #
387
- #############
388
-
389
- #
390
- def size(path) ; FileTest.size(localize(path)) ; end
391
- def size?(path) ; FileTest.size?(localize(path)) ; end
392
- def directory?(path) ; FileTest.directory?(localize(path)) ; end
393
- def symlink?(path) ; FileTest.symlink?(localize(path)) ; end
394
- def readable?(path) ; FileTest.readable?(localize(path)) ; end
395
- def chardev?(path) ; FileTest.chardev?(localize(path)) ; end
396
- def exist?(path) ; FileTest.exist?(localize(path)) ; end
397
- def exists?(path) ; FileTest.exists?(localize(path)) ; end
398
- def zero?(path) ; FileTest.zero?(localize(path)) ; end
399
- def pipe?(path) ; FileTest.pipe?(localize(path)) ; end
400
- def file?(path) ; FileTest.file?(localize(path)) ; end
401
- def sticky?(path) ; FileTest.sticky?(localize(path)) ; end
402
- def blockdev?(path) ; FileTest.blockdev?(localize(path)) ; end
403
- def grpowned?(path) ; FileTest.grpowned?(localize(path)) ; end
404
- def setgid?(path) ; FileTest.setgid?(localize(path)) ; end
405
- def setuid?(path) ; FileTest.setuid?(localize(path)) ; end
406
- def socket?(path) ; FileTest.socket?(localize(path)) ; end
407
- def owned?(path) ; FileTest.owned?(localize(path)) ; end
408
- def writable?(path) ; FileTest.writable?(localize(path)) ; end
409
- def executable?(path) ; FileTest.executable?(localize(path)) ; end
410
-
411
- def safe?(path) ; FileTest.safe?(localize(path)) ; end
412
-
413
- def relative?(path) ; FileTest.relative?(path) ; end
414
- def absolute?(path) ; FileTest.absolute?(path) ; end
415
-
416
- def writable_real?(path) ; FileTest.writable_real?(localize(path)) ; end
417
- def executable_real?(path) ; FileTest.executable_real?(localize(path)) ; end
418
- def readable_real?(path) ; FileTest.readable_real?(localize(path)) ; end
312
+ def relative?(path); ::FileTest.relative?(path.to_s); end
313
+ def absolute?(path); ::FileTest.absolute?(path.to_s); end
419
314
 
315
+ # Are the two paths the same file?
420
316
  def identical?(path, other)
421
- FileTest.identical?(localize(path), localize(other))
422
- end
423
- alias_method :compare_file, :identical?
424
-
425
- # Assert that a path exists.
426
- #def exists?(path)
427
- # paths = Dir.glob(path)
428
- # paths.not_empty?
429
- #end
430
- #alias_method :exist?, :exists? #; module_function :exist?
431
- #alias_method :path?, :exists? #; module_function :path?
432
-
433
- # Is a given path a regular file? If +path+ is a glob
434
- # then checks to see if all matches are regular files.
435
- #def file?(path)
436
- # paths = Dir.glob(path)
437
- # paths.not_empty? && paths.all?{ |f| FileTest.file?(f) }
438
- #end
439
-
440
- # Is a given path a directory? If +path+ is a glob
441
- # checks to see if all matches are directories.
442
- #def dir?(path)
443
- # paths = Dir.glob(path)
444
- # paths.not_empty? && paths.all?{ |f| FileTest.directory?(f) }
445
- #end
446
- #alias_method :directory?, :dir? #; module_function :directory?
447
-
448
-
449
- #############
450
- # FileUtils #
451
- #############
452
-
453
- # Low-level Methods Omitted
454
- # -------------------------
455
- # getwd -> pwd
456
- # compare_file -> cmp
457
- # remove_file -> rm
458
- # copy_file -> cp
459
- # remove_dir -> rmdir
460
- # safe_unlink -> rm_f
461
- # makedirs -> mkdir_p
462
- # rmtree -> rm_rf
463
- # copy_stream
464
- # remove_entry
465
- # copy_entry
466
- # remove_entry_secure
467
- # compare_stream
468
-
469
- # Present working directory.
470
- def pwd
471
- work.to_s
317
+ ::FileTest.identical?(localize(path), localize(other))
472
318
  end
473
319
 
474
- # Same as #identical?
475
- def cmp(a,b)
476
- fileutils.compare_file(a,b)
320
+ # Do the two files have the same content?
321
+ def cmp(path, other)
322
+ ::FileUtils.compare_file(localize(path), localize(other))
477
323
  end
324
+ alias_method :compare_file, :cmp
478
325
 
326
+ # -- FileUtils --
327
+ #
328
+ # Each takes the options of its FileUtils namesake. The shell's mode
329
+ # sets :noop and :verbose.
479
330
  #
480
- def mkdir(dir, options={})
481
- dir = localize(dir)
482
- fileutils.mkdir(dir, options)
331
+ # These low-level FileUtils methods have been omitted.
332
+ #
333
+ # * getwd -> pwd
334
+ # * remove_file -> rm
335
+ # * copy_file -> cp
336
+ # * remove_dir -> rmdir
337
+ # * safe_unlink -> rm_f
338
+ # * copy_stream
339
+ # * remove_entry
340
+ # * copy_entry
341
+ # * remove_entry_secure
342
+ # * compare_stream
343
+
344
+ # Methods taking a path or a list of paths.
345
+ %w{mkdir mkdir_p rmdir rm rm_r rm_f rm_rf touch}.each do |name|
346
+ module_eval <<-EOS, __FILE__, __LINE__ + 1
347
+ def #{name}(list, options={})
348
+ fu(:#{name}, options, list){ |opts| ::FileUtils.#{name}(localize(list), **opts) }
349
+ end
350
+ EOS
483
351
  end
484
352
 
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
491
353
  alias_method :mkpath, :mkdir_p
354
+ alias_method :makedirs, :mkdir_p
355
+ alias_method :remove, :rm
356
+ alias_method :rmtree, :rm_rf
492
357
 
493
- def rmdir(dir, options={})
494
- dir = localize(dir)
495
- fileutils.rmdir(dir, options)
358
+ # Methods taking a source, or a list of them, and a destination.
359
+ %w{ln ln_s ln_sf cp cp_r mv}.each do |name|
360
+ module_eval <<-EOS, __FILE__, __LINE__ + 1
361
+ def #{name}(src, dest, options={})
362
+ fu(:#{name}, options, src, dest){ |opts| ::FileUtils.#{name}(localize(src), localize(dest), **opts) }
363
+ end
364
+ EOS
496
365
  end
497
366
 
498
- # ln(list, destdir, options={})
499
- def ln(old, new, options={})
500
- old = localize(old)
501
- new = localize(new)
502
- fileutils.ln(old, new, options)
503
- end
504
367
  alias_method :link, :ln
505
-
506
- # ln_s(list, destdir, options={})
507
- def ln_s(old, new, options={})
508
- old = localize(old)
509
- new = localize(new)
510
- fileutils.ln_s(old, new, options)
511
- end
512
368
  alias_method :symlink, :ln_s
513
-
514
- def ln_sf(old, new, options={})
515
- old = localize(old)
516
- new = localize(new)
517
- fileutils.ln_sf(old, new, options)
518
- end
519
-
520
- # cp(list, dir, options={})
521
- def cp(src, dest, options={})
522
- src = localize(src)
523
- dest = localize(dest)
524
- fileutils.cp(src, dest, options)
525
- end
526
369
  alias_method :copy, :cp
527
-
528
- # cp_r(list, dir, options={})
529
- def cp_r(src, dest, options={})
530
- src = localize(src)
531
- dest = localize(dest)
532
- fileutils.cp_r(src, dest, options)
533
- end
534
-
535
- # mv(list, dir, options={})
536
- def mv(src, dest, options={})
537
- src = localize(src)
538
- dest = localize(dest)
539
- fileutils.mv(src, dest, options)
540
- end
541
370
  alias_method :move, :mv
542
371
 
543
- def rm(list, options={})
544
- list = localize(list)
545
- fileutils.rm(list, options)
546
- end
547
- alias_method :remove, :rm
548
-
549
- def rm_r(list, options={})
550
- list = localize(list)
551
- fileutils.rm_r(list, options)
552
- end
553
-
554
- def rm_f(list, options={})
555
- list = localize(list)
556
- fileutils.rm_f(list, options)
557
- end
558
-
559
- def rm_rf(list, options={})
560
- list = localize(list)
561
- fileutils.rm_rf(list, options)
562
- end
563
-
564
- def install(src, dest, mode, options={})
565
- src = localize(src)
566
- dest = localize(dest)
567
- fileutils.install(src, dest, mode, options)
372
+ # Copy +src+ to +dest+, setting its mode. The mode can be given as the
373
+ # third argument or as the :mode option.
374
+ def install(src, dest, mode=nil, options={})
375
+ options, mode = mode, nil if Hash === mode
376
+ options = options.merge(:mode=>mode) if mode
377
+ fu(:install, options, src, dest){ |opts| ::FileUtils.install(localize(src), localize(dest), **opts) }
568
378
  end
569
379
 
380
+ #
570
381
  def chmod(mode, list, options={})
571
- list = localize(list)
572
- fileutils.chmod(mode, list, options)
382
+ fu(:chmod, options, (Integer === mode ? '%o' % mode : mode), list){ |opts| ::FileUtils.chmod(mode, localize(list), **opts) }
573
383
  end
574
384
 
575
- def chmod_r(mode, list, options={})
576
- list = localize(list)
577
- fileutils.chmod_r(mode, list, options)
385
+ #
386
+ def chmod_R(mode, list, options={})
387
+ fu(:chmod_R, options, (Integer === mode ? '%o' % mode : mode), list){ |opts| ::FileUtils.chmod_R(mode, localize(list), **opts) }
578
388
  end
579
- #alias_method :chmod_R, :chmod_r
389
+ alias_method :chmod_r, :chmod_R
580
390
 
391
+ #
581
392
  def chown(user, group, list, options={})
582
- list = localize(list)
583
- fileutils.chown(user, group, list, options)
393
+ fu(:chown, options, [user, group].compact.join(':'), list){ |opts| ::FileUtils.chown(user, group, localize(list), **opts) }
584
394
  end
585
395
 
586
- def chown_r(user, group, list, options={})
587
- list = localize(list)
588
- fileutils.chown_r(user, group, list, options)
396
+ #
397
+ def chown_R(user, group, list, options={})
398
+ fu(:chown_R, options, [user, group].compact.join(':'), list){ |opts| ::FileUtils.chown_R(user, group, localize(list), **opts) }
589
399
  end
590
- #alias_method :chown_R, :chown_r
400
+ alias_method :chown_r, :chown_R
591
401
 
592
- def touch(list, options={})
593
- list = localize(list)
594
- fileutils.touch(list, options)
402
+ # Is +path+ newer than all of +sources+?
403
+ def uptodate?(path, *sources)
404
+ ::FileUtils.uptodate?(localize(path), localize(sources.flatten))
595
405
  end
596
406
 
597
- #
598
- # TODO: should this have SOURCE diectory?
599
- # stage(directory, source_dir, files)
600
- #
601
- def stage(stage_dir, files)
602
- #dir = localize(directory)
603
- #files = localize(files)
604
- locally do
605
- fileutils.stage(stage_dir, work, files)
407
+ # Does +path+ need updating, going by +sources+, which can be globs?
408
+ # True if it doesn't exist or is older than any of them.
409
+ def outofdate?(path, *sources)
410
+ path = localize(path)
411
+ return true unless File.exist?(path)
412
+ mtimes = glob(*sources.flatten).map{ |file| File.mtime(localize(file)) }
413
+ return true if mtimes.empty?
414
+ File.mtime(path) < mtimes.max
415
+ end
416
+
417
+ # Hard link +files+, which are relative to the working directory, into
418
+ # a staging directory, making it afresh.
419
+ def stage(stage_dir, files, options={})
420
+ rm_r(stage_dir, options) if directory?(stage_dir)
421
+ mkdir_p(stage_dir, options)
422
+ [files].flatten.each do |file|
423
+ dest = File.join(stage_dir.to_s, file.to_s)
424
+ if directory?(file)
425
+ mkdir_p(dest, options)
426
+ else
427
+ mkdir_p(File.dirname(dest), options) unless directory?(File.dirname(dest))
428
+ ln(file, dest, options)
429
+ end
606
430
  end
431
+ path(stage_dir)
607
432
  end
608
433
 
609
- # An intergrated glob like method that takes a set of include globs,
610
- # exclude globs and ignore globs to produce a collection of paths.
434
+ # A glob-like method that takes a set of include globs, exclude globs
435
+ # and ignore globs and gives the paths that are left. A directory
436
+ # stands for everything in it.
611
437
  #
612
- # Ignore_globs differ from exclude_globs in that they match by
613
- # the basename of the path rather than the whole pathname.
438
+ # The ignore globs differ from the exclude globs in that they are
439
+ # matched against the base name of a path, not the whole of it.
614
440
  #
615
441
  def amass(include_globs, exclude_globs=[], ignore_globs=[])
616
- locally do
617
- fileutils.amass(include_globs, exclude_globs, ignore_globs)
442
+ expand = lambda do |globs|
443
+ [globs].flatten.map{ |g| directory?(g) ? [g, File.join(g.to_s, '**/*')] : g }.flatten
444
+ end
445
+ files = glob(*expand.call(include_globs)) - glob(*expand.call(exclude_globs))
446
+ files = files.uniq
447
+ files.reject do |f|
448
+ [ignore_globs].flatten.any?{ |x| File.fnmatch?(x, File.basename(f)) }
618
449
  end
619
450
  end
620
451
 
621
- #
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
452
+ # -- File --
453
+
454
+ # Get creation timestamp.
455
+ def ctime(path)
456
+ File.ctime(localize(path))
627
457
  end
628
458
 
629
- # # Does a path need updating, based on given +sources+?
630
- # # This compares mtimes of give paths. Returns false
631
- # # if the path needs to be updated.
632
- # #
633
- # # TODO: Put this in FileTest instead?
634
- #
635
- # def out_of_date?(path, *sources)
636
- # return true unless File.exist?(path)
637
- #
638
- # sources = sources.collect{ |source| Dir.glob(source) }.flatten
639
- # mtimes = sources.collect{ |file| File.mtime(file) }
640
- #
641
- # return true if mtimes.empty? # TODO: This the way to go here?
642
- #
643
- # File.mtime(path) < mtimes.max
644
- # end
459
+ # Get, or with a time set, the last access timestamp.
460
+ def atime(path, time=nil, options={})
461
+ options, time = time, nil if Hash === time
462
+ return File.atime(localize(path)) unless time
463
+ utime(time, File.mtime(localize(path)), path, options)
464
+ end
645
465
 
646
- #
647
- def uptodate?(path, *sources)
648
- locally do
649
- fileutils.uptodate?(path, sources.flatten)
650
- end
466
+ # Get, or with a time set, the last modification timestamp.
467
+ def mtime(path, time=nil, options={})
468
+ options, time = time, nil if Hash === time
469
+ return File.mtime(localize(path)) unless time
470
+ utime(File.atime(localize(path)), time, path, options)
651
471
  end
652
472
 
653
- #
654
- #def uptodate?(new, old_list, options=nil)
655
- # new = localize(new)
656
- # old = localize(old_list)
657
- # fileutils.uptodate?(new, old, options)
658
- #end
473
+ # Set the access and modification timestamps of files.
474
+ def utime(atime, mtime, *files)
475
+ opts = fu_options(Hash === files.last ? files.pop : {})
476
+ $stderr.puts "utime #{atime} #{mtime} #{files.join(' ')}" if opts[:verbose]
477
+ File.utime(atime, mtime, *localize(files.flatten)) unless opts[:noop]
478
+ end
659
479
 
660
- =begin
661
- # TODO: Deprecate these?
480
+ # Read file.
481
+ def read(path)
482
+ File.read(localize(path))
483
+ end
662
484
 
663
- # Assert that a path exists.
664
- def exists!(*paths)
665
- abort "path not found #{path}" unless paths.any?{|path| exists?(path)}
485
+ # Write file.
486
+ def write(path, text, options={})
487
+ opts = fu_options(options)
488
+ $stderr.puts "write #{path}" if opts[:verbose]
489
+ File.open(localize(path), 'w'){ |f| f << text } unless opts[:noop]
666
490
  end
667
- alias_method :exist!, :exists! #; module_function :exist!
668
- alias_method :path!, :exists! #; module_function :path!
669
491
 
670
- # Assert that a given path is a file.
671
- def file!(*paths)
672
- abort "file not found #{path}" unless paths.any?{|path| file?(path)}
492
+ # Append to file.
493
+ def append(path, text, options={})
494
+ opts = fu_options(options)
495
+ $stderr.puts "append #{path}" if opts[:verbose]
496
+ File.open(localize(path), 'a'){ |f| f << text } unless opts[:noop]
673
497
  end
674
498
 
675
- # Assert that a given path is a directory.
676
- def dir!(*paths)
677
- paths.each do |path|
678
- abort "Directory not found: '#{path}'." unless dir?(path)
499
+ # -- Dir --
500
+
501
+ # Glob patterns. Returns the matches as strings, relative to the
502
+ # working directory unless a pattern is absolute. A trailing Integer
503
+ # is taken as File::FNM flags.
504
+ def glob(*patterns, &block)
505
+ flags = (::Integer === patterns.last ? patterns.pop : 0)
506
+ matches = []
507
+ patterns.flatten.each do |pattern|
508
+ pattern = pattern.to_s
509
+ if absolute?(pattern)
510
+ matches.concat(Dir.glob(pattern, flags))
511
+ else
512
+ matches.concat(Dir.glob(pattern, flags, :base=>work.to_s))
513
+ end
514
+ end
515
+ if block
516
+ matches.each(&block)
517
+ else
518
+ matches
679
519
  end
680
520
  end
681
- alias_method :directory!, :dir! #; module_function :directory!
682
- =end
683
521
 
684
- #private ?
522
+ private
685
523
 
686
- # Returns a path local to the current working path.
524
+ # Returns a path local to the current working path. An absolute path
525
+ # is left as it is, and so is nil. Given a list, localizes each.
687
526
  def localize(local_path)
688
- # some path arguments are optional
689
- return local_path unless local_path
690
- #
691
527
  case local_path
528
+ when nil
529
+ nil
692
530
  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
531
+ local_path.map{ |lp| localize(lp) }
700
532
  else
701
- # do not localize an absolute path
533
+ local_path = local_path.to_s
702
534
  return local_path if absolute?(local_path)
703
535
  File.expand_path(File.join(work.to_s, local_path))
704
- #(work + local_path).expand_path.to_s
705
536
  end
706
537
  end
707
538
 
708
- # Change directory to the shell's work directory,
709
- # process the +block+ and then return to user directory.
539
+ # Run a block with the process's working directory set to the shell's.
540
+ # Nothing in Shell itself needs this; it is for extensions that call
541
+ # code which only knows the process's directory.
710
542
  def locally(&block)
711
543
  if work.to_s == Dir.pwd
712
544
  block.call
713
545
  else
714
- mutex.synchronize do
715
- #work.chdir(&block)
716
- Dir.chdir(work, &block)
717
- end
546
+ Dir.chdir(work.to_s, &block)
718
547
  end
719
548
  end
720
549
 
721
550
  # TODO: Should naming policy be in a utility extension module?
722
551
 
723
- #
724
552
  #
725
553
  def naming_policy(*policies)
726
554
  if policies.empty?
@@ -730,7 +558,6 @@ module Ratch
730
558
  end
731
559
  end
732
560
 
733
- #
734
561
  #
735
562
  def apply_naming_policy(name, ext)
736
563
  naming_policy.each do |policy|
@@ -748,36 +575,47 @@ module Ratch
748
575
  name
749
576
  end
750
577
 
751
- private
752
-
753
- # Returns FileUtils module based on mode.
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
578
+ # How each file operation reads in a trace.
579
+ FU_COMMANDS = {
580
+ :mkdir_p=>'mkdir -p', :rm_r=>'rm -r', :rm_f=>'rm -f', :rm_rf=>'rm -rf',
581
+ :ln_s=>'ln -s', :ln_sf=>'ln -sf', :cp_r=>'cp -r',
582
+ :chmod_R=>'chmod -R', :chown_R=>'chown -R'
583
+ }
584
+
585
+ # Carry out a file operation in the shell's mode: report it if
586
+ # tracing, with the paths as they were given, and yield the options
587
+ # for FileUtils unless this is a noop.
588
+ def fu(name, options, *args)
589
+ opts = fu_options(options)
590
+ noop = opts.delete(:noop)
591
+ verbose = opts.delete(:verbose)
592
+ if verbose
593
+ $stderr.puts [FU_COMMANDS[name] || name.to_s, *args.flatten].join(' ')
763
594
  end
595
+ yield(opts) unless noop
764
596
  end
765
597
 
766
- # This may be used by script commands to allow for per command
767
- # noop and trace options. Global options have precedence.
768
- def util_options(options)
769
- noop = noop? || options[:noop] || options[:dryrun]
770
- trace = trace? || options[:trace] || options[:dryrun]
771
- return noop, trace
598
+ # The options given for one command, with :noop and :verbose turned
599
+ # on if the shell's mode says so. The shell's own names for them
600
+ # (:trace, :nowrite, :dryrun) are accepted per command as well.
601
+ def fu_options(options)
602
+ opts = {}
603
+ (options || {}).each{ |k, v| opts[k.to_sym] = v }
604
+ given = [:dryrun, :nowrite, :noop, :trace, :verbose].select{ |k| opts.delete(k) }
605
+ dryrun = given.include?(:dryrun)
606
+ opts[:noop] = true if noop? || dryrun || given.include?(:nowrite) || given.include?(:noop)
607
+ opts[:verbose] = true if trace? || dryrun || given.include?(:trace) || given.include?(:verbose)
608
+ opts
772
609
  end
773
610
 
774
- public#class
775
-
776
- def self.[](path)
777
- new(path)
611
+ # This may be used by utility extensions to allow for per command
612
+ # noop and trace options. The shell's mode has precedence. Returns
613
+ # the two as a pair.
614
+ def util_options(options)
615
+ opts = fu_options(options)
616
+ return !!opts[:noop], !!opts[:verbose]
778
617
  end
779
618
 
780
619
  end
781
620
 
782
621
  end
783
-