Class: ShadcnViewComponent::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/shadcn_view_component/install/install_generator.rb

Overview

bin/rails generate shadcn_view_component:install

Wires the gem into a host app. The part worth automating is every line that names a path into this gem: they have to point inside whatever directory bundler put us in, and that differs between a system gem, bundle config set path, a path: checkout and a git: source.

All three lines, not just @source. This generator used to write @import "shadcn.css", which reads like an asset-pipeline path and is not one: tailwindcss-rails runs the CLI with -i and -o and no load path, so the CLI resolves a bare name the way Node does — beside the file, then node_modules — and a Rails app has neither. It stopped the build with Can't resolve 'shadcn.css'. The dummy never showed it because its own entrypoint reaches the engine with ../../../../.., which is a relationship no host has.

Constant Summary collapse

JAVASCRIPT_ENTRYPOINT =

Where a Stimulus application is in scope. controllers/index.js is the file importmap-rails generates, and it opens with import { application } from "controllers/application" — so a line appended there can register onto the app's own instance.

app/javascript/application.js is not that file. It imports "controllers" for the side effect and defines no application binding, which is where this generator used to append: a stock Rails 8 app got ReferenceError: application is not defined, no controllers registered, and every dialog, select and menu inert — with the CSS working, so it looked installed.

"app/javascript/controllers/index.js"
TAILWIND_ENTRYPOINTS =
[
  "app/assets/tailwind/application.css",
  "app/assets/stylesheets/application.tailwind.css",
  "app/assets/stylesheets/application.css"
].freeze

Instance Method Summary collapse

Instance Method Details

#add_javascriptObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/generators/shadcn_view_component/install/install_generator.rb', line 62

def add_javascript
  return say_status :skip, "no importmap; import \"shadcn\" yourself", :yellow unless importmap?

  unless File.exist?(Rails.root.join(JAVASCRIPT_ENTRYPOINT))
    say_status :skip, "#{JAVASCRIPT_ENTRYPOINT} not found; register the controllers yourself", :yellow
    say javascript_block
    return
  end

  if File.read(Rails.root.join(JAVASCRIPT_ENTRYPOINT)).include?("registerShadcnControllers")
    return say_status :identical, JAVASCRIPT_ENTRYPOINT
  end

  append_to_file JAVASCRIPT_ENTRYPOINT, "\n#{javascript_block}"
end

#add_tailwind_importsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/generators/shadcn_view_component/install/install_generator.rb', line 44

def add_tailwind_imports
  entrypoint = TAILWIND_ENTRYPOINTS.find { |path| File.exist?(Rails.root.join(path)) }

  unless entrypoint
    say_status :skip, "no Tailwind entrypoint found; add the block below yourself", :yellow
    say "(paths are written for #{TAILWIND_ENTRYPOINTS.first}; adjust them if yours sits elsewhere)"
    say tailwind_block(TAILWIND_ENTRYPOINTS.first)
    return
  end

  if File.read(Rails.root.join(entrypoint)).include?("shadcn.css")
    say_status :identical, entrypoint
    return
  end

  append_to_file entrypoint, "\n#{tailwind_block(entrypoint)}"
end

#show_layout_hintObject



78
79
80
81
82
83
84
85
86
# File 'lib/generators/shadcn_view_component/install/install_generator.rb', line 78

def show_layout_hint
  say "\nAdd the theming tags to your layout:", :green
  say <<~ERB
    <head>
      <%= shadcn_theme_script_tag %>
    </head>
    <body class="<%= shadcn_theme_class %>">
  ERB
end