|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails/generators/named_base" |
| 4 | +require "generators/kafka_consumer/concerns/configuration" |
| 5 | + |
| 6 | +module KafkaConsumer |
| 7 | + module Generators |
| 8 | + class RegexpConsumerGenerator < Rails::Generators::NamedBase |
| 9 | + include Concerns::Configuration |
| 10 | + |
| 11 | + source_root File.expand_path("templates", __dir__) |
| 12 | + |
| 13 | + desc "Injects a regexp-based consumer group entry into config/kafka_consumer.yml" |
| 14 | + |
| 15 | + argument :consumer_klass, type: :string, |
| 16 | + desc: "Consumer class name (e.g. Ecom::EventStreaming::Kafka::RegularEventsInboxConsumer)" |
| 17 | + |
| 18 | + argument :topic_regexp, type: :string, |
| 19 | + desc: "Topic regexp value (e.g. 'seeker\\.paas-stand\\.event-streaming\\.order(\\.\\d+)?\\$')" |
| 20 | + |
| 21 | + class_option :env_prefix, type: :string, required: true, |
| 22 | + desc: "ENV variable prefix for CONSUMER_GROUP_NAME and TOPIC_REGEXP (e.g. ES_ORDER)" |
| 23 | + |
| 24 | + class_option :init_attrs, type: :hash, default: {}, |
| 25 | + banner: "key:value key:value", |
| 26 | + desc: "Consumer init_attrs as key:value pairs (e.g. inbox_item:Foo skip_on_error:true)" |
| 27 | + |
| 28 | + class_option :deserializer_klass, type: :string, default: nil, |
| 29 | + desc: "Deserializer class (omit to skip deserializer section)" |
| 30 | + |
| 31 | + def inject_consumer_group |
| 32 | + check_config_file! |
| 33 | + insert_into_file CONFIG_PATH, consumer_group_entry, after: "consumer_groups:\n" |
| 34 | + end |
| 35 | + |
| 36 | + private |
| 37 | + |
| 38 | + # Override to resolve path relative to destination_root (works in both |
| 39 | + # app context and generator tests with a tmpdir destination_root). |
| 40 | + def check_config_file! |
| 41 | + return if File.exist?(File.join(destination_root, CONFIG_PATH)) |
| 42 | + |
| 43 | + generator_name = "kafka_consumer:install" |
| 44 | + answer = ask("The file #{CONFIG_PATH} does not appear to exist. Would you like to generate it? [Yn]") |
| 45 | + if (answer.presence || "y").casecmp("y").zero? |
| 46 | + generate generator_name |
| 47 | + else |
| 48 | + raise Rails::Generators::Error, |
| 49 | + "Please generate #{CONFIG_PATH} by running `bin/rails g #{generator_name}` or add this file manually." |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + def group_key |
| 54 | + file_name |
| 55 | + end |
| 56 | + |
| 57 | + def env_prefix |
| 58 | + options[:env_prefix] |
| 59 | + end |
| 60 | + |
| 61 | + def init_attrs |
| 62 | + options[:init_attrs] |
| 63 | + end |
| 64 | + |
| 65 | + def deserializer_klass |
| 66 | + options[:deserializer_klass] |
| 67 | + end |
| 68 | + |
| 69 | + def format_attr_value(value) |
| 70 | + (value == "true" || value == "false") ? value : "\"#{value}\"" |
| 71 | + end |
| 72 | + |
| 73 | + def consumer_group_entry |
| 74 | + template_path = File.join(self.class.source_root, "consumer_group.yml.erb") |
| 75 | + ERB.new(File.read(template_path), trim_mode: "%-").result(binding) |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | +end |
0 commit comments