How to find unused gems in my Gemfile, so that I can cleanup references which are no longer used.
Pretty much everyone agrees you should check that into git. Including your Gemfile. lock in version control is standard practice if you are writing an application.
You can run just bundle or bundle install to install gems based on your Gemfile. That will remove the instance of mygem from your Gemfile. lock file.
run the command bundle install in your shell, once you have your Gemfile created. This command will look your Gemfile and install the relevant Gems on the indicated versions. The Gemfiles are installed because in your Gemfile you are pointing out the source where the gems can be downloaded from.
A Gemfile is a file we created which is used for describing gem dependencies for Ruby programs. The Gemfile is located in the root of the project directory.
Use linux' file access time to see what's actually being used.
This requires:
gems installed on a partition mounted with the atime (strictatime on ubuntu 12.04) option:
sudo mount -o remount,strictatime /
complete test coverage (i.e. we'll be relying on test runs to update file access times)
Note the time and run your tests. Then from your gemdir, do:
ls --time-style long-iso -ltud1 $PWD/*/lib/** | grep "21:44" | sed s/.*gems.// | sed s/.lib.*// | sort -u
Change the 21:44 to whatever time you ran the tests at.
Run your tests and then:
gem stale
Which does the following:
The stale command lists the latest access time for all the files in your installed gems.
You can use this command to discover gems and gem versions you are no longer using.
Any gem should be considered for removal if all tests pass in its absence.
Assuming you have good test coverage - particularly high-level functional tests - you could write a script to selectively remove one gem at a time. ie run all your tests N times, where N is the number of gems in your Gemfile and each test has one missing gem. That will help weed out gems not pulling their weight.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With