summaryrefslogtreecommitdiff
path: root/lib/hoe/rake.rb
blob: 5e6824612f5ee6838a8aa8770e34955c90eaee13 (plain)
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
module Rake
  class Task
    # Used to name the source of a given task
    attr_accessor :plugin
    # :stopdoc:
    alias :old_comment :comment
    # :startdoc:

    ##
    # Overrides the default #comment to provide the plugin name.
    def comment
      "%-#{$plugin_max}s # %s" % [plugin, old_comment] if old_comment
    end
  end

  module TaskManager
    ##
    # This gives us access to the tasks already defined in rake.
    def all_tasks
      @tasks
    end
  end

  ##
  # Simple shortcut for Rake.application.all_tasks
  def self.all_tasks
    Rake.application.all_tasks
  end

  ##
  # Hooks into rake and allows us to clear out a task by name or
  # regexp. Use this if you want to completely override a task instead
  # of extend it.
  def self.clear_tasks(*tasks)
    tasks.flatten.each do |name|
      case name
      when Regexp then
        all_tasks.delete_if { |k,_| k =~ name }
      else
        all_tasks.delete(name)
      end
    end
  end

  ##
  # Removes the last action added to a task. Use this when two
  # libraries define the same task and you only want one of the
  # actions.
  #
  #   require 'hoe'
  #   require 'tasks/rails'
  #   Rake.undo("test") # rolls out rails' test task
  def self.undo(*names)
    names.each do |name|
      all_tasks[name].actions.delete_at(-1)
    end
  end
end