Skip to content

Commit f4a862c

Browse files
committed
Install bundler where we want it from the beginning
We know where bundler will live, we can put it in the right place from the start.
1 parent 12f8e1b commit f4a862c

File tree

7 files changed

+14
-89
lines changed

7 files changed

+14
-89
lines changed

lib/language_pack.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def self.call(app_path:, cache_path:, gemfile_lock:, bundle_default_without:, en
5050
io: warn_io
5151
)
5252

53-
bundler = Helpers::BundlerWrapper.new.install
53+
bundler = Helpers::BundlerWrapper.new(bundler_path: ruby_version.bundler_directory).install
5454
default_config_vars = Ruby.default_config_vars(metadata: metadata, ruby_version: ruby_version, bundler: bundler, environment_name: environment_name)
5555
Ruby.setup_language_pack_environment(
5656
app_path: app_path.expand_path,
@@ -59,7 +59,6 @@ def self.call(app_path:, cache_path:, gemfile_lock:, bundle_default_without:, en
5959
bundle_default_without: bundle_default_without,
6060
default_config_vars: default_config_vars
6161
)
62-
Ruby.install_bundler_in_app(bundler_src_dir: bundler.bundler_path, app_bundler_dir: ruby_version.bundler_directory)
6362
Ruby.load_bundler_cache(
6463
ruby_version: ruby_version,
6564
new_app: new_app,

lib/language_pack/helpers/bundler_wrapper.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# Example:
1111
#
12-
# bundler = LanguagePack::Helpers::BundlerWrapper.new
12+
# bundler = LanguagePack::Helpers::BundlerWrapper.new(bundler_path: "vendor/bundle/ruby/3.2.0")
1313
# bundler.install
1414
# bundler.version => "1.15.2"
1515
# bundler.dir_name => "bundler-1.15.2"
@@ -22,7 +22,7 @@
2222
# of an isolated dyno, you must call `BundlerWrapper#clean`. To reset the environment
2323
# variable:
2424
#
25-
# bundler = LanguagePack::Helpers::BundlerWrapper.new
25+
# bundler = LanguagePack::Helpers::BundlerWrapper.new(bundler_path: "vendor/bundle/ruby/3.2.0")
2626
# bundler.install
2727
# bundler.clean # <========== IMPORTANT =============
2828
#
@@ -109,12 +109,11 @@ def initialize(version_hash, major_minor)
109109
attr_reader :bundler_path
110110

111111
def initialize(
112-
bundler_path: nil,
112+
bundler_path:,
113113
gemfile_path: Pathname.new("./Gemfile"),
114114
report: HerokuBuildReport::GLOBAL
115115
)
116116
@report = report
117-
@bundler_tmp = Pathname.new(Dir.mktmpdir)
118117
@gemfile_path = gemfile_path
119118
@gemfile_lock_path = Pathname.new("#{@gemfile_path}.lock")
120119

@@ -142,7 +141,7 @@ def initialize(
142141
)
143142
@dir_name = "bundler-#{@version}"
144143

145-
@bundler_path = bundler_path || @bundler_tmp.join(@dir_name)
144+
@bundler_path = bundler_path
146145
@orig_bundle_gemfile = ENV['BUNDLE_GEMFILE']
147146
end
148147

@@ -154,7 +153,6 @@ def install
154153

155154
def clean
156155
ENV['BUNDLE_GEMFILE'] = @orig_bundle_gemfile
157-
@bundler_tmp.rmtree if @bundler_tmp.directory?
158156
end
159157

160158
def has_gem?(name)

lib/language_pack/ruby.rb

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -539,13 +539,6 @@ def self.install_ruby(app_path: , ruby_version: , stack:, arch: , metadata:, io:
539539
io.error message
540540
end
541541

542-
# installs vendored gems into the slug
543-
def self.install_bundler_in_app(bundler_src_dir:, app_bundler_dir:)
544-
FileUtils.mkdir_p(app_bundler_dir)
545-
Dir.chdir(app_bundler_dir) do |dir|
546-
`cp -R #{bundler_src_dir}/. .`
547-
end
548-
end
549542

550543
# default set of binaries to install
551544
# @return [Array] resulting list
@@ -931,74 +924,8 @@ def self.load_bundler_cache(cache: , metadata: , stack:, bundler_cache: , bundle
931924
metadata.write(stack_cache, stack)
932925
end
933926

934-
def self.purge_bundler_cache(bundler_cache: , stack: nil, ruby_version: , bundler:)
935-
bundler_cache.clear(stack)
936-
# need to reinstall language pack gems
937-
install_bundler_in_app(bundler_src_dir: bundler.bundler_path, app_bundler_dir: ruby_version.bundler_directory)
938-
end
939-
940-
# writes ERB based database.yml for Rails. The database.yml uses the DATABASE_URL from the environment during runtime.
941-
def create_database_yml
942-
return false unless File.directory?("config")
943-
return false if bundler.has_gem?('activerecord') && bundler.gem_version('activerecord') >= Gem::Version.new('4.1.0.beta1')
944-
945-
topic("Writing config/database.yml to read from DATABASE_URL")
946-
File.open("config/database.yml", "w") do |file|
947-
file.puts <<~DATABASE_YML
948-
<%
949-
950-
require 'cgi'
951-
require 'uri'
952-
953-
begin
954-
uri = URI.parse(ENV["DATABASE_URL"])
955-
rescue URI::InvalidURIError
956-
raise "Invalid DATABASE_URL"
957-
end
958-
959-
raise "No RACK_ENV or RAILS_ENV found" unless ENV["RAILS_ENV"] || ENV["RACK_ENV"]
960-
961-
def attribute(name, value, force_string = false)
962-
if value
963-
value_string =
964-
if force_string
965-
'"' + value + '"'
966-
else
967-
value
968-
end
969-
"\#{name}: \#{value_string}"
970-
else
971-
""
972-
end
973-
end
974-
975-
adapter = uri.scheme
976-
adapter = "postgresql" if adapter == "postgres"
977-
978-
database = (uri.path || "").split("/")[1]
979-
980-
username = uri.user
981-
password = uri.password
982-
983-
host = uri.host
984-
port = uri.port
985-
986-
params = CGI.parse(uri.query || "")
987-
988-
%>
989-
990-
<%= ENV["RAILS_ENV"] || ENV["RACK_ENV"] %>:
991-
<%= attribute "adapter", adapter %>
992-
<%= attribute "database", database %>
993-
<%= attribute "username", username %>
994-
<%= attribute "password", password, true %>
995-
<%= attribute "host", host %>
996-
<%= attribute "port", port %>
997-
998-
<% params.each do |key, value| %>
999-
<%= key %>: <%= value.first %>
1000-
<% end %>
1001-
DATABASE_YML
1002-
end
927+
def purge_bundler_cache(stack = nil)
928+
@bundler_cache.clear(stack)
929+
bundler.install
1003930
end
1004931
end

spec/hatchet/rails6_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
describe "Rails 6" do
44
it "should detect successfully" do
55
Hatchet::App.new('rails61').in_directory_fork do
6-
bundler = LanguagePack::Helpers::BundlerWrapper.new.install
6+
bundler = LanguagePack::Helpers::BundlerWrapper.new(bundler_path: Dir.mktmpdir)
77
expect(LanguagePack::Rails5.use?(bundler: bundler)).to eq(false)
88
expect(LanguagePack::Rails6.use?(bundler: bundler)).to eq(true)
99
end

spec/hatchet/rails7_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
describe "Rails 7" do
44
it "should detect successfully" do
55
Hatchet::App.new('rails-jsbundling').in_directory_fork do
6-
bundler = LanguagePack::Helpers::BundlerWrapper.new.install
6+
bundler = LanguagePack::Helpers::BundlerWrapper.new(bundler_path: Dir.mktmpdir)
77
expect(LanguagePack::Rails6.use?(bundler: bundler)).to eq(false)
88
expect(LanguagePack::Rails7.use?(bundler: bundler)).to eq(true)
99
end

spec/helpers/bundler_wrapper_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
report = HerokuBuildReport.dev_null
7373

7474
bundler = LanguagePack::Helpers::BundlerWrapper.new(
75+
bundler_path: Dir.mktmpdir,
7576
gemfile_path: gemfile,
7677
report: report
7778
)
@@ -96,7 +97,7 @@
9697
ENV['RUBYOPT'] = ENV['RUBYOPT'].sub('-rbundler/setup', '')
9798
end
9899

99-
@bundler = LanguagePack::Helpers::BundlerWrapper.new
100+
@bundler = LanguagePack::Helpers::BundlerWrapper.new(bundler_path: Dir.mktmpdir)
100101
end
101102

102103
after(:each) do
@@ -117,7 +118,7 @@
117118

118119
expect(tmp_gemfile_lock_path.read).to match("BUNDLED")
119120

120-
wrapper = LanguagePack::Helpers::BundlerWrapper.new(gemfile_path: tmp_gemfile_path )
121+
wrapper = LanguagePack::Helpers::BundlerWrapper.new(bundler_path: Dir.mktmpdir, gemfile_path: tmp_gemfile_path)
121122

122123
expect(wrapper.version).to eq(LanguagePack::Helpers::BundlerWrapper::BLESSED_BUNDLER_VERSIONS["2"])
123124

spec/helpers/fetcher_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
lockfile.write("BUNDLED WITH\n #{version}")
1111

1212
fetcher = LanguagePack::Fetcher.new(LanguagePack::Base::VENDOR_URL)
13-
fetcher.fetch_untar("bundler/#{LanguagePack::Helpers::BundlerWrapper.new.dir_name}.tgz")
13+
fetcher.fetch_untar("bundler/#{LanguagePack::Helpers::BundlerWrapper.new(bundler_path: Dir.mktmpdir).dir_name}.tgz")
1414

1515
expect(run!("ls bin")).to match("bundle")
1616
end

0 commit comments

Comments
 (0)