Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce support for hanami generate part #18

Merged
merged 6 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ end
gem "dry-files", require: false, git: "https://github.com/dry-rb/dry-files.git", branch: "main"
gem "dry-logger", require: false, git: "https://github.com/dry-rb/dry-logger.git", branch: "main"
gem "hanami-utils", require: false, git: "https://github.com/hanami/utils.git", branch: "main"
gem "hanami-cli", require: false, git: "https://github.com/hanami/cli.git", branch: "main"
gem "hanami-cli", require: false, git: "https://github.com/hanami/cli.git", branch: "feature/view-part-generator"
gem "hanami", require: false, git: "https://github.com/hanami/hanami.git", branch: "main"
1 change: 1 addition & 0 deletions lib/hanami/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def self.gem_loader
Hanami::CLI.after "install", Commands::Install
Hanami::CLI.after "generate slice", Commands::Generate::Slice
Hanami::CLI.after "generate action", Commands::Generate::Action
Hanami::CLI.after "generate part", Commands::Generate::Part
end
end
end
15 changes: 15 additions & 0 deletions lib/hanami/rspec/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ def call(options, **)
generator.call(app.namespace, slice, controller, action)
end
end

# @since 2.1.0
# @api private
class Part < Hanami::CLI::Commands::App::Command
# @since 2.1.0
# @api private
def call(options, **)
# FIXME: dry-cli kwargs aren't correctly forwarded in Ruby 3
slice = inflector.underscore(Shellwords.shellescape(options[:slice])) if options[:slice]
name = inflector.underscore(Shellwords.shellescape(options[:name]))

generator = Generators::Part.new(fs: fs, inflector: inflector)
generator.call(app.namespace, slice, name)
end
end
end
end
end
Expand Down
60 changes: 60 additions & 0 deletions lib/hanami/rspec/generators/part.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require "erb"

module Hanami
module RSpec
module Generators
# @since 2.1.0
# @api private
class Part
# @since 2.1.0
# @api private
def initialize(fs:, inflector:)
@fs = fs
@inflector = inflector
end

# @since 2.1.0
# @api private
def call(app, slice, name, context: Hanami::CLI::Generators::App::PartContext.new(inflector, app, slice, name))
if slice
fs.write(
"spec/slices/#{slice}/views/parts/#{context.underscored_name}_spec.rb",
t("part_slice_spec.erb", context)
)
else
fs.write(
"spec/views/parts/#{context.underscored_name}_spec.rb",
t("part_spec.erb", context)
)
end
end

private

# @since 2.1.0
# @api private
attr_reader :fs

# @since 2.1.0
# @api private
attr_reader :inflector

# @since 2.1.0
# @api private
def template(path, context)
require "erb"

ERB.new(
File.read(__dir__ + "/part/#{path}")
).result(context.ctx)
end

# @since 2.1.0
# @api private
alias_method :t, :template
end
end
end
end
7 changes: 7 additions & 0 deletions lib/hanami/rspec/generators/part/part_slice_spec.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

RSpec.describe <%= camelized_slice_name %>::Views::Parts::<%= camelized_name %> do
it "works" do
expect(subject).to be_kind_of(described_class)
end
end
7 changes: 7 additions & 0 deletions lib/hanami/rspec/generators/part/part_spec.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

RSpec.describe <%= camelized_app_name %>::Views::Parts::<%= camelized_name %> do
it "works" do
expect(subject).to be_kind_of(described_class)
end
end
96 changes: 96 additions & 0 deletions spec/unit/hanami/rspec/commands/generate/part_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# frozen_string_literal: true

require "hanami"
require "securerandom"

RSpec.describe Hanami::RSpec::Commands::Generate::Part do
describe "#call" do
subject { described_class.new(fs: fs, inflector: inflector) }

let(:fs) { Dry::Files.new }
let(:inflector) { Dry::Inflector.new }

let(:app_name) { "Bookshelf" }

let(:part_name) { "client" }

context "app" do
it "generates spec file" do
within_application_directory do
subject.call({name: part_name})

part_spec = <<~EXPECTED
# frozen_string_literal: true

RSpec.describe #{app_name}::Views::Parts::Client do
it "works" do
expect(subject).to be_kind_of(described_class)
end
end
EXPECTED
expect(fs.read("spec/views/parts/client_spec.rb")).to eq(part_spec)
end
end
end

context "slice" do
let(:slice) { "main" }
let(:slice_name) { "Main" }

it "generates spec file" do
within_application_directory do
subject.call({slice: slice, name: part_name})

part_spec = <<~EXPECTED
# frozen_string_literal: true

RSpec.describe #{slice_name}::Views::Parts::Client do
it "works" do
expect(subject).to be_kind_of(described_class)
end
end
EXPECTED
expect(fs.read("spec/slices/#{slice}/views/parts/client_spec.rb")).to eq(part_spec)
end
end
end
end

private

def within_application_directory(app: app_name)
dir = fs.join(TMP, SecureRandom.uuid, app)

fs.mkdir(dir)
fs.chdir(dir) do
app_code = <<~CODE
# frozen_string_literal: true

require "hanami"

module #{app}
class App < Hanami::App
end
end
CODE
fs.write("config/app.rb", app_code)

routes = <<~CODE
# frozen_string_literal: true

require "hanami/routes"

module #{app}
class Routes < Hanami::Routes
define do
root { "Hello from Hanami" }
end
end
end
CODE
fs.write("config/routes.rb", routes)

yield
end
end
end