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.
- checksums.yaml +7 -0
- data/HISTORY.rdoc +87 -0
- data/LICENSE.txt +204 -0
- data/README.rdoc +133 -0
- data/bin/ludo +32 -0
- data/bin/ratch +1 -8
- data/lib/ratch/batch.rb +541 -0
- data/lib/ratch/core_ext/facets.rb +16 -1
- data/lib/ratch/core_ext/filetest.rb +29 -0
- data/lib/ratch/core_ext/{string.rb → to_actual_filename.rb} +0 -23
- data/lib/ratch/core_ext/to_console.rb +30 -12
- data/lib/ratch/core_ext/{object.rb → to_yamlfrag.rb} +1 -0
- data/lib/ratch/core_ext/unfold_paragraphs.rb +27 -0
- data/lib/ratch/core_ext.rb +1 -4
- data/lib/ratch/file_list.rb +402 -0
- data/lib/ratch/script/help.rb +84 -0
- data/lib/ratch/script.rb +99 -5
- data/lib/ratch/shell.rb +621 -0
- data/lib/ratch/utils/cli.rb +49 -0
- data/lib/ratch/utils/config.rb +52 -0
- data/lib/ratch/{emailer.rb → utils/email.rb} +58 -98
- data/lib/ratch/utils/rdoc.rb +45 -0
- data/lib/ratch/utils/tar.rb +132 -0
- data/lib/ratch/utils/xdg.rb +38 -0
- data/lib/ratch/utils/zlib.rb +55 -0
- data/lib/ratch/version.rb +3 -0
- data/lib/ratch.rb +12 -0
- metadata +160 -126
- data/COPYING +0 -22
- data/HISTORY +0 -6
- data/MANIFEST +0 -53
- data/NEWS +0 -12
- data/README +0 -87
- data/VERSION +0 -1
- data/demo/tryme-task.ratch +0 -12
- data/demo/tryme1.ratch +0 -6
- data/doc/log/basic_stats/index.html +0 -39
- data/doc/log/notes.xml +0 -18
- data/doc/log/stats.log +0 -14
- data/doc/log/syntax.log +0 -0
- data/doc/log/testunit.log +0 -156
- data/lib/ratch/commandline.rb +0 -16
- data/lib/ratch/core_ext/pathname.rb +0 -38
- data/lib/ratch/dsl.rb +0 -420
- data/lib/ratch/index.rb +0 -4
- data/lib/ratch/io.rb +0 -116
- data/lib/ratch/plugin.rb +0 -65
- data/lib/ratch/service.rb +0 -33
- data/lib/ratch/task.rb +0 -249
- data/lib/ratch/task2.rb +0 -298
- data/man/ratch.1 +0 -28
- data/meta/abstract +0 -4
- data/meta/author +0 -1
- data/meta/contact +0 -1
- data/meta/homepage +0 -1
- data/meta/name +0 -1
- data/meta/requires +0 -4
- data/meta/summary +0 -1
- data/test/README +0 -1
- data/test/test_helper.rb +0 -4
- data/test/test_task.rb +0 -46
data/lib/ratch/shell.rb
ADDED
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
require 'pathname'
|
|
2
|
+
require 'fileutils'
|
|
3
|
+
require 'ratch/core_ext'
|
|
4
|
+
require 'ratch/batch'
|
|
5
|
+
|
|
6
|
+
module Ratch
|
|
7
|
+
|
|
8
|
+
# Raised when a path that has to exist doesn't.
|
|
9
|
+
class FileNotFound < StandardError
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# = Shell Prompt class
|
|
13
|
+
#
|
|
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.
|
|
37
|
+
#
|
|
38
|
+
class Shell
|
|
39
|
+
|
|
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.
|
|
47
|
+
#
|
|
48
|
+
# Shell.new(:noop=>true)
|
|
49
|
+
# Shell.new('..', :quiet=>true)
|
|
50
|
+
#
|
|
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 }
|
|
61
|
+
|
|
62
|
+
set_options(opts)
|
|
63
|
+
|
|
64
|
+
path = path.empty? ? Dir.pwd : File.join(*path.map{ |part| part.to_s })
|
|
65
|
+
|
|
66
|
+
raise FileNotFound, "#{path}" unless ::File.directory?(path)
|
|
67
|
+
|
|
68
|
+
@_work = Pathname.new(path).expand_path
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
#
|
|
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])
|
|
78
|
+
end
|
|
79
|
+
|
|
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?}
|
|
83
|
+
end
|
|
84
|
+
|
|
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?
|
|
95
|
+
|
|
96
|
+
# String representation is work directory path.
|
|
97
|
+
def to_s ; work.to_s ; end
|
|
98
|
+
|
|
99
|
+
#
|
|
100
|
+
def inspect
|
|
101
|
+
"#<#{self.class} #{work}>"
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Two Shell's are equal if they have the same working path.
|
|
105
|
+
def ==(other)
|
|
106
|
+
return false unless other.is_a?(self.class)
|
|
107
|
+
return false unless work == other.work
|
|
108
|
+
true
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Same as #== except that #noop? must also be the same.
|
|
112
|
+
def eql?(other)
|
|
113
|
+
return false unless other.is_a?(self.class)
|
|
114
|
+
return false unless work == other.work
|
|
115
|
+
return false unless noop? == other.noop?
|
|
116
|
+
true
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
#
|
|
120
|
+
def hash
|
|
121
|
+
[self.class, work, noop?].hash
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Provides convenient starting points in the file system.
|
|
125
|
+
#
|
|
126
|
+
# root #=> #<Pathname:/>
|
|
127
|
+
# home #=> #<Pathname:/home/jimmy>
|
|
128
|
+
# work #=> #<Pathname:/home/jimmy/Documents>
|
|
129
|
+
#
|
|
130
|
+
# Each takes path parts to add, and the result must be a directory.
|
|
131
|
+
|
|
132
|
+
# Root location.
|
|
133
|
+
def root(*parts)
|
|
134
|
+
dir(File.join('/', *parts.map{ |part| part.to_s }))
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Current home path.
|
|
138
|
+
def home(*parts)
|
|
139
|
+
dir(File.join(File.expand_path('~'), *parts.map{ |part| part.to_s }))
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Current working path.
|
|
143
|
+
def work(*parts)
|
|
144
|
+
return @_work if parts.empty?
|
|
145
|
+
dir(File.join(*parts.map{ |part| part.to_s }))
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Present working directory, as a String.
|
|
149
|
+
def pwd
|
|
150
|
+
work.to_s
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# The directory above the working path.
|
|
154
|
+
def parent
|
|
155
|
+
work.parent
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Returns a Batch of file +patterns+.
|
|
159
|
+
def batch(*patterns)
|
|
160
|
+
Batch.new(work, *patterns, mode_options)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Returns a Batch of file +patterns+, without any exclusions.
|
|
164
|
+
def batch_all(*patterns)
|
|
165
|
+
Batch.all(work, *patterns, mode_options)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# A Pathname for a path, which need not exist.
|
|
169
|
+
def path(path)
|
|
170
|
+
Pathname.new(localize(path))
|
|
171
|
+
end
|
|
172
|
+
alias_method :pathname, :path
|
|
173
|
+
|
|
174
|
+
# A Pathname for a file, which must exist.
|
|
175
|
+
def file(path)
|
|
176
|
+
path = localize(path)
|
|
177
|
+
raise FileNotFound, path unless File.file?(path)
|
|
178
|
+
Pathname.new(path)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# A Pathname for a directory, which must exist.
|
|
182
|
+
def dir(path)
|
|
183
|
+
path = localize(path)
|
|
184
|
+
raise FileNotFound, path unless File.directory?(path)
|
|
185
|
+
Pathname.new(path)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# Lists all entries, as Dir.entries does, but as Pathnames.
|
|
189
|
+
def entries
|
|
190
|
+
work.entries
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Lists directory entries.
|
|
194
|
+
def directory_entries
|
|
195
|
+
entries.select{ |d| (work + d).directory? }
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
#
|
|
199
|
+
alias_method :dir_entries, :directory_entries
|
|
200
|
+
|
|
201
|
+
# Lists file entries.
|
|
202
|
+
def file_entries
|
|
203
|
+
entries.select{ |f| (work + f).file? }
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Like #entries but omits the '.' and '..' paths.
|
|
207
|
+
def pathnames
|
|
208
|
+
work.children(false)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# Returns list of directories.
|
|
212
|
+
def directories
|
|
213
|
+
pathnames.select{ |d| (work + d).directory? }
|
|
214
|
+
end
|
|
215
|
+
alias_method :dirs, :directories
|
|
216
|
+
alias_method :folders, :directories
|
|
217
|
+
|
|
218
|
+
# Returns list of files.
|
|
219
|
+
def files
|
|
220
|
+
pathnames.select{ |f| (work + f).file? }
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# A new shell at a directory under this one, in the same mode.
|
|
224
|
+
#
|
|
225
|
+
# lib = shell / 'lib'
|
|
226
|
+
#
|
|
227
|
+
def /(path)
|
|
228
|
+
self.class.new(dir(path), mode_options)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# Alias for #/.
|
|
232
|
+
alias_method :+, :/
|
|
233
|
+
|
|
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)
|
|
240
|
+
end
|
|
241
|
+
|
|
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?
|
|
251
|
+
return true if noop?
|
|
252
|
+
if quiet?
|
|
253
|
+
system(*cmd, :out=>File::NULL, :err=>File::NULL)
|
|
254
|
+
else
|
|
255
|
+
system(*cmd)
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# Change working directory.
|
|
260
|
+
#
|
|
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.
|
|
266
|
+
#
|
|
267
|
+
def cd(path, &block)
|
|
268
|
+
if block
|
|
269
|
+
work_old = @_work
|
|
270
|
+
begin
|
|
271
|
+
@_work = dir(path)
|
|
272
|
+
Dir.chdir(@_work.to_s){ block.call(self) }
|
|
273
|
+
ensure
|
|
274
|
+
@_work = work_old
|
|
275
|
+
end
|
|
276
|
+
else
|
|
277
|
+
@_work = dir(path)
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
#
|
|
282
|
+
alias_method :chdir, :cd
|
|
283
|
+
|
|
284
|
+
# -- FileTest --
|
|
285
|
+
|
|
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
|
+
}
|
|
293
|
+
|
|
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
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
alias_method :dir?, :directory?
|
|
303
|
+
|
|
304
|
+
# Ruby dropped +exists?+ in 3.2; it is kept here.
|
|
305
|
+
alias_method :exists?, :exist?
|
|
306
|
+
|
|
307
|
+
# Is the path safe from the meddling of others?
|
|
308
|
+
def safe?(path)
|
|
309
|
+
::FileTest.safe?(localize(path))
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
def relative?(path); ::FileTest.relative?(path.to_s); end
|
|
313
|
+
def absolute?(path); ::FileTest.absolute?(path.to_s); end
|
|
314
|
+
|
|
315
|
+
# Are the two paths the same file?
|
|
316
|
+
def identical?(path, other)
|
|
317
|
+
::FileTest.identical?(localize(path), localize(other))
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
# Do the two files have the same content?
|
|
321
|
+
def cmp(path, other)
|
|
322
|
+
::FileUtils.compare_file(localize(path), localize(other))
|
|
323
|
+
end
|
|
324
|
+
alias_method :compare_file, :cmp
|
|
325
|
+
|
|
326
|
+
# -- FileUtils --
|
|
327
|
+
#
|
|
328
|
+
# Each takes the options of its FileUtils namesake. The shell's mode
|
|
329
|
+
# sets :noop and :verbose.
|
|
330
|
+
#
|
|
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
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
alias_method :mkpath, :mkdir_p
|
|
354
|
+
alias_method :makedirs, :mkdir_p
|
|
355
|
+
alias_method :remove, :rm
|
|
356
|
+
alias_method :rmtree, :rm_rf
|
|
357
|
+
|
|
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
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
alias_method :link, :ln
|
|
368
|
+
alias_method :symlink, :ln_s
|
|
369
|
+
alias_method :copy, :cp
|
|
370
|
+
alias_method :move, :mv
|
|
371
|
+
|
|
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) }
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
#
|
|
381
|
+
def chmod(mode, list, options={})
|
|
382
|
+
fu(:chmod, options, (Integer === mode ? '%o' % mode : mode), list){ |opts| ::FileUtils.chmod(mode, localize(list), **opts) }
|
|
383
|
+
end
|
|
384
|
+
|
|
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) }
|
|
388
|
+
end
|
|
389
|
+
alias_method :chmod_r, :chmod_R
|
|
390
|
+
|
|
391
|
+
#
|
|
392
|
+
def chown(user, group, list, options={})
|
|
393
|
+
fu(:chown, options, [user, group].compact.join(':'), list){ |opts| ::FileUtils.chown(user, group, localize(list), **opts) }
|
|
394
|
+
end
|
|
395
|
+
|
|
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) }
|
|
399
|
+
end
|
|
400
|
+
alias_method :chown_r, :chown_R
|
|
401
|
+
|
|
402
|
+
# Is +path+ newer than all of +sources+?
|
|
403
|
+
def uptodate?(path, *sources)
|
|
404
|
+
::FileUtils.uptodate?(localize(path), localize(sources.flatten))
|
|
405
|
+
end
|
|
406
|
+
|
|
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
|
|
430
|
+
end
|
|
431
|
+
path(stage_dir)
|
|
432
|
+
end
|
|
433
|
+
|
|
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.
|
|
437
|
+
#
|
|
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.
|
|
440
|
+
#
|
|
441
|
+
def 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)) }
|
|
449
|
+
end
|
|
450
|
+
end
|
|
451
|
+
|
|
452
|
+
# -- File --
|
|
453
|
+
|
|
454
|
+
# Get creation timestamp.
|
|
455
|
+
def ctime(path)
|
|
456
|
+
File.ctime(localize(path))
|
|
457
|
+
end
|
|
458
|
+
|
|
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
|
|
465
|
+
|
|
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)
|
|
471
|
+
end
|
|
472
|
+
|
|
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
|
|
479
|
+
|
|
480
|
+
# Read file.
|
|
481
|
+
def read(path)
|
|
482
|
+
File.read(localize(path))
|
|
483
|
+
end
|
|
484
|
+
|
|
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]
|
|
490
|
+
end
|
|
491
|
+
|
|
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]
|
|
497
|
+
end
|
|
498
|
+
|
|
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
|
|
519
|
+
end
|
|
520
|
+
end
|
|
521
|
+
|
|
522
|
+
private
|
|
523
|
+
|
|
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.
|
|
526
|
+
def localize(local_path)
|
|
527
|
+
case local_path
|
|
528
|
+
when nil
|
|
529
|
+
nil
|
|
530
|
+
when Array
|
|
531
|
+
local_path.map{ |lp| localize(lp) }
|
|
532
|
+
else
|
|
533
|
+
local_path = local_path.to_s
|
|
534
|
+
return local_path if absolute?(local_path)
|
|
535
|
+
File.expand_path(File.join(work.to_s, local_path))
|
|
536
|
+
end
|
|
537
|
+
end
|
|
538
|
+
|
|
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.
|
|
542
|
+
def locally(&block)
|
|
543
|
+
if work.to_s == Dir.pwd
|
|
544
|
+
block.call
|
|
545
|
+
else
|
|
546
|
+
Dir.chdir(work.to_s, &block)
|
|
547
|
+
end
|
|
548
|
+
end
|
|
549
|
+
|
|
550
|
+
# TODO: Should naming policy be in a utility extension module?
|
|
551
|
+
|
|
552
|
+
#
|
|
553
|
+
def naming_policy(*policies)
|
|
554
|
+
if policies.empty?
|
|
555
|
+
@naming_policy ||= ['down', 'ext']
|
|
556
|
+
else
|
|
557
|
+
@naming_policy = policies
|
|
558
|
+
end
|
|
559
|
+
end
|
|
560
|
+
|
|
561
|
+
#
|
|
562
|
+
def apply_naming_policy(name, ext)
|
|
563
|
+
naming_policy.each do |policy|
|
|
564
|
+
case policy.to_s
|
|
565
|
+
when /^low/, /^down/
|
|
566
|
+
name = name.downcase
|
|
567
|
+
when /^up/
|
|
568
|
+
name = name.upcase
|
|
569
|
+
when /^cap/
|
|
570
|
+
name = name.capitalize
|
|
571
|
+
when /^ext/
|
|
572
|
+
name = name + ".#{ext}"
|
|
573
|
+
end
|
|
574
|
+
end
|
|
575
|
+
name
|
|
576
|
+
end
|
|
577
|
+
|
|
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(' ')
|
|
594
|
+
end
|
|
595
|
+
yield(opts) unless noop
|
|
596
|
+
end
|
|
597
|
+
|
|
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
|
|
609
|
+
end
|
|
610
|
+
|
|
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]
|
|
617
|
+
end
|
|
618
|
+
|
|
619
|
+
end
|
|
620
|
+
|
|
621
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module Ratch
|
|
2
|
+
|
|
3
|
+
# TODO: How best to support CLI interface?
|
|
4
|
+
module CLI
|
|
5
|
+
|
|
6
|
+
#
|
|
7
|
+
def self.included(base)
|
|
8
|
+
require 'facets/argvector'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
#
|
|
12
|
+
def self.extended(base)
|
|
13
|
+
require 'facets/argvector'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
#
|
|
17
|
+
def initialize(*args)
|
|
18
|
+
argv = ArgVector.new(ARGV)
|
|
19
|
+
|
|
20
|
+
@arguments, parameters = *argv.parameters
|
|
21
|
+
|
|
22
|
+
args << {} unless Hash === args.last
|
|
23
|
+
|
|
24
|
+
opts = args.last
|
|
25
|
+
|
|
26
|
+
opts.merge!(parameters)
|
|
27
|
+
|
|
28
|
+
super(*args)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
#
|
|
32
|
+
def arguments
|
|
33
|
+
@arguments
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# DEPRECATE!
|
|
37
|
+
#alias_method :commandline, :cli
|
|
38
|
+
|
|
39
|
+
# DEPRECATE!
|
|
40
|
+
#alias_method :command, :cli
|
|
41
|
+
|
|
42
|
+
#
|
|
43
|
+
#def commandline
|
|
44
|
+
# #@commandline ||= ArgVector.new(ARGV)
|
|
45
|
+
# @commandline
|
|
46
|
+
#end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
module Ratch
|
|
2
|
+
|
|
3
|
+
# Utility extensions for working with configuration files.
|
|
4
|
+
#--
|
|
5
|
+
# TODO: Perhaps utilize confectionary gem in future?
|
|
6
|
+
#++
|
|
7
|
+
module ConfigUtils
|
|
8
|
+
|
|
9
|
+
#
|
|
10
|
+
def self.included(base)
|
|
11
|
+
require 'yaml'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.extended(base)
|
|
15
|
+
included(base)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Load configuration data from a file. Results are cached and an empty
|
|
19
|
+
# Hash is returned if the file is not found.
|
|
20
|
+
#
|
|
21
|
+
# Since they are YAML files, they can optionally end with '.yaml' or '.yml'.
|
|
22
|
+
def configuration(file)
|
|
23
|
+
@configuration ||= {}
|
|
24
|
+
@configuration[file] ||= (
|
|
25
|
+
begin
|
|
26
|
+
configuration!(file)
|
|
27
|
+
rescue LoadError
|
|
28
|
+
Hash.new{ |h,k| h[k] = {} }
|
|
29
|
+
end
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Load configuration data from a file. The "bang" version will raise an error
|
|
34
|
+
# if file is not found. It also does not cache the results.
|
|
35
|
+
#
|
|
36
|
+
# Since they are YAML files, they can optionally end with '.yaml' or '.yml'.
|
|
37
|
+
def configuration!(file)
|
|
38
|
+
@configuration ||= {}
|
|
39
|
+
patt = localize(file) + "{.yml,.yaml,}"
|
|
40
|
+
path = Dir.glob(patt).find{ |f| File.file?(f) }
|
|
41
|
+
if path
|
|
42
|
+
# The || {} is in case the file is empty.
|
|
43
|
+
data = YAML.load_file(path) || {}
|
|
44
|
+
@configuration[file] = data
|
|
45
|
+
else
|
|
46
|
+
raise LoadError, "Missing file -- #{file}"
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|