Skip to content

Commit

Permalink
Merge branch 'main' into prevent_file_overwriting
Browse files Browse the repository at this point in the history
  • Loading branch information
timriley authored Dec 8, 2024
2 parents 3d68320 + bfdf41f commit 5eb81b9
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 155 deletions.
5 changes: 4 additions & 1 deletion lib/hanami/cli/commands/app/generate/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
23 changes: 4 additions & 19 deletions lib/hanami/cli/commands/app/generate/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,28 @@
require "dry/inflector"
require "dry/files"
require "shellwords"

module Hanami
module CLI
module Commands
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)),
%(recommenders.fiction (MyApp::Recommenders::Fiction)),
%(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
Expand Down
46 changes: 13 additions & 33 deletions lib/hanami/cli/generators/app/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.create(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.create(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
Expand Down
8 changes: 0 additions & 8 deletions lib/hanami/cli/generators/app/component/component.erb

This file was deleted.

8 changes: 0 additions & 8 deletions lib/hanami/cli/generators/app/component/slice_component.erb

This file was deleted.

82 changes: 0 additions & 82 deletions lib/hanami/cli/generators/app/component_context.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/hanami/cli/generators/app/ruby_file_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,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),
Expand Down
5 changes: 2 additions & 3 deletions spec/unit/hanami/cli/commands/app/generate/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -167,7 +166,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")

Expand Down

0 comments on commit 5eb81b9

Please sign in to comment.