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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/spring/client/binstub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}\nend")
else
loader = LOADER
end

File.write(command.binstub, "#!/usr/bin/env ruby\n#{LOADER}#{fallback}")
File.write(command.binstub, "#!/usr/bin/env ruby\n#{loader}#{fallback}")
command.binstub.chmod 0755
end

Expand Down
6 changes: 6 additions & 0 deletions lib/spring/command_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def binstub_name
"bin/#{name}"
end

def binstub_prelude
if command.respond_to?(:binstub_prelude)
command.binstub_prelude
end
end

def exec
if binstub.exist?
binstub.to_s
Expand Down
19 changes: 19 additions & 0 deletions test/acceptance/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,25 @@ def exec_name
assert_success "bin/rake -T", stdout: "rake db:migrate"
end

test "binstub with prelude code" do
prelude = "Prelude code line 1\nPrelude code line 2\n"

File.write(app.spring_config, <<-CODE)
class PreludeCode
def binstub_prelude
"#{prelude}"
end
end

Spring.register_command "prelude", PreludeCode.new
CODE

assert_success "bin/spring binstub prelude"
prelude.each_line do |line|
assert app.path("bin/prelude").read.include?(line), "'#{line}' not found in bin/prelude"
end
end

test "binstub when spring is uninstalled" do
app.run! "gem uninstall --ignore-dependencies spring"
File.write(app.gemfile, app.gemfile.read.gsub(/gem 'spring.*/, ""))
Expand Down