Rake

Rake is a Ruby program that builds other Ruby programs.  Each time you execute rake, it knows how to build those programs by reading a file called Rakefile which has a set of tasks.  Those tasks allows us to do some project needs in a very easy and efficient way.

When you generate a rails project you automatically get a Rakefile and it is located in the root of your project.

You can see all rake tasks and their descriptions by running a simple command in your main directory:

rake –tasks

And this should be shown:

 rake backgroundrb:remove # Remove backgroundrb from your rails ...
 rake backgroundrb:restart # Restart backgroundrb server (default...
 rake backgroundrb:setup # Setup backgroundrb in your rails app...
 rake backgroundrb:start # Start backgroundrb server (default v...
 rake backgroundrb:stop # Stop backgroundrb server (default va...
 rake db:abort_if_pending_migrations # Raises an error if there are pending...
 rake db:charset # Retrieves the charset for the curren...
 rake db:collation # Retrieves the collation for the curr...
 rake db:create # Create the database defined in confi...
 rake db:create:all # Create all the local databases defin...
 rake db:drop # Drops the database for the current R...
 rake db:drop:all # Drops all the local databases define...
 rake db:fixtures:identify # Search for a fixture given a LABEL o...
 rake db:fixtures:load # Load fixtures into the current envir...
 rake db:migrate # Migrate the database through scripts...
 rake db:migrate:redo # Rollbacks the database one migration...
 rake db:migrate:reset # Resets your database using your migr...
 rake db:reset # Drops and recreates the database fro...
 rake db:rollback # Rolls the schema back to the previou...
 rake db:schema:dump # Create a db/schema.rb file that can ...
 rake db:schema:load # Load a schema.rb file into the database
 rake db:sessions:clear # Clear the sessions table
 rake db:sessions:create # Creates a sessions migration for use...
 rake db:structure:dump # Dump the database structure to a SQL...
 rake db:test:clone # Recreate the test database from the ...
 rake db:test:clone_structure # Recreate the test databases from the...
 rake db:test:prepare # Prepare the test database and load t...
 rake db:test:purge # Empty the test database
 rake db:version # Retrieves the current schema version...
 rake doc:app # Build the app HTML Files
 rake doc:clobber_app # Remove rdoc products
 rake doc:clobber_plugins # Remove plugin documentation
 rake doc:clobber_rails # Remove rdoc products
 rake doc:plugins # Generate documentation for all insta...
 rake doc:rails # Build the rails HTML Files
 rake doc:reapp # Force a rebuild of the RDOC files
 rake doc:rerails # Force a rebuild of the RDOC files
 rake log:clear # Truncates all *.log files in log/ to...
 rake notes # Enumerate all annotations
 rake notes:fixme # Enumerate all FIXME annotations
 rake notes:optimize # Enumerate all OPTIMIZE annotations
 rake notes:todo # Enumerate all TODO annotations
 rake rails:freeze:edge # Lock to latest Edge Rails or a speci...
 rake rails:freeze:gems # Lock this application to the current...
 rake rails:unfreeze # Unlock this application from freeze ...
 rake rails:update # Update both configs, scripts and pub...
 rake rails:update:configs # Update config/boot.rb from your curr...
 rake rails:update:javascripts # Update your javascripts from your cu...
 rake rails:update:scripts # Add new scripts to the application s...
 rake remove_simple_captcha_files # Remove unuseful captcha images and s...
 rake routes # Print out all defined routes in matc...
 rake secret # Generate a crytographically secure s...
 rake solr:destroy_index # Remove Solr index
 rake solr:start # Starts Solr.
 rake solr:stop # Stops Solr.
 rake stats # Report code statistics (KLOCs, etc) ...
 rake test # Test all units and functionals
 rake test:functionals # Run tests for functionalsdb:test:pre...
 rake test:integration # Run tests for integrationdb:test:pre...
 rake test:plugins # Run tests for pluginsenvironment / R...
 rake test:recent # Run tests for recentdb:test:prepare ...
 rake test:uncommitted # Run tests for uncommitteddb:test:pre...
 rake test:units # Run tests for unitsdb:test:prepare /...
 rake tmp:cache:clear # Clears all files and directories in ...
 rake tmp:clear # Clear session, cache, and socket fil...
 rake tmp:create # Creates tmp directories for sessions...
 rake tmp:pids:clear # Clears all files in tmp/pids
 rake tmp:sessions:clear # Clears all files in tmp/sessions
 rake tmp:sockets:clear # Clears all files in tmp/sockets
 rake uml:schema # Generate an XMI db/schema.xml file d...

Those rake tasks will be used many times when developing your rails application.
For example, there is an interesting task rake stats which generates detailed statistics about your application code and provides a dashboard of information.  This is what I get:

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |  6080 |  5241 |      54 |     369 |   6 |    12 |
| Helpers              |   552 |   501 |       0 |      25 |   0 |    18 |
| Models               |  3855 |  3274 |     157 |     326 |   2 |     8 |
| Libraries            |  3068 |  2806 |      18 |      71 |   3 |    37 |
| APIs                 |     9 |     9 |       1 |       0 |   0 |     0 |
| Components           |     0 |     0 |       0 |       0 |   0 |     0 |
| Integration tests    |     0 |     0 |       0 |       0 |   0 |     0 |
| Functional tests     |  1261 |   942 |      97 |     200 |   2 |     2 |
| Unit tests           |  1246 |   884 |     117 |     129 |   1 |     4 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                | 16071 | 13657 |     444 |    1120 |   2 |    10 |
+----------------------+-------+-------+---------+---------+-----+-------+
Code LOC: 11831     Test LOC: 1826     Code to Test Ratio: 1:0.2

There are many other rake commands very useful for rails development.

Thanks for your visit to the blog. You can follow me on Twitter:

Leave a comment