Skip to content

Instantly share code, notes, and snippets.

What would you like to do?

I have been carrying this Rakefile around between projects, that uses another file which reopens the FlogTask class and adds an attr_accessor to it, which the task definition in the Rakefile then uses.

This works but, as you can readily tell, it's less than ideal. In particular, the Rakefile defines multiple tasks which are the same for each project; the only change is usually the Flog threshold value.

What I want to be able to do is:

  1. Subclass a tool's standard Rake task (e.g., FlogTask). The subclass' #initialize method would then set up our standard settings as is presently done in the Rakefile;
  2. Define namespaced Rake tasks (in, e.g., lib/tasks/custom_flog.rake) that use those subclasses rather than the tool's standard Rake task; reducing boilerplate and possibility of copy/paste errors;
  3. Have those tasks be available in the Rakefile so that the only code in Rakefile would be the default task setup.

I've been reading the docs, and I've spent a couple hours with Google reading through blogs that people have posted, and I still haven't figured out what I need to do.


(Files referenced reproduced below, as taken from a small open-source project of ours.)

EDIT: Thanks to @csmr (aka c-c on Freenode) for helping me think through rewording the goal list.

EDIT 2: Added c11e-flog.rake as an example of what I've tried to do that hasn't worked yet.

namespace :c11e do
desc 'Custom Flog configuration and instantiation'
task :flog do
require 'flog'
require 'flog_task'
class C11eFlog < ::FlogTask
def initialize(threshold = 200)
super :flog, threshold, %w(app lib), nil, true
end # class C11eFlog
end # task :flog
# frozen_string_literal: true
# From
require 'rake/tasklib'
require 'flog'
require 'flog_task'
# Redefinition of standard task's Rake invocation. Because we don't like
# inconsistency in option settings.
class FlogTask < Rake::TaskLib
# Reek bitches that this is a :reek:Attribute (writable). That's the *point*.
attr_accessor :methods_only
# from
require "bundler/gem_tasks"
require "rake/testtask"
require 'rake/tasklib'
require 'flay'
require 'flay_task'
require 'tasks/prolog_flog_task'
require 'reek/rake/task'
require 'rubocop/rake_task' do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList['test/**/*_test.rb']
end do |task|
task.patterns = [
task.formatters = ['simple', 'd']
task.fail_on_error = true
task.options << '--display-cop-names'
end do |t|
t.config_file = 'config.reek'
t.source_files = 'lib/**/*.rb'
t.reek_opts = '--sort-by smelliness --no-progress -s'
end do |t|
t.verbose = true
t.dirs = %w(app lib)
end do |t|
t.verbose = true
t.threshold = 200 # default is 200
t.methods_only = true
t.dirs = %w(lib)
task default: [:test, :rubocop, :flay, :flog, :reek]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment