twitter-bootstrap-rails 5.5.1 → 5.6.1

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 +51 -0
  3. data/README.md +84 -5
  4. data/app/assets/javascripts/twitter/bootstrap/bootstrap.min.js +3 -3
  5. data/app/assets/stylesheets/twitter-bootstrap-static/bootstrap.css +3 -3
  6. data/lib/generators/bootstrap/agent/agent_generator.rb +12 -0
  7. data/lib/generators/bootstrap/agent/templates/guide.md.erb +23 -3
  8. data/lib/generators/bootstrap/bootstrap5_markup.rb +22 -0
  9. data/lib/generators/bootstrap/cdn/cdn_generator.rb +32 -2
  10. data/lib/generators/bootstrap/cdn_tags.rb +11 -0
  11. data/lib/generators/bootstrap/install/install_generator.rb +77 -21
  12. data/lib/generators/bootstrap/layout/layout_generator.rb +15 -0
  13. data/lib/generators/bootstrap/layout/templates/layout.html.erb +4 -1
  14. data/lib/generators/bootstrap/layout/templates/layout.html.haml +4 -1
  15. data/lib/generators/bootstrap/layout/templates/layout.html.slim +4 -1
  16. data/lib/generators/bootstrap/nes/nes_generator.rb +101 -0
  17. data/lib/generators/bootstrap/snippets/snippet_base.rb +4 -0
  18. data/lib/generators/bootstrap/stimulus/stimulus_generator.rb +49 -0
  19. data/lib/generators/bootstrap/stimulus/templates/bootstrap_controller.js.tt +67 -0
  20. data/lib/generators/bootstrap/themed/themed_generator.rb +4 -0
  21. data/lib/twitter/bootstrap/rails/config.rb +17 -4
  22. data/lib/twitter/bootstrap/rails/doctor.rb +125 -6
  23. data/lib/twitter/bootstrap/rails/version.rb +37 -5
  24. data/spec/lib/generators/cdn_generator_spec.rb +31 -0
  25. data/spec/lib/generators/doctor_generator_spec.rb +52 -0
  26. data/spec/lib/generators/install_generator_spec.rb +51 -1
  27. data/spec/lib/generators/layout_generator_spec.rb +24 -0
  28. data/spec/lib/generators/nes_generator_spec.rb +164 -0
  29. data/spec/lib/generators/no_hardcoded_versions_spec.rb +3 -1
  30. data/spec/lib/generators/stimulus_generator_spec.rb +65 -0
  31. data/spec/support/generator_spec_helper.rb +2 -0
  32. data/vendor/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js +3 -3
  33. data/vendor/assets/javascripts/twitter/bootstrap/bootstrap.esm.min.js +7 -0
  34. data/vendor/assets/javascripts/twitter/bootstrap/popper.esm.js +15 -0
  35. data/vendor/assets/stylesheets/twitter/bootstrap/bootstrap.min.css +3 -3
  36. metadata +28 -10
@@ -71,6 +71,18 @@ module Bootstrap
71
71
  Twitter::Bootstrap::Rails::V6Migration
72
72
  end
73
73
 
74
+ def nes?
75
+ facts[:asset_mode] == "nes"
76
+ end
77
+
78
+ def nes
79
+ Twitter::Bootstrap::Rails::BOOTSTRAP_NES
80
+ end
81
+
82
+ def v4_skill_command
83
+ "npx skills add twbs/bootstrap --skill bootstrap-v4-v6-migration"
84
+ end
85
+
74
86
  def pipeline_note
75
87
  case facts[:asset_pipeline]
76
88
  when "propshaft"
@@ -1,6 +1,12 @@
1
1
  # Bootstrap in this app
2
2
 
3
+ <% if nes? -%>
4
+ This app uses Bootstrap <%= facts[:bootstrap_version] %> from <%= nes[:name] %>: security-patched Bootstrap 4.6, installed with npm from HeroDevs' private registry and set up by the twitter-bootstrap-rails gem (<%= facts[:gem_version] %>).
5
+
6
+ **Write Bootstrap 4 markup.** Bootstrap 4 uses <%= legacy::DATA_ATTRIBUTES.first(3).map { |name| "`data-#{name}`" }.join(", ") %> and so on, not `data-bs-*`, and Bootstrap 4 classes. The gem's layout, themed and snippet generators and its view helpers write Bootstrap 5 markup, which doesn't work here, so don't use them in this app.
7
+ <% else -%>
3
8
  This app uses Bootstrap <%= facts[:bootstrap_version] %> through the twitter-bootstrap-rails gem (<%= facts[:gem_version] %>). Prefer the gem's generators and helpers over writing Bootstrap markup from memory, which is often out of date.
9
+ <% end -%>
4
10
 
5
11
  This guide is generated. Run `rails g bootstrap:agent` again after upgrading the gem or changing the asset setup.
6
12
 
@@ -9,6 +15,18 @@ This guide is generated. Run `rails g bootstrap:agent` again after upgrading the
9
15
  - Asset pipeline: <%= pipeline_note %>
10
16
  - Bootstrap assets: <%= facts[:asset_mode] %>
11
17
  - Template engine: <%= facts[:template_engine] %>
18
+ <% if nes? -%>
19
+
20
+ ## Workflow
21
+
22
+ 1. Run `rails g bootstrap:doctor --json` to check the NES setup: the package in `package.json`, and the registry and token in `.npmrc`. It writes nothing, and it exits 1 if a check reports an error.
23
+ 2. npm reads the access token from the `<%= nes[:token_env] %>` environment variable (or the name in `.npmrc`). Never write the token into a file.
24
+ 3. Bootstrap 4's JavaScript needs jQuery, imported before Bootstrap.
25
+
26
+ ## Moving off Bootstrap 4
27
+
28
+ NES keeps Bootstrap 4.6 patched, but the gem's tooling targets Bootstrap 5. To migrate, install the upstream skill with `<%= v4_skill_command %>` and follow it. To stop at Bootstrap 5, switch `package.json` back to the public `bootstrap` package, run `rails g bootstrap:install`, then use `rails g bootstrap:doctor` to find the Bootstrap 4 markup that needs changing.
29
+ <% else -%>
12
30
 
13
31
  ## Workflow
14
32
 
@@ -33,9 +51,10 @@ Snippets are always written as ERB partials. They render from any template engin
33
51
 
34
52
  ## Other generators
35
53
 
36
- - `bootstrap:install static` or `bootstrap:install cdn`: sets up Bootstrap's assets.
37
- - `bootstrap:layout [NAME]`: writes a layout that loads Bootstrap. Don't overwrite an existing layout without checking its contents first.
54
+ - `bootstrap:install static`, `bootstrap:install cdn` or `bootstrap:install importmap`: sets up Bootstrap's assets. `importmap` pins Bootstrap as an ES module, so `import { Tooltip } from "bootstrap"` works.
55
+ - `bootstrap:layout [NAME]`: writes a layout that loads Bootstrap, and keeps `javascript_importmap_tags` on importmap apps. Don't overwrite an existing layout without checking its contents first.
38
56
  - `bootstrap:themed RESOURCE`: writes Bootstrap views for a scaffold.
57
+ - `bootstrap:stimulus`: writes a Stimulus controller that sets up tooltips and popovers on each Turbo render and closes modals before Turbo caches a page. Attach it with `data-controller="bootstrap"` on `<body>`.
39
58
  - `bootstrap:cdn`: prints the CDN tags and writes nothing.
40
59
 
41
60
  ## View helpers
@@ -53,7 +72,7 @@ To move the app to Bootstrap 6:
53
72
 
54
73
  1. Install the upstream migration skill: `<%= v6::SKILL_COMMAND %>`. Follow it alongside this guide.
55
74
  2. Run `rails g bootstrap:doctor --v6 --json`. It lists every Bootstrap 5 class, data attribute, Sass import and JavaScript call that changes, with file, line and replacement, under `v6_migration`.
56
- 3. Switch the Bootstrap assets before changing markup, because the vendored files and `bootstrap:install` are Bootstrap 5. Re-running `bootstrap:layout`, `bootstrap:themed` or a snippet generator writes Bootstrap 5 markup again.
75
+ 3. Switch the Bootstrap assets before changing markup, because the vendored files and `bootstrap:install` are Bootstrap 5. `rails g bootstrap:cdn --bootstrap=6` prints the prerelease CDN tags. Re-running `bootstrap:layout`, `bootstrap:themed` or a snippet generator writes Bootstrap 5 markup again.
57
76
  4. The helpers in "View helpers" render Bootstrap 5 markup at runtime, so the scan can't see their output. Replace them with Bootstrap 6 markup in the views that use them.
58
77
  5. v6 changes the spacing scale (`p-3` is smaller), so check spacing utilities by eye; the scan doesn't flag them.
59
78
 
@@ -71,3 +90,4 @@ Data attributes: <%= legacy::DATA_ATTRIBUTES.map { |name| "`data-#{name}`" }.joi
71
90
  <% legacy::CLASS_PATTERNS.each do |_pattern, _replacement, example| -%>
72
91
  | <%= example.split(" become").first %> | <%= example.split(/ becomes? /).last %> |
73
92
  <% end -%>
93
+ <% end -%>
@@ -0,0 +1,22 @@
1
+ require 'twitter/bootstrap/rails/config'
2
+ require 'twitter/bootstrap/rails/version'
3
+
4
+ module Bootstrap
5
+ module Generators
6
+ # For generators that write Bootstrap 5 markup. On an app running HeroDevs
7
+ # Bootstrap NES 4.6 that markup doesn't work, so say so before writing it.
8
+ module Bootstrap5Markup
9
+ private
10
+
11
+ def warn_if_nes
12
+ return unless Twitter::Bootstrap::Rails.nes?
13
+
14
+ say_status :warn,
15
+ "This app runs Bootstrap #{Twitter::Bootstrap::Rails::BOOTSTRAP_NES[:bootstrap_version]} (NES), but this " \
16
+ "generator writes Bootstrap 5 markup, which needs data-bs-* attributes and Bootstrap 5 classes. " \
17
+ "Convert it to Bootstrap 4 markup, or migrate the app first.",
18
+ :yellow
19
+ end
20
+ end
21
+ end
22
+ end
@@ -17,21 +17,51 @@ module Bootstrap
17
17
  :desc => 'Print only the JavaScript bundle script'
18
18
  class_option :'separate-popper', :type => :boolean, :default => false,
19
19
  :desc => 'Print Popper and bootstrap.js as separate tags instead of the bundle'
20
+ class_option :bootstrap, :type => :string, :default => "5",
21
+ :desc => "Bootstrap major version: 5 (#{Twitter::Bootstrap::Rails::BOOTSTRAP_VERSION}) or 6 " \
22
+ "(the #{Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_VERSION} prerelease)"
23
+
24
+ def validate_version
25
+ unless %w[5 6].include?(options[:bootstrap])
26
+ raise ::Rails::Generators::Error,
27
+ "--bootstrap must be 5 or 6. For HeroDevs Bootstrap NES 4.6, run `rails g bootstrap:nes`."
28
+ end
29
+ return unless next_version? && options[:'separate-popper']
30
+
31
+ raise ::Rails::Generators::Error,
32
+ "--separate-popper does not apply to Bootstrap 6, whose bundle includes Floating UI."
33
+ end
20
34
 
21
35
  def print_tags
22
36
  say tags.join("\n")
23
37
  end
24
38
 
39
+ def warn_about_prerelease
40
+ return unless next_version?
41
+
42
+ say_status :warn,
43
+ "Bootstrap #{Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_VERSION} is a prerelease. This gem's generators and helpers " \
44
+ "write Bootstrap 5 markup, which needs migrating: run `rails g bootstrap:doctor --v6` for the list.",
45
+ :yellow
46
+ end
47
+
25
48
  private
26
49
 
50
+ def next_version?
51
+ options[:bootstrap] == "6"
52
+ end
53
+
27
54
  def tags
28
- return [css_tag] if options[:css] && !options[:js]
55
+ css = next_version? ? next_css_tag : css_tag
56
+ return [css] if options[:css] && !options[:js]
29
57
  return [javascript_tags] if options[:js] && !options[:css]
30
58
 
31
- [css_tag, javascript_tags]
59
+ [css, javascript_tags]
32
60
  end
33
61
 
34
62
  def javascript_tags
63
+ return next_bundle_tag if next_version?
64
+
35
65
  options[:'separate-popper'] ? separate_popper_tags : bundle_tag
36
66
  end
37
67
  end
@@ -23,6 +23,17 @@ module Bootstrap
23
23
  script_tag(:popper)
24
24
  end
25
25
 
26
+ # Bootstrap 6 ships only ES modules, so its bundle needs type="module".
27
+ def next_css_tag
28
+ entry = Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_CDN.fetch(:css)
29
+ %{<link href="#{entry[:url]}" rel="stylesheet" integrity="#{entry[:integrity]}" crossorigin="anonymous">}
30
+ end
31
+
32
+ def next_bundle_tag
33
+ entry = Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_CDN.fetch(:bundle)
34
+ %{<script type="module" src="#{entry[:url]}" integrity="#{entry[:integrity]}" crossorigin="anonymous"></script>}
35
+ end
36
+
26
37
  # Popper and bootstrap.js as the separate pair, in the docs' order.
27
38
  def separate_popper_tags
28
39
  [popper_tag, js_tag].join("\n")
@@ -12,22 +12,43 @@ module Bootstrap
12
12
 
13
13
  source_root File.expand_path("../templates", __FILE__)
14
14
  desc "This generator installs Bootstrap #{Twitter::Bootstrap::Rails::BOOTSTRAP_VERSION} to the Asset Pipeline"
15
+ MODES = %w[static cdn importmap].freeze
16
+
15
17
  argument :asset_mode, :type => :string, :default => 'static',
16
- :banner => 'static or cdn',
17
- :desc => 'static vendors Bootstrap into the app; cdn links to jsDelivr instead'
18
-
19
- VENDORED_ASSETS = [
20
- ['vendor/assets/stylesheets/twitter/bootstrap/bootstrap.min.css',
21
- 'app/assets/stylesheets/twitter/bootstrap/bootstrap.min.css'],
22
- ['vendor/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js',
23
- 'app/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js']
18
+ :banner => MODES.join(' | '),
19
+ :desc => 'static vendors Bootstrap into the asset pipeline; cdn links to jsDelivr; ' \
20
+ 'importmap vendors the stylesheet and pins Bootstrap as an ES module'
21
+
22
+ STYLESHEET = ['vendor/assets/stylesheets/twitter/bootstrap/bootstrap.min.css',
23
+ 'app/assets/stylesheets/twitter/bootstrap/bootstrap.min.css'].freeze
24
+ BUNDLE = ['vendor/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js',
25
+ 'app/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js'].freeze
26
+ # importmap-rails serves vendor/javascript, and these paths are what the pins name.
27
+ ES_MODULES = [
28
+ ['vendor/assets/javascripts/twitter/bootstrap/bootstrap.esm.min.js',
29
+ 'vendor/javascript/twitter/bootstrap/bootstrap.esm.min.js'],
30
+ ['vendor/assets/javascripts/twitter/bootstrap/popper.esm.js',
31
+ 'vendor/javascript/twitter/bootstrap/popper.esm.js']
24
32
  ].freeze
33
+ PINS = {
34
+ "bootstrap" => ["twitter/bootstrap/bootstrap.esm.min.js", "Bootstrap #{Twitter::Bootstrap::Rails::BOOTSTRAP_VERSION}"],
35
+ "@popperjs/core" => ["twitter/bootstrap/popper.esm.js", "Popper #{Twitter::Bootstrap::Rails::POPPER_VERSION}"]
36
+ }.freeze
37
+ IMPORTMAP = "config/importmap.rb".freeze
38
+ APPLICATION_JS = "app/javascript/application.js".freeze
25
39
 
26
40
  def validate_asset_mode
27
- return if %w[static cdn].include?(asset_mode)
41
+ unless MODES.include?(asset_mode)
42
+ raise ::Rails::Generators::Error,
43
+ "Unknown asset mode #{asset_mode.inspect}. Expected one of #{MODES.join(', ')}. " \
44
+ "For HeroDevs Bootstrap NES 4.6, run `rails g bootstrap:nes`."
45
+ end
46
+
47
+ return unless importmap? && !exists_in_app?(IMPORTMAP)
28
48
 
29
49
  raise ::Rails::Generators::Error,
30
- "Unknown asset mode #{asset_mode.inspect}. Expected 'static' or 'cdn'."
50
+ "importmap mode needs importmap-rails, but #{IMPORTMAP} does not exist. " \
51
+ "Run `bin/rails importmap:install` first, or use `bootstrap:install static`."
31
52
  end
32
53
 
33
54
  def add_assets
@@ -38,7 +59,9 @@ module Bootstrap
38
59
 
39
60
  js_manifest = 'app/assets/javascripts/application.js'
40
61
 
41
- if exists_in_app?(js_manifest)
62
+ if importmap?
63
+ # Bootstrap's JavaScript comes from the importmap, not the manifest.
64
+ elsif exists_in_app?(js_manifest)
42
65
  add_require(js_manifest,
43
66
  "//= require twitter/bootstrap/bootstrap.bundle.min\n",
44
67
  "application\n",
@@ -62,7 +85,8 @@ module Bootstrap
62
85
  def copy_bootstrap_assets
63
86
  return if cdn?
64
87
 
65
- VENDORED_ASSETS.each do |relative_source, destination|
88
+ vendored = [STYLESHEET] + (importmap? ? ES_MODULES : [BUNDLE])
89
+ vendored.each do |relative_source, destination|
66
90
  source = File.join(gem_root, relative_source)
67
91
 
68
92
  unless File.file?(source)
@@ -75,22 +99,46 @@ module Bootstrap
75
99
  end
76
100
  end
77
101
 
102
+ def pin_modules
103
+ return unless importmap?
104
+
105
+ existing = read_from_app(IMPORTMAP)
106
+ pins = PINS.reject { |name, _| existing.match?(/^\s*pin\s+["']#{Regexp.escape(name)}["']/) }
107
+ .map { |name, (path, label)| %{pin "#{name}", to: "#{path}" # #{label} via twitter-bootstrap-rails\n} }
108
+ append_to_file IMPORTMAP, "\n" + pins.join unless pins.empty?
109
+
110
+ if exists_in_app?(APPLICATION_JS)
111
+ unless read_from_app(APPLICATION_JS).match?(/^\s*import\s+(?:\*\s+as\s+\w+\s+from\s+)?["']bootstrap["']/)
112
+ append_to_file APPLICATION_JS, %{import "bootstrap"\n}
113
+ end
114
+ else
115
+ say_status :warn, "#{APPLICATION_JS} not found. Add `import \"bootstrap\"` to your JavaScript entry point.", :yellow
116
+ end
117
+ end
118
+
78
119
  def add_bootstrap
79
120
  return if cdn?
80
121
  return if propshaft?
81
122
 
82
- copy_file "bootstrap.js", "app/assets/javascripts/bootstrap.js"
123
+ copy_file "bootstrap.js", "app/assets/javascripts/bootstrap.js" unless importmap?
83
124
  copy_file "bootstrap_and_overrides.css", "app/assets/stylesheets/bootstrap_and_overrides.css"
84
125
  end
85
126
 
86
- def add_cdn_initializer
87
- return unless cdn?
88
-
89
- create_file "config/initializers/bootstrap.rb", <<~RUBY
90
- # Bootstrap's CSS and JS are loaded from jsDelivr rather than vendored into
91
- # this app. `rails g bootstrap:layout` reads this to emit CDN tags by default.
92
- Twitter::Bootstrap::Rails.asset_mode = :cdn
93
- RUBY
127
+ def add_mode_initializer
128
+ if cdn?
129
+ create_file "config/initializers/bootstrap.rb", <<~RUBY
130
+ # Bootstrap's CSS and JS are loaded from jsDelivr rather than vendored into
131
+ # this app. `rails g bootstrap:layout` reads this to emit CDN tags by default.
132
+ Twitter::Bootstrap::Rails.asset_mode = :cdn
133
+ RUBY
134
+ elsif importmap?
135
+ create_file "config/initializers/bootstrap.rb", <<~RUBY
136
+ # Bootstrap's stylesheet is vendored into the asset pipeline and its JavaScript
137
+ # is pinned in config/importmap.rb. `rails g bootstrap:layout` reads this to
138
+ # leave Bootstrap's JavaScript to the importmap.
139
+ Twitter::Bootstrap::Rails.asset_mode = :importmap
140
+ RUBY
141
+ end
94
142
  end
95
143
 
96
144
  def add_locale
@@ -111,6 +159,10 @@ module Bootstrap
111
159
  if cdn?
112
160
  say "Bootstrap #{version} will be loaded from jsDelivr. " \
113
161
  "Run `rails g bootstrap:layout` to generate a layout with the CDN tags."
162
+ elsif importmap?
163
+ say "Bootstrap #{version} pinned in #{IMPORTMAP} and imported from #{APPLICATION_JS}. " \
164
+ "Use `import { Tooltip } from \"bootstrap\"` in your own modules. " \
165
+ "Run `rails g bootstrap:layout` if your layout doesn't link the stylesheet yet."
114
166
  elsif propshaft?
115
167
  say "Bootstrap #{version} vendored for Propshaft. " \
116
168
  "Run `rails g bootstrap:layout` to generate a layout that links it."
@@ -125,6 +177,10 @@ module Bootstrap
125
177
  asset_mode == 'cdn'
126
178
  end
127
179
 
180
+ def importmap?
181
+ asset_mode == 'importmap'
182
+ end
183
+
128
184
  def propshaft?
129
185
  Twitter::Bootstrap::Rails.propshaft?
130
186
  end
@@ -2,11 +2,13 @@ require 'rails/generators'
2
2
  require 'twitter/bootstrap/rails/version'
3
3
  require 'twitter/bootstrap/rails/config'
4
4
  require 'generators/bootstrap/cdn_tags'
5
+ require 'generators/bootstrap/bootstrap5_markup'
5
6
 
6
7
  module Bootstrap
7
8
  module Generators
8
9
  class LayoutGenerator < ::Rails::Generators::Base
9
10
  include CdnTags
11
+ include Bootstrap5Markup
10
12
 
11
13
  source_root File.expand_path("../templates", __FILE__)
12
14
  desc "This generator generates a Bootstrap #{Twitter::Bootstrap::Rails::BOOTSTRAP_VERSION} layout file with navigation."
@@ -29,6 +31,7 @@ module Bootstrap
29
31
  end
30
32
 
31
33
  def generate_layout
34
+ warn_if_nes
32
35
  app = ::Rails.application
33
36
  @app_name = app.class.to_s.split("::").first
34
37
  ext = app.config.generators.options[:rails][:template_engine] || :erb
@@ -43,6 +46,18 @@ module Bootstrap
43
46
  Twitter::Bootstrap::Rails.cdn?
44
47
  end
45
48
 
49
+ # Rails 7+ apps load their own JavaScript (Turbo, Stimulus) through the
50
+ # importmap. A layout without these tags silently drops all of it.
51
+ def importmap_app?
52
+ File.exist?(File.join(destination_root, "config/importmap.rb"))
53
+ end
54
+
55
+ # In importmap mode Bootstrap's JavaScript is imported from application.js,
56
+ # so the layout links no script of its own for it.
57
+ def bootstrap_script?
58
+ cdn? || !Twitter::Bootstrap::Rails.importmap?
59
+ end
60
+
46
61
  def separate_popper?
47
62
  options[:'separate-popper'] && cdn?
48
63
  end
@@ -11,6 +11,9 @@
11
11
  <% else -%>
12
12
 
13
13
  <%%= stylesheet_link_tag <%= stylesheet_arguments %>, :media => "all" %>
14
+ <% end -%>
15
+ <% if importmap_app? -%>
16
+ <%%= javascript_importmap_tags %>
14
17
  <% end -%>
15
18
  </head>
16
19
  <body>
@@ -40,7 +43,7 @@
40
43
 
41
44
  <% if cdn? -%>
42
45
  <%= indent_tags(script_tags, " ") %>
43
- <% else -%>
46
+ <% elsif bootstrap_script? -%>
44
47
  <%%= javascript_include_tag <%= javascript_arguments %> %>
45
48
  <% end -%>
46
49
  </body>
@@ -10,6 +10,9 @@
10
10
  <% else -%>
11
11
  = stylesheet_link_tag <%= stylesheet_arguments %>, :media => "all"
12
12
  <% end -%>
13
+ <% if importmap_app? -%>
14
+ = javascript_importmap_tags
15
+ <% end -%>
13
16
 
14
17
  %body
15
18
  %nav.navbar.navbar-expand-lg.navbar-dark.bg-primary
@@ -30,6 +33,6 @@
30
33
 
31
34
  <% if cdn? -%>
32
35
  <%= indent_tags(script_tags, " ") %>
33
- <% else -%>
36
+ <% elsif bootstrap_script? -%>
34
37
  = javascript_include_tag <%= javascript_arguments %>
35
38
  <% end -%>
@@ -10,6 +10,9 @@ html lang="en"
10
10
  <% else -%>
11
11
  = stylesheet_link_tag <%= stylesheet_arguments %>, :media => "all"
12
12
  <% end -%>
13
+ <% if importmap_app? -%>
14
+ = javascript_importmap_tags
15
+ <% end -%>
13
16
 
14
17
  body
15
18
  nav.navbar.navbar-expand-lg.navbar-dark.bg-primary
@@ -30,6 +33,6 @@ html lang="en"
30
33
 
31
34
  <% if cdn? -%>
32
35
  <%= slim_tags(script_tags, " ") %>
33
- <% else -%>
36
+ <% elsif bootstrap_script? -%>
34
37
  = javascript_include_tag <%= javascript_arguments %>
35
38
  <% end -%>
@@ -0,0 +1,101 @@
1
+ require 'json'
2
+ require 'rails/generators'
3
+ require 'twitter/bootstrap/rails/version'
4
+
5
+ module Bootstrap
6
+ module Generators
7
+ # Points an npm-based app (jsbundling-rails / cssbundling-rails) at HeroDevs
8
+ # Bootstrap NES 4.6: security-patched Bootstrap 4 for apps that can't move to 5
9
+ # yet. NES is a paid product from a private registry, so nothing is vendored.
10
+ # The access token stays in an environment variable and never touches a file.
11
+ # Never prompts, so agents and CI can run it.
12
+ class NesGenerator < ::Rails::Generators::Base
13
+ NES = Twitter::Bootstrap::Rails::BOOTSTRAP_NES
14
+ PACKAGE_JSON = "package.json".freeze
15
+ NPMRC = ".npmrc".freeze
16
+
17
+ desc "Sets up HeroDevs Bootstrap NES #{NES[:bootstrap_version]} (paid, security-patched Bootstrap 4.6) through npm"
18
+
19
+ class_option :'token-env', :type => :string, :default => NES[:token_env],
20
+ :desc => 'Environment variable that holds the HeroDevs access token'
21
+
22
+ def require_package_json
23
+ return if File.exist?(app_path(PACKAGE_JSON))
24
+
25
+ raise ::Rails::Generators::Error,
26
+ "Bootstrap NES is installed with npm, but #{PACKAGE_JSON} does not exist. Set up jsbundling-rails or " \
27
+ "cssbundling-rails first (`bin/rails javascript:install:esbuild` or `css:install:bootstrap`)."
28
+ end
29
+
30
+ def update_package_json
31
+ json = JSON.parse(File.read(app_path(PACKAGE_JSON)))
32
+ (json["dependencies"] ||= {})["bootstrap"] = alias_spec
33
+ # The override makes packages that depend on bootstrap get NES too.
34
+ (json["overrides"] ||= {})["bootstrap"] = { "." => alias_spec }
35
+
36
+ create_file PACKAGE_JSON, JSON.pretty_generate(json) + "\n", :force => true
37
+ end
38
+
39
+ def update_npmrc
40
+ lines = File.exist?(app_path(NPMRC)) ? File.read(app_path(NPMRC)).lines.map(&:chomp) : []
41
+ lines << scope_line unless lines.any? { |line| line.start_with?("@neverendingsupport:registry=") }
42
+ lines << auth_line unless lines.any? { |line| line.start_with?("#{registry_key}:_authToken=") }
43
+
44
+ create_file NPMRC, lines.join("\n") + "\n", :force => true
45
+ end
46
+
47
+ def add_initializer
48
+ create_file "config/initializers/bootstrap.rb", <<~RUBY, :force => true
49
+ # Bootstrap comes from #{NES[:name]} #{NES[:bootstrap_version]} through npm
50
+ # (package.json and .npmrc). `rails g bootstrap:doctor` checks that setup, and the
51
+ # gem's generators warn that they write Bootstrap 5 markup.
52
+ Twitter::Bootstrap::Rails.asset_mode = :nes
53
+ RUBY
54
+ end
55
+
56
+ def next_steps
57
+ say <<~TEXT
58
+
59
+ #{NES[:name]} #{NES[:bootstrap_version]} is set up in #{PACKAGE_JSON} and #{NPMRC}. Next:
60
+
61
+ 1. Get an access token from HeroDevs (a paid NES subscription; see #{NES[:docs]}).
62
+ 2. Export it where npm runs, locally and in CI: export #{token_env}=...
63
+ #{NPMRC} reads it from that variable, so don't commit the token.
64
+ 3. Run `npm install` (or yarn / bun).
65
+ 4. Bootstrap 4's JavaScript needs jQuery; the bundle includes Popper 1. Import
66
+ jQuery before Bootstrap in your JavaScript entry point.
67
+
68
+ This gem's layout, themed and snippet generators and its view helpers write
69
+ Bootstrap 5 markup, which doesn't work on Bootstrap 4. Run `rails g bootstrap:doctor`
70
+ to check the NES setup, and `rails g bootstrap:agent` so coding agents know.
71
+ TEXT
72
+ end
73
+
74
+ private
75
+
76
+ def alias_spec
77
+ "npm:#{NES[:package]}@#{NES[:version]}"
78
+ end
79
+
80
+ def registry_key
81
+ NES[:registry].sub(%r{\Ahttps?:}, "")
82
+ end
83
+
84
+ def scope_line
85
+ "@neverendingsupport:registry=#{NES[:registry]}"
86
+ end
87
+
88
+ def auth_line
89
+ "#{registry_key}:_authToken=${#{token_env}}"
90
+ end
91
+
92
+ def token_env
93
+ options[:'token-env']
94
+ end
95
+
96
+ def app_path(relative)
97
+ File.join(destination_root, relative)
98
+ end
99
+ end
100
+ end
101
+ end
@@ -2,6 +2,7 @@ require 'json'
2
2
  require 'rails/generators'
3
3
  require 'twitter/bootstrap/rails/version'
4
4
  require 'generators/bootstrap/snippets/snippet_catalog'
5
+ require 'generators/bootstrap/bootstrap5_markup'
5
6
 
6
7
  module Bootstrap
7
8
  module Generators
@@ -9,6 +10,8 @@ module Bootstrap
9
10
  # bootstrap:hero, ...). Each subclass only declares which docs category it
10
11
  # serves; everything else lives here.
11
12
  class SnippetBase < ::Rails::Generators::Base
13
+ include Bootstrap5Markup
14
+
12
15
  ICONS_PARTIAL = 'bootstrap_icons'.freeze
13
16
 
14
17
  argument :requested_variant, :type => :string, :required => false,
@@ -53,6 +56,7 @@ module Bootstrap
53
56
  def copy_snippet
54
57
  return if listing?
55
58
 
59
+ warn_if_nes
56
60
  copy_file "#{category}/#{variant_name}.html.erb", partial_path
57
61
  end
58
62
 
@@ -0,0 +1,49 @@
1
+ require 'rails/generators'
2
+
3
+ module Bootstrap
4
+ module Generators
5
+ # Writes a Stimulus controller that sets up Bootstrap's tooltips and popovers
6
+ # when Turbo renders a page, and closes open components before Turbo caches
7
+ # it, so a cached page doesn't come back with a stuck modal or tooltip.
8
+ class StimulusGenerator < ::Rails::Generators::Base
9
+ CONTROLLERS = "app/javascript/controllers".freeze
10
+ IMPORTMAP = "config/importmap.rb".freeze
11
+
12
+ source_root File.expand_path("../templates", __FILE__)
13
+ desc "Writes a Stimulus controller that keeps Bootstrap's JavaScript working with Turbo"
14
+
15
+ def require_stimulus
16
+ return if File.directory?(File.join(destination_root, CONTROLLERS))
17
+
18
+ raise ::Rails::Generators::Error,
19
+ "#{CONTROLLERS} does not exist. Install Stimulus first (`bundle add stimulus-rails && bin/rails stimulus:install`)."
20
+ end
21
+
22
+ def write_controller
23
+ template "bootstrap_controller.js.tt", "#{CONTROLLERS}/bootstrap_controller.js"
24
+ end
25
+
26
+ def explain
27
+ say %(Add data-controller="bootstrap" to <body> in your layout, so it covers every page.)
28
+ return unless manual_registration?
29
+
30
+ say "Your controllers are registered by hand. Run `bin/rails stimulus:manifest:update`, " \
31
+ "or register BootstrapController in #{CONTROLLERS}/index.js."
32
+ end
33
+
34
+ private
35
+
36
+ # Importmap apps import the ES module build; everything else gets the
37
+ # global that the bundle (vendored or CDN) defines.
38
+ def import_bootstrap?
39
+ path = File.join(destination_root, IMPORTMAP)
40
+ File.exist?(path) && File.read(path).match?(/^\s*pin\s+["']bootstrap["']/)
41
+ end
42
+
43
+ def manual_registration?
44
+ index = File.join(destination_root, CONTROLLERS, "index.js")
45
+ File.exist?(index) && File.read(index).include?("application.register(")
46
+ end
47
+ end
48
+ end
49
+ end