Skip to content

Allow arbitrary setup code to be inserted into binstub #329

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

Merged
merged 1 commit into from
Aug 16, 2014

Conversation

merhard
Copy link

@merhard merhard commented Jul 14, 2014

This commit is designed to be the first step to solving this issue.

The pull request would allow the spring-commands-rspec gem to be changed to:

# lib/spring/commands/rspec.rb

module Spring
  module Commands
    class RSpec
      def env(*)
        "test"
      end

      def exec_name
        "rspec"
      end

      def gem_name
        "rspec-core"
      end

      def binstub_code
        "RSpec.configuration.start_time = Time.now"
      end
    end

    Spring.register_command "rspec", RSpec.new
    Spring::Commands::Rake.environment_matchers[/^spec($|:)/] = "test"
  end
end

The generated rspec binstub would then be:

#!/usr/bin/env ruby
begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError
end
RSpec.configuration.start_time = Time.now
require 'bundler/setup'
load Gem.bin_path('rspec-core', 'rspec')

@@ -69,6 +69,12 @@ def binstub_name
"bin/#{name}"
end

def binstub_code
if command.respond_to?(:binstub_code)
command.binstub_code.gsub(/[^\n]\z/) { |eos| "#{eos}\n" }
Copy link
Member

Choose a reason for hiding this comment

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

How about command.binstub_code + "\n"? - no worries about an extra newline

Copy link
Member

Choose a reason for hiding this comment

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

Actually, better to handle this when the binstub is written anyway. Introduce a \n there.

Copy link
Author

Choose a reason for hiding this comment

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

So basically revert 0b46807 and only have 3d5653e?

I added the optional newline code because of a failed test (AppTest#test_binstub_upgrade [../test/acceptance/app_test.rb:224]). I'll make the changes and fix the failing test.

@merhard
Copy link
Author

merhard commented Aug 6, 2014

@jeremy - How's this?

@jeremy
Copy link
Member

jeremy commented Aug 7, 2014

@merhard - Cool. Considering this is a shim that needs to run only when the executable is sprung, consider giving binstub_code a more precise name and putting it in the loader code:

begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError
  # command's post-spring shim doesn't load if spring isn't in use
else
  <command's post-spring shim>
end

@merhard
Copy link
Author

merhard commented Aug 7, 2014

@jeremy - I renamed binstub_code to runner_setup_code as it is setup code for spring's rspec runner. I also moved the shim into the loader code.

@jeremy
Copy link
Member

jeremy commented Aug 9, 2014

@merhard Nice. runner_setup_code describes the code you're injecting rather than the disposition of the code in the binstub, though. Consider something like binstub_prelude, indicating that it's code that before the original binstub.

@merhard
Copy link
Author

merhard commented Aug 15, 2014

@jeremy How's this? I can squash the commits if there isn't anything else.

@@ -104,8 +104,14 @@ def generate(fallback = nil)
fallback = "require 'bundler/setup'\n" \
"load Gem.bin_path('#{command.gem_name}', '#{command.exec_name}')\n"
end
if prelude = command.binstub_prelude
formatted_prelude = prelude.chomp.gsub(/^(?!$)/, ' ')
loader = LOADER.sub(/^end$/, "else\n#{formatted_prelude}\n\\0")
Copy link
Member

Choose a reason for hiding this comment

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

What's the story with the trailing NUL byte?

Copy link
Author

Choose a reason for hiding this comment

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

@jeremy It isn't a NUL byte. '\0' or "\\0" in the replacement string of #sub inserts the match from the regexp into the resulting string. I first saw it in thor. It is also in the "Search And Replace" section of this site. Perhaps replacing "\\0" with "end" would be better?

Copy link
Member

Choose a reason for hiding this comment

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

Oh cool! Double backslash. That's cool - but yeah, end would be clearer 😁

@merhard
Copy link
Author

merhard commented Aug 16, 2014

@jeremy changed

@jeremy
Copy link
Member

jeremy commented Aug 16, 2014

@merhard Super! Squash?

@merhard
Copy link
Author

merhard commented Aug 16, 2014

@jeremy squashed

jeremy added a commit that referenced this pull request Aug 16, 2014
Allow arbitrary setup code to be inserted into binstub
@jeremy jeremy merged commit 2480a8e into rails:master Aug 16, 2014
@jeremy
Copy link
Member

jeremy commented Aug 16, 2014

👍

@merhard
Copy link
Author

merhard commented Aug 16, 2014

thanks

@jonleighton
Copy link
Member

Was this change specifically aimed at fixing the rspec issue or was some more general use case envisaged?

@merhard
Copy link
Author

merhard commented Aug 20, 2014

It is designed to be used by any spring-commands-* gem to add code to the binstub. I had the rspec issue in mind when writing this code, but any spring command can use it.

@jonleighton
Copy link
Member

Sure, I realise that it can be used by any command, however I'm dubious about whether we actually need this feature, hence I wanted to see if there were other reasons to add it. (For others: I suggested an alternative fix to the rspec issue at jonleighton/spring-commands-rspec#20 (comment))

jonleighton added a commit that referenced this pull request Nov 22, 2014
This reverts commit 2480a8e, reversing
changes made to 9cd21d2.

Revert "Changelog: binstub_prelude support for commands. #329."

This reverts commit 8a3546d.

Conflicts:
	CHANGELOG.md

Reasoning:

* #329 (comment)
* jonleighton/spring-commands-rspec#20 (comment)
* 2480a8e

Since the thing that this feature was aiming to allow is already
possible via the commands API, I prefer not to add an extra feature to
Spring.
JulianSage added a commit to JulianSage/spring that referenced this pull request Oct 23, 2024
This reverts commit 2480a8eb4e88ad720e3d5a30bc23b53201673d58, reversing
changes made to 9cd21d2249fdc33477dfc99f0e99c5d307108447.

Revert "Changelog: binstub_prelude support for commands. #329."

This reverts commit 8a3546d15892f28f19787d88e4a834e68b91c438.

Conflicts:
	CHANGELOG.md

Reasoning:

* rails/spring#329 (comment)
* jonleighton/spring-commands-rspec#20 (comment)
* rails/spring@2480a8e

Since the thing that this feature was aiming to allow is already
possible via the commands API, I prefer not to add an extra feature to
Spring.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants