Skip to content

Commit

Permalink
Allow cleaning namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Dec 21, 2020
1 parent c2eeae2 commit b151e4f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
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

0 comments on commit b151e4f

Please sign in to comment.