Skip to content

Commit b151e4f

Browse files
committed
Allow cleaning namespaces
1 parent c2eeae2 commit b151e4f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/rake/name_space.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def [](name)
2121
@task_manager.lookup(name, @scope)
2222
end
2323

24+
def path
25+
@scope.path
26+
end
27+
2428
##
2529
# The scope of the namespace (a LinkedList)
2630

@@ -35,4 +39,20 @@ def tasks
3539
@task_manager.tasks_in_scope(@scope)
3640
end
3741

42+
def namespaces
43+
@task_manager.namespaces_in_scope(@scope)
44+
end
45+
46+
def clear
47+
namespaces.each do |ns|
48+
@task_manager.remove_namespace(ns.path)
49+
end
50+
end
51+
52+
class << self
53+
def [](namespace_name)
54+
Rake.application.lookup_namespace(namespace_name)
55+
end
56+
end
57+
3858
end

lib/rake/task_manager.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module TaskManager
88

99
def initialize # :nodoc:
1010
super
11+
@namespaces = Hash.new
1112
@tasks = Hash.new
1213
@rules = Array.new
1314
@scope = Scope.make
@@ -50,6 +51,10 @@ def intern(task_class, task_name)
5051
@tasks[task_name.to_s] ||= task_class.new(task_name, self)
5152
end
5253

54+
def remove_task(task_name) # :nodoc:
55+
@tasks.delete(task_name)
56+
end
57+
5358
# Find a matching task for +task_name+.
5459
def [](task_name, scopes=nil)
5560
task_name = task_name.to_s
@@ -178,6 +183,13 @@ def tasks_in_scope(scope)
178183
}
179184
end
180185

186+
def namespaces_in_scope(scope)
187+
prefix = scope.path
188+
@namespaces.select { |name|
189+
name.start_with?(prefix)
190+
}.values
191+
end
192+
181193
# Clear all tasks in this application.
182194
def clear
183195
@tasks.clear
@@ -229,12 +241,24 @@ def in_namespace(name)
229241
name ||= generate_name
230242
@scope = Scope.new(name, @scope)
231243
ns = NameSpace.new(self, @scope)
244+
@namespaces[ns.path] = ns
232245
yield(ns)
233246
ns
234247
ensure
235248
@scope = @scope.tail
236249
end
237250

251+
def lookup_namespace(name) # :nodoc:
252+
@namespaces[name]
253+
end
254+
255+
def remove_namespace(name) # :nodoc:
256+
ns = @namespaces.delete(name)
257+
if ns
258+
ns.tasks.each { |t| remove_task(t.name) }
259+
end
260+
end
261+
238262
private
239263

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

0 commit comments

Comments
 (0)