Skip to content

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

@kwatch

Description

@kwatch

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions