Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make rake easier to use as a library #211

Merged
merged 5 commits into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions lib/rake/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def initialize
add_loader("rake", DefaultLoader.new)
@tty_output = STDOUT.tty?
@terminal_columns = ENV["RAKE_COLUMNS"].to_i

set_default_options
end

# Run the Rake application. The run method performs the following
Expand All @@ -74,19 +76,19 @@ def initialize
# If you wish to build a custom rake command, you should call
# +init+ on your application. Then define any tasks. Finally,
# call +top_level+ to run your top level tasks.
def run
def run(argv = ARGV)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems it will caused #233 problem

standard_exception_handling do
init
init 'rake', argv
load_rakefile
top_level
end
end

# Initialize the command line parameters and app name.
def init(app_name="rake")
def init(app_name="rake", argv = ARGV)
standard_exception_handling do
@name = app_name
args = handle_options
args = handle_options argv
collect_command_line_tasks(args)
end
end
Expand Down Expand Up @@ -618,9 +620,8 @@ def select_trace_output(options, trace_option, value) # :nodoc:
# Read and handle the command line options. Returns the command line
# arguments that we didn't understand, which should (in theory) be just
# task names and env vars.
def handle_options # :nodoc:
options.rakelib = ["rakelib"]
options.trace_output = $stderr
def handle_options(argv) # :nodoc:
set_default_options

OptionParser.new do |opts|
opts.banner = "#{Rake.application.name} [-f rakefile] {options} targets..."
Expand All @@ -634,7 +635,7 @@ def handle_options # :nodoc:

standard_rake_options.each { |args| opts.on(*args) }
opts.environment("RAKEOPT")
end.parse(ARGV)
end.parse(argv)
end

# Similar to the regular Ruby +require+ command, but will check
Expand Down Expand Up @@ -782,5 +783,28 @@ def rakefile_location(backtrace=caller) # :nodoc:
backtrace.find { |str| str =~ re } || ""
end

def set_default_options
options.always_multitask = false
options.backtrace = false
options.build_all = false
options.dryrun = false
options.ignore_deprecate = false
options.ignore_system = false
options.job_stats = false
options.load_system = false
options.nosearch = false
options.rakelib = %w[rakelib]
options.show_all_tasks = false
options.show_prereqs = false
options.show_task_pattern = nil
options.show_tasks = nil
options.silent = false
options.suppress_backtrace_pattern = nil
options.thread_pool_size = Rake.suggested_thread_count
options.trace = false
options.trace_output = $stderr
options.trace_rules = false
end

end
end
28 changes: 28 additions & 0 deletions lib/rake/rake_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ def add_rakelib(*files)
application.options.rakelib ||= []
application.options.rakelib.concat(files)
end

# Make +block_application+ the default rake application inside a block so
# you can load rakefiles into a different application.
#
# This is useful when you want to run rake tasks inside a library without
# running rake in a sub-shell.
#
# Example:
#
# Dir.chdir 'other/directory'
#
# other_rake = Rake.with_application do |rake|
# rake.load_rakefile
# end
#
# puts other_rake.tasks

def with_application(block_application = Rake::Application.new)
orig_application = Rake.application

Rake.application = block_application

yield block_application

block_application
ensure
Rake.application = orig_application
end
end

end
Loading