Class: InertiaHanami::Generators::InstallGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/inertia_hanami/install_generator.rb

Overview

Scaffolds everything a Hanami app needs to start using inertia_hanami: the provider, middleware wiring, layout, view helper, a sample page, and @inertiajs/* package.json guidance.

Mirrors the shape of hanami-cli's own generators (fs:, inflector:, out: injected, #call performs the work), so it composes naturally with Hanami::CLI::Commands::App::Command subclasses.

Constant Summary collapse

TEMPLATES_DIR =
File.expand_path("templates", __dir__)
FRAMEWORK_PACKAGES =
{
  "react" => %w[@inertiajs/react react react-dom],
  "vue" => %w[@inertiajs/vue3 vue],
  "svelte" => %w[@inertiajs/svelte svelte]
}.freeze
MIDDLEWARE =
{
  "version" => "Version",
  "redirects" => "Redirects",
  "csrf" => "Csrf"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(fs:, inflector:, out:) ⇒ InstallGenerator

Returns a new instance of InstallGenerator.



31
32
33
34
35
# File 'lib/generators/inertia_hanami/install_generator.rb', line 31

def initialize(fs:, inflector:, out:)
  @fs = fs
  @inflector = inflector
  @out = out
end

Instance Method Details

#call(base_path:, namespace:, framework: "react", force: false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/generators/inertia_hanami/install_generator.rb', line 37

def call(base_path:, namespace:, framework: "react", force: false)
  unless FRAMEWORK_PACKAGES.key?(framework.to_s)
    raise ArgumentError, "unknown framework #{framework.inspect}, expected one of #{FRAMEWORK_PACKAGES.keys}"
  end

  @base_path = base_path.to_s
  @namespace = namespace.to_s
  @framework = framework.to_s
  @force = force

  generate_provider
  generate_middleware
  generate_layout
  generate_helpers
  generate_sample_page
  generate_package_json_guidance

  out.puts
  out.puts "Next steps:"
  out.puts "  - Run `npm install` (or yarn/pnpm) to install the @inertiajs/* packages."
  out.puts "  - Wire up your JS entrypoint (Vite/hanami-assets config) - inertia_hanami " \
           "does not manage frontend bundling."
end