views:

183

answers:

5

I am now responsible for a Rails application that was built in a very quick-and-dirty fashion. It has many view files (html templates) that are not used. It also has many css files that are not used.

What is the best way to determine which files are no longer needed so they can be deleted?

I'm looking for a generic solution and not a Rails-specific solution, but a Ruby/Rails-only solution will definitely be welcomed.

Just to get things started, one idea I had (which I'm not particularly fond of) for determining which views were used is as follows:

  1. Write a script that will insert a line of code into the top of every view file. The line of code will append the name of the view to some sort of log.
  2. Recursively wget the entire site
  3. Go through the log and remove duplicates - then we have a list of views that are used
  4. Delete views that are not listed in the log
+4  A: 

If you are on a unix-like system:

Make sure the partition with the pages are mounted with access time (atime) enabled. On linux, mounts with relatime or noatime will not help you.)

Pick the current time.

Run the webserver for a few days.

Identify files whose atime is older than the previously picked time. They are not being used.

I am on a Unix (Ubuntu), and this sounds like a reasonable approach. Thanks Juan. I've leave the question open for a bit longer before I try it out.
Joel
be careful with this. some files may not be touched depending on the execution path. i've worked on legacy projects (not web related) where some routines hadn't ever been invoked in years - and when they were, they crashed because they had never been tested and in fact weren't finished!
frankodwyer
+2  A: 

You can use the webserver logs to enumerate all the client-facing resources that are served up to your script.

Depending on the OS, one could also monitor (with a filter) what files are loaded as the pages are exercised. On windows there's FileMon, for BSD/OSX there's fs_usage, and Linux has dnotify and inotify.

Tautologistics
+4  A: 

As with any refactoring, start by writing tests, once you have those, you can either delete one file at the time and undo if the tests fail or use rcov to see what's being used.

krusty.ar
+4  A: 

Krusty.ar has it right and the best approach.

Personally I would write up some integration tests that check all of the functionality from a high level. Then use with rcov and you will get some very readable out put that will tell you what files have 0% usage.

Then you can start deleting, or start writing unit/functional tests for more safety.

A: 

Your best bet is to become so fluent at testing that you can rely 100% on your own tests. In other words, if you can remove a file, and the tests are still passing, you should be able to assume that this file is not needed by anything, because you should have tests for everything in your app.

August Lilleaas