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

Conversation

drbrain
Copy link
Member

@drbrain drbrain commented Jun 29, 2017

I want to be able to load a set of rake tasks from within another application. Doing this presently is difficult, and looks something like this:

Dir.chdir 'other/dir'

rake = Rake::Application.new
Rake.application = rake

rake.options.trace = true
rake.options.backtrace = true
rake.options.rakelib = %w[rakelib]

rake.load_rakefile

puts rake.tasks

Figuring out which options you need to set is hard (and not documented), figuring out you need to set Rake.application is not documented, and the process of getting your Rakefile loaded is not documented.

This PR replaces the above with a much simpler option:

Dir.chdir 'other/dir'

rake = Rake.with_application do |rake|
  rake.load_rakefile
end

puts rake.tasks

Figuring out which options is now hidden from the user.

There are a few other API niceties in here like being able to create a new Rake application using a separate set of command line arguments from ARGV (so you don't have to overwrite ARGV).

Some tests that use capture_io got a little more confusing because the output IO was set to real-$stderr before capture_io changed $stderr to a StringIO.

Currently it is impossible to start rake from within a library.  By
setting the default options explicitly we won't have to call
handle_options after replacing ARGV.
This allows users to pass in an explicit ARGV for rake instead of
changing their application's ARGV.

While the previous commit allows users to create a Rake application
explicitly by calling parts of Rake::Application#init directly, some
users will not want to dig through the source to set up a Rake
application.

Swapping ARGV is confusing and error-prone, so this allows them to
specify a set of Rake options directly.
These are no longer necessary
This allows users to use Rake as a library more easily.  This allows
loading multiple rakefiles from separate sources into separate Rake
applications without task name collisions.
This allows Rake.with_application to work without users having to know
to set the default options manually.
@hsbt hsbt self-requested a review June 29, 2017 12:35
Copy link
Member

@hsbt hsbt left a comment

Choose a reason for hiding this comment

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

It seems useful for me too.

@drbrain drbrain merged commit 32dcaa6 into master Sep 19, 2017
@drbrain drbrain deleted the drbrain/rake-as-library branch September 19, 2017 02:29
@@ -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

This was referenced Mar 22, 2018
Hamms added a commit to code-dot-org/code-dot-org that referenced this pull request Mar 22, 2023
Specifically to pick up ruby/rake#211, which is required for Rails 6.1 support; without it, we get `undefined method \`with_application' for Rake:Module (NoMethodError)` when trying to run any rails script.

Note that we specifically implement this by updating the `scss_lint` gem to pick up the change "Relax gem dependency constraints to allow rake 12.x"

- https://github.com/sds/scss-lint/blob/master/CHANGELOG.md#0510
- https://github.com/ruby/rake/releases/tag/v12.2.0
Hamms added a commit to code-dot-org/code-dot-org that referenced this pull request Mar 22, 2023
Specifically to pick up ruby/rake#211, which is required for Rails 6.1 support; without it, we get `undefined method ``with_application' for Rake:Module (NoMethodError)` when trying to run any rails script.

Specifically, I implemented this by running `bundle update rake scss_lint`, to also pick up the `scss_lint` change "Relax gem dependency constraints to allow rake 12.x"

- https://github.com/sds/scss-lint/blob/master/CHANGELOG.md#0510
- https://github.com/ruby/rake/releases/tag/v12.2.0

Tested locally that this resolves for me the problems with running rake tasks on Rails 6.1

Relying on existing tests to verify that this does not otherwise result in any change to functionality.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants