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
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# TODO: Replace with facets/string/unfold
|
|
2
|
+
|
|
3
|
+
class String
|
|
4
|
+
|
|
5
|
+
#
|
|
6
|
+
def unfold_paragraphs
|
|
7
|
+
blank = false
|
|
8
|
+
text = ''
|
|
9
|
+
split(/\n/).each do |line|
|
|
10
|
+
if /\S/ !~ line
|
|
11
|
+
text << "\n\n"
|
|
12
|
+
blank = true
|
|
13
|
+
else
|
|
14
|
+
if /^(\s+|[*])/ =~ line
|
|
15
|
+
text << (line.rstrip + "\n")
|
|
16
|
+
else
|
|
17
|
+
text << (line.rstrip + " ")
|
|
18
|
+
end
|
|
19
|
+
blank = false
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
text = text.gsub("\n\n\n","\n\n")
|
|
23
|
+
return text
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
end
|
|
27
|
+
|
data/lib/ratch/core_ext.rb
CHANGED
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
#require 'rake/file_utils_ext'
|
|
2
|
+
|
|
3
|
+
module Ratch
|
|
4
|
+
|
|
5
|
+
# A FileList is essentially an array with a few helper methods defined to
|
|
6
|
+
# make file manipulation a bit easier.
|
|
7
|
+
#
|
|
8
|
+
# FileLists are lazy. When given a list of glob patterns for possible files
|
|
9
|
+
# to be included in the file list, instead of searching the file structures
|
|
10
|
+
# to find the files, a FileList holds the pattern for latter use.
|
|
11
|
+
#
|
|
12
|
+
# This allows us to define a number of FileList to match any number of
|
|
13
|
+
# files, but only search out the actual files when then FileList itself is
|
|
14
|
+
# actually used. The key is that the first time an element of the
|
|
15
|
+
# FileList/Array is requested, the pending patterns are resolved into a real
|
|
16
|
+
# list of file names.
|
|
17
|
+
#
|
|
18
|
+
class FileList
|
|
19
|
+
|
|
20
|
+
# == Method Delegation
|
|
21
|
+
#
|
|
22
|
+
# The lazy evaluation magic of FileLists happens by implementing all the
|
|
23
|
+
# array specific methods to call +resolve+ before delegating the heavy
|
|
24
|
+
# lifting to an embedded array object (@items).
|
|
25
|
+
#
|
|
26
|
+
# In addition, there are two kinds of delegation calls. The regular kind
|
|
27
|
+
# delegates to the @items array and returns the result directly. Well,
|
|
28
|
+
# almost directly. It checks if the returned value is the @items object
|
|
29
|
+
# itself, and if so will return the FileList object instead.
|
|
30
|
+
#
|
|
31
|
+
# The second kind of delegation call is used in methods that normally
|
|
32
|
+
# return a new Array object. We want to capture the return value of these
|
|
33
|
+
# methods and wrap them in a new FileList object. We enumerate these
|
|
34
|
+
# methods in the +SPECIAL_RETURN+ list below.
|
|
35
|
+
|
|
36
|
+
# List of array methods (that are not in +Object+) that need to be
|
|
37
|
+
# delegated.
|
|
38
|
+
ARRAY_METHODS = (Array.instance_methods - Object.instance_methods).map { |n| n.to_s }
|
|
39
|
+
|
|
40
|
+
# List of additional methods that must be delegated.
|
|
41
|
+
MUST_DEFINE = %w[to_a inspect <=>]
|
|
42
|
+
|
|
43
|
+
# List of methods that should not be delegated here (we define special
|
|
44
|
+
# versions of them explicitly below).
|
|
45
|
+
MUST_NOT_DEFINE = %w[to_a to_ary partition *]
|
|
46
|
+
|
|
47
|
+
# List of delegated methods that return new array values which need
|
|
48
|
+
# wrapping.
|
|
49
|
+
SPECIAL_RETURN = %w[
|
|
50
|
+
map collect sort sort_by select find_all reject grep
|
|
51
|
+
compact flatten uniq values_at
|
|
52
|
+
+ - & |
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
DELEGATING_METHODS = (ARRAY_METHODS + MUST_DEFINE - MUST_NOT_DEFINE).collect{ |s| s.to_s }.sort.uniq
|
|
56
|
+
|
|
57
|
+
# Now do the delegation.
|
|
58
|
+
DELEGATING_METHODS.each_with_index do |sym, i|
|
|
59
|
+
if SPECIAL_RETURN.include?(sym)
|
|
60
|
+
ln = __LINE__+1
|
|
61
|
+
class_eval %{
|
|
62
|
+
def #{sym}(*args, &block)
|
|
63
|
+
resolve
|
|
64
|
+
result = @items.send(:#{sym}, *args, &block)
|
|
65
|
+
FileList.new.import(result)
|
|
66
|
+
end
|
|
67
|
+
}, __FILE__, ln
|
|
68
|
+
else
|
|
69
|
+
ln = __LINE__+1
|
|
70
|
+
class_eval %{
|
|
71
|
+
def #{sym}(*args, &block)
|
|
72
|
+
resolve
|
|
73
|
+
result = @items.send(:#{sym}, *args, &block)
|
|
74
|
+
result.object_id == @items.object_id ? self : result
|
|
75
|
+
end
|
|
76
|
+
}, __FILE__, ln
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Create a new file list including the files listed. Similar to:
|
|
81
|
+
#
|
|
82
|
+
# FileList.new(*args)
|
|
83
|
+
def self.[](*args)
|
|
84
|
+
new(*args)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Same a #new but removes the default exclusions.
|
|
88
|
+
def self.all(*args)
|
|
89
|
+
obj = new(*args)
|
|
90
|
+
obj.clear_exclude
|
|
91
|
+
obj
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Create a file list from the globbable patterns given. If you wish to
|
|
95
|
+
# perform multiple includes or excludes at object build time, use the
|
|
96
|
+
# "yield self" pattern.
|
|
97
|
+
#
|
|
98
|
+
# Example:
|
|
99
|
+
# file_list = FileList.new('lib/**/*.rb', 'test/test*.rb')
|
|
100
|
+
#
|
|
101
|
+
# pkg_files = FileList.new('lib/**/*') do |fl|
|
|
102
|
+
# fl.exclude(/\bCVS\b/)
|
|
103
|
+
# end
|
|
104
|
+
#
|
|
105
|
+
def initialize(*patterns)
|
|
106
|
+
@pending_add = []
|
|
107
|
+
@pending = false
|
|
108
|
+
@exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
|
|
109
|
+
@exclude_procs = DEFAULT_IGNORE_PROCS.dup
|
|
110
|
+
@items = []
|
|
111
|
+
patterns.each { |pattern| include(pattern) }
|
|
112
|
+
yield self if block_given?
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Add file names defined by glob patterns to the file list. If an array
|
|
116
|
+
# is given, add each element of the array.
|
|
117
|
+
#
|
|
118
|
+
# Example:
|
|
119
|
+
# file_list.include("*.java", "*.cfg")
|
|
120
|
+
# file_list.include %w( math.c lib.h *.o )
|
|
121
|
+
#
|
|
122
|
+
def include(*filenames)
|
|
123
|
+
# TODO: check for pending
|
|
124
|
+
filenames.each do |fn|
|
|
125
|
+
if fn.respond_to? :to_ary
|
|
126
|
+
include(*fn.to_ary)
|
|
127
|
+
else
|
|
128
|
+
@pending_add << fn
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
@pending = true
|
|
132
|
+
self
|
|
133
|
+
end
|
|
134
|
+
alias :add :include
|
|
135
|
+
|
|
136
|
+
# Register a list of file name patterns that should be excluded from the
|
|
137
|
+
# list. Patterns may be regular expressions, glob patterns or regular
|
|
138
|
+
# strings. In addition, a block given to exclude will remove entries that
|
|
139
|
+
# return true when given to the block.
|
|
140
|
+
#
|
|
141
|
+
# Note that glob patterns are expanded against the file system. If a file
|
|
142
|
+
# is explicitly added to a file list, but does not exist in the file
|
|
143
|
+
# system, then an glob pattern in the exclude list will not exclude the
|
|
144
|
+
# file.
|
|
145
|
+
#
|
|
146
|
+
# Examples:
|
|
147
|
+
# FileList['a.c', 'b.c'].exclude("a.c") => ['b.c']
|
|
148
|
+
# FileList['a.c', 'b.c'].exclude(/^a/) => ['b.c']
|
|
149
|
+
#
|
|
150
|
+
# If "a.c" is a file, then ...
|
|
151
|
+
# FileList['a.c', 'b.c'].exclude("a.*") => ['b.c']
|
|
152
|
+
#
|
|
153
|
+
# If "a.c" is not a file, then ...
|
|
154
|
+
# FileList['a.c', 'b.c'].exclude("a.*") => ['a.c', 'b.c']
|
|
155
|
+
#
|
|
156
|
+
def exclude(*patterns, &block)
|
|
157
|
+
patterns.each do |pat|
|
|
158
|
+
@exclude_patterns << pat
|
|
159
|
+
end
|
|
160
|
+
if block_given?
|
|
161
|
+
@exclude_procs << block
|
|
162
|
+
end
|
|
163
|
+
resolve_exclude if ! @pending
|
|
164
|
+
self
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
# Clear all the exclude patterns so that we exclude nothing.
|
|
169
|
+
def clear_exclude
|
|
170
|
+
@exclude_patterns = []
|
|
171
|
+
@exclude_procs = []
|
|
172
|
+
self
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Define equality.
|
|
176
|
+
def ==(array)
|
|
177
|
+
to_ary == array
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Return the internal array object.
|
|
181
|
+
def to_a
|
|
182
|
+
resolve
|
|
183
|
+
@items
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# Return the internal array object.
|
|
187
|
+
def to_ary
|
|
188
|
+
to_a
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# Lie about our class.
|
|
192
|
+
def is_a?(klass)
|
|
193
|
+
klass == Array || super(klass)
|
|
194
|
+
end
|
|
195
|
+
alias kind_of? is_a?
|
|
196
|
+
|
|
197
|
+
# Redefine * to return either a string or a new file list.
|
|
198
|
+
def *(other)
|
|
199
|
+
result = @items * other
|
|
200
|
+
case result
|
|
201
|
+
when Array
|
|
202
|
+
FileList.new.import(result)
|
|
203
|
+
else
|
|
204
|
+
result
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Resolve all the pending adds now.
|
|
209
|
+
def resolve
|
|
210
|
+
if @pending
|
|
211
|
+
@pending = false
|
|
212
|
+
@pending_add.each do |fn| resolve_add(fn) end
|
|
213
|
+
@pending_add = []
|
|
214
|
+
resolve_exclude
|
|
215
|
+
end
|
|
216
|
+
self
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def resolve_add(fn)
|
|
220
|
+
case fn
|
|
221
|
+
when %r{[*?\[\{]}
|
|
222
|
+
add_matching(fn)
|
|
223
|
+
else
|
|
224
|
+
self << fn
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
private :resolve_add
|
|
228
|
+
|
|
229
|
+
def resolve_exclude
|
|
230
|
+
reject! { |fn| exclude?(fn) }
|
|
231
|
+
self
|
|
232
|
+
end
|
|
233
|
+
private :resolve_exclude
|
|
234
|
+
|
|
235
|
+
# Return a new FileList with the results of running +sub+ against each
|
|
236
|
+
# element of the orignal list.
|
|
237
|
+
#
|
|
238
|
+
# Example:
|
|
239
|
+
# FileList['a.c', 'b.c'].sub(/\.c$/, '.o') => ['a.o', 'b.o']
|
|
240
|
+
#
|
|
241
|
+
def sub(pat, rep)
|
|
242
|
+
inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) }
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# Return a new FileList with the results of running +gsub+ against each
|
|
246
|
+
# element of the original list.
|
|
247
|
+
#
|
|
248
|
+
# Example:
|
|
249
|
+
# FileList['lib/test/file', 'x/y'].gsub(/\//, "\\")
|
|
250
|
+
# => ['lib\\test\\file', 'x\\y']
|
|
251
|
+
#
|
|
252
|
+
def gsub(pat, rep)
|
|
253
|
+
inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Same as +sub+ except that the oringal file list is modified.
|
|
257
|
+
def sub!(pat, rep)
|
|
258
|
+
each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
|
|
259
|
+
self
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# Same as +gsub+ except that the original file list is modified.
|
|
263
|
+
def gsub!(pat, rep)
|
|
264
|
+
each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
|
|
265
|
+
self
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# Return a new FileList with the extension of each file name changed
|
|
269
|
+
# to +newext+, or removed if none is given.
|
|
270
|
+
#
|
|
271
|
+
# FileList['a.c', 'b.c'].ext('o') #=> ['a.o', 'b.o']
|
|
272
|
+
#
|
|
273
|
+
def ext(newext='')
|
|
274
|
+
newext = newext.to_s
|
|
275
|
+
newext = '.' + newext unless newext.empty? || newext.start_with?('.')
|
|
276
|
+
collect { |fn| fn.chomp(File.extname(fn)) + newext }
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# Grep each of the files in the filelist using the given pattern. If a
|
|
280
|
+
# block is given, call the block on each matching line, passing the file
|
|
281
|
+
# name, line number, and the matching line of text. If no block is given,
|
|
282
|
+
# a standard emac style file:linenumber:line message will be printed to
|
|
283
|
+
# standard out. Returns the number of matched items.
|
|
284
|
+
def egrep(pattern, *options)
|
|
285
|
+
matched = 0
|
|
286
|
+
each do |fn|
|
|
287
|
+
begin
|
|
288
|
+
open(fn, "rb", *options) do |inf|
|
|
289
|
+
count = 0
|
|
290
|
+
inf.each do |line|
|
|
291
|
+
count += 1
|
|
292
|
+
if pattern.match(line)
|
|
293
|
+
matched += 1
|
|
294
|
+
if block_given?
|
|
295
|
+
yield fn, count, line
|
|
296
|
+
else
|
|
297
|
+
puts "#{fn}:#{count}:#{line}"
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
rescue StandardError => ex
|
|
303
|
+
puts "Error while processing '#{fn}': #{ex}"
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
matched
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# Return a new file list that only contains file names from the current
|
|
310
|
+
# file list that exist on the file system.
|
|
311
|
+
def existing
|
|
312
|
+
select { |fn| File.exist?(fn) }
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
# Modify the current file list so that it contains only file name that
|
|
316
|
+
# exist on the file system.
|
|
317
|
+
def existing!
|
|
318
|
+
resolve
|
|
319
|
+
@items = @items.select { |fn| File.exist?(fn) }
|
|
320
|
+
self
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
# FileList version of partition. Needed because the nested arrays should
|
|
324
|
+
# be FileLists in this version.
|
|
325
|
+
def partition(&block) # :nodoc:
|
|
326
|
+
resolve
|
|
327
|
+
result = @items.partition(&block)
|
|
328
|
+
[
|
|
329
|
+
FileList.new.import(result[0]),
|
|
330
|
+
FileList.new.import(result[1]),
|
|
331
|
+
]
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# Convert a FileList to a string by joining all elements with a space.
|
|
335
|
+
def to_s
|
|
336
|
+
resolve
|
|
337
|
+
self.join(' ')
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
# Add matching glob patterns.
|
|
341
|
+
def add_matching(pattern)
|
|
342
|
+
Dir[pattern].each do |fn|
|
|
343
|
+
self << fn unless exclude?(fn)
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
private :add_matching
|
|
347
|
+
|
|
348
|
+
# Should the given file name be excluded?
|
|
349
|
+
def exclude?(fn)
|
|
350
|
+
return true if @exclude_patterns.any? do |pat|
|
|
351
|
+
case pat
|
|
352
|
+
when Regexp
|
|
353
|
+
fn =~ pat
|
|
354
|
+
when /[*?]/
|
|
355
|
+
File.fnmatch?(pat, fn, File::FNM_PATHNAME)
|
|
356
|
+
else
|
|
357
|
+
fn == pat
|
|
358
|
+
end
|
|
359
|
+
end
|
|
360
|
+
@exclude_procs.any? { |p| p.call(fn) }
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
def import(array)
|
|
364
|
+
@items = array
|
|
365
|
+
self
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
# Clone an object by making a new object and setting all the instance
|
|
369
|
+
# variables to the same values.
|
|
370
|
+
def dup
|
|
371
|
+
sibling = self.class.new
|
|
372
|
+
instance_variables.each do |ivar|
|
|
373
|
+
value = self.instance_variable_get(ivar)
|
|
374
|
+
new_value = value.clone rescue value
|
|
375
|
+
sibling.instance_variable_set(ivar, new_value)
|
|
376
|
+
end
|
|
377
|
+
sibling.taint if tainted?
|
|
378
|
+
sibling
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
def clone
|
|
382
|
+
sibling = dup
|
|
383
|
+
sibling.freeze if frozen?
|
|
384
|
+
sibling
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
#
|
|
388
|
+
DEFAULT_IGNORE_PATTERNS = [
|
|
389
|
+
/(^|[\/\\])CVS([\/\\]|$)/,
|
|
390
|
+
/(^|[\/\\])\.svn([\/\\]|$)/,
|
|
391
|
+
/\.bak$/,
|
|
392
|
+
/~$/
|
|
393
|
+
]
|
|
394
|
+
|
|
395
|
+
#
|
|
396
|
+
DEFAULT_IGNORE_PROCS = [
|
|
397
|
+
proc { |fn| fn =~ /(^|[\/\\])core$/ && ! File.directory?(fn) }
|
|
398
|
+
]
|
|
399
|
+
|
|
400
|
+
end
|
|
401
|
+
end
|
|
402
|
+
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
module Ratch
|
|
2
|
+
|
|
3
|
+
# Support class for displaying command help lists.
|
|
4
|
+
class Help
|
|
5
|
+
|
|
6
|
+
#
|
|
7
|
+
def self.list(*dirs)
|
|
8
|
+
tasks = script_descriptions(*dirs)
|
|
9
|
+
tmax = tasks.keys.max{ |a,b| a.size <=> b.size }.size
|
|
10
|
+
#dmax = dirs.flatten.max{ |a,b| a.size <=> b.size }.size
|
|
11
|
+
#if dir == ''
|
|
12
|
+
# max += 4 + 2
|
|
13
|
+
#else
|
|
14
|
+
max = tmax + 4
|
|
15
|
+
#end
|
|
16
|
+
tasks = tasks.sort_by{|k,v| k }
|
|
17
|
+
tasks.each do |name, sum|
|
|
18
|
+
#if dir == ''
|
|
19
|
+
# cmd = "ratch #{name}"
|
|
20
|
+
#else
|
|
21
|
+
cmd = name
|
|
22
|
+
#end
|
|
23
|
+
puts "%-#{max}s # %s" % [cmd, sum]
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Scan task scripts for descriptions.
|
|
28
|
+
def self.script_descriptions(*dirs)
|
|
29
|
+
opts = Hash === dirs.last ? dirs.pop : {}
|
|
30
|
+
dirs = dirs.flatten
|
|
31
|
+
help = {}
|
|
32
|
+
dirs.each do |dir|
|
|
33
|
+
files = Dir.glob(File.join(dir,'**/*'))
|
|
34
|
+
files.each do |fname|
|
|
35
|
+
next if FileTest.directory?(fname)
|
|
36
|
+
next if opts[:exe] and !FileTest.executable?(fname)
|
|
37
|
+
desc = ''
|
|
38
|
+
File.open(fname) do |f|
|
|
39
|
+
line = ''
|
|
40
|
+
until f.eof?
|
|
41
|
+
line = f.gets
|
|
42
|
+
case line
|
|
43
|
+
when /^(#!|\s*$)/
|
|
44
|
+
next
|
|
45
|
+
when /^\s*#(.*)/
|
|
46
|
+
desc = $1.strip; break
|
|
47
|
+
else
|
|
48
|
+
desc = nil; break
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
key = opts[:exe] ? fname : fname.sub(dir+'/', '')
|
|
53
|
+
help[key] = desc
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
help
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Scan script for description header.
|
|
60
|
+
def self.header(file, opts={})
|
|
61
|
+
#next if FileTest.directory?(file)
|
|
62
|
+
#next if opts[:exe] and !FileTest.executable?(file)
|
|
63
|
+
desc = "\n"
|
|
64
|
+
File.open(file) do |f|
|
|
65
|
+
line = ''
|
|
66
|
+
until f.eof?
|
|
67
|
+
line = f.gets
|
|
68
|
+
case line
|
|
69
|
+
when /^(#!|\s*$)/
|
|
70
|
+
next
|
|
71
|
+
when /^\s*#\s?(.*)/
|
|
72
|
+
desc << $1.rstrip + "\n"
|
|
73
|
+
else
|
|
74
|
+
break
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
desc + "\n"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
end
|
|
84
|
+
|
data/lib/ratch/script.rb
CHANGED
|
@@ -1,14 +1,108 @@
|
|
|
1
|
-
require 'ratch/
|
|
1
|
+
require 'ratch/core_ext'
|
|
2
|
+
require 'ratch/shell'
|
|
3
|
+
#require 'ratch/plugin'
|
|
4
|
+
|
|
5
|
+
# CLI extension is required.
|
|
6
|
+
require 'ratch/utils/cli'
|
|
2
7
|
|
|
3
8
|
module Ratch
|
|
4
9
|
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
# The Ratch Script class is used to run stand-alone ratch scripts.
|
|
10
|
+
# The Ratch Script class is used to run stand-alone Ratch scripts.
|
|
8
11
|
# Yep, this is actaully a class named exactly for what it is.
|
|
9
12
|
# How rare.
|
|
10
13
|
#
|
|
11
|
-
|
|
14
|
+
#--
|
|
15
|
+
# Previous versions of this class subclassed Module and called `self extend`
|
|
16
|
+
# in the +initialize+ method. Then used +method_missing+ to route to an
|
|
17
|
+
# instance of Shell. The current design works the other way round.
|
|
18
|
+
# Both approaches seem to work just as well, though perhaps one is more
|
|
19
|
+
# robust than another? I have chosen this design simply b/c the shell
|
|
20
|
+
# methods should dispatch a bit faster.
|
|
21
|
+
#++
|
|
22
|
+
class Script < Shell #< Module
|
|
23
|
+
|
|
24
|
+
include CLI
|
|
25
|
+
|
|
26
|
+
#
|
|
27
|
+
def self.execute(script, *args)
|
|
28
|
+
script = new(script, *args)
|
|
29
|
+
script.execute!
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
#
|
|
33
|
+
def initialize(file, *args)
|
|
34
|
+
@_file = file.to_s
|
|
35
|
+
|
|
36
|
+
#extend self
|
|
37
|
+
|
|
38
|
+
super(*args)
|
|
39
|
+
|
|
40
|
+
#@_stdout = options[:stdout] || $stdout
|
|
41
|
+
#@_stderr = options[:stderr] || $stderr
|
|
42
|
+
#@_stdin = options[:stdin] || $stdin
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Returns the file name of the script.
|
|
46
|
+
def script_file
|
|
47
|
+
@_file
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Be cautious about calling this in a script --an infinite loop could
|
|
51
|
+
# easily ensue.
|
|
52
|
+
def execute!
|
|
53
|
+
old = $0
|
|
54
|
+
begin
|
|
55
|
+
$0 = script_file
|
|
56
|
+
instance_eval(File.read(script_file), script_file, 1)
|
|
57
|
+
ensure
|
|
58
|
+
$0 = old
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
alias_method :run!, :execute!
|
|
63
|
+
|
|
64
|
+
#
|
|
65
|
+
#def print(str=nil)
|
|
66
|
+
# super(str.to_s) unless quiet?
|
|
67
|
+
#end
|
|
68
|
+
|
|
69
|
+
#
|
|
70
|
+
#def puts(str=nil)
|
|
71
|
+
# super(str.to_s) unless quiet?
|
|
72
|
+
#end
|
|
73
|
+
|
|
74
|
+
# TODO: Deprecate one of the three #report, #status, #trace.
|
|
75
|
+
def report(message)
|
|
76
|
+
#@_stdout.puts(message) unless quiet?
|
|
77
|
+
puts(message) unless quiet?
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
#
|
|
81
|
+
def status(message)
|
|
82
|
+
#@_stdout.puts message unless quiet?
|
|
83
|
+
puts message unless quiet? # dryrun? or trace?
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Internal status report. Only output if in trace mode.
|
|
87
|
+
#
|
|
88
|
+
def trace(message)
|
|
89
|
+
#@_stdout.puts message if trace?
|
|
90
|
+
puts message if trace?
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Pass-thru to singleton class.
|
|
94
|
+
def define_method(name, &block)
|
|
95
|
+
(class << self; self; end).__send__(:define_method, name, &block)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
## If method is missing, try the singleton class. This allows the script
|
|
99
|
+
## to use methods like +define_method+.
|
|
100
|
+
##
|
|
101
|
+
## TODO: Perhaps it would be best to limit the selection of methods?
|
|
102
|
+
#def method_missing(sym, *args, &blk)
|
|
103
|
+
# (class << self; self; end).__send__(sym, *args, &blk)
|
|
104
|
+
#end
|
|
105
|
+
|
|
12
106
|
end
|
|
13
107
|
|
|
14
108
|
end
|