Skip to content

Commit

Permalink
Merge pull request #39 from pjump/chained_extensions
Browse files Browse the repository at this point in the history
Chained extensions
  • Loading branch information
hsbt authored Apr 18, 2017
2 parents b89f8a6 + c2ab781 commit 252ad9e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/rake/task_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def enhance_with_matching_rule(task_name, level=0)
"Rule Recursion Too Deep" if level >= 16
@rules.each do |pattern, args, extensions, block|
if pattern.match(task_name)
task = attempt_rule(task_name, args, extensions, block, level)
task = attempt_rule(task_name, pattern, args, extensions, block, level)
return task if task
end
end
Expand Down Expand Up @@ -241,8 +241,8 @@ def trace_rule(level, message) # :nodoc:
end

# Attempt to create a rule given the list of prerequisites.
def attempt_rule(task_name, args, extensions, block, level)
sources = make_sources(task_name, extensions)
def attempt_rule(task_name, task_pattern, args, extensions, block, level)
sources = make_sources(task_name, task_pattern, extensions)
prereqs = sources.map { |source|
trace_rule level, "Attempting Rule #{task_name} => #{source}"
if File.exist?(source) || Rake::Task.task_defined?(source)
Expand All @@ -263,15 +263,16 @@ def attempt_rule(task_name, args, extensions, block, level)

# Make a list of sources from the list of file name extensions /
# translation procs.
def make_sources(task_name, extensions)
def make_sources(task_name, task_pattern, extensions)
result = extensions.map { |ext|
case ext
when /%/
task_name.pathmap(ext)
when %r{/}
ext
when /^\./
task_name.ext(ext)
source = task_name.sub(task_pattern, ext)
source == ext ? task_name.ext(ext) : source
when String
ext
when Proc, Method
Expand Down
12 changes: 12 additions & 0 deletions test/test_rake_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TestRakeRules < Rake::TestCase
OBJFILE = "abc.o"
FOOFILE = "foo"
DOTFOOFILE = ".foo"
MINFILE = 'abc.min.o'

def setup
super
Expand Down Expand Up @@ -385,4 +386,15 @@ def obj.find_prereq(task_name)
assert_equal ["#{OBJFILE} - abc.c"], @runs
end

def test_works_with_chained_extensions_in_rules
create_file(OBJFILE)
rule('.min.o' => ['.o']) do |t|
@runs << t.name
assert_equal OBJFILE, t.source
assert_equal MINFILE, t.name
end
Task[MINFILE].invoke
assert_equal [MINFILE], @runs
end

end

0 comments on commit 252ad9e

Please sign in to comment.