Last active
January 29, 2020 20:26
-
-
Save zdennis/4f0f22f273c671b1346260f33d2a70e7 to your computer and use it in GitHub Desktop.
utility script to find which gems are dependent on a given gem provided a filter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'shellwords' | |
options = {} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: #{__FILE__} [options]" | |
opts.on("-g", "--gem=GEM") do |gem| | |
options[:gem] = gem | |
end | |
opts.on("-f", "--filter=FILTER") do |filter| | |
options[:filter] = Regexp.new(filter) | |
end | |
opts.on("-h", "--help", "Print this message") do | |
puts opts | |
exit | |
end | |
end.parse! | |
fail "Must provide a gem name. See --help for more info." unless options[:gem] | |
fail "Must provide a gem filter. See --help for more info." unless options[:filter] | |
puts "Searching for gems that match the filter #{options[:filter]} for a dependency on #{options[:gem]}" | |
puts | |
command = <<~SHELL | |
gem search -r "#{options[:filter]}" | cut -f1 -d" " | |
SHELL | |
output = `#{command}` | |
gems = output.split(/\n/) | |
dependent_gems = gems.select do |gem| | |
printf "%-40s", gem | |
if `gem dependency -r #{gem}` =~ /#{Regexp.new(options[:gem])}/m | |
puts "\e[32mdepends\e[0m" | |
true | |
else | |
puts "no dependency" | |
end | |
end | |
puts | |
puts "Done. #{dependent_gems.length} depend on #{options[:gem]} matching filter #{options[:filter]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment