ratch 1.1.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 (61) hide show
  1. checksums.yaml +7 -0
  2. data/HISTORY.rdoc +87 -0
  3. data/LICENSE.txt +204 -0
  4. data/README.rdoc +133 -0
  5. data/bin/ludo +32 -0
  6. data/bin/ratch +1 -8
  7. data/lib/ratch/batch.rb +541 -0
  8. data/lib/ratch/core_ext/facets.rb +16 -1
  9. data/lib/ratch/core_ext/filetest.rb +29 -0
  10. data/lib/ratch/core_ext/{string.rb → to_actual_filename.rb} +0 -23
  11. data/lib/ratch/core_ext/to_console.rb +30 -12
  12. data/lib/ratch/core_ext/{object.rb → to_yamlfrag.rb} +1 -0
  13. data/lib/ratch/core_ext/unfold_paragraphs.rb +27 -0
  14. data/lib/ratch/core_ext.rb +1 -4
  15. data/lib/ratch/file_list.rb +402 -0
  16. data/lib/ratch/script/help.rb +84 -0
  17. data/lib/ratch/script.rb +99 -5
  18. data/lib/ratch/shell.rb +621 -0
  19. data/lib/ratch/utils/cli.rb +49 -0
  20. data/lib/ratch/utils/config.rb +52 -0
  21. data/lib/ratch/{emailer.rb → utils/email.rb} +58 -98
  22. data/lib/ratch/utils/rdoc.rb +45 -0
  23. data/lib/ratch/utils/tar.rb +132 -0
  24. data/lib/ratch/utils/xdg.rb +38 -0
  25. data/lib/ratch/utils/zlib.rb +55 -0
  26. data/lib/ratch/version.rb +3 -0
  27. data/lib/ratch.rb +12 -0
  28. metadata +160 -126
  29. data/COPYING +0 -22
  30. data/HISTORY +0 -6
  31. data/MANIFEST +0 -53
  32. data/NEWS +0 -12
  33. data/README +0 -87
  34. data/VERSION +0 -1
  35. data/demo/tryme-task.ratch +0 -12
  36. data/demo/tryme1.ratch +0 -6
  37. data/doc/log/basic_stats/index.html +0 -39
  38. data/doc/log/notes.xml +0 -18
  39. data/doc/log/stats.log +0 -14
  40. data/doc/log/syntax.log +0 -0
  41. data/doc/log/testunit.log +0 -156
  42. data/lib/ratch/commandline.rb +0 -16
  43. data/lib/ratch/core_ext/pathname.rb +0 -38
  44. data/lib/ratch/dsl.rb +0 -420
  45. data/lib/ratch/index.rb +0 -4
  46. data/lib/ratch/io.rb +0 -116
  47. data/lib/ratch/plugin.rb +0 -65
  48. data/lib/ratch/service.rb +0 -33
  49. data/lib/ratch/task.rb +0 -249
  50. data/lib/ratch/task2.rb +0 -298
  51. data/man/ratch.1 +0 -28
  52. data/meta/abstract +0 -4
  53. data/meta/author +0 -1
  54. data/meta/contact +0 -1
  55. data/meta/homepage +0 -1
  56. data/meta/name +0 -1
  57. data/meta/requires +0 -4
  58. data/meta/summary +0 -1
  59. data/test/README +0 -1
  60. data/test/test_helper.rb +0 -4
  61. data/test/test_task.rb +0 -46
@@ -0,0 +1,541 @@
1
+ require 'ratch/core_ext'
2
+ require 'ratch/file_list'
3
+
4
+ module Ratch
5
+
6
+ # Proccess a list of files in batch.
7
+ #
8
+ # The Batch interface mimics the Shell class in most respects.
9
+ #
10
+ # TODO: Should FileList use Pathname, or only in Batch?
11
+ class Batch
12
+ include Enumerable
13
+
14
+ # A batch of the files matching +patterns+ under the present
15
+ # directory.
16
+ def self.[](*patterns)
17
+ new('.', *patterns)
18
+ end
19
+
20
+ # Like #new, but without the usual exclusions (version control
21
+ # directories, backup and core files).
22
+ def self.all(local, *patterns)
23
+ options = (Hash === patterns.last ? patterns.pop.dup : {})
24
+ new(local, *patterns, options.merge(:all=>true))
25
+ end
26
+
27
+ # New batch of the files matching +patterns+, which are relative to
28
+ # the directory +local+. A final Hash gives options: :noop, :verbose
29
+ # (or :trace) and :dryrun, as for Shell, and :all to leave nothing out.
30
+ def initialize(local, *patterns)
31
+ @local = Pathname.new(local.to_s)
32
+ @options = {}
33
+ (Hash === patterns.last ? patterns.pop : {}).each{ |k, v| @options[k.to_sym] = v }
34
+
35
+ @file_list = @options[:all] ? FileList.all : FileList.new
36
+
37
+ patterns.flatten.each do |pattern|
38
+ pattern = pattern.to_s
39
+ if @local == Pathname.new('.') || FileTest.absolute?(pattern)
40
+ @file_list.add(pattern)
41
+ else
42
+ @file_list.add(File.join(@local.to_s, pattern))
43
+ end
44
+ end
45
+ end
46
+
47
+ #
48
+ attr :local
49
+
50
+ # Returns the the underlying FileList object.
51
+ def file_list
52
+ @file_list
53
+ end
54
+
55
+ # Iterate over pathnames.
56
+ def each(&block)
57
+ @file_list.each{ |file| block.call(Pathname.new(file)) }
58
+ end
59
+
60
+ # Returns the Integer size of the list of files.
61
+ def size
62
+ @file_list.size
63
+ end
64
+
65
+ # Return the list of files as Pathname objects.
66
+ def to_a
67
+ @file_list.map{ |file| Pathname.new(file) }
68
+ end
69
+
70
+ # Return the list of files as Pathname objects.
71
+ alias_method :pathnames, :to_a
72
+
73
+ # Returns the list of files as Strings, rather than Pathname objects.
74
+ def list
75
+ @file_list.to_a
76
+ end
77
+ alias_method :filenames, :list
78
+
79
+ # Returns an Array of file paths relative to +local+.
80
+ def entries
81
+ map{ |entry| entry.sub(local.to_s+'/','') }
82
+ end
83
+
84
+ # Limit list to files.
85
+ def file!
86
+ @file_list = @file_list.select{ |f| File.file?(f) }
87
+ end
88
+
89
+ # Limit list to directories.
90
+ def directory!
91
+ @file_list = @file_list.select{ |f| File.directory?(f) }
92
+ end
93
+
94
+ # Limit list to selection block.
95
+ def select!(&block)
96
+ #@file_list = FileList.all(*to_a.select{ |f| block.call(f) })
97
+ @file_list = @file_list.select{ |f| block.call(f) }
98
+ end
99
+
100
+ #############
101
+ # FileTest #
102
+ #############
103
+
104
+ # This is called #size in FileTest but must be renamed to
105
+ # avoid the clash with the Enumerable mixin.
106
+ def byte_size
107
+ inject(0){ |sum, path| sum + FileTest.size(path) }
108
+ end
109
+
110
+ # An alias for #byte_size.
111
+ alias_method :size?, :byte_size
112
+
113
+ def directory? ; all?{ |path| FileTest.directory?(path) } ; end
114
+ def symlink? ; all?{ |path| FileTest.symlink?(path) } ; end
115
+ def readable? ; all?{ |path| FileTest.readable?(path) } ; end
116
+ def chardev? ; all?{ |path| FileTest.chardev?(path) } ; end
117
+ def exist? ; all?{ |path| FileTest.exist?(path) } ; end
118
+ def exists? ; all?{ |path| FileTest.exist?(path) } ; end
119
+ def zero? ; all?{ |path| FileTest.zero?(path) } ; end
120
+ def pipe? ; all?{ |path| FileTest.pipe?(path) } ; end
121
+ def file? ; all?{ |path| FileTest.file?(path) } ; end
122
+ def sticky? ; all?{ |path| FileTest.sticky?(path) } ; end
123
+ def blockdev? ; all?{ |path| FileTest.blockdev?(path) } ; end
124
+ def grpowned? ; all?{ |path| FileTest.grpowned?(path) } ; end
125
+ def setgid? ; all?{ |path| FileTest.setgid?(path) } ; end
126
+ def setuid? ; all?{ |path| FileTest.setuid?(path) } ; end
127
+ def socket? ; all?{ |path| FileTest.socket?(path) } ; end
128
+ def owned? ; all?{ |path| FileTest.owned?(path) } ; end
129
+ def writable? ; all?{ |path| FileTest.writable?(path) } ; end
130
+ def executable? ; all?{ |path| FileTest.executable?(path) } ; end
131
+ def safe? ; all?{ |path| FileTest.safe?(path) } ; end
132
+
133
+ # TODO: Will this work since all paths are localized?
134
+ def relative? ; all?{ |path| FileTest.relative?(path) } ; end
135
+ def absolute? ; all?{ |path| FileTest.absolute?(path) } ; end
136
+
137
+ def writable_real? ; all?{ |path| FileTest.writable_real?(path) } ; end
138
+ def executable_real? ; all?{ |path| FileTest.executable_real?(path) } ; end
139
+ def readable_real? ; all?{ |path| FileTest.readable_real?(path) } ; end
140
+
141
+ alias_method :dir?, :directory?
142
+
143
+ def identical?(other)
144
+ all?{ |path| FileTest.identical?(path, other) }
145
+ end
146
+
147
+ # TODO: Really?
148
+ alias_method :compare_file, :identical?
149
+ alias_method :cmp, :identical?
150
+
151
+
152
+ #############
153
+ # FileUtils #
154
+ #############
155
+
156
+ # Low-level Methods Omitted
157
+ # -------------------------
158
+ # getwd -> pwd
159
+ # compare_file -> cmp
160
+ # remove_file -> rm
161
+ # copy_file -> cp
162
+ # remove_dir -> rmdir
163
+ # safe_unlink -> rm_f
164
+ # makedirs -> mkdir_p
165
+ # rmtree -> rm_rf
166
+ # copy_stream
167
+ # remove_entry
168
+ # copy_entry
169
+ # remove_entry_secure
170
+ # compare_stream
171
+
172
+ # Present working directory.
173
+ # TODO: Does this make sense?
174
+ def pwd
175
+ @local
176
+ end
177
+
178
+ # Make a directory for every entry.
179
+ def mkdir(options={})
180
+ list.each do |dir|
181
+ if File.exist?(dir)
182
+ raise Errno::EEXIST, "File exists - #{dir}"
183
+ end
184
+ end
185
+ list.map{ |dir| fu(:mkdir, options, dir){ |opts| ::FileUtils.mkdir(dir, **opts) } }
186
+ end
187
+
188
+ # Make a directory for every entry.
189
+ def mkdir_p(options={})
190
+ list.each do |dir|
191
+ if File.exist?(dir) && !File.directory?(dir)
192
+ raise Errno::EEXIST, "File exists - #{dir}"
193
+ end
194
+ end
195
+ list.map{ |dir| fu(:mkdir_p, options, dir){ |opts| ::FileUtils.mkdir_p(dir, **opts) } }
196
+ end
197
+ alias_method :mkpath, :mkdir_p
198
+
199
+ # Remove every directory.
200
+ def rmdir(options={})
201
+ list.map{ |dir| fu(:rmdir, options, dir){ |opts| ::FileUtils.rmdir(dir, **opts) } }
202
+ end
203
+
204
+ # ln(list, destdir, options={})
205
+ def ln(dir, options={})
206
+ fu(:ln, options, list, dir){ |opts| ::FileUtils.ln(list, localize(dir), **opts) }
207
+ end
208
+ alias_method :link, :ln
209
+
210
+ # ln_s(list, destdir, options={})
211
+ def ln_s(dir, options={})
212
+ fu(:ln_s, options, list, dir){ |opts| ::FileUtils.ln_s(list, localize(dir), **opts) }
213
+ end
214
+ alias_method :symlink, :ln_s
215
+
216
+ def ln_sf(dir, options={})
217
+ fu(:ln_sf, options, list, dir){ |opts| ::FileUtils.ln_sf(list, localize(dir), **opts) }
218
+ end
219
+
220
+ # cp(list, dir, options={})
221
+ def cp(dir, options={})
222
+ #src = list.to_a
223
+ #dest = localize(dest)
224
+ fu(:cp, options, list, dir){ |opts| ::FileUtils.cp(list, localize(dir), **opts) }
225
+ end
226
+ alias_method :copy, :cp
227
+
228
+ # cp_r(list, dir, options={})
229
+ def cp_r(dir, options={})
230
+ #src = list.to_a
231
+ #dest = localize(dest)
232
+ fu(:cp_r, options, list, dir){ |opts| ::FileUtils.cp_r(list, localize(dir), **opts) }
233
+ end
234
+
235
+ # mv(list, dir, options={})
236
+ def mv(dir, options={})
237
+ #src = list.to_a
238
+ #dest = localize(dest)
239
+ fu(:mv, options, list, dir){ |opts| ::FileUtils.mv(list, localize(dir), **opts) }
240
+ end
241
+ alias_method :move, :mv
242
+
243
+ #
244
+ def rm(options={})
245
+ fu(:rm, options, list){ |opts| ::FileUtils.rm(list, **opts) }
246
+ end
247
+
248
+ # Alias for #rm.
249
+ alias_method :remove, :rm
250
+
251
+ # Remove, recursively removing the contents of directories.
252
+ def rm_r(options={})
253
+ fu(:rm_r, options, list){ |opts| ::FileUtils.rm_r(list, **opts) }
254
+ end
255
+
256
+ # Remove, with force option.
257
+ def rm_f(options={})
258
+ fu(:rm_f, options, list){ |opts| ::FileUtils.rm_f(list, **opts) }
259
+ end
260
+
261
+ # Remove with force option, recursively removing the contents of directories.
262
+ def rm_rf(options={})
263
+ fu(:rm_rf, options, list){ |opts| ::FileUtils.rm_rf(list, **opts) }
264
+ end
265
+
266
+ # Install files to a directory with given mode. Unlike #cp, this will
267
+ # not copy the file if an up-to-date copy already exists.
268
+ def install(dir, mode, options={})
269
+ #dest = localize(dest)
270
+ fu(:install, options.merge(:mode=>mode), list, dir){ |opts| ::FileUtils.install(list, localize(dir), **opts) }
271
+ end
272
+
273
+ # Change mode of files.
274
+ def chmod(mode, options={})
275
+ fu(:chmod, options, mode, list){ |opts| ::FileUtils.chmod(mode, list, **opts) }
276
+ end
277
+
278
+ # Change mode of files, following directories recursively.
279
+ def chmod_r(mode, options={})
280
+ fu(:chmod_R, options, mode, list){ |opts| ::FileUtils.chmod_R(mode, list, **opts) }
281
+ end
282
+ #alias_method :chmod_R, :chmod_r
283
+
284
+ # Change owner of files.
285
+ def chown(user, group, options={})
286
+ fu(:chown, options, user, group, list){ |opts| ::FileUtils.chown(user, group, list, **opts) }
287
+ end
288
+
289
+ # Change owner of files, following directories recursively.
290
+ def chown_r(user, group, options={})
291
+ fu(:chown_R, options, user, group, list){ |opts| ::FileUtils.chown_R(user, group, list, **opts) }
292
+ end
293
+ #alias_method :chown_R, :chown_r
294
+
295
+ # Touch each file.
296
+ def touch(options={})
297
+ fu(:touch, options, list){ |opts| ::FileUtils.touch(list, **opts) }
298
+ end
299
+
300
+ # Stage files: hard link each into +dir+, at its path relative to
301
+ # the batch's own directory. This is like #install but uses hardlinks.
302
+ def stage(dir, options={})
303
+ stage = localize(dir)
304
+ entries.each do |entry|
305
+ src = localize(entry)
306
+ dest = File.join(stage, entry.to_s)
307
+ if File.directory?(src)
308
+ fu(:mkdir_p, options, dest){ |opts| ::FileUtils.mkdir_p(dest, **opts) }
309
+ else
310
+ fu(:mkdir_p, options, File.dirname(dest)){ |opts| ::FileUtils.mkdir_p(File.dirname(dest), **opts) }
311
+ fu(:ln, options, src, dest){ |opts| ::FileUtils.ln(src, dest, **opts) }
312
+ end
313
+ end
314
+ dir
315
+ end
316
+
317
+ # Convenient alias for #map_mv.
318
+ def rename(options={}, &block)
319
+ map_mv(options, &block)
320
+ end
321
+
322
+ # Rename the list of files in batch, using a block to determine the new
323
+ # names. If the block returns nil, the file will not be renamed.
324
+ #
325
+ # This is similar to #mv, but allows for detailed control over the renaming.
326
+ #
327
+ # Unlike the other `map_*` methods, this changes the Batch list in-place,
328
+ # since the files renamed no loner exist.
329
+ #
330
+ # Returns the changed FileList instance.
331
+ def map_mv(options={}, &block)
332
+ @file_list = map_send(:mv, options, &block)
333
+ end
334
+
335
+ # Hard link the list of files in batch, using a block to determine the new
336
+ # names. If the block returns nil, the file will not be linked.
337
+ #
338
+ # This is similar to #ln, but allows for detailed control over the link names.
339
+ def map_ln(options={}, &block)
340
+ map_send(:ln, options, &block)
341
+ end
342
+
343
+ # Soft link the list of files in batch, using a block to determine the new
344
+ # names. If the block returns nil, the file will not be linked.
345
+ #
346
+ # This is similar to #ln_s, but allows for detailed control over the link names.
347
+ def map_ln_s(options={}, &block)
348
+ map_send(:ln_s, options, &block)
349
+ end
350
+
351
+ # Force soft linking of the list of files in batch, using a block to
352
+ # determine the new names. If the block returns nil, the file will not
353
+ # be linked.
354
+ #
355
+ # This is similar to #ln_sf, but allows for detailed control over the
356
+ # link names.
357
+ def map_ln_sf(options={}, &block)
358
+ map_send(:ln_sf, options, &block)
359
+ end
360
+
361
+ # Copy the list of files in batch, using a block to determine the new
362
+ # file names. If the block returns nil, the file will not be copied.
363
+ #
364
+ # This is similar to #cp, but allows for detailed control over the new
365
+ # file names.
366
+ def map_cp(options={}, &block)
367
+ map_send(:cp, options, &block)
368
+ end
369
+
370
+ # Copy the list of files recursively in batch, using a block to determine
371
+ # the new file names. If the block returns nil, the file will not be copied.
372
+ #
373
+ # This is similar to #cp_r, but allows for detailed control over the new
374
+ # file names.
375
+ def map_cp_r(options={}, &block)
376
+ map_send(:cp_r, options, &block)
377
+ end
378
+
379
+ # # Force copy the list of files recursively in batch, using a block to
380
+ # # determine the new file names. If the block returns nil, the file will not
381
+ # # be copied.
382
+ # #
383
+ # # This is similar to #cp_rf, but allows for detailed control over the new
384
+ # # file names.
385
+ # def map_cp_rf(options={}, &block)
386
+ # map_send(:cp_rf, options, &block)
387
+ # end
388
+
389
+ private
390
+
391
+ SAFE_METHODS = [:ln]
392
+
393
+ # Generic name mapping procedure which can be used for any
394
+ # FileUtils method that has a `src, dest` interface.
395
+ #--
396
+ # TODO: Make public?
397
+ #++
398
+ def map_send(method, options={}, &block)
399
+
400
+ map = {}
401
+ list.each do |file|
402
+ if dest = block.call(file)
403
+ map[file] = dest
404
+ end
405
+ end
406
+
407
+ if options[:safe] or !options[:force] or SAFE_METHODS.include?(method)
408
+ ensure_safe(map)
409
+ end
410
+
411
+ map.each do |src, dest|
412
+ fu(method, options, src, dest) do |opts|
413
+ takes = ::FileUtils.method(method).parameters.map{ |_, name| name }
414
+ opts.delete(:force) unless takes.include?(:force)
415
+ ::FileUtils.rm_f(dest) if options[:force] && !takes.include?(:force) && File.file?(dest)
416
+ ::FileUtils.__send__(method, src, dest, **opts)
417
+ end
418
+ end
419
+ FileList.all(*map.values)
420
+ end
421
+
422
+ # Given a map of source to destination, this method ensures
423
+ # a FileUtils operation can is "safe" --that the destination does not
424
+ # yet and exist and/or the source and destination are compatible.
425
+ # If nota file rrror is raised.
426
+ def ensure_safe(map)
427
+ map.each do |src, dest|
428
+ raise Errno::ENOENT, src.to_s unless File.exist?(src)
429
+ if File.directory?(src)
430
+ if File.directory?(dest)
431
+ new_map = {}
432
+ Dir.entries(src).each do |e|
433
+ next if e == '.' or e == '..'
434
+ new_map[File.join(src, e)] = File.join(dest, e)
435
+ end
436
+ ensure_safe(new_map)
437
+ else
438
+ raise Errno::EISDIR, "Is a directory - #{src}"
439
+ end
440
+ else
441
+ if File.directory?(dest)
442
+ check = File.join(dest, src)
443
+ if File.exist?(check)
444
+ raise Errno::EEXIST, "File exists - #{check}"
445
+ end
446
+ else
447
+ raise Errno::EEXIST, "File exists - #{dest}" if File.exist?(dest)
448
+ end
449
+ end
450
+ end
451
+ end
452
+
453
+ public
454
+
455
+ # An intergrated glob like method that takes a set of include globs,
456
+ # exclude globs and ignore globs to produce a collection of paths.
457
+ #
458
+ # Ignore_globs differ from exclude_globs in that they match by
459
+ # the basename of the path rather than the whole pathname.
460
+ #
461
+ #def amass(include_globs, exclude_globs=[], ignore_globs=[])
462
+ # locally do
463
+ # fileutils.amass(include_globs, exclude_globs, ignore_globs)
464
+ # end
465
+ #end
466
+
467
+ # Is +path+ out-of-date in comparsion to all files in batch.
468
+ def outofdate?(path)
469
+ path = localize(path)
470
+ return true unless File.exist?(path)
471
+ return true if list.empty?
472
+ File.mtime(path) < list.map{ |f| File.mtime(f) }.max
473
+ end
474
+
475
+ # Is +path+ up-to-date in comparsion to all files in batch.
476
+ def uptodate?(path)
477
+ ::FileUtils.uptodate?(localize(path), list)
478
+ end
479
+
480
+ #
481
+ def noop?
482
+ !!(@options[:noop] or @options[:nowrite] or @options[:dryrun])
483
+ end
484
+
485
+ #
486
+ def verbose?
487
+ !!(@options[:verbose] or @options[:trace] or @options[:dryrun])
488
+ end
489
+ alias_method :trace?, :verbose?
490
+
491
+ #
492
+ def dryrun?
493
+ noop? && verbose?
494
+ end
495
+
496
+ private
497
+
498
+ # A path taken from the batch's own directory, unless it is absolute.
499
+ def localize(path)
500
+ path = path.to_s
501
+ return path if FileTest.absolute?(path) || local.to_s == '.'
502
+ File.join(local.to_s, path)
503
+ end
504
+
505
+ # How each file operation reads in a trace.
506
+ FU_COMMANDS = {
507
+ :mkdir_p=>'mkdir -p', :rm_r=>'rm -r', :rm_f=>'rm -f', :rm_rf=>'rm -rf',
508
+ :ln_s=>'ln -s', :ln_sf=>'ln -sf', :cp_r=>'cp -r',
509
+ :chmod_R=>'chmod -R', :chown_R=>'chown -R'
510
+ }
511
+
512
+ # Carry out a file operation in the batch's mode: report it if
513
+ # tracing, with the paths as they were given, and yield the options
514
+ # for FileUtils unless this is a noop.
515
+ def fu(name, options, *args)
516
+ opts = fu_options(options)
517
+ noop = opts.delete(:noop)
518
+ verbose = opts.delete(:verbose)
519
+ if verbose
520
+ $stderr.puts [FU_COMMANDS[name] || name.to_s, *args.flatten].join(' ')
521
+ end
522
+ yield(opts) unless noop
523
+ end
524
+
525
+ # The options given for one command, with :noop and :verbose turned
526
+ # on if the batch's mode says so.
527
+ def fu_options(options)
528
+ opts = {}
529
+ (options || {}).each{ |k, v| opts[k.to_sym] = v }
530
+ given = [:dryrun, :nowrite, :noop, :trace, :verbose].select{ |k| opts.delete(k) }
531
+ dryrun = given.include?(:dryrun)
532
+ opts[:noop] = true if noop? || dryrun || given.include?(:nowrite) || given.include?(:noop)
533
+ opts[:verbose] = true if verbose? || dryrun || given.include?(:trace) || given.include?(:verbose)
534
+ opts.delete(:safe)
535
+ opts
536
+ end
537
+
538
+ end
539
+
540
+ end
541
+
@@ -1 +1,16 @@
1
- require 'facets'
1
+ # I know some people will be deterred by the dependency on Facets b/c they
2
+ # see it as a "heavy" dependency. But really that is far from true, consider
3
+ # the facet that the following libs are all that it used.
4
+
5
+ require 'facets/array/not_empty'
6
+ require 'facets/dir/multiglob' # DEPRECATE: when new #glob is working.
7
+ require 'facets/module/basename'
8
+ require 'facets/module/alias_accessor'
9
+ require 'facets/kernel/yes' # pulls in #ask and #no? too.
10
+ require 'facets/kernel/silence' # FIXME ???
11
+ require 'facets/kernel/disable_warnings'
12
+
13
+ require 'facets/pathname'
14
+ require 'facets/filetest'
15
+ require 'facets/fileutils'
16
+ require 'facets/hash/rekey'
@@ -0,0 +1,29 @@
1
+ module FileTest
2
+
3
+ # Return a cached list of the PATH environment variable.
4
+ # This is a support method used by #bin?
5
+ def command_paths
6
+ @command_paths ||= ENV['PATH'].split(/[:;]/)
7
+ end
8
+
9
+ # Is a file a bin/ executable?
10
+ #
11
+ # TODO: Make more robust. Probably needs to be fixed for Windows.
12
+ def bin?(fname)
13
+ is_bin = command_paths.any? do |f|
14
+ FileTest.exist?(File.join(f, fname))
15
+ end
16
+ #is_bin ? File.basename(fname) : false
17
+ is_bin ? fname : false
18
+ end
19
+
20
+ ## Is a file a task?
21
+ #
22
+ #def task?(path)
23
+ # task = File.dirname($0) + "/#{path}"
24
+ # task.chomp!('!')
25
+ # task if FileTest.file?(task) && FileTest.executable?(task)
26
+ #end
27
+
28
+ end
29
+
@@ -1,7 +1,5 @@
1
1
  class String
2
2
 
3
- attr_accessor :color
4
-
5
3
  # Find actual filename (casefolding) and returns it.
6
4
  # Returns nil if no file is found.
7
5
 
@@ -17,26 +15,5 @@ class String
17
15
  replace(filename) if filename
18
16
  end
19
17
 
20
- #
21
- def unfold_paragraphs
22
- blank = false
23
- text = ''
24
- split(/\n/).each do |line|
25
- if /\S/ !~ line
26
- text << "\n\n"
27
- blank = true
28
- else
29
- if /^(\s+|[*])/ =~ line
30
- text << (line.rstrip + "\n")
31
- else
32
- text << (line.rstrip + " ")
33
- end
34
- blank = false
35
- end
36
- end
37
- text = text.gsub("\n\n\n","\n\n")
38
- return text
39
- end
40
-
41
18
  end
42
19
 
@@ -1,18 +1,27 @@
1
+ # TODO: Improve the naming scheme of these methods.
2
+
1
3
  #
2
- class Array
4
+ class Array #:nodoc:
3
5
 
4
6
  # Convert an array into commandline parameters.
5
7
  # The array is accepted in the format of Ruby
6
8
  # method arguments --ie. [arg1, arg2, ..., hash]
7
9
 
8
10
  def to_console
9
- flags = (Hash===last ? pop : {})
10
- flags = flags.to_console
11
- flags + ' ' + join(" ")
11
+ require 'shellwords'
12
+ Shellwords.join(to_argv)
12
13
  end
