File: rakefile_definitions.rb

package info (click to toggle)
rake 13.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 928 kB
  • sloc: ruby: 9,635; sh: 19; ansic: 19; makefile: 11
file content (501 lines) | stat: -rw-r--r-- 9,823 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# frozen_string_literal: true
module RakefileDefinitions
  include FileUtils

  def rakefile_access
    rakefile <<~ACCESS
      TOP_LEVEL_CONSTANT = 0

      def a_top_level_function
      end

      task :default => [:work, :obj, :const]

      task :work do
        begin
          a_top_level_function
          puts "GOOD:M Top level methods can be called in tasks"
        rescue NameError => ex
          puts "BAD:M  Top level methods can not be called in tasks"
        end
      end

      task :obj do
        begin
          Object.new.instance_eval { task :xyzzy }
          puts "BAD:D  Rake DSL are polluting objects"
        rescue StandardError => ex
          puts "GOOD:D Rake DSL are not polluting objects"
        end
      end

      task :const do
        begin
          TOP_LEVEL_CONSTANT
          puts "GOOD:C Top level constants are available in tasks"
        rescue StandardError => ex
          puts "BAD:C  Top level constants are NOT available in tasks"
        end
      end
    ACCESS
  end

  def rakefile_test_task
    rakefile <<~RAKEFILE
      require "rake/testtask"

      Rake::TestTask.new(:unit) do |t|
        t.description = "custom test task description"
      end
    RAKEFILE
  end

  def rakefile_test_task_verbose
    rakefile <<~RAKEFILE
      require "rake/testtask"

      Rake::TestTask.new(:unit) do |t|
        t.verbose = true
      end
    RAKEFILE
  end

  def rakefile_chains
    rakefile <<~DEFAULT
      task :default => "play.app"

      file "play.scpt" => "base" do |t|
        cp t.prerequisites.first, t.name
      end

      rule ".app" => ".scpt" do |t|
        cp t.source, t.name
      end

      file 'base' do
        touch 'base'
      end
    DEFAULT
  end

  def rakefile_file_chains
    rakefile <<~RAKEFILE
      file "fileA" do |t|
        sh "echo contentA >\#{t.name}"
      end

      file "fileB" => "fileA" do |t|
        sh "(cat fileA; echo transformationB) >\#{t.name}"
      end

      file "fileC" => "fileB" do |t|
        sh "(cat fileB; echo transformationC) >\#{t.name}"
      end

      task default: "fileC"
    RAKEFILE
  end

  def rakefile_comments
    rakefile <<~COMMENTS
      # comment for t1
      task :t1 do
      end

      # no comment or task because there's a blank line

      task :t2 do
      end

      desc "override comment for t3"
      # this is not the description
      multitask :t3 do
      end

      # this is not the description
      desc "override comment for t4"
      file :t4 do
      end
    COMMENTS
  end

  def rakefile_override
    rakefile <<~OVERRIDE
      task :t1 do
        puts :foo
      end

      task :t1 do
        puts :bar
      end
    OVERRIDE
  end

  def rakefile_default
    rakefile <<~DEFAULT
      if ENV['TESTTOPSCOPE']
        puts "TOPSCOPE"
      end

      task :default do
        puts "DEFAULT"
      end

      task :other => [:default] do
        puts "OTHER"
      end

      task :task_scope do
        if ENV['TESTTASKSCOPE']
          puts "TASKSCOPE"
        end
      end
    DEFAULT
  end

  def rakefile_dryrun
    rakefile <<~DRYRUN
      task :default => ["temp_main"]

      file "temp_main" => [:all_apps]  do touch "temp_main" end

      task :all_apps => [:one, :two]
      task :one => ["temp_one"]
      task :two => ["temp_two"]

      file "temp_one" do |t|
        touch "temp_one"
      end
      file "temp_two" do |t|
        touch "temp_two"
      end

      task :clean do
        ["temp_one", "temp_two", "temp_main"].each do |file|
          rm_f file
        end
      end
    DRYRUN

    FileUtils.touch "temp_main"
    FileUtils.touch "temp_two"
  end

  def rakefile_extra
    rakefile "task :default"

    FileUtils.mkdir_p "rakelib"

    File.write File.join("rakelib", "extra.rake"), <<~EXTRA_RAKE
      # Added for testing

      namespace :extra do
        desc "An Extra Task"
        task :extra do
          puts "Read all about it"
        end
      end
    EXTRA_RAKE
  end

  def rakefile_file_creation
    rakefile <<~'FILE_CREATION'
      N = 2

      task :default => :run

      BUILD_DIR = 'build'
      task :clean do
        rm_rf 'build'
        rm_rf 'src'
      end

      task :run

      TARGET_DIR = 'build/copies'

      FileList['src/*'].each do |src|
        directory TARGET_DIR
        target = File.join TARGET_DIR, File.basename(src)
        file target => [src, TARGET_DIR] do
          cp src, target
        end
        task :run => target
      end

      task :prep => :clean do
        mkdir_p 'src'
        N.times do |n|
          touch "src/foo#{n}"
        end
      end
    FILE_CREATION
  end

  def rakefile_imports
    rakefile <<~IMPORTS
      require 'rake/loaders/makefile'

      task :default

      task :other do
        puts "OTHER"
      end

      file "dynamic_deps" do |t|
        File.write t.name, "puts 'DYNAMIC'\n"
      end

      import "dynamic_deps"
      import "static_deps"
      import "static_deps"
      import "deps.mf"
      puts "FIRST"
    IMPORTS

    File.write "deps.mf", <<~DEPS
      default: other
    DEPS

    File.write "static_deps", "puts 'STATIC'\n"
  end

  def rakefile_regenerate_imports
    rakefile <<~REGENERATE_IMPORTS
      task :default

      task :regenerate do
        File.write "deps", <<~CONTENT
          file "deps" => :regenerate
          puts "REGENERATED"
        CONTENT
      end

      import "deps"
    REGENERATE_IMPORTS

    File.write "deps", <<~CONTENT
      file "deps" => :regenerate
      puts "INITIAL"
    CONTENT
  end

  def rakefile_multidesc
    rakefile <<~MULTIDESC
      task :b

      desc "A"
      task :a

      desc "B"
      task :b

      desc "A2"
      task :a

      task :c

      desc "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
      task :d
    MULTIDESC
  end

  def rakefile_namespace
    rakefile <<~NAMESPACE
      desc "copy"
      task :copy do
        puts "COPY"
      end

      namespace "nest" do
        desc "nest copy"
        task :copy do
          puts "NEST COPY"
        end
        task :xx => :copy
      end

      anon_ns = namespace do
        desc "anonymous copy task"
        task :copy do
          puts "ANON COPY"
        end
      end

      desc "Top level task to run the anonymous version of copy"
      task :anon => anon_ns[:copy]

      namespace "very" do
        namespace "nested" do
          task "run" => "rake:copy"
        end
      end

      namespace "a" do
        desc "Run task in the 'a' namespace"
        task "run" do
          puts "IN A"
        end
      end

      namespace "b" do
        desc "Run task in the 'b' namespace"
        task "run" => "a:run" do
          puts "IN B"
        end
      end

      namespace "file1" do
        file "xyz.rb" do
          puts "XYZ1"
        end
      end

      namespace "file2" do
        file "xyz.rb" do
          puts "XYZ2"
        end
      end

      namespace "scopedep" do
        task :prepare do
          touch "scopedep.rb"
          puts "PREPARE"
        end
        file "scopedep.rb" => [:prepare] do
          puts "SCOPEDEP"
        end
      end
    NAMESPACE
  end

  def rakefile_nosearch
    FileUtils.touch "dummy"
  end

  def rakefile_rakelib
    FileUtils.mkdir_p "rakelib"

    Dir.chdir "rakelib" do
      File.write "test1.rb", <<~TEST1
        task :default do
          puts "TEST1"
        end
      TEST1

      File.write "test2.rake", <<~TEST2
        task :default do
          puts "TEST2"
        end
      TEST2
    end
  end

  def rakefile_rbext
    File.write "rakefile.rb", 'task :default do puts "OK" end'
  end

  def rakefile_unittest
    rakefile "# Empty Rakefile for Unit Test"

    readme = File.join "subdir", "README"
    FileUtils.mkdir_p File.dirname readme

    FileUtils.touch readme
  end

  def rakefile_verbose
    rakefile <<~VERBOSE
      task :standalone_verbose_true do
        verbose true
        sh "#{RUBY} -e '0'"
      end

      task :standalone_verbose_false do
        verbose false
        sh "#{RUBY} -e '0'"
      end

      task :inline_verbose_default do
        sh "#{RUBY} -e '0'"
      end

      task :inline_verbose_false do
        sh "#{RUBY} -e '0'", :verbose => false
      end

      task :inline_verbose_true do
        sh "#{RUBY} -e '0'", :verbose => true
      end

      task :block_verbose_true do
        verbose(true) do
          sh "#{RUBY} -e '0'"
        end
      end

      task :block_verbose_false do
        verbose(false) do
          sh "#{RUBY} -e '0'"
        end
      end
    VERBOSE
  end

  def rakefile_test_signal
    rakefile <<~TEST_SIGNAL
      require 'rake/testtask'

      Rake::TestTask.new(:a) do |t|
        t.test_files = ['a_test.rb']
      end

      Rake::TestTask.new(:b) do |t|
        t.test_files = ['b_test.rb']
      end

      task :test do
        Rake::Task[:a].invoke
        Rake::Task[:b].invoke
      end

      task :default => :test
    TEST_SIGNAL
    File.write "a_test.rb", <<~A_TEST
      puts "ATEST"
      $stdout.flush
      Process.kill("TERM", $$)
    A_TEST
    File.write "b_test.rb", <<~B_TEST
      puts "BTEST"
      $stdout.flush
    B_TEST
  end

  def rakefile_failing_test_task
    rakefile <<~TEST_TASK
      require 'rake/testtask'

      task :default => :test
      Rake::TestTask.new(:test) do |t|
        t.test_files = ['a_test.rb']
      end
    TEST_TASK
    File.write "a_test.rb", <<~A_TEST
      require 'minitest/autorun'
      class ExitTaskTest < Minitest::Test
        def test_exit
          assert false, 'this should fail'
        end
      end
    A_TEST
  end

  def rakefile_stand_alone_filelist
    File.write "stand_alone_filelist.rb", <<~STAND_ALONE
      require 'rake/file_list'
      FL = Rake::FileList['*.rb']
      puts FL
    STAND_ALONE
  end
end