twitter-bootstrap-rails 5.5.0 → 5.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +64 -0
  3. data/README.md +115 -4
  4. data/lib/generators/bootstrap/agent/agent_generator.rb +17 -0
  5. data/lib/generators/bootstrap/agent/templates/guide.md.erb +34 -2
  6. data/lib/generators/bootstrap/bootstrap5_markup.rb +22 -0
  7. data/lib/generators/bootstrap/cdn/cdn_generator.rb +32 -2
  8. data/lib/generators/bootstrap/cdn_tags.rb +11 -0
  9. data/lib/generators/bootstrap/doctor/doctor_generator.rb +12 -2
  10. data/lib/generators/bootstrap/install/install_generator.rb +77 -21
  11. data/lib/generators/bootstrap/layout/layout_generator.rb +15 -0
  12. data/lib/generators/bootstrap/layout/templates/layout.html.erb +4 -1
  13. data/lib/generators/bootstrap/layout/templates/layout.html.haml +4 -1
  14. data/lib/generators/bootstrap/layout/templates/layout.html.slim +4 -1
  15. data/lib/generators/bootstrap/nes/nes_generator.rb +101 -0
  16. data/lib/generators/bootstrap/snippets/snippet_base.rb +4 -0
  17. data/lib/generators/bootstrap/stimulus/stimulus_generator.rb +49 -0
  18. data/lib/generators/bootstrap/stimulus/templates/bootstrap_controller.js.tt +67 -0
  19. data/lib/generators/bootstrap/themed/themed_generator.rb +4 -0
  20. data/lib/twitter/bootstrap/rails/config.rb +17 -4
  21. data/lib/twitter/bootstrap/rails/doctor.rb +161 -11
  22. data/lib/twitter/bootstrap/rails/v6_migration.rb +156 -0
  23. data/lib/twitter/bootstrap/rails/version.rb +33 -1
  24. data/spec/lib/generators/agent_generator_spec.rb +9 -0
  25. data/spec/lib/generators/cdn_generator_spec.rb +31 -0
  26. data/spec/lib/generators/doctor_generator_spec.rb +108 -0
  27. data/spec/lib/generators/install_generator_spec.rb +51 -1
  28. data/spec/lib/generators/layout_generator_spec.rb +24 -0
  29. data/spec/lib/generators/nes_generator_spec.rb +164 -0
  30. data/spec/lib/generators/no_hardcoded_versions_spec.rb +3 -1
  31. data/spec/lib/generators/stimulus_generator_spec.rb +65 -0
  32. data/spec/lib/twitter_bootstrap_rails/v6_migration_spec.rb +98 -0
  33. data/spec/support/generator_spec_helper.rb +2 -0
  34. data/vendor/assets/javascripts/twitter/bootstrap/bootstrap.esm.min.js +7 -0
  35. data/vendor/assets/javascripts/twitter/bootstrap/popper.esm.js +15 -0
  36. metadata +31 -10
@@ -0,0 +1,156 @@
1
+ require 'twitter/bootstrap/rails/legacy_markup'
2
+
3
+ module Twitter
4
+ module Bootstrap
5
+ module Rails
6
+ # Finds Bootstrap 5 markup, Sass and JavaScript that changes in Bootstrap 6,
7
+ # following the upstream migration skill
8
+ # (npx skills add twbs/bootstrap --skill bootstrap-v5-v6-migration).
9
+ # The gem still ships Bootstrap 5, so this is a worklist for an app that is
10
+ # moving to 6, not markup to avoid today. `bootstrap:doctor --v6` reports it.
11
+ module V6Migration
12
+ SKILL_COMMAND = "npx skills add twbs/bootstrap --skill bootstrap-v5-v6-migration".freeze
13
+ GUIDE_URL = "https://getbootstrap.com/docs/6.0/guides/migration/".freeze
14
+
15
+ BREAKPOINTS = { "sm" => "sm", "md" => "md", "lg" => "lg", "xl" => "xl", "xxl" => "2xl" }.freeze
16
+ THEMES = %w[primary secondary success danger warning info].freeze
17
+ FONT_SIZES = { "1" => "4xl", "2" => "3xl", "3" => "2xl", "4" => "xl", "5" => "lg", "6" => "md" }.freeze
18
+ RENAMED_COMPONENTS = { "modal" => "dialog", "offcanvas" => "drawer", "dropdown" => "menu" }.freeze
19
+ # Classes that take a breakpoint as a suffix in v5 (navbar-expand-lg).
20
+ SUFFIXED = %w[container navbar-expand offcanvas table-responsive list-group-horizontal vstack hstack col].freeze
21
+
22
+ CLASSES = {
23
+ "modal" => "dialog on a <dialog> element",
24
+ "modal-dialog" => "nothing (remove the wrapper; use one <dialog class=\"dialog\">)",
25
+ "modal-content" => "nothing (remove the wrapper; use one <dialog class=\"dialog\">)",
26
+ "offcanvas" => "drawer on a <dialog> element",
27
+ "dropdown" => "nothing (remove the wrapper; the toggle and .menu are siblings)",
28
+ "dropdown-menu" => "menu",
29
+ "dropdown-item" => "menu-item",
30
+ "dropdown-divider" => "menu-divider",
31
+ "dropdown-header" => "menu-header",
32
+ "dropdown-toggle" => "nothing (the menu toggle needs no class)",
33
+ "dropdown-toggle-split" => "nothing (remove it)",
34
+ "text-muted" => "fg-secondary",
35
+ "form-select" => "form-control (on the select)",
36
+ "form-check" => "form-field",
37
+ "form-check-input" => "check or radio (on the input)",
38
+ "form-check-label" => "nothing (a plain label)",
39
+ "form-switch" => "a switch wrapper around the input",
40
+ "needs-validation" => "data-bs-validate on the form",
41
+ "was-validated" => "nothing (:user-invalid handles it)",
42
+ "has-validation" => "nothing (remove it)",
43
+ "clearfix" => "d-flow-root",
44
+ "lh-base" => "lh-md",
45
+ "lead" => "fs-* and fw-* utilities",
46
+ "placeholder-glow" => "placeholder-pulse (or placeholder-wave)",
47
+ "btn-close-white" => "nothing (set the text colour instead)",
48
+ "card-link" => "a plain link",
49
+ "accordion-button" => "nothing (put the title in summary.accordion-header)",
50
+ "accordion-collapse" => "nothing (use details.accordion-item)",
51
+ "navbar-light" => "nothing (remove it)",
52
+ "navbar-dark" => "nothing (use data-bs-theme=\"dark\")",
53
+ "carousel-control-prev" => "btn-icon with data-bs-slide=\"prev\"",
54
+ "carousel-control-next" => "btn-icon with data-bs-slide=\"next\"",
55
+ "carousel-control-prev-icon" => "carousel-icon-prev",
56
+ "carousel-control-next-icon" => "carousel-icon-next",
57
+ "carousel-caption" => "your own markup",
58
+ "carousel-dark" => "data-bs-theme=\"dark\""
59
+ }.freeze
60
+
61
+ # [pattern, block turning the match into its replacement, example for docs]
62
+ CLASS_PATTERNS = [
63
+ [/\Amodal-(header|body|footer|title|sm|lg|xl|fullscreen)\z/, ->(m) { "dialog-#{m[1]}" }, "modal-body becomes dialog-body"],
64
+ [/\Aoffcanvas-(header|body|title|start|end|top|bottom)\z/, ->(m) { "drawer-#{m[1]}" }, "offcanvas-start becomes drawer-start"],
65
+ [/\Adropdown-menu-(start|end)\z/, ->(m) { "menu-#{m[1]}" }, "dropdown-menu-end becomes menu-end"],
66
+ [/\Amodal-fullscreen-(sm|md|lg|xl|xxl)-down\z/,
67
+ ->(m) { "#{BREAKPOINTS[m[1]]}-down:dialog-fullscreen" }, "modal-fullscreen-sm-down becomes sm-down:dialog-fullscreen"],
68
+ [/\A([a-z][\w-]*)-hover\z/,
69
+ ->(m) { "hover:#{V6Migration.class_replacement(m[1])}" }, "opacity-50-hover becomes hover:opacity-50"],
70
+ [/\Abtn-(outline-)?(#{THEMES.join('|')})\z/,
71
+ ->(m) { "btn-#{m[1] ? 'outline' : 'solid'} theme-#{m[2]}" }, "btn-primary and btn-outline-primary become btn-solid theme-primary and btn-outline theme-primary"],
72
+ [/\Aalert-(#{THEMES.join('|')})\z/, ->(m) { "alert theme-#{m[1]}" }, "alert-info becomes alert theme-info"],
73
+ [/\Atext-bg-(#{THEMES.join('|')})\z/, ->(m) { "bg-#{m[1]} fg-contrast-#{m[1]}" }, "text-bg-warning becomes bg-warning fg-contrast-warning"],
74
+ [/\Atext-(#{THEMES.join('|')})\z/, ->(m) { "fg-#{m[1]}" }, "text-danger becomes fg-danger"],
75
+ [/\A(?:btn|btn-outline|alert|text|text-bg)-(light|dark)\z/,
76
+ ->(_) { "a neutral surface, a colour mode or theme-inverse (v6 has no theme-light or theme-dark)" },
77
+ "btn-light and text-dark become a neutral surface, a colour mode or theme-inverse"],
78
+ [/\Afs-([1-6])\z/, ->(m) { "fs-#{FONT_SIZES[m[1]]}" }, "fs-1 and fs-6 become fs-4xl and fs-md"],
79
+ [/\Adisplay-[1-6]\z/, ->(_) { "fs-* and fw-* utilities" }, "display-1 becomes fs-* and fw-* utilities"],
80
+ [/\Am(h|w)-(.+)\z/, ->(m) { "max-#{m[1]}-#{m[2]}" }, "mw-100 becomes max-w-100"],
81
+ [/\Alink-(offset|underline)(-.+)?\z/,
82
+ ->(m) { "underline#{m[1] == 'offset' ? '-offset' : ''}#{m[2]}" }, "link-offset-2 becomes underline-offset-2"],
83
+ [/\Am[tbsexy]?-n\d\z/, ->(_) { "ms--1, ms--2, me--1, me--2 or your own style" }, "mt-n1 becomes ms--1 / me--1 or your own style"],
84
+ [/\A(#{SUFFIXED.join('|')})-(sm|md|lg|xl|xxl)\z/,
85
+ ->(m) { "#{BREAKPOINTS[m[2]]}:#{V6Migration.class_replacement(m[1])}" }, "navbar-expand-lg becomes lg:navbar-expand"],
86
+ [/\A([a-z][\w-]*?)-print-([\w-]+)\z/,
87
+ ->(m) { "print:#{V6Migration.class_replacement("#{m[1]}-#{m[2]}")}" }, "d-print-none becomes print:d-none"],
88
+ [/\A([a-z][\w-]*?)-(sm|md|lg|xl|xxl)-([\w-]+)\z/,
89
+ ->(m) { "#{BREAKPOINTS[m[2]]}:#{V6Migration.class_replacement("#{m[1]}-#{m[3]}")}" },
90
+ "col-md-6 and d-xxl-none become md:col-6 and 2xl:d-none"]
91
+ ].freeze
92
+
93
+ TOGGLE = /(?:data-bs-|\bbs[_-])(toggle|dismiss)["']?\s*(?:=>|=|:)\s*(["'])(#{RENAMED_COMPONENTS.keys.join('|')})\2/
94
+
95
+ # [pattern, replacement, example for docs]. Applied to every scanned file.
96
+ LINE_PATTERNS = [
97
+ [/(?:data-bs-|\bbs[_-])ride(?![\w-])/, %{data-bs-autoplay="true" (carousels no longer start on their own)}],
98
+ [/@import\s+(["'])bootstrap/, %{@use "bootstrap/scss/bootstrap" (add `as *` to keep v5 names)}],
99
+ [/\bbootstrap\.(?:Modal|Offcanvas|Dropdown)\b/, "import { Dialog, Drawer, Menu } from Bootstrap's module build"],
100
+ [/\bpopperConfig\b/, "floatingConfig"],
101
+ [/@popperjs\/core/, "@floating-ui/dom"],
102
+ [/--bs-[\w-]+-rgb\b/, "color-mix(in oklab, var(--bs-…), transparent N%)"]
103
+ ].freeze
104
+
105
+ # Files whose class attributes and shorthand are read for class names.
106
+ MARKUP_EXTENSIONS = %w[.erb .haml .slim .rb .html].freeze
107
+
108
+ module_function
109
+
110
+ # Returns [{ :line, :found, :replacement }, ...] for a file's contents.
111
+ def scan(text, extension)
112
+ text.each_line.with_index(1).flat_map do |line, number|
113
+ scan_line(line, extension).map { |found, replacement| { :line => number, :found => found, :replacement => replacement } }
114
+ end
115
+ end
116
+
117
+ def scan_line(line, extension)
118
+ classes = MARKUP_EXTENSIONS.include?(extension) ? LegacyMarkup.class_names(line, extension) : []
119
+ renamed = classes.filter_map do |name|
120
+ replacement = replacement_for(name)
121
+ [name, replacement] if replacement
122
+ end
123
+
124
+ toggles = line.scan(TOGGLE).map do |attribute, _quote, value|
125
+ [%{data-bs-#{attribute}="#{value}"}, %{data-bs-#{attribute}="#{RENAMED_COMPONENTS[value]}"}]
126
+ end
127
+
128
+ other = LINE_PATTERNS.flat_map do |pattern, replacement|
129
+ matches = []
130
+ line.scan(pattern) { matches << [Regexp.last_match(0).sub(/\Abs[_-]/, "data-bs-"), replacement] }
131
+ matches
132
+ end
133
+
134
+ renamed + toggles + other
135
+ end
136
+
137
+ def replacement_for(name)
138
+ return CLASSES[name] if CLASSES.key?(name)
139
+
140
+ CLASS_PATTERNS.each do |pattern, replacement, _example|
141
+ match = pattern.match(name)
142
+ return replacement.call(match) if match
143
+ end
144
+ nil
145
+ end
146
+
147
+ # The v6 name for a class when it is a plain rename, otherwise the class itself.
148
+ # Used under a breakpoint prefix, where a sentence would make no sense.
149
+ def class_replacement(name)
150
+ replacement = RENAMED_COMPONENTS[name] || replacement_for(name)
151
+ replacement && replacement.match?(/\A[\w:-]+\z/) ? replacement : name
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
@@ -1,7 +1,7 @@
1
1
  module Twitter
2
2
  module Bootstrap
3
3
  module Rails
4
- VERSION = "5.5.0"
4
+ VERSION = "5.6.0"
5
5
 
6
6
  # The Bootstrap release this gem vendors and links to. The gem version no
7
7
  # longer tracks it; this constant is the authoritative statement.
@@ -29,6 +29,38 @@ module Twitter
29
29
  :integrity => "sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
30
30
  }
31
31
  }.freeze
32
+
33
+ # The Bootstrap 6 prerelease that `rails g bootstrap:cdn --bootstrap=6` prints.
34
+ # The gem's generators and helpers still write Bootstrap 5 markup, so this is
35
+ # only for trying 6 out or migrating to it. Its bundle is an ES module and
36
+ # includes Floating UI, so there is no separate Popper pair. The integrity
37
+ # hashes are sha384 of the files jsDelivr serves for this version.
38
+ BOOTSTRAP_NEXT_VERSION = "6.0.0-alpha.1"
39
+ BOOTSTRAP_NEXT_CDN = {
40
+ :css => {
41
+ :url => "https://cdn.jsdelivr.net/npm/bootstrap@#{BOOTSTRAP_NEXT_VERSION}/dist/css/bootstrap.min.css",
42
+ :integrity => "sha384-B/GM4XqrwHnWXNOWMbloTmrYXZg10cakYGmpfsR/bbzQ6JAJI4ihuyADKLnBgrCe"
43
+ },
44
+ :bundle => {
45
+ :url => "https://cdn.jsdelivr.net/npm/bootstrap@#{BOOTSTRAP_NEXT_VERSION}/dist/js/bootstrap.bundle.min.js",
46
+ :integrity => "sha384-1a/pXj49ZQ1aHEmrJ+gMw1otqoVsYwlEnlD8mIfY2TV03r20Y0CN7uqx1tQogjPL"
47
+ }
48
+ }.freeze
49
+
50
+ # HeroDevs Bootstrap NES (Never-Ending Support): security-patched Bootstrap 4.6
51
+ # for apps that can't move to 5 yet. It is a paid product served only from
52
+ # HeroDevs' private npm registry, so the gem can't vendor it; `rails g
53
+ # bootstrap:nes` points an app's package.json and .npmrc at it instead.
54
+ # Source: https://docs.herodevs.com/bootstrap/bootstrap-4-6 and its release notes.
55
+ BOOTSTRAP_NES = {
56
+ :name => "HeroDevs Bootstrap NES",
57
+ :package => "@neverendingsupport/bootstrap",
58
+ :version => "4.6.2-bootstrap-4.6.5",
59
+ :bootstrap_version => "4.6.5",
60
+ :registry => "https://registry.nes.herodevs.com/npm/pkg/",
61
+ :token_env => "HERODEVS_NES_TOKEN",
62
+ :docs => "https://docs.herodevs.com/bootstrap/bootstrap-4-6"
63
+ }.freeze
32
64
  end
33
65
  end
34
66
  end
@@ -107,5 +107,14 @@ describe Bootstrap::Generators::AgentGenerator do
107
107
 
108
108
  expect(skill).to include("rails g bootstrap:doctor --json")
109
109
  end
110
+
111
+ it "keeps agents on the shipped Bootstrap and points them at the upstream skill for Bootstrap 6" do
112
+ run_generator(described_class)
113
+ section = skill[/^## Bootstrap 6\n.*?(?=^## |\z)/m]
114
+
115
+ expect(section).to include("Don't write Bootstrap 6 markup")
116
+ expect(section).to include(Twitter::Bootstrap::Rails::V6Migration::SKILL_COMMAND)
117
+ expect(section).to include("rails g bootstrap:doctor --v6 --json")
118
+ end
110
119
  end
111
120
  end
@@ -39,4 +39,35 @@ describe Bootstrap::Generators::CdnGenerator do
39
39
 
40
40
  expect(Dir.children(destination_root)).to be_empty
41
41
  end
42
+ describe "--bootstrap=6" do
43
+ let(:next_cdn) { Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_CDN }
44
+
45
+ it "prints the Bootstrap 6 prerelease tags, with the bundle as an ES module" do
46
+ output = run_generator(described_class, %w[--bootstrap=6])
47
+
48
+ expect(output).to include(next_cdn[:css][:url], next_cdn[:css][:integrity])
49
+ expect(output).to include(%{<script type="module" src="#{next_cdn[:bundle][:url]}" integrity="#{next_cdn[:bundle][:integrity]}"})
50
+ expect(output).not_to include(cdn[:css][:url])
51
+ end
52
+
53
+ it "warns that it is a prerelease the gem's markup does not match" do
54
+ output = run_generator(described_class, %w[--bootstrap=6])
55
+
56
+ expect(output).to include(Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_VERSION).and include("bootstrap:doctor --v6")
57
+ end
58
+
59
+ it "points at Bootstrap 6 files" do
60
+ expect(next_cdn.values.map { |entry| entry[:url] }).to all(include("bootstrap@#{Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_VERSION}/"))
61
+ end
62
+
63
+ it "has no separate Popper pair" do
64
+ expect { run_generator(described_class, %w[--bootstrap=6 --separate-popper]) }
65
+ .to raise_error(Rails::Generators::Error, /separate-popper/)
66
+ end
67
+
68
+ it "rejects other versions" do
69
+ expect { run_generator(described_class, %w[--bootstrap=4]) }
70
+ .to raise_error(Rails::Generators::Error, /bootstrap:nes/)
71
+ end
72
+ end
42
73
  end
@@ -74,6 +74,50 @@ describe Bootstrap::Generators::DoctorGenerator do
74
74
  expect(check("bootstrap_js")[:status]).to eq("warn")
75
75
  end
76
76
 
77
+ describe "on importmap" do
78
+ def install_importmap
79
+ rails_mod.asset_pipeline = :propshaft
80
+ write_destination("config/importmap.rb", %{pin "application"\n})
81
+ write_destination("app/javascript/application.js", %{import "@hotwired/turbo-rails"\n})
82
+ run_generator(Bootstrap::Generators::InstallGenerator, %w[importmap])
83
+ rails_mod.asset_mode = :importmap
84
+ run_generator(Bootstrap::Generators::LayoutGenerator)
85
+ end
86
+
87
+ it "is happy with an app set up by the generators" do
88
+ install_importmap
89
+
90
+ expect(report).to include(:status => "ok", :asset_mode => "importmap")
91
+ expect(check("bootstrap_js")[:message]).to include("importmap")
92
+ end
93
+
94
+ it "reports an error when Bootstrap is not pinned" do
95
+ install_importmap
96
+ write_destination("config/importmap.rb", %{pin "application"\n})
97
+
98
+ expect(check("bootstrap_js")).to include(:status => "error", :fix => a_string_including("bootstrap:install importmap"))
99
+ end
100
+
101
+ it "reports an error when nothing imports Bootstrap" do
102
+ install_importmap
103
+ write_destination("app/javascript/application.js", %{import "@hotwired/turbo-rails"\n})
104
+
105
+ expect(check("bootstrap_js")).to include(:status => "error", :message => a_string_including("imports"))
106
+ end
107
+
108
+ it "warns when no layout loads the importmap, which drops the app's own JavaScript" do
109
+ install_importmap
110
+ write_destination("app/views/layouts/application.html.erb",
111
+ %{<%= stylesheet_link_tag "twitter/bootstrap/bootstrap.min" %>})
112
+
113
+ expect(check("importmap_tags")).to include(:status => "warn", :fix => a_string_including("javascript_importmap_tags"))
114
+ end
115
+ end
116
+
117
+ it "has nothing to say about importmap tags in an app without importmap" do
118
+ expect(check("importmap_tags")[:status]).to eq("ok")
119
+ end
120
+
77
121
  it "reads the mode from the layout if the initializer is missing" do
78
122
  write_destination("app/views/layouts/application.html.erb",
79
123
  %{<link href="https://cdn.jsdelivr.net/npm/bootstrap@#{version}/dist/css/bootstrap.min.css">})
@@ -99,6 +143,14 @@ describe Bootstrap::Generators::DoctorGenerator do
99
143
  expect(check("bootstrap_version")).to include(:status => "warn", :message => a_string_including("5.3.3"))
100
144
  end
101
145
 
146
+ it "names a Bootstrap 6 prerelease and points at the migration worklist" do
147
+ write_destination("app/views/layouts/application.html.erb",
148
+ %{<link href="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.1/dist/css/bootstrap.min.css">})
149
+
150
+ expect(check("bootstrap_version")).to include(:status => "warn", :message => a_string_including("6.0.0-alpha.1"),
151
+ :fix => a_string_including("--v6"))
152
+ end
153
+
102
154
  it "accepts what the generators installed" do
103
155
  install_static_on_propshaft
104
156
 
@@ -166,6 +218,55 @@ describe Bootstrap::Generators::DoctorGenerator do
166
218
  end
167
219
  end
168
220
 
221
+ describe "Bootstrap 6 migration" do
222
+ def v6_report
223
+ rails_mod::Doctor.new(destination_root).report(:v6 => true)
224
+ end
225
+
226
+ it "is left out unless asked for" do
227
+ write_destination("app/views/tasks/index.html.erb", %{<div class="modal-body"></div>})
228
+
229
+ expect(report).not_to have_key(:v6_migration)
230
+ expect(report[:checks].map { |c| c[:id] }).not_to include("v6_migration")
231
+ end
232
+
233
+ it "lists what changes in Bootstrap 6, with the location" do
234
+ write_destination("app/views/tasks/index.html.erb", %{<p>ok</p>\n<button data-bs-toggle="modal" class="btn btn-primary">x</button>})
235
+
236
+ expect(v6_report[:v6_migration]).to contain_exactly(
237
+ a_hash_including(:file => "app/views/tasks/index.html.erb", :line => 2, :found => "btn-primary"),
238
+ a_hash_including(:file => "app/views/tasks/index.html.erb", :line => 2, :found => %{data-bs-toggle="modal"})
239
+ )
240
+ check = v6_report[:checks].find { |c| c[:id] == "v6_migration" }
241
+ expect(check).to include(:status => "warn", :fix => a_string_including("bootstrap-v5-v6-migration"))
242
+ end
243
+
244
+ it "scans the app's JavaScript and stylesheets" do
245
+ write_destination("app/javascript/controllers/menu_controller.js", "new bootstrap.Dropdown(this.element)")
246
+ write_destination("app/assets/stylesheets/custom.scss", %{@import "bootstrap/scss/functions";})
247
+
248
+ expect(v6_report[:v6_migration].map { |f| f[:file] })
249
+ .to eq(%w[app/assets/stylesheets/custom.scss app/javascript/controllers/menu_controller.js])
250
+ end
251
+
252
+ it "skips the Bootstrap files the gem vendored" do
253
+ install_static_on_propshaft
254
+ layout_findings = v6_report[:v6_migration].reject { |f| f[:file].start_with?("app/views/") }
255
+
256
+ expect(layout_findings).to be_empty
257
+ end
258
+
259
+ it "finds the Bootstrap 5 markup in a layout the generators wrote" do
260
+ install_static_on_propshaft
261
+
262
+ expect(v6_report[:v6_migration].map { |f| f[:found] }).to include("navbar-expand-lg")
263
+ end
264
+
265
+ it "is happy when nothing needs to change" do
266
+ expect(v6_report[:checks].find { |c| c[:id] == "v6_migration" }[:status]).to eq("ok")
267
+ end
268
+ end
269
+
169
270
  describe "facts" do
170
271
  it "states the versions and template engine" do
171
272
  expect(report).to include(
@@ -207,6 +308,13 @@ describe Bootstrap::Generators::DoctorGenerator do
207
308
  .to raise_error(SystemExit) { |e| expect(e.status).to eq(1) }
208
309
  end
209
310
 
311
+ it "adds the Bootstrap 6 worklist with --v6" do
312
+ parsed = JSON.parse(run_generator(described_class, %w[--v6 --json]))
313
+
314
+ expect(parsed["v6_migration"]).to include(a_hash_including("found" => "navbar-expand-lg", "replacement" => "lg:navbar-expand"))
315
+ expect(run_generator(described_class, %w[--v6])).to match(%r{\[warn\]\s+v6_migration.*\n(.*\n)*\s+app/views/layouts/application\.html\.erb:\d+\s+navbar-expand-lg -> lg:navbar-expand})
316
+ end
317
+
210
318
  it "exits normally on warnings" do
211
319
  write_destination("app/views/tasks/index.html.erb", %{<a class="btn-default"></a>})
212
320
 
@@ -140,9 +140,59 @@ describe Bootstrap::Generators::InstallGenerator do
140
140
  end
141
141
  end
142
142
 
143
+ describe "importmap" do
144
+ let(:esm) { "vendor/javascript/twitter/bootstrap/bootstrap.esm.min.js" }
145
+ let(:popper) { "vendor/javascript/twitter/bootstrap/popper.esm.js" }
146
+
147
+ before do
148
+ Twitter::Bootstrap::Rails.asset_pipeline = :propshaft
149
+ write_destination("config/importmap.rb", %{pin "application"\n})
150
+ write_destination("app/javascript/application.js", %{import "@hotwired/turbo-rails"\n})
151
+ end
152
+
153
+ it "vendors the ES module builds of Bootstrap and Popper, and the stylesheet" do
154
+ run_generator(described_class, %w[importmap])
155
+
156
+ expect(read_destination(esm)).to start_with("/*!\n * Bootstrap v#{Twitter::Bootstrap::Rails::BOOTSTRAP_VERSION} ")
157
+ expect(read_destination(popper)).to include("@popperjs/core v#{Twitter::Bootstrap::Rails::POPPER_VERSION}")
158
+ expect(destination_exist?("app/assets/stylesheets/twitter/bootstrap/bootstrap.min.css")).to be true
159
+ expect(destination_exist?("app/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js")).to be false
160
+ end
161
+
162
+ it "pins both modules and imports Bootstrap from application.js" do
163
+ run_generator(described_class, %w[importmap])
164
+
165
+ expect(read_destination("config/importmap.rb"))
166
+ .to include(%{pin "bootstrap", to: "twitter/bootstrap/bootstrap.esm.min.js"})
167
+ .and include(%{pin "@popperjs/core", to: "twitter/bootstrap/popper.esm.js"})
168
+ expect(read_destination("app/javascript/application.js")).to include(%{import "bootstrap"})
169
+ end
170
+
171
+ it "does not pin or import twice when run again" do
172
+ run_generator(described_class, %w[importmap])
173
+ run_generator(described_class, %w[importmap --force])
174
+
175
+ expect(read_destination("config/importmap.rb").scan(/pin "bootstrap"/).size).to eq(1)
176
+ expect(read_destination("app/javascript/application.js").scan(/import "bootstrap"/).size).to eq(1)
177
+ end
178
+
179
+ it "writes an initializer selecting importmap mode" do
180
+ run_generator(described_class, %w[importmap])
181
+
182
+ expect(read_destination("config/initializers/bootstrap.rb")).to include("asset_mode = :importmap")
183
+ end
184
+
185
+ it "refuses an app without importmap" do
186
+ FileUtils.rm(destination_path("config/importmap.rb"))
187
+
188
+ expect { run_generator(described_class, %w[importmap]) }
189
+ .to raise_error(Rails::Generators::Error, /config\/importmap\.rb/)
190
+ end
191
+ end
192
+
143
193
  it "rejects an unknown mode" do
144
194
  expect {
145
195
  run_generator(described_class, %w[less])
146
- }.to raise_error(Rails::Generators::Error, /Expected 'static' or 'cdn'/)
196
+ }.to raise_error(Rails::Generators::Error, /Expected one of static, cdn, importmap/)
147
197
  end
148
198
  end
@@ -121,4 +121,28 @@ describe Bootstrap::Generators::LayoutGenerator do
121
121
  expect(layout).to include('stylesheet_link_tag "application"')
122
122
  expect(layout).not_to include("cdn.jsdelivr.net")
123
123
  end
124
+ describe "on an importmap app" do
125
+ before do
126
+ Twitter::Bootstrap::Rails.asset_pipeline = :propshaft
127
+ write_destination("config/importmap.rb", %{pin "application"\n})
128
+ end
129
+
130
+ [:erb, :haml, :slim].each do |engine|
131
+ it "keeps javascript_importmap_tags in #{engine}, so the app's own JavaScript still loads" do
132
+ expect(generate_layout([], :engine => engine)).to include("javascript_importmap_tags")
133
+ end
134
+
135
+ it "leaves Bootstrap's JavaScript to the importmap in importmap mode, in #{engine}" do
136
+ Twitter::Bootstrap::Rails.asset_mode = :importmap
137
+ layout = generate_layout([], :engine => engine)
138
+
139
+ expect(layout).to include('stylesheet_link_tag "twitter/bootstrap/bootstrap.min", "application"')
140
+ expect(layout).not_to include("bootstrap.bundle")
141
+ end
142
+ end
143
+ end
144
+
145
+ it "adds no importmap tags to an app without importmap" do
146
+ expect(generate_layout).not_to include("javascript_importmap_tags")
147
+ end
124
148
  end
@@ -0,0 +1,164 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+
4
+ describe Bootstrap::Generators::NesGenerator do
5
+ let(:nes) { Twitter::Bootstrap::Rails::BOOTSTRAP_NES }
6
+ let(:alias_spec) { "npm:#{nes[:package]}@#{nes[:version]}" }
7
+
8
+ def package_json
9
+ JSON.parse(read_destination("package.json"))
10
+ end
11
+
12
+ before do
13
+ stub_rails_application
14
+ write_destination("package.json", JSON.pretty_generate(
15
+ "name" => "app", "dependencies" => { "bootstrap" => "^4.6.2", "jquery" => "^3.7.1" }
16
+ ))
17
+ end
18
+
19
+ it "points bootstrap at the NES package, as a dependency and an override" do
20
+ run_generator(described_class)
21
+
22
+ expect(package_json["dependencies"]).to include("bootstrap" => alias_spec, "jquery" => "^3.7.1")
23
+ expect(package_json["overrides"]).to eq("bootstrap" => { "." => alias_spec })
24
+ end
25
+
26
+ it "adds the registry to .npmrc, reading the token from an environment variable" do
27
+ write_destination(".npmrc", "save-exact=true\n")
28
+ run_generator(described_class)
29
+ npmrc = read_destination(".npmrc")
30
+
31
+ expect(npmrc).to include("save-exact=true")
32
+ expect(npmrc).to include("@neverendingsupport:registry=#{nes[:registry]}")
33
+ expect(npmrc).to include("//registry.nes.herodevs.com/npm/pkg/:_authToken=${HERODEVS_NES_TOKEN}")
34
+ end
35
+
36
+ it "takes another variable name for the token" do
37
+ run_generator(described_class, %w[--token-env=NES_TOKEN])
38
+
39
+ expect(read_destination(".npmrc")).to include(":_authToken=${NES_TOKEN}")
40
+ end
41
+
42
+ it "does not repeat itself when run again" do
43
+ run_generator(described_class)
44
+ run_generator(described_class, %w[--force])
45
+
46
+ expect(read_destination(".npmrc").scan("@neverendingsupport:registry").size).to eq(1)
47
+ end
48
+
49
+ it "keeps an auth line the app already has" do
50
+ write_destination(".npmrc", "//registry.nes.herodevs.com/npm/pkg/:_authToken=${MY_TOKEN}\n")
51
+ run_generator(described_class)
52
+
53
+ expect(read_destination(".npmrc").scan("_authToken").size).to eq(1)
54
+ end
55
+
56
+ it "records NES mode" do
57
+ run_generator(described_class)
58
+
59
+ expect(read_destination("config/initializers/bootstrap.rb")).to include("asset_mode = :nes")
60
+ end
61
+
62
+ it "says what to do next, and that the gem's markup is Bootstrap 5" do
63
+ output = run_generator(described_class)
64
+
65
+ expect(output).to include("HERODEVS_NES_TOKEN").and include("npm install").and include("jQuery")
66
+ expect(output).to include("Bootstrap 5 markup")
67
+ end
68
+
69
+ it "refuses an app without package.json" do
70
+ FileUtils.rm(destination_path("package.json"))
71
+
72
+ expect { run_generator(described_class) }.to raise_error(Rails::Generators::Error, /package\.json/)
73
+ end
74
+
75
+ describe "the doctor on an NES app" do
76
+ before { run_generator(described_class) }
77
+
78
+ def report
79
+ Twitter::Bootstrap::Rails::Doctor.new(destination_root).report
80
+ end
81
+
82
+ def check(id)
83
+ report[:checks].find { |c| c[:id] == id }
84
+ end
85
+
86
+ it "reports the NES version and distribution" do
87
+ expect(report).to include(:asset_mode => "nes", :bootstrap_version => nes[:bootstrap_version],
88
+ :bootstrap_distribution => "HeroDevs Bootstrap NES")
89
+ end
90
+
91
+ it "is happy with what the generator wrote" do
92
+ expect(check("nes_package")[:status]).to eq("ok")
93
+ expect(check("nes_registry")[:status]).to eq("ok")
94
+ end
95
+
96
+ it "reports an error when package.json no longer uses the NES package" do
97
+ write_destination("package.json", JSON.pretty_generate("dependencies" => { "bootstrap" => "^4.6.2" }))
98
+
99
+ expect(check("nes_package")).to include(:status => "error", :fix => a_string_including("bootstrap:nes"))
100
+ end
101
+
102
+ it "warns about an older NES release" do
103
+ json = package_json
104
+ json["dependencies"]["bootstrap"] = "npm:#{nes[:package]}@4.6.2-bootstrap-4.6.3"
105
+ write_destination("package.json", JSON.pretty_generate(json))
106
+
107
+ expect(check("nes_package")).to include(:status => "warn", :message => a_string_including("4.6.3"))
108
+ end
109
+
110
+ it "warns when a token is written into .npmrc" do
111
+ write_destination(".npmrc", "@neverendingsupport:registry=#{nes[:registry]}\n" \
112
+ "//registry.nes.herodevs.com/npm/pkg/:_authToken=abc123secret\n")
113
+
114
+ expect(check("nes_registry")).to include(:status => "warn", :fix => a_string_including("${HERODEVS_NES_TOKEN}"))
115
+ expect(check("nes_registry").to_s).not_to include("abc123secret")
116
+ end
117
+
118
+ it "reports an error without the registry" do
119
+ write_destination(".npmrc", "")
120
+
121
+ expect(check("nes_registry")[:status]).to eq("error")
122
+ end
123
+
124
+ it "does not treat Bootstrap 4 markup as legacy" do
125
+ write_destination("app/views/tasks/index.html.erb", %{<a data-toggle="modal" class="btn btn-default">x</a>})
126
+
127
+ expect(report[:legacy_markup]).to be_empty
128
+ expect(check("legacy_markup")[:message]).to include("Bootstrap 4")
129
+ end
130
+
131
+ it "does not look for the vendored Bootstrap 5 files" do
132
+ expect(report[:checks].map { |c| c[:id] }).not_to include("bootstrap_css", "bootstrap_js")
133
+ end
134
+ end
135
+
136
+ describe "the agent guide on an NES app" do
137
+ let(:skill) { read_destination(".claude/skills/bootstrap-rails/SKILL.md") }
138
+
139
+ before do
140
+ run_generator(described_class)
141
+ Twitter::Bootstrap::Rails.asset_mode = :nes
142
+ run_generator(Bootstrap::Generators::AgentGenerator)
143
+ end
144
+
145
+ it "tells agents to write Bootstrap 4 markup and not to use the Bootstrap 5 generators" do
146
+ expect(skill).to include("Bootstrap #{nes[:bootstrap_version]}").and include("HeroDevs")
147
+ expect(skill).to include("Write Bootstrap 4 markup")
148
+ expect(skill).to include("bootstrap-v4-v6-migration")
149
+ end
150
+
151
+ it "does not list Bootstrap 4 markup as markup to avoid" do
152
+ expect(skill).not_to include("## Markup to avoid")
153
+ end
154
+ end
155
+
156
+ describe "generators that write Bootstrap 5 markup" do
157
+ before { Twitter::Bootstrap::Rails.asset_mode = :nes }
158
+
159
+ it "warn on an NES app" do
160
+ expect(run_generator(Bootstrap::Generators::LayoutGenerator)).to include("Bootstrap 5 markup")
161
+ expect(run_generator(Bootstrap::Generators::HeaderGenerator)).to include("Bootstrap 5 markup")
162
+ end
163
+ end
164
+ end
@@ -34,7 +34,9 @@ describe "generator templates" do
34
34
  "vendor/assets/stylesheets/twitter/bootstrap/bootstrap.min.css" => "Bootstrap v#{version}",
35
35
  "vendor/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js" => "Bootstrap v#{version}",
36
36
  "app/assets/stylesheets/twitter-bootstrap-static/bootstrap.css" => "Bootstrap v#{version}",
37
- "app/assets/javascripts/twitter/bootstrap/bootstrap.min.js" => "Bootstrap v#{version}"
37
+ "app/assets/javascripts/twitter/bootstrap/bootstrap.min.js" => "Bootstrap v#{version}",
38
+ "vendor/assets/javascripts/twitter/bootstrap/bootstrap.esm.min.js" => "Bootstrap v#{version}",
39
+ "vendor/assets/javascripts/twitter/bootstrap/popper.esm.js" => "@popperjs/core v#{Twitter::Bootstrap::Rails::POPPER_VERSION}"
38
40
  }.each do |path, banner|
39
41
  full = File.join(gem_root, path)
40
42
  expect(File.exist?(full)).to be(true), "missing vendored asset #{path}"