13
14
 
15
+ # TODO: DEPRECATE
14
16
  alias_method :to_params, :to_console
15
17
 
18
+ #
19
+ def to_argv
20
+ args = dup
21
+ flags = (Hash===args.last ? args.pop : {})
22
+ flags.to_argv + args.map{ |a| a.to_s }
23
+ end
24
+
16
25
  # def to_console
17
26
  # flags = (Hash===last ? pop : {})
18
27
  # flags = flags.collect do |f,v|
@@ -35,25 +44,34 @@ end
35
44
 
36
45
  class Hash
37
46
 
38
- # Convert an array into command line parameters.
47
+ # Convert a Hash into command line arguments.
39
48
  # The array is accepted in the format of Ruby
40
49
  # method arguments --ie. [arg1, arg2, ..., hash]
41
-
42
50
  def to_console
43
- flags = collect do |f,v|
51
+ require 'shellwords'
52
+ Shellwords.join(to_argv)
53
+ end
54
+
55
+ # Convert a Hash into command line parameters, one string per
56
+ # parameter, as a program receives them (so with no quoting).
57
+ # The array is accepted in the format of Ruby
58
+ # method arguments --ie. [arg1, arg2, ..., hash]
59
+ def to_argv
60
+ flags = []
61
+ each do |f,v|
44
62
  m = f.to_s.size == 1 ? '-' : '--'
45
63
  case v
46
64
  when Array
47
- v.collect{ |e| "#{m}#{f}='#{e}'" }.join(' ')
65
+ v.each{ |e| flags << (m == '-' ? "-#{f}#{e}" : "--#{f}=#{e}") }
48
66
  when true
49
- "#{m}#{f}"
67
+ flags << "#{m}#{f}"
50
68
  when false, nil
51
- ''
69
+ # nothing
52
70
  else
53
- "#{m}#{f}='#{v}'"
71
+ flags << (m == '-' ? "-#{f}#{v}" : "--#{f}=#{v}")
54
72
  end
55
73
  end
56
- flags.join(" ")
74
+ flags
57
75
  end
58
76
 
59
77
  # Turn a hash into arguments.
@@ -1,5 +1,6 @@
1
1
  class Object
2
2
 
3
+ #
3
4
  def to_yamlfrag
4
5
  to_yaml.sub("---",'').rstrip
5
6
  end