Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add a folder to rake stats?

I've added some folders (lib, spec, cells, etc.) to my Rails app and would like to add them to the rake stats list. Is it possible to add new folders?

like image 690
Adam Gotterer Avatar asked Feb 05 '13 01:02

Adam Gotterer


People also ask

Where are rake tasks stored?

How to Write a Rake Task. You can put this code inside a file named Rakefile , or if you're using Rails, you can save this under lib/tasks/apple. rake .

How do I create a rake file?

Simply create a new file and name it as task. rake. Normally we put the rake task in directory lib/tasks. But you can put it anywhere you like.

What is the rake command?

What is rake? Rake is a native tool for Ruby, similar to Unix's “make”. Written by Jim Weirich, It is used to handle administrative commands or tasks, which are stored either in a Rakefile or in a . rake file. One can write their own rake tasks, specific to their application.

What is the difference between rake and Ruby?

rake is a Make-like program implemented in Ruby. rails is a web framework, which also has some rake tasks. This means that you can have a ruby program with rake but without rails, but not the other way around. By itself, rake will be faster because you don't need to load the whole rails application.


3 Answers

I know I'm late to the party, but as I did not find more information, I'm adding my answer into the mix.

I've added a rake-task to my project with content similar to the following:

task :stats => "todolist:statsetup"

namespace :todolist do
  task :statsetup do
    require 'rails/code_statistics'
    ::STATS_DIRECTORIES << ["Policies", "app/policies"]
    ::STATS_DIRECTORIES << ["Services", "app/services"]

    # For test folders not defined in CodeStatistics::TEST_TYPES (ie: spec/)
    ::STATS_DIRECTORIES << ["Services specs", "specs/services"]
    CodeStatistics::TEST_TYPES << "Services specs"
  end
end

This added both folders to my rake stats

like image 193
Erik Avatar answered Oct 04 '22 02:10

Erik


Here is an excellent answer to find where a specific rake task is defined.

With that tip, found that the rake stats task is defined in gems/railties-3.2.11/lib/rails/tasks/statistics.rake file; so it is in the railties gem which is part of the rails repository.

At the very top of the file, the directories to be considered are included in the STATS_DIRECTORIES variable.

Probably best to implement a custom rake task - named my_stats for example - with the same code, and add the new folders that you want to include.

like image 34
Prakash Murthy Avatar answered Oct 04 '22 00:10

Prakash Murthy


I found it useful to insert new stats at a sensible position in the list.

Here's what I'm using (with rails 5.1):

# lib/stats.rake

require "rails/code_statistics"

task stats: :more_stats

task :more_stats do
  %w[Forms Policies Presenters Serializers Services].each_with_index do |type, i|
    STATS_DIRECTORIES.insert i + 5, [type, "app/#{type.downcase}"]
    STATS_DIRECTORIES.insert i * 2 + 13, ["#{type} tests", "test/#{type.downcase}"]
    CodeStatistics::TEST_TYPES << "#{type} tests"
  end
end
like image 37
tee Avatar answered Oct 04 '22 00:10

tee