Sha256: 1f95008fe3fa6011c4109f28f6e8bef79c444d78b041832a94e61a5c81ed671f

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module Kril
  # Extracts schemas from avro generated java files.
  module SchemaExtractor
    # Parse schemas from avro generated java files and
    # load them into the schema repository.
    #
    # source_dir - root directory of java files [String]
    # output_dir - schema repository [String]
    # returns    - [nil]
    def self.extract(source_dir:, output_dir:)
      find_java_files(source_dir) do |file|
        schema = parse_avro_java_class(file)
        write_avsc(schema, output_dir) if schema
      end
      nil
    end

    module_function

    def find_java_files(root_dir)
      old_dir = Dir.pwd
      Dir.chdir(root_dir)
      java_files = File.join('**', '*.java')
      Dir.glob(java_files) do |file|
        yield File.new(file)
      end
    ensure
      Dir.chdir(old_dir)
    end

    def write_avsc(contents, directory)
      FileUtils.mkdir_p(directory) unless File.directory?(directory)
      path = File.join(directory, "#{contents['name']}.avsc")
      File.open(path, 'w') do |file|
        file.write(JSON.pretty_generate(contents))
      end
    end

    def dejavafy(java_string)
      java_string.split('","').join.gsub(/\\?\\"/, '"')
    end

    def parse_avro_java_class(file)
      result = file.each_line do |line|
        extraction = line[/SCHEMA.*parse\("(.*)"\);/, 1]
        break JSON.parse(dejavafy(extraction)) if extraction
      end
      result.is_a?(File) ? nil : result
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kril-0.2.1 lib/kril/schema_extractor.rb
kril-0.2.0 lib/kril/schema_extractor.rb
kril-0.1.5 lib/kril/schema_extractor.rb