Module: AvroGen::Upgrader

Defined in:
lib/avro_gen/upgrader.rb

Overview

Rewrites references in checked-in generated schema-class files from the old Deimos constants to their AvroGen equivalents, so users can migrate off the deprecated const_missing fallback in one shot.

Constant Summary collapse

REPLACEMENTS =
{
  'Deimos::SchemaClass::Record' => 'AvroGen::SchemaClass::Record',
  'Deimos::SchemaClass::Enum' => 'AvroGen::SchemaClass::Enum',
  'Deimos::SchemaClass::Base' => 'AvroGen::SchemaClass::Base',
  'Deimos::Utils::SchemaClass' => 'AvroGen::SchemaClass',
  # The autogenerated-by header that older Deimos-generated files carry.
  'autogenerated by Deimos' => 'autogenerated by AvroGen'
}.freeze

Class Method Summary collapse

Class Method Details

.rewrite(contents) ⇒ String

Parameters:

  • contents (String)

Returns:

  • (String)


38
39
40
41
42
# File 'lib/avro_gen/upgrader.rb', line 38

def rewrite(contents)
  REPLACEMENTS.reduce(contents) do |text, (from, to)|
    text.gsub(from, to)
  end
end

.run(path: nil) ⇒ Array<String>

Returns the list of files that were rewritten.

Parameters:

  • path (String) (defaults to: nil)

    directory containing generated classes; defaults to config

Returns:

  • (Array<String>)

    the list of files that were rewritten



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/avro_gen/upgrader.rb', line 22

def run(path: nil)
  path ||= AvroGen.config.generated_class_path
  changed = []
  Dir["#{path}/**/*.rb"].each do |file|
    original = File.read(file)
    updated = rewrite(original)
    next if updated == original

    File.write(file, updated)
    changed << file
  end
  changed
end