Skip to content

Instantly share code, notes, and snippets.

@techiferous
Created May 27, 2010 19:41
Show Gist options
  • Save techiferous/416249 to your computer and use it in GitHub Desktop.
Save techiferous/416249 to your computer and use it in GitHub Desktop.
# This extends the behavior of Rake to allow you to see all of the dependencies
# of a Rake task recursively, not just the immediate dependencies.
#
# It adds a -d option (also called --dependency-tree). Left blank, this
# option shows you dependencies for all Rake tasks. Alternatively, you
# can pass a regular expression to this option and it will only show
# dependencies for matching tasks.
#
# To use this special version of Rake, save it in a Ruby file called
# raketree.rb (but the name really doesn't matter). Here are some examples of
# how to invoke it:
#
# $ ruby raketree.rb -h #--> shows help
# $ ruby raketree.rb -T #--> lists all Rake tasks
#
# Here are some examples of how to use the special dependency-tree
# functionality:
#
# $ ruby raketree.rb -d #--> shows dependencies for all rake tasks
# $ ruby raketree.rb -d db #--> shows dependencies for all rake tasks
# that match the regex /db/
# $ ruby raketree.rb -d t?st #--> shows dependencies for all rake tasks
# that match the regex /t?st/
#
#**********************************************************
# LOAD RAKE
#******
begin
require 'rake'
rescue LoadError
require 'rubygems'
require 'rake'
end
#**********************************************************
# CUSTOMIZE RAKE
#******
module Rake
class Application
# Add our own custom Rake option.
alias_method :old_standard_rake_options, :standard_rake_options
def standard_rake_options
old_standard_rake_options <<
['--dependency-tree', '-d [PATTERN]', "Display all the dependencies for tasks (matching optional PATTERN) then exit.",
lambda { |value|
options.dependency_tree = true
options.show_task_pattern = Regexp.new(value || '')
}
]
end
# Redefine this method so that it calls our own display_dependency_tree method.
def top_level
standard_exception_handling do
if options.show_tasks
display_tasks_and_comments
elsif options.show_prereqs
display_prerequisites
elsif options.dependency_tree
display_dependency_tree
else
top_level_tasks.each { |task_name| invoke_task(task_name) }
end
end
end
def display_dependency_tree
displayable_tasks = tasks.select do |t|
t.comment && t.name =~ options.show_task_pattern
end
for task in displayable_tasks
$indent = 0
puts task.name
display_dependent_tasks(task)
end
end
def display_dependent_tasks(task)
$indent += 1
for t in task.dependencies
if t.is_a? String
# No Rake Task object could be built to represent this,
# so we just display it.
puts(' ' * $indent + t)
elsif t.is_a? FileTask
# We choose not to display FileTasks. A Rake task can be defined
# to depend on the timestamps of files; hence the need for
# FileTasks. If we chose to display FileTasks, the rdoc task
# would display a long list of files as dependencies.
else
puts(' ' * $indent + t.name)
display_dependent_tasks(t)
end
end
$indent -= 1
end
end
class Task
def dependencies
result = []
# @prerequisites is an array of strings that name tasks
for p in @prerequisites
begin
result << Task[p] # build a Task object from the task name
rescue
# the Task object could not be built, so we just remember the name
result << p
end
end
result
end
end
end
#**********************************************************
# INVOKE RAKE
#******
Rake.application.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment