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?
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 .
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 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.
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.
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
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.
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
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