ruby / ruby/rake

`$RAKE_PRELOAD` to load custom Ruby file before starting application

Open
#595 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.5k
Forks
650
Avg merge
6m
Merged PRs (30d)
3

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the rake command entry point and follow how Rake.application.run loads the Rakefile and invokes handle_options. Determine how a preload file could be loaded before run while preserving the proposed RAKE_PRELOAD behavior; done means the mechanism is agreed, implemented, and covered by tests for customizing Rake.application.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
build-system, cli, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.