Class: MagicLoader::Task

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/magic_loader/tasks.rb

Overview

Generates the MagicLoader rake task

Instance Method Summary collapse

Constructor Details

#initialize(*paths) ⇒ Task

Returns a new instance of Task.



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
# File 'lib/magic_loader/tasks.rb', line 22

def initialize(*paths)
  options = paths.last.is_a?(Hash) ? paths.pop : {}
  task_name = options[:name] || 'magicload'

  task task_name do
    load_order = MagicLoader.require_all(*paths)
    strip_paths!(load_order, options[:strip]) if options[:strip]

    magic_block = [
      BEGIN_MAGIC,
      MAGIC_WARNING,
      "# Run \"rake #{task_name}\" to regenerate",
      load_order.map { |t| "require #{t.dump}" },
      END_MAGIC
    ].flatten.join("\n")
  
    if options[:target]
      if File.exists? options[:target]
        annotate_file options[:target], magic_block
      else
        File.open(options[:target], "w") { |f| f << magic_block }
      end
    else
      puts magic_block
    end
  end
end