Skip to content

Commit 27c3f70

Browse files
committed
Quick-fix the pubgrub initial install race
Fixes #14, though not very elegantly.
1 parent 06ee2e9 commit 27c3f70

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

lib/gel/environment.rb

+35-24
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,39 @@ def self.lockfile_name(gemfile = self.gemfile&.filename)
104104
"Gemfile.lock"
105105
end
106106

107+
def self.with_root_store
108+
app_store = Gel::Environment.store
109+
110+
base_store = app_store
111+
base_store = base_store.inner if base_store.is_a?(Gel::LockedStore)
112+
113+
# Work around the fact Gel::Environment is a singleton: we really
114+
# want to treat the environment we're running in separately from
115+
# the application's environment we're working on. But for now, we
116+
# can just cheat and swap them.
117+
@store = base_store
118+
119+
yield base_store
120+
ensure
121+
@store = app_store
122+
end
123+
124+
def self.auto_install_pub_grub!
125+
with_root_store do |base_store|
126+
base_store.monitor.synchronize do
127+
if base_store.each("pub_grub").none?
128+
require_relative "work_pool"
129+
130+
Gel::WorkPool.new(2) do |work_pool|
131+
catalog = Gel::Catalog.new("https://rubygems.org", work_pool: work_pool)
132+
133+
install_gem([catalog], "pub_grub", [">= 0.5.0"])
134+
end
135+
end
136+
end
137+
end
138+
end
139+
107140
def self.lock(store: store(), output: nil, gemfile: Gel::Environment.load_gemfile, lockfile: Gel::Environment.lockfile_name, catalog_options: {}, preference_strategy: nil)
108141
output = nil if $DEBUG
109142

@@ -165,31 +198,9 @@ def self.lock(store: store(), output: nil, gemfile: Gel::Environment.load_gemfil
165198
end
166199
end
167200

168-
begin
169-
app_store = Gel::Environment.store
170-
171-
base_store = app_store
172-
base_store = base_store.inner if base_store.is_a?(Gel::LockedStore)
173-
174-
# Work around the fact Gel::Environment is a singleton: we really
175-
# want to treat the environment we're running in separately from
176-
# the application's environment we're working on. But for now, we
177-
# can just cheat and swap them.
178-
@store = base_store
179-
180-
if base_store.each("pub_grub").none?
181-
require_relative "work_pool"
182-
183-
Gel::WorkPool.new(2) do |work_pool|
184-
catalog = Gel::Catalog.new("https://rubygems.org", work_pool: work_pool)
185-
186-
install_gem([catalog], "pub_grub", [">= 0.5.0"])
187-
end
188-
end
189-
201+
auto_install_pub_grub!
202+
with_root_store do
190203
gem "pub_grub"
191-
ensure
192-
@store = app_store
193204
end
194205
require_relative "pub_grub/source"
195206

lib/gel/multi_store.rb

+3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ class Gel::MultiStore
77
VERSION = "#{RbConfig::CONFIG["ruby_version"]}"
88

99
attr_reader :root
10+
attr_reader :monitor
1011

1112
def initialize(root, stores)
1213
@root = File.realpath(File.expand_path(root)) if root
1314
@stores = stores
15+
16+
@monitor = Monitor.new
1417
end
1518

1619
def stub_set

lib/gel/store.rb

+3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
class Gel::Store
66
attr_reader :root
7+
attr_reader :monitor
78

89
def initialize(root)
910
@root = File.realpath(File.expand_path(root))
1011
@primary_db = Gel::DB.new(root, "store")
1112
@lib_db = Gel::DB.new(root, "libs")
1213
@rlib_db = Gel::DB::File.new(root, "meta")
14+
15+
@monitor = Monitor.new
1316
end
1417

1518
def stub_set

0 commit comments

Comments
 (0)