-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate struct (with RubyFileGenerator, add RubyFileWriter) (#199)
* Add struct generator * Extract App::Generate::Command * Specify full name, to use App::Command * Use App::Generate::Command for Struct * Use KEY_SEPARATOR from constants file * Provide generator class to command * Extract Helper, use for Struct generator * Fix specs with Helper * kwargs * Rename helper to RubyFileWriter * Fix examples * Remove optional kwarg * Reorder args * Rename to relative_parent_class * Remove duplicate implementation * Add api doc comments * Reorder methods * Refactor initialize * Refactor to use method instead of arg * Refactor to move logic into generator * Reorder assignments * Add Hanami::CLI::RubyFileGenerator, convert Operation to use it instead of ERB (#186) * Add dry-operation to default Gemfile * Add base Operation class, based on dry-operation * Fix view spec * Add Operation generators * Add empty `call` method definition * Remove ostruct * Allow slash separator for generator * Allow slash separator for generator * Rename module to admin * Remove newlines in generated files By adding new templates for un-nested operations * Remove input as default args * Remove Operations namespace, generate in app/ or slices/SLICE_NAME/ * Prevent generating operation without namespace * Revert "Prevent generating operation without namespace" This reverts commit a5bd2f3. * Add recommendation to add namespace to operations * Change examples * Switch to outputting directly, remove Files#recommend * Add Hanami::CLI::RubyFileGenerator * x.x.x => 2.2.0 * x.x.x => 2.2.0 * Include Dry::Monads[:result] in base Action * Add .module tests * Convert top-level app operation to use RubyFileGenerator * Convert nested app operation to use RubyFileGenerator * Support slash separators * Convert top-level slice operation to use RubyFileGenerator * Remove OperationContext * Remove namespaces instance variable * Refactor to variables * Remove last temporary instance variable * Refactor * More refactoring, for clarity * Rename variable for clarity * Rename helper method * Simplify RubyFileGenerator, support older versions * Convert Operation generator to use simplified RubyFileGenerator * Remove un-used errors * Refactor * Older kwargs forwarding style * Refactor * Rename variable * Add explanatory comment Add dry-monads include for slice base action * Fix base slice action * Remove un-used ERB templates * Remove OperationContext * Ternary over and/or * Fix missing 'end' from bad merge * Fix namespace recommendation * Extract App::Generate::Command * Specify full name, to use App::Command * Use constants file * Move class methods above initialize * Use constants file * Add yard comments * Revert "Use constants file" This reverts commit 303f502. Would need to namespace it and we may want to this to standalone so keeping it here. It's just two little spaces anyway * Fix indent to be two spaces * Remove extraneous requires * Use out.string.chomp * Fix name of expectation
- Loading branch information
Showing
9 changed files
with
364 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hanami | ||
module CLI | ||
module Commands | ||
module App | ||
module Generate | ||
# @since 2.2.0 | ||
# @api private | ||
class Struct < Command | ||
argument :name, required: true, desc: "Struct name" | ||
option :slice, required: false, desc: "Slice name" | ||
|
||
example [ | ||
%(book (MyApp::Structs::Book)), | ||
%(book/published_book (MyApp::Structs::Book::PublishedBook)), | ||
%(book --slice=admin (Admin::Structs::Book)), | ||
] | ||
|
||
def generator_class | ||
Generators::App::Struct | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
# frozen_string_literal: true | ||
|
||
require "erb" | ||
require "dry/files" | ||
require_relative "../constants" | ||
require_relative "../../errors" | ||
|
||
module Hanami | ||
module CLI | ||
module Generators | ||
module App | ||
# @since 2.2.0 | ||
# @api private | ||
class RubyFileWriter | ||
# @since 2.2.0 | ||
# @api private | ||
def initialize( | ||
fs:, | ||
inflector:, | ||
app_namespace:, | ||
key:, | ||
slice:, | ||
relative_parent_class:, | ||
extra_namespace: nil, | ||
body: [] | ||
) | ||
@fs = fs | ||
@inflector = inflector | ||
@app_namespace = app_namespace | ||
@key = key | ||
@slice = slice | ||
@extra_namespace = extra_namespace&.downcase | ||
@relative_parent_class = relative_parent_class | ||
@body = body | ||
raise_missing_slice_error_if_missing(slice) if slice | ||
end | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
def call | ||
fs.mkdir(directory) | ||
fs.write(path, file_contents) | ||
end | ||
|
||
private | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
attr_reader( | ||
:fs, | ||
:inflector, | ||
:app_namespace, | ||
:key, | ||
:slice, | ||
:extra_namespace, | ||
:relative_parent_class, | ||
:body, | ||
) | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
def file_contents | ||
class_definition( | ||
class_name: class_name, | ||
container_namespace: container_namespace, | ||
local_namespaces: local_namespaces, | ||
) | ||
end | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
def class_name | ||
key.split(KEY_SEPARATOR)[-1] | ||
end | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
def container_namespace | ||
slice || app_namespace | ||
end | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
def local_namespaces | ||
Array(extra_namespace) + key.split(KEY_SEPARATOR)[..-2] | ||
end | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
def directory | ||
base = if slice | ||
fs.join("slices", slice) | ||
else | ||
fs.join("app") | ||
end | ||
|
||
@directory ||= if local_namespaces.any? | ||
fs.join(base, local_namespaces) | ||
else | ||
fs.join(base) | ||
end | ||
end | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
def path | ||
fs.join(directory, "#{class_name}.rb") | ||
end | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
def class_definition(class_name:, container_namespace:, local_namespaces:) | ||
container_module = normalize(container_namespace) | ||
|
||
modules = local_namespaces | ||
.map { normalize(_1) } | ||
.compact | ||
.prepend(container_module) | ||
|
||
parent_class = [container_module, relative_parent_class].join("::") | ||
|
||
RubyFileGenerator.class( | ||
normalize(class_name), | ||
parent_class: parent_class, | ||
modules: modules, | ||
header: ["# frozen_string_literal: true"], | ||
body: body | ||
) | ||
end | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
def normalize(name) | ||
inflector.camelize(name).gsub(/[^\p{Alnum}]/, "") | ||
end | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
def raise_missing_slice_error_if_missing(slice) | ||
if slice | ||
slice_directory = fs.join("slices", slice) | ||
raise MissingSliceError.new(slice) unless fs.directory?(slice_directory) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require "erb" | ||
require "dry/files" | ||
require_relative "../constants" | ||
require_relative "../../errors" | ||
|
||
module Hanami | ||
module CLI | ||
module Generators | ||
module App | ||
# @since 2.2.0 | ||
# @api private | ||
class Struct | ||
# @since 2.2.0 | ||
# @api private | ||
def initialize(fs:, inflector:, out: $stdout) | ||
@fs = fs | ||
@inflector = inflector | ||
@out = out | ||
end | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
def call(app_namespace, key, slice) | ||
RubyFileWriter.new( | ||
fs: fs, | ||
inflector: inflector, | ||
app_namespace: app_namespace, | ||
key: key, | ||
slice: slice, | ||
extra_namespace: "Structs", | ||
relative_parent_class: "DB::Struct", | ||
).call | ||
end | ||
|
||
private | ||
|
||
attr_reader :fs, :inflector, :out | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.