Skip to content

Instantly share code, notes, and snippets.

@jsm
Last active August 29, 2015 13:57
Show Gist options
  • Save jsm/9923760 to your computer and use it in GitHub Desktop.
Save jsm/9923760 to your computer and use it in GitHub Desktop.
def default_task(deps=nil, &block)
create_default_task(false, deps, &block)
end
def default_multitask(deps=nil, &block)
create_default_task(true, deps, &block)
end
def create_default_task(multitask, deps=nil, &block)
# Save the description
description = Rake.application.last_description
Rake.application.last_description = nil
# Define the default task within the namespace
task_call = multitask ? :multitask : :task
if deps.is_a? Hash
self.send(task_call, :default, deps, &block)
else
if deps
self.send(task_call, :default => deps, &block)
else
self.send(task_call, :default, &block)
end
end
# Save the current scope
current_scope = Rake.application.instance_variable_get("@scope")
# Get the task_name from the scope
scope_path = current_scope.to_a
task_name = scope_path.shift
# Create a scope to imitate the parent scope
parent_scope = Rake::Scope.make(*scope_path)
Rake.application.instance_variable_set("@scope", parent_scope)
# Define the task
desc description
if deps.is_a? Hash
task task_name, deps.keys.first => current_scope.path_with_task_name("default")
else
task task_name => current_scope.path_with_task_name("default")
end
# Reset the scope back to what it was
Rake.application.instance_variable_set("@scope", current_scope)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment