diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cec7b3..75a16b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # avrolution +## v0.8.0 + +- Added the ability to register all schemas found under `Avrolution.root` with the task + `rake avro:register_all_schemas`. + ## v0.7.2 - Fix a bug related to Ruby 3.0 keyword arguments in AvroSchemaRegistry::Client#register_without_lookup. diff --git a/README.md b/README.md index 979bcce..0f62c6a 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,24 @@ require 'avroluation/rake/register_schemas_task' Avrolution::Rake::RegisterSchemasTask.define ``` +### Avro Register All Schemas Rake Task + +This rake task allows you to register all schemas discovered under `Avrolution.root`. + +Similarly to the task `avro:register_schemas`, it will register them against the configured +registry. Additionally, this task will be auto included for Rails applications. + +```bash +rake avro:register_all_schemas +``` + +For non-Rails projects, tasks can be defined as: + +```ruby +require 'avroluation/rake/register_all_schemas_task' +Avrolution::Rake::RegisterAllSchemasTask.define +``` + ### Avro Add Compatibility Break Rake Task There is a rake task add an entry to the `Avrolution.compatibility_breaks_file`. diff --git a/avrolution.gemspec b/avrolution.gemspec index 84c8c35..2e02691 100644 --- a/avrolution.gemspec +++ b/avrolution.gemspec @@ -25,8 +25,8 @@ Gem::Specification.new do |spec| end spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } - spec.bindir = 'bin' - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.bindir = 'exe' + spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] spec.required_ruby_version = '>= 2.6' diff --git a/lib/avrolution.rb b/lib/avrolution.rb index 85c1b35..abad75c 100644 --- a/lib/avrolution.rb +++ b/lib/avrolution.rb @@ -17,6 +17,7 @@ def initialize(*) require 'avrolution/compatibility_break' require 'avrolution/compatibility_breaks_file' require 'avrolution/compatibility_check' +require 'avrolution/discover_schemas' require 'avrolution/register_schemas' require 'avrolution/railtie' if defined?(Rails) diff --git a/lib/avrolution/compatibility_check.rb b/lib/avrolution/compatibility_check.rb index a9ac28c..a05b04f 100644 --- a/lib/avrolution/compatibility_check.rb +++ b/lib/avrolution/compatibility_check.rb @@ -39,10 +39,7 @@ def success? private def check_schemas(path) - vendor_bundle_path = File.join(path, 'vendor/bundle/') - Dir[File.join(path, '**/*.avsc')].reject do |file| - file.start_with?(vendor_bundle_path) - end.each do |schema_file| + Avrolution::DiscoverSchemas.discover(path).each do |schema_file| check_schema_compatibility(schema_file) end end diff --git a/lib/avrolution/discover_schemas.rb b/lib/avrolution/discover_schemas.rb new file mode 100644 index 0000000..68028d5 --- /dev/null +++ b/lib/avrolution/discover_schemas.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module Avrolution + class DiscoverSchemas + def self.discover(path) + vendor_bundle_path = File.join(path, 'vendor/bundle/') + Dir[File.join(path, '**/*.avsc')].reject do |file| + file.start_with?(vendor_bundle_path) + end + end + end +end diff --git a/lib/avrolution/rake/rails_avrolution.rake b/lib/avrolution/rake/rails_avrolution.rake index fbcceb3..d427a0d 100644 --- a/lib/avrolution/rake/rails_avrolution.rake +++ b/lib/avrolution/rake/rails_avrolution.rake @@ -2,8 +2,10 @@ require 'avrolution/rake/check_compatibility_task' require 'avrolution/rake/add_compatibility_break_task' +require 'avrolution/rake/register_all_schemas_task' require 'avrolution/rake/register_schemas_task' Avrolution::Rake::AddCompatibilityBreakTask.define(dependencies: [:environment]) Avrolution::Rake::CheckCompatibilityTask.define(dependencies: [:environment]) +Avrolution::Rake::RegisterAllSchemasTask.define(dependencies: [:environment]) Avrolution::Rake::RegisterSchemasTask.define(dependencies: [:environment]) diff --git a/lib/avrolution/rake/register_all_schemas_task.rb b/lib/avrolution/rake/register_all_schemas_task.rb new file mode 100644 index 0000000..ca71fc7 --- /dev/null +++ b/lib/avrolution/rake/register_all_schemas_task.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'avrolution/rake/base_task' + +module Avrolution + module Rake + class RegisterAllSchemasTask < BaseTask + + def initialize(**) + super + @name ||= :register_all_schemas + @task_desc ||= 'Register all discovered Avro JSON schemas (using Avrolution.root)' + end + + private + + def perform + schemas = Avrolution::DiscoverSchemas.discover(Avrolution.root) + + if schemas.blank? + puts 'could not find any schemas' + exit(1) + else + Avrolution::RegisterSchemas.call(schemas) + end + end + end + end +end diff --git a/lib/avrolution/version.rb b/lib/avrolution/version.rb index 4bd458c..d10b504 100644 --- a/lib/avrolution/version.rb +++ b/lib/avrolution/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Avrolution - VERSION = '0.7.2' + VERSION = '0.8.0' end diff --git a/spec/avrolution/tasks_spec.rb b/spec/avrolution/tasks_spec.rb index a2feff9..586768c 100644 --- a/spec/avrolution/tasks_spec.rb +++ b/spec/avrolution/tasks_spec.rb @@ -28,6 +28,23 @@ end end + describe "register_all_schemas" do + let(:task_name) { 'avro:register_all_schemas' } + let(:schema_files) { Avrolution::DiscoverSchemas.discover(Dir.pwd) } + let(:register_schemas) { instance_spy(Avrolution::RegisterSchemas) } + + before do + allow(Avrolution::RegisterSchemas).to receive(:new).and_return(register_schemas) + allow(Avrolution::DiscoverSchemas).to receive(:discover).and_return(schema_files) + end + + it "dispatches to a RegisterSchemas instance" do + task.invoke + + expect(register_schemas).to have_received(:call) + end + end + describe "check_compatibility" do let(:task_name) { 'avro:check_compatibility' } let(:compatibility_check) { Avrolution::CompatibilityCheck.new } diff --git a/spec/fixtures/sample_gem/avro/schema/com/foo/person.avsc b/spec/fixtures/sample_gem/avro/schema/com/foo/person.avsc new file mode 100644 index 0000000..e015b03 --- /dev/null +++ b/spec/fixtures/sample_gem/avro/schema/com/foo/person.avsc @@ -0,0 +1,9 @@ +{ + "type" : "record", + "namespace" : "foo", + "name" : "person", + "fields" : [ + { "name" : "Name" , "type" : "string" }, + { "name" : "Age" , "type" : "int" } + ] +} diff --git a/spec/fixtures/sample_gem/avro/schema/com/foo/pet.avsc b/spec/fixtures/sample_gem/avro/schema/com/foo/pet.avsc new file mode 100644 index 0000000..ffa2a06 --- /dev/null +++ b/spec/fixtures/sample_gem/avro/schema/com/foo/pet.avsc @@ -0,0 +1,9 @@ +{ + "type" : "record", + "namespace" : "foo", + "name" : "pet", + "fields" : [ + { "name" : "Name" , "type" : "string" }, + { "name" : "Age" , "type" : "int" } + ] +}