Last active
November 1, 2017 17:33
-
-
Save seanlinsley/77ec2bfc02c91b2c44252ca5bed3c62d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# written for https://github.com/rails/rails/pull/30919 | |
# 1. find all gems that depend on a particular gem | |
# 2. download them all | |
# 3. search through their source! | |
auth = 'Authorization:YOUR_API_KEY' | |
gems = JSON.parse(`curl -H '#{auth}' https://rubygems.org/api/v1/gems/activerecord/reverse_dependencies.json`); nil | |
gems.size | |
# => 4479 | |
dir = Dir.mktmpdir | |
gems.each_with_index do |name, index| | |
next if Dir["#{dir}/#{name}/source/*"].any? | |
print "\r" | |
print "#{index + 1} / #{gems.size}" | |
sleep 1 | |
download_link = JSON.parse(`curl -s -H '#{auth}' https://rubygems.org/api/v1/gems/#{name}.json`)['gem_uri'] | |
`curl -s -o #{dir}/#{name}.gem #{download_link}` | |
`mkdir -p #{dir}/#{name}/source` | |
`tar zxf #{dir}/#{name}.gem -C #{dir}/#{name}` | |
`tar zxf #{dir}/#{name}/data.tar.gz -C #{dir}/#{name}/source` | |
end; nil | |
gems.map do |name| | |
# ** is recursive in Ruby, but not in Bash | |
files = Dir["#{dir}/#{name}/source/**/*.rb"].select do |file| | |
begin | |
File.read(file) =~ /(before|after|around)_save .*(on:|:on) / | |
rescue => e | |
puts "failed to read #{file}" | |
end | |
end | |
files.map!{ |file| file.sub "#{dir}/#{name}/source/", '' } | |
[name, files] if files.any? | |
end.compact.to_h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment