Class: RailsuiCharts::Generators::InstallGenerator

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

Constant Summary collapse

LAYOUT =

The JavaScript is no longer copied. It used to be, and that meant bundle update moved the Ruby while the controllers stayed on whatever version was installed — a fix could ship and reach nobody. Bundled apps take it from npm, importmap apps from the pin this engine adds, and both follow the gem from then on.

The stylesheet now follows the same rule, for the same reason and after the same mistake. See remove_broken_css_import.

"app/views/layouts/application.html.erb"
TAILWIND_ENTRY =
"app/assets/tailwind/application.css"
STYLESHEET_TAG =
%q(<%= stylesheet_link_tag "railsui_charts" %>).freeze
BROKEN_IMPORT =

What 0.1.0 through 0.2.2 wrote into the Tailwind entry. It never worked. Relative to app/assets/tailwind it names app/stylesheets — not the application's own app/assets/stylesheets, and certainly not the file it was after, which lives inside the gem where no relative path from the application can reach it. Tailwind does not quietly skip an import it cannot resolve; it fails the build. So this is removed rather than corrected, from anyone who ran the old generator.

%r{^@import\s+"\.\./\.\./stylesheets/railsui_charts";?[ \t]*\r?\n}.freeze

Instance Method Summary collapse

Instance Method Details

Served from the gem through the asset pipeline, which already carries it: the engine puts app/assets/stylesheets on the load path and precompiles the file. Nothing is copied, so bundle update moves the CSS too.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/generators/railsui_charts/install/install_generator.rb', line 43

def add_stylesheet_link
  path = File.join(destination_root, LAYOUT)

  unless File.exist?(path)
    say "⚠️  #{LAYOUT} not found. Add this to your layout's <head>:", :yellow
    say "  #{STYLESHEET_TAG}"
    return
  end

  contents = File.read(path)

  if contents.include?(%("railsui_charts"))
    say "✓ Rails UI Charts stylesheet already linked", :green
    return
  end

  unless contents.match?(%r{</head>})
    say "⚠️  No </head> found in #{LAYOUT}. Add this to your layout's <head>:", :yellow
    say "  #{STYLESHEET_TAG}"
    return
  end

  inject_into_file LAYOUT, "    #{STYLESHEET_TAG}\n", before: %r{^[ \t]*</head>}
  say "✓ Linked the Rails UI Charts stylesheet in #{LAYOUT}", :green
end


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/generators/railsui_charts/install/install_generator.rb', line 69

def print_next_steps
  say ""
  say "RailsUI Charts #{RailsuiCharts::VERSION} installed.", :green
  say ""

  if importmap?
    say "Your importmap already has the controllers — this engine pins them.", :cyan
    say "Register them in app/javascript/controllers/index.js:", :cyan
  else
    say "Add the JavaScript package and ApexCharts:", :cyan
    say "  yarn add @getrailsui/charts apexcharts", :cyan
    say ""
    say "Then register the controllers in app/javascript/controllers/index.js:", :cyan
  end

  say ""
  say '  import { registerRailsuiCharts } from "@getrailsui/charts"', :cyan
  say "  registerRailsuiCharts(application)", :cyan
  say ""

  if importmap?
    say "ApexCharts is a peer dependency; pin it too:", :cyan
    say '  pin "apexcharts", to: "https://esm.sh/apexcharts@3.45.2"', :cyan
    say ""
  elsif File.exist?(File.join(destination_root, TAILWIND_ENTRY))
    say "Prefer the CSS inside your Tailwind build? Once the package is", :cyan
    say "installed you can drop the stylesheet_link_tag and import it:", :cyan
    say ""
    say "  /* #{TAILWIND_ENTRY} */", :cyan
    say '  @import "@getrailsui/charts/styles.css";', :cyan
    say ""
  end

  say "Then use <%= railsui_chart data, type: :area %> in your views.", :cyan
  say ""
end

#remove_broken_css_importObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/railsui_charts/install/install_generator.rb', line 28

def remove_broken_css_import
  path = File.join(destination_root, TAILWIND_ENTRY)
  return unless File.exist?(path)

  contents = File.read(path)
  return unless contents.match?(BROKEN_IMPORT)

  File.write(path, contents.sub(BROKEN_IMPORT, ""))
  say "✓ Removed the unresolvable @import from #{TAILWIND_ENTRY}", :green
  say "  It named a path that does not exist and failed the Tailwind build.", :yellow
end