deprecation_toolkit
deprecation_toolkit copied to clipboard
How to load the rake task for CIRecordHelper?
The README mentions a deprecation_toolkit:record_from_ci_output rake task, but it's not loaded by default (I guess it might be if you didn't use require: false in the Gemfile? But I'd rather not load the gem outside of our tests) and I can't figure out how to require it such that it'd be available to use.
Okay, if I create a deprecation_toolkit.rake file in my Rails app with the following, it works:
begin
require 'deprecation_toolkit'
rescue LoadError
# deprecation_toolkit isn't in the production bundle, don't try to load
end
Although I believe that means it'll load the whole gem whenever the Rake tasks are initialized (in dev/test, at least), which isn't ideal.
Second problem: I don't think shell_command = "cat #{file} | sed -n -e 's/^.* \\[DeprecationToolkit\\] \\(.*\\)/\\1/p' > #{tmp_file.path}" works on macOS, which makes testing this rather difficult on a Mac.
I've rewritten this to use Ruby rather than sed:
# frozen_string_literal: true
require "tempfile"
require "json"
require "active_support/core_ext/hash"
require "rake"
require_relative "../deprecation_toolkit/read_write_helper"
class CIRecorder
include Rake::DSL
include DeprecationToolkit::ReadWriteHelper
def initialize
namespace(:deprecation_toolkit) do
desc("Parse a file generated with the CIOutputHelper and generate deprecations out of it")
task(:record_from_ci_output) do
raw_file = ENV.fetch("FILEPATH")
deprecations = extract_deprecations_output(raw_file) do |file|
parse_file(file)
end
generate_deprecations_file(deprecations)
end
end
end
private
def extract_deprecations_output(file)
tmp_file = Tempfile.new
parsed_lines = File.readlines(file).map do |line|
# Scan returns a nested array so we have to do this.
line.scan(/\[DeprecationToolkit\] (.*)/m).last
end.compact.flatten
tmp_file << parsed_lines.join
# This is necessary for the tmpfile to be readable later.
tmp_file.rewind
yield(tmp_file)
ensure
tmp_file.delete
end
def parse_file(file)
file.each.with_object({}) do |line, hash|
hash.deep_merge!(JSON.parse(line))
end
end
def generate_deprecations_file(deprecations_to_record)
deprecations_to_record.each do |filename, deprecations|
write(Pathname(filename), deprecations)
end
end
end
CIRecorder.new