bee 0.3.1 → 0.4.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.
@@ -0,0 +1,525 @@
1
+ # Copyright 2006-2007 Michel Casabianca <michel.casabianca@gmail.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'bee'
16
+ require 'bee_task'
17
+ require 'bee_util'
18
+
19
+ module Bee
20
+
21
+ module Task
22
+
23
+ # Package for default tasks (tasks with no package).
24
+ class Default < Package
25
+
26
+ include Bee::Util::FileSelector
27
+
28
+ # Print a message on console. If message is a string, it is printed
29
+ # on standard output, if message is another Ruby object, this task
30
+ # outputs its inspected value instead.
31
+ #
32
+ # - message: message to print.
33
+ #
34
+ # Example
35
+ #
36
+ # - print: "Hello World!"
37
+ def print(message)
38
+ case message
39
+ when String
40
+ puts message
41
+ else
42
+ puts message.inspect
43
+ end
44
+ end
45
+
46
+ # Print contents of a given file on the console. Parameter is the name
47
+ # of the file to output, as a String.
48
+ #
49
+ # Example
50
+ #
51
+ # - cat: "doc/welcome-message.txt"
52
+ def cat(file)
53
+ file = file
54
+ error "Parameter must be a String" unless file.kind_of?(String)
55
+ error "File '#{file}' not found" unless
56
+ File.exists?(file) or File.directory?(file)
57
+ puts File.read(file).strip
58
+ end
59
+
60
+ # Change working directory. This change will persist for all tasks in
61
+ # the target but not in other targets. Parameter is a String with
62
+ # directory to change to.
63
+ #
64
+ # Example
65
+ #
66
+ # - cd: "build"
67
+ def cd(dir)
68
+ dir = dir
69
+ error "cd parameter is a String" unless dir.kind_of?(String)
70
+ Dir.chdir(dir)
71
+ end
72
+
73
+ # Make a directory and parent directories if necessary. Doesn't complain
74
+ # if directory already exists. Parameter is directory to create as a
75
+ # String or a list of directories as Strings.
76
+ #
77
+ # Example
78
+ #
79
+ # - mkdir: "foo/bar"
80
+ # - mkdir: ["foo", "bar"]
81
+ def mkdir(dirs)
82
+ require 'fileutils'
83
+ dirs = dirs
84
+ error "mkdir parameter must a String or an array of Strings" unless
85
+ dirs.kind_of?(String) or dirs.kind_of?(Array)
86
+ for dir in dirs
87
+ error "mkdir parameter must a String or an array of Strings" unless
88
+ dir.kind_of?(String)
89
+ FileUtils.makedirs(dir)
90
+ end
91
+ end
92
+
93
+ # Copy files or directories to destination file or directory. Parameter
94
+ # is a Hash with following entries:
95
+ #
96
+ # - includes: glob or list of globs for source files or directories to
97
+ # include.
98
+ # - excludes: glob or list of globs for source files or directories to
99
+ # exclude.
100
+ # - dest: destination file or directory.
101
+ #
102
+ # Example
103
+ #
104
+ # - cp:
105
+ # includes: "doc/img/**/*"
106
+ # excludes: ["**/*.bmp", "**/*.psd"]
107
+ # dest: :doc
108
+ def cp(params)
109
+ require 'fileutils'
110
+ params_desc = {
111
+ 'includes' => :mandatory,
112
+ 'excludes' => :optional,
113
+ 'dest' => :mandatory
114
+ }
115
+ check_task_parameters(params, params_desc)
116
+ includes = params['includes']
117
+ excludes = params['excludes']
118
+ dest = params['dest']
119
+ error "cp 'dest' parameter must a String" unless dest.kind_of?(String)
120
+ files = select_files(includes, excludes)
121
+ files = files[0] if files.length == 1
122
+ FileUtils.cp_r(files, dest)
123
+ end
124
+
125
+ # Move source file(s) or directory(ies) to dest file or directory.
126
+ # Parameter is a Hash with following entries:
127
+ #
128
+ # - includes: glob or list of globs for files or directories to move.
129
+ # - excludes: glob or list of globs for files or directories that should
130
+ # not move.
131
+ # - dest: file or directory to move file(s) to.
132
+ #
133
+ # Example
134
+ #
135
+ # - mv: { includes: "**/*~", dest: "trash" }
136
+ def mv(params)
137
+ require 'fileutils'
138
+ params_desc = {
139
+ 'includes' => :mandatory,
140
+ 'excludes' => :optional,
141
+ 'dest' => :mandatory
142
+ }
143
+ check_task_parameters(params, params_desc)
144
+ includes = params['includes']
145
+ excludes = params['excludes']
146
+ dest = params['dest']
147
+ error "mv 'dest' parameter must a String" unless dest.kind_of?(String)
148
+ files = select_files(includes, excludes)
149
+ files = files[0] if files.length == 1
150
+ FileUtils.mv(files, dest)
151
+ end
152
+
153
+ # Delete files for a given glob or list of globs. Parameter is a glob or
154
+ # list of globs for files to delete.
155
+ #
156
+ # Example
157
+ #
158
+ # - rm: "**/*.bak"
159
+ # - rm: ["**/*~", "**/.DS_Store"]
160
+ def rm(globs)
161
+ require 'fileutils'
162
+ globs = globs
163
+ error "rm parameter is a String or Array of Strings" unless
164
+ globs.kind_of?(String) or globs.kind_of?(Array)
165
+ for glob in globs
166
+ error "rm parameter is a String or Array of Strings" unless
167
+ glob.kind_of?(String)
168
+ files = Dir.glob(glob)
169
+ for file in files
170
+ FileUtils.rm_f(file)
171
+ end
172
+ end
173
+ end
174
+
175
+ # Delete directories recursively. Parameter is a string for glob of
176
+ # directories to delete.
177
+ #
178
+ # Example
179
+ #
180
+ # - rmdir: :build
181
+ def rmdir(globs)
182
+ require 'fileutils'
183
+ globs = globs
184
+ error "rmdir parameter is a String or an Array of Strings" unless
185
+ globs.kind_of?(String) or globs.kind_of?(Array)
186
+ for glob in globs
187
+ error "rmdir parameter is a String or an Array of Strings" unless
188
+ glob.kind_of?(String)
189
+ dirs = Dir.glob(glob)
190
+ for dir in dirs
191
+ FileUtils.rm_rf(dir)
192
+ end
193
+ end
194
+ end
195
+
196
+ # Find files for a glob or list of globs and store list in a property.
197
+ # Parameter is a Hash with entries:
198
+ #
199
+ # - includes: glob or list of globs for files to look for.
200
+ # - excludes: glob or list of globs for files to exclude from search.
201
+ # - property: name of the property to set.
202
+ #
203
+ # Example
204
+ #
205
+ # - find: { includes: "**/*.rb", property: "rb_files_to_check" }
206
+ # - find:
207
+ # - includes: "doc/img/**/*"
208
+ # - excludes: "**/*.psd"
209
+ # - property: "image_files"
210
+ def find(parameters)
211
+ params_desc = {
212
+ 'includes' => :mandatory,
213
+ 'excludes' => :optional,
214
+ 'property' => :mandatory
215
+ }
216
+ check_task_parameters(parameters, params_desc)
217
+ includes = parameters['includes']
218
+ excludes = parameters['excludes']
219
+ property = parameters['property']
220
+ files = select_files(includes, excludes)
221
+ @build.context.set_property(property, files)
222
+ end
223
+
224
+ # Run a set of tests selected using globs. Parameter is a glob or list of
225
+ # globs for files or directories to run.
226
+ #
227
+ # Example
228
+ #
229
+ # - test: "**/tc_*.rb"
230
+ #
231
+ # FIXME
232
+ #
233
+ # if tasks runs twice, it will run twice loaded tests...
234
+ def test(globs)
235
+ require 'test/unit'
236
+ require 'test/unit/testresult'
237
+ files = []
238
+ globs = globs
239
+ error "test parameter must be a String or a list of Strings" unless
240
+ globs.kind_of?(String) or globs.kind_of?(Array)
241
+ for glob in globs
242
+ error "Parameter must be a String or a list of Strings" unless
243
+ glob.kind_of?(String)
244
+ files += Dir.glob(glob)
245
+ end
246
+ for file in files
247
+ load file
248
+ end
249
+ Test::Unit::AutoRunner.run
250
+ end
251
+
252
+ # Run an ERB file or source in bee context and store result in a file or
253
+ # property. Parameter is a Hash with following entries:
254
+ #
255
+ # - source: ERB source text (if no 'src').
256
+ # - src: ERB file name (if no 'source').
257
+ # - dest: file where to store result (if no 'property').
258
+ # - property: property name where to store result (if no 'dest').
259
+ #
260
+ # Example
261
+ #
262
+ # - erb: { src: "gem.spec.erb", dest: "gem.spec" }
263
+ #
264
+ # Notes
265
+ #
266
+ # In these ERB files, you can access a property _foo_ writing:
267
+ #
268
+ # <p>Hello <%= foo %>!</p>
269
+ def erb(params)
270
+ require 'erb'
271
+ # check parameters
272
+ params_desc = {
273
+ 'source' => :optional,
274
+ 'src' => :optional,
275
+ 'dest' => :optional,
276
+ 'property' => :optional
277
+ }
278
+ check_task_parameters(params, params_desc)
279
+ source = params['source']
280
+ error "erb 'source' parameter must be a String" unless
281
+ source.kind_of?(String) or source == nil
282
+ src = params['src']
283
+ error "erb 'src' parameter must be a String" unless
284
+ src.kind_of?(String) or src == nil
285
+ dest = params['dest']
286
+ error "erb 'dest' parameter must be a String" unless
287
+ dest.kind_of?(String) or dest == nil
288
+ property = params['property']
289
+ error "erb 'property' parameter must be a String" unless
290
+ property.kind_of?(String) or property == nil
291
+ error "Must pass one of 'source' or 'src' parameters to erb task" if
292
+ not source and not src
293
+ error "Must pass one of 'dest' or 'property' parameters to erb task" if
294
+ not dest and not property
295
+ # load ERB source
296
+ erb_source = source||File.read(src)
297
+ template = ERB.new(erb_source)
298
+ begin
299
+ result = template.result(@build.context.context_binding)
300
+ rescue
301
+ error "Error processing ERB: #{$!}"
302
+ end
303
+ # write result in file or set property
304
+ if dest
305
+ File.open(dest, 'w') { |file| file.write(result) }
306
+ else
307
+ @build.context.set_property(property, result)
308
+ end
309
+ end
310
+
311
+ # Generate RDoc documentation for a given list of globs to include or
312
+ # exclude and a destination directory. Parameter is a Hash with following
313
+ # entries:
314
+ #
315
+ # - includes: glob or list of globs for files to document.
316
+ # - excludes: glob or list of globs for files that should not be
317
+ # documented.
318
+ # - dest: destination directory for generated documentation.
319
+ # - options: additional options as a string or list of strings.
320
+ #
321
+ # Example
322
+ #
323
+ # - rdoc:
324
+ # includes: ["README", "LICENSE", :src]
325
+ # dest: :api
326
+ def rdoc(parameters)
327
+ require 'rdoc/rdoc'
328
+ params_desc = {
329
+ 'includes' => :mandatory,
330
+ 'excludes' => :optional,
331
+ 'dest' => :mandatory,
332
+ 'options' => :optional
333
+ }
334
+ check_task_parameters(parameters, params_desc)
335
+ includes = parameters['includes']
336
+ excludes = parameters['excludes']
337
+ dest = parameters['dest']
338
+ options = parameters['options']
339
+ error "rdoc 'dest' parameter must be a String" unless
340
+ dest.kind_of?(String)
341
+ files = select_files(includes, excludes)
342
+ command_line = ['-S', '-o', dest]
343
+ command_line << options if options
344
+ command_line += files
345
+ rdoc = RDoc::RDoc.new
346
+ rdoc.document(command_line)
347
+ end
348
+
349
+ # Generate a Gem package. Parameter is the name of the Gem description
350
+ # file. Resulting Gem package is generated in current directory (as with
351
+ # command line tool).
352
+ #
353
+ # Example
354
+ #
355
+ # - gembuild: :gem_spec
356
+ def gembuild(description)
357
+ require 'rubygems'
358
+ description = description
359
+ arguments = ['build', description]
360
+ Gem.manage_gems
361
+ Gem::GemRunner.new.run(arguments)
362
+ end
363
+
364
+ # Generate a ZIP archive. Parameter is a Hash with following entries:
365
+ #
366
+ # - includes: glob or list of globs for files to select for the archive.
367
+ # - excludes: glob or list of globs for files to exclude from the archive.
368
+ # - dest: the archive file to generate.
369
+ # - prefix: prefix for archive entries (default to nil).
370
+ #
371
+ # Example
372
+ #
373
+ # - zip:
374
+ # includes: "**/*"
375
+ # excludes: ["build", "build/**/*", "**/*~"]
376
+ # dest: :zip_archive
377
+ #
378
+ # Note
379
+ #
380
+ # If archive already exists, files are added to the archive.
381
+ def zip(parameters)
382
+ require 'zip/zip'
383
+ # parse parameters
384
+ params_desc = {
385
+ 'includes' => :mandatory,
386
+ 'excludes' => :optional,
387
+ 'dest' => :mandatory,
388
+ 'prefix' => :optional
389
+ }
390
+ check_task_parameters(parameters, params_desc)
391
+ includes = parameters['includes']
392
+ excludes = parameters['excludes']
393
+ dest = parameters['dest']
394
+ error "zip 'dest' parameter must be a String" unless
395
+ dest.kind_of?(String)
396
+ prefix = parameters['prefix']
397
+ error "zip 'prefix' parameter must be a String" unless
398
+ !prefix or prefix.kind_of?(String)
399
+ files = select_files(includes, excludes)
400
+ # build the archive
401
+ zipfile = Zip::ZipFile.open(dest, Zip::ZipFile::CREATE) do |zip|
402
+ for file in files
403
+ entry = prefix ? File.join(prefix, file) : file
404
+ puts "Adding '#{entry}'"
405
+ zip.add(entry, file)
406
+ end
407
+ zip.close
408
+ end
409
+ end
410
+
411
+ # Generate a TAR archive. Parameter is a Hash with following entries:
412
+ #
413
+ # - includes: glob or list of globs for files to select for the archive.
414
+ # - excludes: glob or list of globs for files to exclude from the archive.
415
+ # - dest: the archive file to generate.
416
+ #
417
+ # Example
418
+ #
419
+ # - tar:
420
+ # includes: "**/*"
421
+ # excludes: ["build", "build/**/*", "**/*~"]
422
+ # dest: :tar_archive
423
+ #
424
+ # Note
425
+ #
426
+ # If archive already exists, its is overwritten.
427
+ def tar(parameters)
428
+ require 'archive/tar/minitar'
429
+ # parse parameters
430
+ params_desc = {
431
+ 'includes' => :mandatory,
432
+ 'excludes' => :optional,
433
+ 'dest' => :mandatory
434
+ }
435
+ check_task_parameters(parameters, params_desc)
436
+ includes = parameters['includes']
437
+ excludes = parameters['excludes']
438
+ dest = parameters['dest']
439
+ error "tar 'dest' parameter must be a String" unless
440
+ dest.kind_of?(String)
441
+ files = select_files(includes, excludes)
442
+ # build the archive
443
+ Archive::Tar::Minitar::Output.open(dest) do |tarfile|
444
+ for file in files
445
+ puts "Adding '#{file}'"
446
+ Archive::Tar::Minitar.pack_file(file, tarfile)
447
+ end
448
+ end
449
+ end
450
+
451
+ # Generate a GZIP archive for a given file. Parameter is a Hash with
452
+ # following entries:
453
+ #
454
+ # - src: source file to generate GZIP for.
455
+ # - dest: GZIP file to generate.
456
+ #
457
+ # Example
458
+ #
459
+ # - gzip:
460
+ # src: "dist.tar"
461
+ # dest: "dist.tar.gz"
462
+ def gzip(parameters)
463
+ require 'zlib'
464
+ # parse parameters
465
+ params_desc = {
466
+ 'src' => :mandatory,
467
+ 'dest' => :mandatory
468
+ }
469
+ check_task_parameters(parameters, params_desc)
470
+ src = parameters['src']
471
+ dest = parameters['dest']
472
+ # compress file
473
+ File.open(src) do |input|
474
+ output = Zlib::GzipWriter.new(File.open(dest, 'wb'))
475
+ output.write(input.read)
476
+ output.close
477
+ end
478
+ end
479
+
480
+ # Generate a TAR.GZ archive. Parameter is a Hash with following entries:
481
+ #
482
+ # - includes: glob or list of globs for files to select for the archive.
483
+ # - excludes: glob or list of globs for files to exclude from the archive.
484
+ # - dest: the archive file to generate.
485
+ #
486
+ # Example
487
+ #
488
+ # - targz:
489
+ # includes: "**/*"
490
+ # excludes: ["build", "build/**/*", "**/*~"]
491
+ # dest: :targz_archive
492
+ #
493
+ # Note
494
+ #
495
+ # If archive already exists, its is overwritten.
496
+ def targz(parameters)
497
+ require 'archive/tar/minitar'
498
+ require 'zlib'
499
+ # parse parameters
500
+ params_desc = {
501
+ 'includes' => :mandatory,
502
+ 'excludes' => :optional,
503
+ 'dest' => :mandatory
504
+ }
505
+ check_task_parameters(parameters, params_desc)
506
+ includes = parameters['includes']
507
+ excludes = parameters['excludes']
508
+ dest = parameters['dest']
509
+ error "targz 'dest' parameter must be a String" unless
510
+ dest.kind_of?(String)
511
+ files = select_files(includes, excludes)
512
+ # build the archive
513
+ Archive::Tar::Minitar::Output.open(Zlib::GzipWriter.new(File.open(dest, 'wb'))) do |tgz|
514
+ for file in files
515
+ puts "Adding '#{file}'"
516
+ Archive::Tar::Minitar.pack_file(file, tgz)
517
+ end
518
+ end
519
+ end
520
+
521
+ end
522
+
523
+ end
524
+
525
+ end