Skip to content

Commit

Permalink
Deprecated top level import in favor of Rake.import
Browse files Browse the repository at this point in the history
Also added generic deprecation warnings for other deprecated features.
  • Loading branch information
jimweirich committed Mar 5, 2011
1 parent 5ca24ba commit 3b60390
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 43 deletions.
19 changes: 12 additions & 7 deletions doc/rakefile.rdoc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -358,12 +358,17 @@ invoked. This make generated dependency files difficult to use. By
the time rake gets around to updating the dependencies file, it is too the time rake gets around to updating the dependencies file, it is too
late to load it. late to load it.


The +import+ command addresses this by specifying a file to be loaded The +Rake.import+ command addresses this by specifying a file to be
_after_ the main rakefile is loaded, but _before_ any targets on the loaded _after_ the main rakefile is loaded, but _before_ any targets
command line are specified. In addition, if the file name matches an on the command line are invoked. In addition, if the file name
explicit task, that task is invoked before loading the file. This matches an explicit task, that task is invoked before loading the
allows dependency files to be generated and used in a single rake file. This allows dependency files to be generated and used in a
command invocation. single rake command invocation.

<b>NOTE:</b> Starting in Rake version 0.9.0, the top level +import+
command is deprecated and we recommend using the scoped
"+Rake.import+" command mentioned above. Future versions of Rake will
drop support for the top level +import+ command.


=== Example: === Example:


Expand All @@ -373,7 +378,7 @@ command invocation.
sh "makedepend -f- -- #{CFLAGS} -- #{t.prerequisites} > #{t.name}" sh "makedepend -f- -- #{CFLAGS} -- #{t.prerequisites} > #{t.name}"
end end


import ".depends.mf" Rake.import ".depends.mf"


If ".depends" does not exist, or is out of date w.r.t. the source If ".depends" does not exist, or is out of date w.r.t. the source
files, a new ".depends" file is generated using +makedepend+ before files, a new ".depends" file is generated using +makedepend+ before
Expand Down
17 changes: 17 additions & 0 deletions lib/rake/application.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ def display_error_message(ex)
$stderr.puts "(See full trace by running task with --trace)" unless options.trace $stderr.puts "(See full trace by running task with --trace)" unless options.trace
end end


# Warn about deprecated usage.
#
# Example:
# Rake.application.deprecate("import", "Rake.import", caller.first)
#
def deprecate(old_usage, new_usage, call_site)
return if options.ignore_deprecate
$stderr.puts "WARNING: '#{old_usage}' is deprecated. " +
"Please use '#{new_usage}' instead.\n" +
" at #{call_site}"
end

# Does the exception have a task invocation chain? # Does the exception have a task invocation chain?
def has_chain?(exception) def has_chain?(exception)
exception.respond_to?(:chain) && exception.chain exception.respond_to?(:chain) && exception.chain
Expand Down Expand Up @@ -392,6 +404,11 @@ def standard_rake_options
Rake::TaskManager.record_task_metadata = true Rake::TaskManager.record_task_metadata = true
} }
], ],
['--no-deprecation-warnings', '-X', "Disable the deprecation warnings.",
lambda { |value|
options.ignore_deprecate = true
}
],
] ]
end end


Expand Down
43 changes: 25 additions & 18 deletions lib/rake/dsl_definition.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -116,24 +116,31 @@ def desc(description)
Rake.application.last_description = description Rake.application.last_description = description
end end


