Skip to content

Instantly share code, notes, and snippets.

@kevinlinxc
Last active August 15, 2022 22:56
Show Gist options
  • Save kevinlinxc/b73929a2ca5facb2d6e1f3710c3e194a to your computer and use it in GitHub Desktop.
Save kevinlinxc/b73929a2ca5facb2d6e1f3710c3e194a to your computer and use it in GitHub Desktop.
Determine all transitive dependencies of a gem using Bundler
def bundle_lock_deps(gem_name)
# reads Gemfile.lock resolved dependencies and return them in an Array of [name, version] pairs
# requires bundler
delete_gemfiles
write_gem_to_gemfile(gem_name)
raise "bundle lock errored" unless system("bundle lock > /dev/null")
# read Gemfile.lock and return an Array of gem names
parser = Bundler::LockfileParser.new(Bundler.read_file('Gemfile.lock'))
output = []
parser.specs.each do |item|
item = item.to_s
name = item.split(' ')[0]
version = item.split(' ')[1].tr!('()', '')
output << [name, version]
end
delete_gemfiles
output
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment