lib/spoom/context/bundle.rb in spoom-1.2.1 vs lib/spoom/context/bundle.rb in spoom-1.2.2

- old
+ new

@@ -8,16 +8,22 @@ extend T::Sig extend T::Helpers requires_ancestor { Context } - # Read the `contents` of the Gemfile in this context directory + # Read the contents of the Gemfile in this context directory sig { returns(T.nilable(String)) } def read_gemfile read("Gemfile") end + # Read the contents of the Gemfile.lock in this context directory + sig { returns(T.nilable(String)) } + def read_gemfile_lock + read("Gemfile.lock") + end + # Set the `contents` of the Gemfile in this context directory sig { params(contents: String, append: T::Boolean).void } def write_gemfile!(contents, append: false) write!("Gemfile", contents, append: append) end @@ -39,20 +45,23 @@ sig { params(command: String, version: T.nilable(String), capture_err: T::Boolean).returns(ExecResult) } def bundle_exec(command, version: nil, capture_err: true) bundle("exec #{command}", version: version, capture_err: capture_err) end + sig { returns(T::Hash[String, Bundler::LazySpecification]) } + def gemfile_lock_specs + return {} unless file?("Gemfile.lock") + + parser = Bundler::LockfileParser.new(read_gemfile_lock) + parser.specs.map { |spec| [spec.name, spec] }.to_h + end + # Get `gem` version from the `Gemfile.lock` content # # Returns `nil` if `gem` cannot be found in the Gemfile. sig { params(gem: String).returns(T.nilable(String)) } def gem_version_from_gemfile_lock(gem) - return nil unless file?("Gemfile.lock") - - content = read("Gemfile.lock").match(/^ #{gem} \(.*(\d+\.\d+\.\d+).*\)/) - return nil unless content - - content[1] + gemfile_lock_specs[gem]&.version&.to_s end end end end