# Import the partial Rakefiles +fn+. Imported files are loaded # The DSL level import command is now deprecated and moved the
# _after_ the current file is completely loaded. This allows the # Rake.import.
# import statement to appear anywhere in the importing file, and yet def import(*fn)
# allowing the imported files to depend on objects defined in the Rake.application.deprecate("import", "Rake.import", caller.first)
# importing file. Rake.import(*fn)
# end
# A common use of the import statement is to include files end
# containing dependency declarations.
# # Import the partial Rakefiles +fn+. Imported files are loaded
# See also the --rakelibdir command line option. # _after_ the current file is completely loaded. This allows the
# # import statement to appear anywhere in the importing file, and yet
# Example: # allowing the imported files to depend on objects defined in the
# import ".depend", "my_rules" # importing file.
# #
def import(*fns) # A common use of the import statement is to include files
fns.each do |fn| # containing dependency declarations.
Rake.application.add_import(fn) #
end # See also the --rakelibdir command line option.
#
# Example:
# import ".depend", "my_rules"
#
def Rake.import(*fns)
fns.each do |fn|
Rake.application.add_import(fn)
end end
end end


Expand Down
4 changes: 4 additions & 0 deletions lib/rake/task_manager.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def resolve_args_with_dependencies(args, hash) # :nodoc:
arg_names = [] arg_names = []
deps = value deps = value
elsif key == :needs elsif key == :needs
Rake.application.deprecate(
"task :t, arg, :needs => [deps]",
"task :t, [args] => [deps]",
caller.detect { |c| c !~ /\blib\/rake\b/ })
task_name = args.shift task_name = args.shift
arg_names = args arg_names = args
deps = value deps = value
Expand Down
1 change: 1 addition & 0 deletions test/data/deprecated_import/Rakefile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
import "a"
4 changes: 4 additions & 0 deletions test/data/deprecated_import/a
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/ruby -wKU
task :imported do
puts "imported!"
end
16 changes: 16 additions & 0 deletions test/functional/session_based_tests.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -254,6 +254,22 @@ def test_trace_bug
assert_status assert_status
end end


def test_deprecated_import
in_environment("PWD" => "test/data/deprecated_import") do
rake "imported"
end
assert_match(/deprecated/, @err)
assert_match(/imported!/, @out)
end

def test_suppressed_deprecated_message
in_environment("PWD" => "test/data/deprecated_import") do
rake "imported -X"
end
assert_not_match(/deprecated/, @err)
assert_match(/imported!/, @out)
end

def test_imports def test_imports
open("test/data/imports/static_deps", "w") do |f| open("test/data/imports/static_deps", "w") do |f|
f.puts 'puts "STATIC"' f.puts 'puts "STATIC"'
Expand Down
10 changes: 10 additions & 0 deletions test/lib/task_manager_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ def test_correctly_scoped_prerequisites_are_invoked
end end


class TestTaskManagerArgumentResolution < Test::Unit::TestCase class TestTaskManagerArgumentResolution < Test::Unit::TestCase
def setup
super
Rake.application.options.ignore_deprecate = true
end

def teardown
Rake.application.options.ignore_deprecate = false
super
end

def test_good_arg_patterns def test_good_arg_patterns
assert_equal [:t, [], []], task(:t) assert_equal [:t, [], []], task(:t)
assert_equal [:t, [], [:x]], task(:t => :x) assert_equal [:t, [], [:x]], task(:t => :x)
Expand Down
32 changes: 15 additions & 17 deletions test/lib/task_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def test_create
end end


def test_inspect def test_inspect
t = task(:foo, :needs => [:bar, :baz]) # t = task(:foo, :needs => [:bar, :baz])
t = task(:foo => [:bar, :baz])
assert_equal "<Rake::Task foo => [bar, baz]>", t.inspect assert_equal "<Rake::Task foo => [bar, baz]>", t.inspect
end end


Expand Down Expand Up @@ -308,23 +309,20 @@ def test_name_and_needs
assert_equal ["pre"], t.prerequisites assert_equal ["pre"], t.prerequisites
end end


def test_name_and_explicit_needs
t = task(:t, :needs => [:pre])
assert_equal "t", t.name
assert_equal [], t.arg_names
assert_equal ["pre"], t.prerequisites
end

def test_name_args_and_explicit_needs def test_name_args_and_explicit_needs
t = task(:t, :x, :y, :needs => [:pre]) ignore_deprecations do
assert_equal "t", t.name t = task(:t, :x, :y, :needs => [:pre])
assert_equal [:x, :y], t.arg_names assert_equal "t", t.name
assert_equal ["pre"], t.prerequisites assert_equal [:x, :y], t.arg_names
assert_equal ["pre"], t.prerequisites
end
end end


def test_illegal_keys_in_task_name_hash def test_illegal_keys_in_task_name_hash
assert_exception RuntimeError do ignore_deprecations do
t = task(:t, :x, :y => 1, :needs => [:pre]) assert_exception RuntimeError do
t = task(:t, :x, :y => 1, :needs => [:pre])
end
end end
end end


Expand Down Expand Up @@ -415,7 +413,7 @@ def test_name_with_args
def test_named_args_are_passed_to_prereqs def test_named_args_are_passed_to_prereqs
value = nil value = nil
pre = task(:pre, :rev) { |t, args| value = args.rev } pre = task(:pre, :rev) { |t, args| value = args.rev }
t = task(:t, :name, :rev, :needs => [:pre]) t = task(:t, [:name, :rev] => [:pre])
t.invoke("bill", "1.2") t.invoke("bill", "1.2")
assert_equal "1.2", value assert_equal "1.2", value
end end
Expand All @@ -425,15 +423,15 @@ def test_args_not_passed_if_no_prereq_names
assert_equal({}, args.to_hash) assert_equal({}, args.to_hash)
assert_equal "bill", args.name assert_equal "bill", args.name
} }
t = task(:t, :name, :rev, :needs => [:pre]) t = task(:t, [:name, :rev] => [:pre])
t.invoke("bill", "1.2") t.invoke("bill", "1.2")
end end


def test_args_not_passed_if_no_arg_names def test_args_not_passed_if_no_arg_names
pre = task(:pre, :rev) { |t, args| pre = task(:pre, :rev) { |t, args|
assert_equal({}, args.to_hash) assert_equal({}, args.to_hash)
} }
t = task(:t, :needs => [:pre]) t = task(:t => [:pre])
t.invoke("bill", "1.2") t.invoke("bill", "1.2")
end end
end end
4 changes: 3 additions & 1 deletion test/lib/top_level_functions_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def setup
super super
@app = Rake.application @app = Rake.application
Rake.application = flexmock("app") Rake.application = flexmock("app")
Rake.application.should_receive(:deprecate).
and_return { |old, new, call| @app.deprecate(old, new, call) }
end end


def teardown def teardown
Expand All @@ -36,7 +38,7 @@ def test_import
Rake.application.should_receive(:add_import).with("x").once.ordered Rake.application.should_receive(:add_import).with("x").once.ordered
Rake.application.should_receive(:add_import).with("y").once.ordered Rake.application.should_receive(:add_import).with("y").once.ordered
Rake.application.should_receive(:add_import).with("z").once.ordered Rake.application.should_receive(:add_import).with("z").once.ordered
import('x', 'y', 'z') Rake.import('x', 'y', 'z')
end end


def test_when_writing def test_when_writing
Expand Down
1 change: 1 addition & 0 deletions test/rake_test_setup.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'flexmock/test_unit' require 'flexmock/test_unit'
require 'test/filecreation' require 'test/filecreation'
require 'test/capture_stdout' require 'test/capture_stdout'
require 'test/test_helper'


module TestMethods module TestMethods
# Shim method for compatibility # Shim method for compatibility
Expand Down
7 changes: 7 additions & 0 deletions test/test_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@


class Test::Unit::TestCase class Test::Unit::TestCase
include Rake::DSL include Rake::DSL

def ignore_deprecations
Rake.application.options.ignore_deprecate = true
yield
ensure
Rake.application.options.ignore_deprecate = false
end
end end

0 comments on commit 3b60390

Please sign in to comment.