Skip to content

Commit bfdf41f

Browse files
authored
Convert component generator to use RubyFileWriter (#277)
1 parent 8ee5f6d commit bfdf41f

File tree

8 files changed

+24
-155
lines changed

8 files changed

+24
-155
lines changed

lib/hanami/cli/commands/app/generate/command.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ def generator_class
3939
# @api private
4040
def call(name:, slice: nil, **)
4141
if slice
42+
base_path = fs.join("slices", inflector.underscore(slice))
43+
raise MissingSliceError.new(slice) unless fs.exist?(base_path)
44+
4245
generator.call(
4346
key: name,
4447
namespace: slice,
45-
base_path: fs.join("slices", inflector.underscore(slice))
48+
base_path: base_path,
4649
)
4750
else
4851
generator.call(

lib/hanami/cli/commands/app/generate/component.rb

+4-19
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,28 @@
33
require "dry/inflector"
44
require "dry/files"
55
require "shellwords"
6+
67
module Hanami
78
module CLI
89
module Commands
910
module App
1011
module Generate
1112
# @api private
1213
# @since 2.2.0
13-
class Component < App::Command
14+
class Component < Command
1415
argument :name, required: true, desc: "Component name"
15-
option :slice, required: false, desc: "Slice name"
1616

1717
example [
1818
%(isbn_decoder (MyApp::IsbnDecoder)),
1919
%(recommenders.fiction (MyApp::Recommenders::Fiction)),
2020
%(isbn_decoder --slice=admin (Admin::IsbnDecoder)),
2121
%(Exporters::Complete::CSV (MyApp::Exporters::Complete::CSV)),
2222
]
23-
attr_reader :generator
24-
private :generator
2523

26-
# @api private
2724
# @since 2.2.0
28-
def initialize(
29-
fs:, inflector:,
30-
generator: Generators::App::Component.new(fs: fs, inflector: inflector),
31-
**opts
32-
)
33-
@generator = generator
34-
super(fs: fs, inflector: inflector, **opts)
35-
end
36-
3725
# @api private
38-
# @since 2.2.0
39-
def call(name:, slice: nil, **)
40-
slice = inflector.underscore(Shellwords.shellescape(slice)) if slice
41-
42-
generator.call(app.namespace, name, slice)
26+
def generator_class
27+
Generators::App::Component
4328
end
4429
end
4530
end

lib/hanami/cli/generators/app/component.rb

+13-33
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,29 @@ module App
1212
class Component
1313
# @api private
1414
# @since 2.2.0
15-
def initialize(fs:, inflector:)
15+
def initialize(fs:, inflector:, out: $stdout)
1616
@fs = fs
1717
@inflector = inflector
18+
@out = out
1819
end
1920

2021
# @api private
2122
# @since 2.2.0
22-
def call(app, key, slice)
23-
context = ComponentContext.new(inflector, app, slice, key)
24-
25-
if slice
26-
generate_for_slice(context, slice)
27-
else
28-
generate_for_app(context)
29-
end
23+
def call(key:, namespace:, base_path:)
24+
RubyFileWriter.new(
25+
fs: fs,
26+
inflector: inflector,
27+
).call(
28+
namespace: namespace,
29+
key: inflector.underscore(key),
30+
base_path: base_path,
31+
relative_parent_class: nil,
32+
)
3033
end
3134

3235
private
3336

34-
attr_reader :fs
35-
36-
attr_reader :inflector
37-
38-
def generate_for_slice(context, slice)
39-
slice_directory = fs.join("slices", slice)
40-
raise MissingSliceError.new(slice) unless fs.directory?(slice_directory)
41-
42-
fs.mkdir(directory = fs.join(slice_directory, context.namespaces))
43-
fs.write(fs.join(directory, "#{context.underscored_name}.rb"), t("slice_component.erb", context))
44-
end
45-
46-
def generate_for_app(context)
47-
fs.mkdir(directory = fs.join("app", context.namespaces))
48-
fs.write(fs.join(directory, "#{context.underscored_name}.rb"), t("component.erb", context))
49-
end
50-
51-
def template(path, context)
52-
ERB.new(
53-
File.read(__dir__ + "/component/#{path}")
54-
).result(context.ctx)
55-
end
56-
57-
alias_method :t, :template
37+
attr_reader :fs, :inflector, :out
5838
end
5939
end
6040
end

lib/hanami/cli/generators/app/component/component.erb

-8
This file was deleted.

lib/hanami/cli/generators/app/component/slice_component.erb

-8
This file was deleted.

lib/hanami/cli/generators/app/component_context.rb

-82
This file was deleted.

lib/hanami/cli/generators/app/ruby_file_writer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def class_definition(class_name:, local_namespaces:)
128128
.compact
129129
.prepend(container_module)
130130

131-
parent_class = [container_module, relative_parent_class].join("::")
131+
parent_class = [container_module, relative_parent_class].join("::") if relative_parent_class
132132

133133
RubyFileGenerator.class(
134134
normalize(class_name),

spec/unit/hanami/cli/commands/app/generate/component_spec.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
require "ostruct"
55

66
RSpec.describe Hanami::CLI::Commands::App::Generate::Component, :app do
7-
subject { described_class.new(fs: fs, inflector: inflector, generator: generator) }
7+
subject { described_class.new(fs: fs, inflector: inflector, out: out) }
88

99
let(:out) { StringIO.new }
1010
let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) }
1111
let(:inflector) { Dry::Inflector.new }
12-
let(:generator) { Hanami::CLI::Generators::App::Component.new(fs: fs, inflector: inflector) }
1312
let(:app) { Hanami.app.namespace }
1413
let(:underscored_app) { inflector.underscore(app) }
1514
let(:dir) { underscored_app }
@@ -119,7 +118,7 @@ class WelcomeEmail
119118
end
120119
end
121120

122-
context "with constantized name for component given" do
121+
context "with namespaced constant name for component given" do
123122
it "generates the component" do
124123
subject.call(name: "Operations::SendWelcomeEmail")
125124

0 commit comments

Comments
 (0)