Skip to content

Commit 1969c9b

Browse files
committed
lazy task definition: take codeclimate into account
1 parent b550157 commit 1969c9b

6 files changed

+128
-98
lines changed

lib/rake.rb

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ module Rake; end
5858
require "rake/late_time"
5959
require "rake/name_space"
6060
require "rake/task_manager"
61+
require "rake/lazy_task_definition"
6162
require "rake/application"
6263
require "rake/backtrace"
6364

lib/rake/application.rb

+14-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require "optparse"
33

44
require "rake/task_manager"
5+
require "rake/lazy_task_definition"
56
require "rake/file_list"
67
require "rake/thread_pool"
78
require "rake/thread_history_display"
@@ -18,6 +19,7 @@ module Rake
1819

1920
class Application
2021
include TaskManager
22+
include LazyTaskDefinition
2123
include TraceOutput
2224

2325
# The name of the application (typically 'rake')
@@ -107,17 +109,20 @@ def load_rakefile
107109

108110
# Run the top level tasks of a Rake application.
109111
def top_level
110-
run_with_threads do
111-
execute_all_lazy_definitions if options.loadlazydefinitions
112-
if options.show_tasks
113-
display_tasks_and_comments
114-
elsif options.show_prereqs
115-
display_prerequisites
116-
else
117-
top_level_tasks.each { |task_name| invoke_task(task_name) }
118-
end
112+
run_with_threads { top_level_intern }
113+
end
114+
115+
def top_level_intern
116+
execute_all_lazy_definitions if options.loadlazydefinitions
117+
if options.show_tasks
118+
display_tasks_and_comments
119+
elsif options.show_prereqs
120+
display_prerequisites
121+
else
122+
top_level_tasks.each { |task_name| invoke_task(task_name) }
119123
end
120124
end
125+
private :top_level_intern
121126

122127
# Run the given block with the thread startup and shutdown.
123128
def run_with_threads

lib/rake/lazy_task_definition.rb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
module Rake
3+
4+
# The LazyTaskDefinition module is a mixin for managing lazy defined tasks.
5+
module LazyTaskDefinition
6+
7+
# Execute all definitions: usefull for rake -T for instance
8+
def execute_all_lazy_definitions
9+
lazy_definitions.each do |scope_path, _definitions|
10+
execute_lazy_definitions(scope_path)
11+
end
12+
end
13+
14+
# Execute all definitions linked to specified +scope_path+
15+
# and its parent scopes.
16+
def execute_lazy_definitions(scope_path)
17+
scope_path_elements = scope_path.split(':')
18+
sub_scope_elements = []
19+
scope_path_elements.each do |e|
20+
sub_scope_elements << e
21+
sub_scope_path = sub_scope_elements.join(':')
22+
definitions = lazy_definitions[sub_scope_path]
23+
next unless definitions
24+
definitions.each do |definition|
25+
definition.call
26+
end
27+
definitions.clear
28+
end
29+
end
30+
31+
# Evaluate the block in specified +scope+.
32+
def in_scope(scope)
33+
cur_scope = @scope
34+
@scope = scope
35+
yield
36+
ensure
37+
@scope = cur_scope
38+
end
39+
40+
# Register a block which will be called only when necessary during the lookup
41+
# of tasks
42+
def register_lazy_definition(&block)
43+
cur_scope = @scope
44+
lazy_definitions[cur_scope.path] ||= []
45+
lazy_definitions[cur_scope.path] << ->() { in_scope(cur_scope, &block) }
46+
end
47+
48+
def lazy_definitions
49+
@lazy_definitions ||= {}
50+
end
51+
private :lazy_definitions
52+
end
53+
end

lib/rake/task_manager.rb

-42
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def initialize # :nodoc:
1212
@rules = Array.new
1313
@scope = Scope.make
1414
@last_description = nil
15-
@lazy_definitions = {}
1615
end
1716

1817
def create_rule(*args, &block) # :nodoc:
@@ -237,47 +236,6 @@ def in_namespace(name)
237236
@scope = @scope.tail
238237
end
239238

240-
# Execute all definitions: usefull for rake -T for instance
241-
def execute_all_lazy_definitions
242-
@lazy_definitions.each do |scope_path, _definitions|
243-
execute_lazy_definitions(scope_path)
244-
end
245-
end
246-
247-
# Execute all definitions linked to specified +scope_path+
248-
# and its parent scopes.
249-
def execute_lazy_definitions(scope_path)
250-
scope_path_elements = scope_path.split(':')
251-
sub_scope_elements = []
252-
scope_path_elements.each do |e|
253-
sub_scope_elements << e
254-
sub_scope_path = sub_scope_elements.join(':')
255-
definitions = @lazy_definitions[sub_scope_path]
256-
next unless definitions
257-
definitions.each do |definition|
258-
definition.call
259-
end
260-
definitions.clear
261-
end
262-
end
263-
264-
# Evaluate the block in specified +scope+.
265-
def in_scope(scope)
266-
cur_scope = @scope
267-
@scope = scope
268-
yield
269-
ensure
270-
@scope = cur_scope
271-
end
272-
273-
# Register a block which will be called only when necessary during the lookup
274-
# of tasks
275-
def register_lazy_definition(&block)
276-
cur_scope = @scope
277-
@lazy_definitions[cur_scope.path] ||= []
278-
@lazy_definitions[cur_scope.path] << ->() { in_scope(cur_scope, &block) }
279-
end
280-
281239
private
282240

283241
# Add a location to the locations field of the given task.
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
require File.expand_path("../helper", __FILE__)
3+
4+
class TestRakeLazyTaskDefinition < Rake::TestCase # :nodoc:
5+
6+
def setup
7+
super
8+
9+
@tm = Rake::TestCase::TaskManager.new
10+
@tm.extend Rake::LazyTaskDefinition
11+
end
12+
13+
def test_lazy_definition
14+
t1, t2, t3 = nil, nil, nil
15+
lazy_definition_call_count = 0
16+
@tm.in_namespace("a1") do
17+
@tm.register_lazy_definition do
18+
t1 = @tm.define_task(Rake::Task, :t1)
19+
lazy_definition_call_count += 1
20+
@tm.in_namespace("a2") do
21+
t2 = @tm.define_task(Rake::Task, :t2)
22+
end
23+
end
24+
end
25+
@tm.in_namespace("b") do
26+
t3 = @tm.define_task(Rake::Task, :t3)
27+
end
28+
# task t3 is not lazy. It can be found
29+
assert_equal t3, @tm[:t3, Rake::Scope.make("b")]
30+
# lazy definition is not called until we look for task in namespace a
31+
assert_equal lazy_definition_call_count, 0
32+
33+
# task t2 can be found
34+
found_task_t2 = @tm[:t2, Rake::Scope.make("a1:a2")]
35+
assert_equal t2, found_task_t2
36+
# lazy definition is expected to be called
37+
assert_equal lazy_definition_call_count, 1
38+
39+
# task t1 can also be found
40+
found_task_t1 = @tm[:t1, Rake::Scope.make("a1")]
41+
assert_equal t1, found_task_t1
42+
# lazy definition is called at most once
43+
assert_equal lazy_definition_call_count, 1
44+
end
45+
46+
def test_execute_all_lazy_definitions
47+
lazy_definition_call_count = 0
48+
@tm.in_namespace("a") do
49+
@tm.register_lazy_definition do
50+
lazy_definition_call_count += 1
51+
end
52+
end
53+
assert_equal lazy_definition_call_count, 0
54+
@tm.execute_all_lazy_definitions
55+
assert_equal lazy_definition_call_count, 1
56+
@tm.execute_all_lazy_definitions
57+
assert_equal lazy_definition_call_count, 1
58+
end
59+
60+
end

test/test_rake_task_manager.rb

-47
Original file line numberDiff line numberDiff line change
@@ -186,51 +186,4 @@ def test_correctly_scoped_prerequisites_are_invoked
186186
assert_equal ["next z"], values
187187
end
188188

189-
def test_lazy_definition
190-
t1, t2, t3 = nil, nil, nil
191-
lazy_definition_call_count = 0
192-
@tm.in_namespace("a1") do
193-
@tm.register_lazy_definition do
194-
t1 = @tm.define_task(Rake::Task, :t1)
195-
lazy_definition_call_count += 1
196-
@tm.in_namespace("a2") do
197-
t2 = @tm.define_task(Rake::Task, :t2)
198-
end
199-
end
200-
end
201-
@tm.in_namespace("b") do
202-
t3 = @tm.define_task(Rake::Task, :t3)
203-
end
204-
# task t3 is not lazy. It can be found
205-
assert_equal t3, @tm[:t3, Rake::Scope.make("b")]
206-
# lazy definition is not called until we look for task in namespace a
207-
assert_equal lazy_definition_call_count, 0
208-
209-
# task t2 can be found
210-
found_task_t2 = @tm[:t2, Rake::Scope.make("a1:a2")]
211-
assert_equal t2, found_task_t2
212-
# lazy definition is expected to be called
213-
assert_equal lazy_definition_call_count, 1
214-
215-
# task t1 can also be found
216-
found_task_t1 = @tm[:t1, Rake::Scope.make("a1")]
217-
assert_equal t1, found_task_t1
218-
# lazy definition is called at most once
219-
assert_equal lazy_definition_call_count, 1
220-
end
221-
222-
def test_execute_all_lazy_definitions
223-
lazy_definition_call_count = 0
224-
@tm.in_namespace("a") do
225-
@tm.register_lazy_definition do
226-
lazy_definition_call_count += 1
227-
end
228-
end
229-
assert_equal lazy_definition_call_count, 0
230-
@tm.execute_all_lazy_definitions
231-
assert_equal lazy_definition_call_count, 1
232-
@tm.execute_all_lazy_definitions
233-
assert_equal lazy_definition_call_count, 1
234-
end
235-
236189
end

0 commit comments

Comments
 (0)