Skip to content

Commit

Permalink
Merge pull request #211 from ruby/drbrain/rake-as-library
Browse files Browse the repository at this point in the history
Make rake easier to use as a library
  • Loading branch information
drbrain authored Sep 19, 2017
2 parents cff33d7 + 4f9c156 commit 32dcaa6
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 95 deletions.
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)
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

0 comments on commit 32dcaa6

Please sign in to comment.