Skip to content

Commit 2480a8e

Browse files
committed
Merge pull request #329 from merhard/master
Allow arbitrary setup code to be inserted into binstub
2 parents 9cd21d2 + 3606811 commit 2480a8e

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

lib/spring/client/binstub.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,14 @@ def generate(fallback = nil)
104104
fallback = "require 'bundler/setup'\n" \
105105
"load Gem.bin_path('#{command.gem_name}', '#{command.exec_name}')\n"
106106
end
107+
if prelude = command.binstub_prelude
108+
formatted_prelude = prelude.chomp.gsub(/^(?!$)/, ' ')
109+
loader = LOADER.sub(/^end$/, "else\n#{formatted_prelude}\nend")
110+
else
111+
loader = LOADER
112+
end
107113

108-
File.write(command.binstub, "#!/usr/bin/env ruby\n#{LOADER}#{fallback}")
114+
File.write(command.binstub, "#!/usr/bin/env ruby\n#{loader}#{fallback}")
109115
command.binstub.chmod 0755
110116
end
111117

lib/spring/command_wrapper.rb

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ def binstub_name
6969
"bin/#{name}"
7070
end
7171

72+
def binstub_prelude
73+
if command.respond_to?(:binstub_prelude)
74+
command.binstub_prelude
75+
end
76+
end
77+
7278
def exec
7379
if binstub.exist?
7480
binstub.to_s

test/acceptance/app_test.rb

+19
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,25 @@ def exec_name
188188
assert_success "bin/rake -T", stdout: "rake db:migrate"
189189
end
190190

191+
test "binstub with prelude code" do
192+
prelude = "Prelude code line 1\nPrelude code line 2\n"
193+
194+
File.write(app.spring_config, <<-CODE)
195+
class PreludeCode
196+
def binstub_prelude
197+
"#{prelude}"
198+
end
199+
end
200+
201+
Spring.register_command "prelude", PreludeCode.new
202+
CODE
203+
204+
assert_success "bin/spring binstub prelude"
205+
prelude.each_line do |line|
206+
assert app.path("bin/prelude").read.include?(line), "'#{line}' not found in bin/prelude"
207+
end
208+
end
209+
191210
test "binstub when spring is uninstalled" do
192211
app.run! "gem uninstall --ignore-dependencies spring"
193212
File.write(app.gemfile, app.gemfile.read.gsub(/gem 'spring.*/, ""))

0 commit comments

Comments
 (0)