Skip to content

Instantly share code, notes, and snippets.

@jdickey
Last active March 25, 2023 04:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdickey/6efd0d77a74f7eeae0304a51234e540a to your computer and use it in GitHub Desktop.
Save jdickey/6efd0d77a74f7eeae0304a51234e540a to your computer and use it in GitHub Desktop.

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.

Pointers?

(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
end # class C11eFlog
C11eFlog.new
end # task :flog
end
# frozen_string_literal: true
# From https://github.com/TheProlog/prolog_minitest_matchers/blob/master/lib/tasks/prolog_flog_task.rb
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
end
# from https://github.com/TheProlog/prolog_minitest_matchers/blob/master/Rakefile
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'
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList['test/**/*_test.rb']
end
RuboCop::RakeTask.new(:rubocop) do |task|
task.patterns = [
'lib/**/*.rb',
'test/**/*.rb'
]
task.formatters = ['simple', 'd']
task.fail_on_error = true
task.options << '--display-cop-names'
end
Reek::Rake::Task.new do |t|
t.config_file = 'config.reek'
t.source_files = 'lib/**/*.rb'
t.reek_opts = '--sort-by smelliness --no-progress -s'
end
FlayTask.new do |t|
t.verbose = true
t.dirs = %w(app lib)
end
FlogTask.new do |t|
t.verbose = true
t.threshold = 200 # default is 200
t.methods_only = true
t.dirs = %w(lib)
end
task(:default).clear
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