Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow cleaning namespaces #371

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/rake/name_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def [](name)
@task_manager.lookup(name, @scope)
end

def path
@scope.path
end

##
# The scope of the namespace (a LinkedList)

Expand All @@ -35,4 +39,20 @@ def tasks
@task_manager.tasks_in_scope(@scope)
end

def namespaces
@task_manager.namespaces_in_scope(@scope)
end

def clear
namespaces.each do |ns|
@task_manager.remove_namespace(ns.path)
end
end

class << self
def [](namespace_name)
Rake.application.lookup_namespace(namespace_name)
end
end

end
24 changes: 24 additions & 0 deletions lib/rake/task_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module TaskManager

def initialize # :nodoc:
super
@namespaces = Hash.new
@tasks = Hash.new
@rules = Array.new
@scope = Scope.make
Expand Down Expand Up @@ -50,6 +51,10 @@ def intern(task_class, task_name)
@tasks[task_name.to_s] ||= task_class.new(task_name, self)
end

def remove_task(task_name) # :nodoc:
@tasks.delete(task_name)
end

# Find a matching task for +task_name+.
def [](task_name, scopes=nil)
task_name = task_name.to_s
Expand Down Expand Up @@ -178,6 +183,13 @@ def tasks_in_scope(scope)
}
end

def namespaces_in_scope(scope)
prefix = scope.path
@namespaces.select { |name|
name.start_with?(prefix)
}.values
end

# Clear all tasks in this application.
def clear
@tasks.clear
Expand Down Expand Up @@ -229,12 +241,24 @@ def in_namespace(name)
name ||= generate_name
@scope = Scope.new(name, @scope)
ns = NameSpace.new(self, @scope)
@namespaces[ns.path] = ns
yield(ns)
ns
ensure
@scope = @scope.tail
end

def lookup_namespace(name) # :nodoc:
@namespaces[name]
end

def remove_namespace(name) # :nodoc:
ns = @namespaces.delete(name)
if ns
ns.tasks.each { |t| remove_task(t.name) }
end
end

private

# Add a location to the locations field of the given task.
Expand Down