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

$RAKE_PRELOAD to load custom Ruby file before starting application #595

Open
kwatch opened this issue Oct 11, 2024 · 0 comments
Open

$RAKE_PRELOAD to load custom Ruby file before starting application #595

kwatch opened this issue Oct 11, 2024 · 0 comments

Comments

@kwatch
Copy link

kwatch commented Oct 11, 2024

Rake application loads Rakefile AFTER Rake.application.run() started.
Therefore it is not able to customize Rake.application.run() (or other methods defined in Rake.application) in Rakefile.

For example, I want to change Rake::Application#handle_options() to use parser.order(argv) instead of parser.parse(argv) in order not to parse options after task names.
(I'm developing rake extension to allow command-line options for each task, and parser.order() is necessary for this purpose.)

Here is my challenge (Rakefile):

## Rakefile

module Rake
  Application.class_eval do
    def handle_options(argv)
      set_default_options
      OptionParser.new do |opts|
        opts.banner = "#{Rake.application.name} [-f rakefile] {options} targets..."
        opts.separator ""
        opts.separator "Options are ..."

        opts.on_tail("-h", "--help", "-H", "Display this help message.") do
          puts opts
          exit
        end

        standard_rake_options.each { |args| opts.on(*args) }
        opts.environment("RAKEOPT")
      #end.parse(argv)    # !! original !!
      end.order(argv)     # !! customize !!
    end
  end
end

But the above Rakefile doesn't work intendedly because Rake application loads Rakefile after Rake.application.run() started.

Therefore, I hope Rake to provide a certain method to load custom ruby file before Rake.application.run() started.

One solution is to load a ruby file specified by the $RAKE_PRELOAD environment variable in rake command.

rake command:

#!/usr/bin/env ruby

require "rake"

preload = ENV['RAKE_PRELOAD']         # !!!
if preload && ! preload.empty?        # !!!
  require preload                     # !!!
end                                   # !!!

Rake.application.run

I'd like the dev team to consider this idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant