0

Gemfile contains the list of all the gems used in a rails app, how can i get a list of all the gems along with their dependency gems ?

Gemfile.lock can give a list of all the gems along with their dependency gems for each gem. However, the list contains duplicate entries for the gems if a gem is a dependency of more than one gem. Here is a sample from my Gemfile.lock where activesupport (= 4.2.5) is a dependency of two different gems actionview (4.2.5) and activejob (4.2.5).

actionview (4.2.5)
  activesupport (= 4.2.5)
  builder (~> 3.1)
  erubis (~> 2.7.0)
  rails-dom-testing (~> 1.0, >= 1.0.5)
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
activejob (4.2.5)
  activesupport (= 4.2.5)
  globalid (>= 0.3.0)

The list in question can be extracted from filtering out these duplicate entries from the file. But then that's a lot of work if there are say 50 gems in my app. I am looking if someone has figured out a better solution.

Also is there a way to figure out which of these gems from the list are actually used in the application and which are just there bloating the application ?

My goal is to cleanup an old app that has some unused gems.

sa77
  • 3,563
  • 3
  • 24
  • 37
  • Possible duplicate of [Figuring Out Which Gems Rails App Does Not Use](http://stackoverflow.com/questions/9793360/figuring-out-which-gems-rails-app-does-not-use) – Brad Werth Jan 08 '16 at 17:34
  • 1
    This is two questions. The first is look in the Gemfile.lock, the second is a dupe. Actually the first is surely a dupe, too, but I don't have time to chase it down... – Brad Werth Jan 08 '16 at 17:35
  • @BradWerth: regarding first question..`Gemfile.lock` lists out dependency for each gem..so say my `actionpack` has dependency to `activesupport` and my another gem `activejob` also has dependency `activesupport`..i can filter out a long list of duplicate entries from `Gemfile.lock` until i get a list..but was looking for a better solution – sa77 Jan 08 '16 at 17:47
  • and as for the second question..there is a single answer on the question you pointed out and that was from 2012..and not even an accepted answer on that..so i was just looking maybe there is some tool or idea someone might have used to navigate the application codebase to figure out unused gems. – sa77 Jan 08 '16 at 17:52
  • http://stackoverflow.com/questions/14333963/how-to-find-unused-gems-in-my-gemfile – Brad Werth Jan 08 '16 at 21:55
  • http://stackoverflow.com/questions/19167563/how-to-find-unused-gems-and-cleanup-gemfile – Brad Werth Jan 08 '16 at 21:55

1 Answers1

2

For starters, you can run bundle clean --force to remove unwanted gems from your system that aren't being used in your Gemfile. However, this won't remove gems from your app that you have included but aren't using.

Since this is your app that you've been building, you should be able to go though and remove the gems manually. This is why TDD is important, if you can remove a gem and all of your tests pass, you can safely assume that you no longer need that gem.

I don't really know if I would trust automated tools anyway. It's likely a much safer bet to do this manually. The process may be tedious, but you'll thank yourself in the end.

Colto
  • 612
  • 4
  • 14