Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 97 lines (72 sloc) 2.566 kb
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
namespace :debug do

  desc "Create a dot graph of the Rakefile's dependencies. Use :exclude to exclude rules and dependencies."
  task :graph, :include, :exclude, :minimum do |_, args|
    begin
      $: << File.expand_path("~/Work/p4/zss/src/ZenHacks/dev/lib/")
      require 'graph'
    rescue LoadError
      abort 'install ZenHacks'
    end

    exclude = Regexp.new(args[:exclude]) if args[:exclude]
    include = Regexp.new(args[:include]) if args[:exclude]
    minimum = (args[:minimum] || 10).to_i

    graph = Graph.new

    Rake::Task.tasks.each do |task|
      next unless task.name =~ /^vm/
      next if include and task.name !~ include
      next if exclude and task.name =~ exclude

      prereqs = task.prerequisites
      prereqs = prereqs.reject do |prereq|
        prereq =~ exclude
      end if exclude

      graph[task.name] = prereqs
    end

    invert_graph = graph.invert
    invert_graph.filter_size minimum if minimum > 0
    invert_graph.prefix << 'rankdir="LR"'

    invert_graph.keys_by_count.first(10).each_with_index do |key, i|
      if i < 5 then
        invert_graph.attribs[key] << "color = red"
      else
        invert_graph.attribs[key] << "color = orange"
      end
    end

    open 'rubinius_tasks.dot', 'w' do |io| io << invert_graph end
  end

  def get_task name
    Rake::Task.tasks.find { |t| t.name == name }
  end

  desc "Display prerequisites for a task"
  task :prereqs, :task do |_, args|
    raise "supply task argument" unless args[:task]

    task = get_task args[:task]

    raise "No such task #{args[:task].inspect}" unless task

    puts task.prerequisites.join("\n")
  end

  desc "Display prerequisites for a task"
  task :all_prereqs, :task do |_, args|
    raise "supply task argument" unless args[:task]

    task = get_task args[:task]

    raise "No such task #{args[:task].inspect}" unless task

    all = task.prerequisites
    all += all.map { |n| get_task(n).prerequisites }
    all = all.flatten.uniq

    puts all.join("\n")
  end

  def print_deps(task, depth = 0, done = {})
    deps = Rake::Task.tasks.select { |t| t.prerequisites.include? task.name }
    deps.each do |dep|
      next if done[dep.name]
      done[dep.name] = true
      puts "#{' ' * depth}#{dep.name}"
      print_deps(dep, depth+1, done)
    end
  end

  desc "Display tasks that depend on a task"
  task :dependees, :task do |_, args|
    raise "supply task argument" unless args[:task]

    task = get_task args[:task]

    raise "No such task #{args[:task].inspect}" unless task

    print_deps task
  end

end
Something went wrong with that request. Please try again.