From bfdf41fd8dceac94941a74a57bb5225c386c132c Mon Sep 17 00:00:00 2001 From: Sean Collins Date: Wed, 27 Nov 2024 14:41:21 -0500 Subject: [PATCH] Convert component generator to use RubyFileWriter (#277) --- .../cli/commands/app/generate/command.rb | 5 +- .../cli/commands/app/generate/component.rb | 23 +----- lib/hanami/cli/generators/app/component.rb | 46 +++-------- .../generators/app/component/component.erb | 8 -- .../app/component/slice_component.erb | 8 -- .../cli/generators/app/component_context.rb | 82 ------------------- .../cli/generators/app/ruby_file_writer.rb | 2 +- .../commands/app/generate/component_spec.rb | 5 +- 8 files changed, 24 insertions(+), 155 deletions(-) delete mode 100644 lib/hanami/cli/generators/app/component/component.erb delete mode 100644 lib/hanami/cli/generators/app/component/slice_component.erb delete mode 100644 lib/hanami/cli/generators/app/component_context.rb diff --git a/lib/hanami/cli/commands/app/generate/command.rb b/lib/hanami/cli/commands/app/generate/command.rb index 66175cb7..873de988 100644 --- a/lib/hanami/cli/commands/app/generate/command.rb +++ b/lib/hanami/cli/commands/app/generate/command.rb @@ -39,10 +39,13 @@ def generator_class # @api private def call(name:, slice: nil, **) if slice + base_path = fs.join("slices", inflector.underscore(slice)) + raise MissingSliceError.new(slice) unless fs.exist?(base_path) + generator.call( key: name, namespace: slice, - base_path: fs.join("slices", inflector.underscore(slice)) + base_path: base_path, ) else generator.call( diff --git a/lib/hanami/cli/commands/app/generate/component.rb b/lib/hanami/cli/commands/app/generate/component.rb index b8d33e68..91528b05 100644 --- a/lib/hanami/cli/commands/app/generate/component.rb +++ b/lib/hanami/cli/commands/app/generate/component.rb @@ -3,6 +3,7 @@ require "dry/inflector" require "dry/files" require "shellwords" + module Hanami module CLI module Commands @@ -10,9 +11,8 @@ module App module Generate # @api private # @since 2.2.0 - class Component < App::Command + class Component < Command argument :name, required: true, desc: "Component name" - option :slice, required: false, desc: "Slice name" example [ %(isbn_decoder (MyApp::IsbnDecoder)), @@ -20,26 +20,11 @@ class Component < App::Command %(isbn_decoder --slice=admin (Admin::IsbnDecoder)), %(Exporters::Complete::CSV (MyApp::Exporters::Complete::CSV)), ] - attr_reader :generator - private :generator - # @api private # @since 2.2.0 - def initialize( - fs:, inflector:, - generator: Generators::App::Component.new(fs: fs, inflector: inflector), - **opts - ) - @generator = generator - super(fs: fs, inflector: inflector, **opts) - end - # @api private - # @since 2.2.0 - def call(name:, slice: nil, **) - slice = inflector.underscore(Shellwords.shellescape(slice)) if slice - - generator.call(app.namespace, name, slice) + def generator_class + Generators::App::Component end end end diff --git a/lib/hanami/cli/generators/app/component.rb b/lib/hanami/cli/generators/app/component.rb index 0d1143a4..4e5aef75 100644 --- a/lib/hanami/cli/generators/app/component.rb +++ b/lib/hanami/cli/generators/app/component.rb @@ -12,49 +12,29 @@ module App class Component # @api private # @since 2.2.0 - def initialize(fs:, inflector:) + def initialize(fs:, inflector:, out: $stdout) @fs = fs @inflector = inflector + @out = out end # @api private # @since 2.2.0 - def call(app, key, slice) - context = ComponentContext.new(inflector, app, slice, key) - - if slice - generate_for_slice(context, slice) - else - generate_for_app(context) - end + def call(key:, namespace:, base_path:) + RubyFileWriter.new( + fs: fs, + inflector: inflector, + ).call( + namespace: namespace, + key: inflector.underscore(key), + base_path: base_path, + relative_parent_class: nil, + ) end private - attr_reader :fs - - attr_reader :inflector - - def generate_for_slice(context, slice) - slice_directory = fs.join("slices", slice) - raise MissingSliceError.new(slice) unless fs.directory?(slice_directory) - - fs.mkdir(directory = fs.join(slice_directory, context.namespaces)) - fs.write(fs.join(directory, "#{context.underscored_name}.rb"), t("slice_component.erb", context)) - end - - def generate_for_app(context) - fs.mkdir(directory = fs.join("app", context.namespaces)) - fs.write(fs.join(directory, "#{context.underscored_name}.rb"), t("component.erb", context)) - end - - def template(path, context) - ERB.new( - File.read(__dir__ + "/component/#{path}") - ).result(context.ctx) - end - - alias_method :t, :template + attr_reader :fs, :inflector, :out end end end diff --git a/lib/hanami/cli/generators/app/component/component.erb b/lib/hanami/cli/generators/app/component/component.erb deleted file mode 100644 index 7f2b5c48..00000000 --- a/lib/hanami/cli/generators/app/component/component.erb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -module <%= camelized_app_name %> -<%= module_namespace_declaration %> -<%= module_namespace_offset %>class <%= camelized_name %> -<%= module_namespace_offset %>end -<%= module_namespace_end %> -end diff --git a/lib/hanami/cli/generators/app/component/slice_component.erb b/lib/hanami/cli/generators/app/component/slice_component.erb deleted file mode 100644 index 6a29b380..00000000 --- a/lib/hanami/cli/generators/app/component/slice_component.erb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -module <%= camelized_slice_name %> -<%= module_namespace_declaration %> -<%= module_namespace_offset %>class <%= camelized_name %> -<%= module_namespace_offset %>end -<%= module_namespace_end %> -end diff --git a/lib/hanami/cli/generators/app/component_context.rb b/lib/hanami/cli/generators/app/component_context.rb deleted file mode 100644 index 9e8882b7..00000000 --- a/lib/hanami/cli/generators/app/component_context.rb +++ /dev/null @@ -1,82 +0,0 @@ -# frozen_string_literal: true - -require_relative "../constants" - -module Hanami - module CLI - module Generators - module App - class ComponentContext < SliceContext - # @api private - # @since 2.2.0 - attr_reader :key - - # @api private - # @since 2.2.0 - def initialize(inflector, app, slice, key) - @key = key - super(inflector, app, slice, nil) - end - - # @api private - # @since 2.2.0 - def namespaces - @namespaces ||= key.split(MATCHER_PATTERN)[..-2].map { inflector.underscore(_1) } - end - - # @api private - # @since 2.2.0 - def name - @name ||= key.split(MATCHER_PATTERN)[-1] - end - - # @api private - # @since 2.2.0 - def camelized_namespace - namespaces.map { inflector.camelize(_1) }.join(NAMESPACE_SEPARATOR) - end - - # @api private - # @since 2.2.0 - def camelized_name - inflector.camelize(name) - end - - # @api private - # @since 2.2.0 - def underscored_namespace - namespaces.map { inflector.underscore(_1) } - end - - # @api private - # @since 2.2.0 - def underscored_name - inflector.underscore(name) - end - - # @api private - # @since 2.2.0 - def module_namespace_declaration - namespaces.each_with_index.map { |token, i| - "#{OFFSET}#{INDENTATION * i}module #{inflector.camelize(token)}" - }.join($/) - end - - # @api private - # @since 2.2.0 - def module_namespace_end - namespaces.each_with_index.map { |_, i| - "#{OFFSET}#{INDENTATION * i}end" - }.reverse.join($/) - end - - # @api private - # @since 2.2.0 - def module_namespace_offset - "#{OFFSET}#{INDENTATION * namespaces.count}" - end - end - end - end - end -end diff --git a/lib/hanami/cli/generators/app/ruby_file_writer.rb b/lib/hanami/cli/generators/app/ruby_file_writer.rb index 203de554..26585aca 100644 --- a/lib/hanami/cli/generators/app/ruby_file_writer.rb +++ b/lib/hanami/cli/generators/app/ruby_file_writer.rb @@ -128,7 +128,7 @@ def class_definition(class_name:, local_namespaces:) .compact .prepend(container_module) - parent_class = [container_module, relative_parent_class].join("::") + parent_class = [container_module, relative_parent_class].join("::") if relative_parent_class RubyFileGenerator.class( normalize(class_name), diff --git a/spec/unit/hanami/cli/commands/app/generate/component_spec.rb b/spec/unit/hanami/cli/commands/app/generate/component_spec.rb index 54a336fa..a7e47d53 100644 --- a/spec/unit/hanami/cli/commands/app/generate/component_spec.rb +++ b/spec/unit/hanami/cli/commands/app/generate/component_spec.rb @@ -4,12 +4,11 @@ require "ostruct" RSpec.describe Hanami::CLI::Commands::App::Generate::Component, :app do - subject { described_class.new(fs: fs, inflector: inflector, generator: generator) } + subject { described_class.new(fs: fs, inflector: inflector, out: out) } let(:out) { StringIO.new } let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } let(:inflector) { Dry::Inflector.new } - let(:generator) { Hanami::CLI::Generators::App::Component.new(fs: fs, inflector: inflector) } let(:app) { Hanami.app.namespace } let(:underscored_app) { inflector.underscore(app) } let(:dir) { underscored_app } @@ -119,7 +118,7 @@ class WelcomeEmail end end - context "with constantized name for component given" do + context "with namespaced constant name for component given" do it "generates the component" do subject.call(name: "Operations::SendWelcomeEmail")