Skip to content

Commit e2451ee

Browse files
authored
Isolate and namespace StimulusReflex::Installer (#699)
This pull request isolates the methods defined `lib/stimulus_reflex/installer.rb` into a `StimulusReflex::Installer` constant so they don't pollute the global namespace if the file is required. CableReady got a similar PR: stimulusreflex/cable_ready#295 ## Why should this be added Exposing all the methods from the installer in the global namespace could lead to issues down the road. Methods like `options` are likely to be overridden.
1 parent 004355b commit e2451ee

28 files changed

+612
-586
lines changed

lib/generators/stimulus_reflex/stimulus_reflex_generator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ def execute
2020

2121
template(reflex_src, reflex_path) unless options[:skip_reflex]
2222

23-
if !options[:skip_stimulus] && entrypoint.blank?
23+
if !options[:skip_stimulus] && StimulusReflex::Installer.entrypoint.blank?
2424
puts "❌ You must specify a valid JavaScript entrypoint."
2525
exit
2626
end
2727

2828
stimulus_controller_src = "app/javascript/controllers/%file_name%_controller.js.tt"
29-
stimulus_controller_path = Rails.root.join(entrypoint, "controllers/#{file_name}_controller.js")
29+
stimulus_controller_path = Rails.root.join(StimulusReflex::Installer.entrypoint, "controllers/#{file_name}_controller.js")
3030

3131
template(stimulus_controller_src, stimulus_controller_path) unless options[:skip_stimulus]
3232

lib/generators/stimulus_reflex/templates/app/javascript/config/cable_ready.js.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%- if bundler.importmap? -%>
1+
<%- if StimulusReflex::Installer.bundler.importmap? -%>
22
import consumer from "channels/consumer"
33
<%- else -%>
44
import consumer from "../channels/consumer"

lib/generators/stimulus_reflex/templates/app/javascript/config/index.js.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%- if bundler.importmap? -%>
1+
<%- if StimulusReflex::Installer.bundler.importmap? -%>
22
import "config/cable_ready"
33
import "config/stimulus_reflex"
44
<%- else -%>

lib/generators/stimulus_reflex/templates/app/javascript/config/stimulus_reflex.js.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%- if bundler.importmap? -%>
1+
<%- if StimulusReflex::Installer.bundler.importmap? -%>
22
import { application } from "controllers/application"
33
import controller from "controllers/application_controller"
44
<%- else -%>

lib/generators/stimulus_reflex/templates/app/javascript/controllers/%file_name%_controller.js.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%- if bundler.importmap? -%>
1+
<%- if StimulusReflex::Installer.bundler.importmap? -%>
22
import ApplicationController from "controllers/application_controller"
33
<%- else -%>
44
import ApplicationController from "./application_controller"

lib/generators/stimulus_reflex/templates/app/javascript/controllers/application.js.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Application } from "@hotwired/stimulus"
2-
<%- if bundler.importmap? %>
2+
<%- if StimulusReflex::Installer.bundler.importmap? %>
33
import consumer from "channels/consumer"
44
<%- else %>
55
import consumer from "../channels/consumer"

lib/install/action_cable.rb

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
if defined?(ActionCable::Engine)
77
say "⏩ ActionCable::Engine is already loaded and in scope. Skipping"
88
else
9-
halt "ActionCable::Engine is not loaded, please add or uncomment `require \"action_cable/engine\"` to your `config/application.rb`"
9+
StimulusReflex::Installer.halt "ActionCable::Engine is not loaded, please add or uncomment `require \"action_cable/engine\"` to your `config/application.rb`"
1010
return
1111
end
1212

13-
return if pack_path_missing?
13+
return if StimulusReflex::Installer.pack_path_missing?
1414

1515
# verify that the Action Cable pubsub config is created
1616
cable_config = Rails.root.join("config/cable.yml")
@@ -37,37 +37,37 @@
3737
"url" => "<%= ENV.fetch(\"REDIS_URL\") { \"redis://localhost:6379/1\" } %>",
3838
"channel_prefix" => "#{app_name}_development"
3939
}
40-
backup(cable_config) do
40+
StimulusReflex::Installer.backup(cable_config) do
4141
cable_config.write(yaml.to_yaml)
4242
end
4343
say "✅ config/cable.yml was updated to use the redis adapter in development"
4444
else
4545
say "🤷 config/cable.yml should use the redis adapter - or something like it - in development. You have something else specified, and we trust that you know what you're doing."
4646
end
4747

48-
if gemfile.match?(/gem ['"]redis['"]/)
48+
if StimulusReflex::Installer.gemfile.match?(/gem ['"]redis['"]/)
4949
say "⏩ redis gem is already present in Gemfile. Skipping."
5050
elsif Rails::VERSION::MAJOR >= 7
51-
add_gem "redis@~> 5"
51+
StimulusReflex::Installer.add_gem "redis@~> 5"
5252
else
53-
add_gem "redis@~> 4"
53+
StimulusReflex::Installer.add_gem "redis@~> 4"
5454
end
5555

5656
# install action-cable-redis-backport gem if using Action Cable < 7.1
5757
unless ActionCable::VERSION::MAJOR >= 7 && ActionCable::VERSION::MINOR >= 1
58-
if gemfile.match?(/gem ['"]action-cable-redis-backport['"]/)
58+
if StimulusReflex::Installer.gemfile.match?(/gem ['"]action-cable-redis-backport['"]/)
5959
say "⏩ action-cable-redis-backport gem is already present in Gemfile. Skipping."
6060
else
61-
add_gem "action-cable-redis-backport@~> 1"
61+
StimulusReflex::Installer.add_gem "action-cable-redis-backport@~> 1"
6262
end
6363
end
6464

6565
# verify that the Action Cable channels folder and consumer class is available
6666
step_path = "/app/javascript/channels/"
67-
channels_path = Rails.root.join(entrypoint, "channels")
68-
consumer_src = fetch(step_path, "consumer.js.tt")
67+
channels_path = Rails.root.join(StimulusReflex::Installer.entrypoint, "channels")
68+
consumer_src = StimulusReflex::Installer.fetch(step_path, "consumer.js.tt")
6969
consumer_path = channels_path / "consumer.js"
70-
index_src = fetch(step_path, "index.js.#{bundler}.tt")
70+
index_src = StimulusReflex::Installer.fetch(step_path, "index.js.#{StimulusReflex::Installer.bundler}.tt")
7171
index_path = channels_path / "index.js"
7272
friendly_index_path = index_path.relative_path_from(Rails.root).to_s
7373

@@ -79,7 +79,7 @@
7979
if index_path.read == index_src.read
8080
say "⏩ #{friendly_index_path} is already present. Skipping."
8181
else
82-
backup(index_path) do
82+
StimulusReflex::Installer.backup(index_path) do
8383
copy_file(index_src, index_path, verbose: false)
8484
end
8585
say "✅ #{friendly_index_path} has been updated"
@@ -92,45 +92,45 @@
9292
# import Action Cable channels into application pack
9393
channels_pattern = /import ['"](\.\.\/|\.\/)?channels['"]/
9494
channels_commented_pattern = /\s*\/\/\s*#{channels_pattern}/
95-
channel_import = "import \"#{prefix}channels\"\n"
95+
channel_import = "import \"#{StimulusReflex::Installer.prefix}channels\"\n"
9696

97-
if pack.match?(channels_pattern)
98-
if pack.match?(channels_commented_pattern)
99-
proceed = if options.key? "uncomment"
100-
options["uncomment"]
97+
if StimulusReflex::Installer.pack.match?(channels_pattern)
98+
if StimulusReflex::Installer.pack.match?(channels_commented_pattern)
99+
proceed = if StimulusReflex::Installer.options.key? "uncomment"
100+
StimulusReflex::Installer.options["uncomment"]
101101
else
102102
!no?("✨ Action Cable seems to be commented out in your application.js. Do you want to uncomment it? (Y/n)")
103103
end
104104

105105
if proceed
106106
# uncomment_lines only works with Ruby comments 🙄
107-
lines = pack_path.readlines
107+
lines = StimulusReflex::Installer.pack_path.readlines
108108
matches = lines.select { |line| line =~ channels_commented_pattern }
109109
lines[lines.index(matches.last).to_i] = channel_import
110-
pack_path.write lines.join
111-
say "✅ Uncommented channels import in #{friendly_pack_path}"
110+
StimulusReflex::Installer.pack_path.write lines.join
111+
say "✅ Uncommented channels import in #{StimulusReflex::Installer.friendly_pack_path}"
112112
else
113113
say "🤷 your Action Cable channels are not being imported in your application.js. We trust that you have a reason for this."
114114
end
115115
else
116-
say "⏩ channels are already being imported in #{friendly_pack_path}. Skipping."
116+
say "⏩ channels are already being imported in #{StimulusReflex::Installer.friendly_pack_path}. Skipping."
117117
end
118118
else
119-
lines = pack_path.readlines
119+
lines = StimulusReflex::Installer.pack_path.readlines
120120
matches = lines.select { |line| line =~ /^import / }
121121
lines.insert lines.index(matches.last).to_i + 1, channel_import
122-
pack_path.write lines.join
123-
say "✅ channels imported in #{friendly_pack_path}"
122+
StimulusReflex::Installer.pack_path.write lines.join
123+
say "✅ channels imported in #{StimulusReflex::Installer.friendly_pack_path}"
124124
end
125125

126126
# create working copy of Action Cable initializer in tmp
127-
if action_cable_initializer_path.exist?
128-
FileUtils.cp(action_cable_initializer_path, action_cable_initializer_working_path)
127+
if StimulusReflex::Installer.action_cable_initializer_path.exist?
128+
FileUtils.cp(StimulusReflex::Installer.action_cable_initializer_path, StimulusReflex::Installer.action_cable_initializer_working_path)
129129

130130
say "⏩ Action Cable initializer already exists. Skipping"
131131
else
132132
# create Action Cable initializer if it doesn't already exist
133-
create_file(action_cable_initializer_working_path, verbose: false) do
133+
create_file(StimulusReflex::Installer.action_cable_initializer_working_path, verbose: false) do
134134
<<~RUBY
135135
# frozen_string_literal: true
136136
@@ -140,10 +140,10 @@
140140
end
141141

142142
# silence notoriously chatty Action Cable logs
143-
if action_cable_initializer_working_path.read.match?(/^[^#]*ActionCable.server.config.logger/)
143+
if StimulusReflex::Installer.action_cable_initializer_working_path.read.match?(/^[^#]*ActionCable.server.config.logger/)
144144
say "⏩ Action Cable logger is already being silenced. Skipping"
145145
else
146-
append_file(action_cable_initializer_working_path, verbose: false) do
146+
append_file(StimulusReflex::Installer.action_cable_initializer_working_path, verbose: false) do
147147
<<~RUBY
148148
ActionCable.server.config.logger = Logger.new(nil)
149149
@@ -152,4 +152,4 @@
152152
say "✅ Action Cable logger silenced for performance and legibility"
153153
end
154154

155-
complete_step :action_cable
155+
StimulusReflex::Installer.complete_step :action_cable

lib/install/broadcaster.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def needs_broadcaster?(path)
1111
channel_path = Rails.root.join("app/channels/application_cable/channel.rb")
1212
controller_path = Rails.root.join("app/controllers/application_controller.rb")
1313
job_path = Rails.root.join("app/jobs/application_job.rb")
14-
model_path = Rails.root.join(application_record_path)
14+
model_path = Rails.root.join(StimulusReflex::Installer.application_record_path)
1515

1616
include_in_channel = needs_broadcaster?(channel_path)
1717
include_in_controller = needs_broadcaster?(controller_path)
@@ -21,20 +21,20 @@ def needs_broadcaster?(path)
2121
proceed = [include_in_channel, include_in_controller, include_in_job, include_in_model].reduce(:|)
2222

2323
unless proceed
24-
complete_step :broadcaster
24+
StimulusReflex::Installer.complete_step :broadcaster
2525

2626
puts "⏩ CableReady::Broadcaster already included in all files. Skipping."
2727
return
2828
end
2929

30-
proceed = if options.key? "broadcaster"
31-
options["broadcaster"]
30+
proceed = if StimulusReflex::Installer.options.key? "broadcaster"
31+
StimulusReflex::Installer.options["broadcaster"]
3232
else
3333
!no?("✨ Make CableReady::Broadcaster available to channels, controllers, jobs and models? (Y/n)")
3434
end
3535

3636
unless proceed
37-
complete_step :broadcaster
37+
StimulusReflex::Installer.complete_step :broadcaster
3838

3939
puts "⏩ Skipping."
4040
return
@@ -44,7 +44,7 @@ def needs_broadcaster?(path)
4444

4545
# include CableReady::Broadcaster in Action Cable Channel classes
4646
if include_in_channel
47-
backup(channel_path) do
47+
StimulusReflex::Installer.backup(channel_path) do
4848
inject_into_file channel_path, broadcaster_include, after: /class (ApplicationCable::)?Channel < ActionCable::Channel::Base/, verbose: false
4949
end
5050

@@ -55,7 +55,7 @@ def needs_broadcaster?(path)
5555

5656
# include CableReady::Broadcaster in Action Controller classes
5757
if include_in_controller
58-
backup(controller_path) do
58+
StimulusReflex::Installer.backup(controller_path) do
5959
inject_into_class controller_path, "ApplicationController", broadcaster_include, verbose: false
6060
end
6161

@@ -67,7 +67,7 @@ def needs_broadcaster?(path)
6767
# include CableReady::Broadcaster in Active Job classes, if present
6868

6969
if include_in_job
70-
backup(job_path) do
70+
StimulusReflex::Installer.backup(job_path) do
7171
inject_into_class job_path, "ApplicationJob", broadcaster_include, verbose: false
7272
end
7373

@@ -78,13 +78,13 @@ def needs_broadcaster?(path)
7878

7979
# include CableReady::Broadcaster in Active Record model classes
8080
if include_in_model
81-
backup(application_record_path) do
82-
inject_into_class application_record_path, "ApplicationRecord", broadcaster_include, verbose: false
81+
StimulusReflex::Installer.backup(StimulusReflex::Installer.application_record_path) do
82+
inject_into_class StimulusReflex::Installer.application_record_path, "ApplicationRecord", broadcaster_include, verbose: false
8383
end
8484

8585
puts "✅ include CableReady::Broadcaster in ApplicationRecord"
8686
else
8787
puts "⏩ Not including CableReady::Broadcaster in ApplicationRecord. Skipping"
8888
end
8989

90-
complete_step :broadcaster
90+
StimulusReflex::Installer.complete_step :broadcaster

lib/install/bundle.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
require "stimulus_reflex/installer"
44

5-
hash = gemfile_hash
5+
hash = StimulusReflex::Installer.gemfile_hash
66

77
# run bundle only when gems are waiting to be added or removed
8-
add = add_gem_list.exist? ? add_gem_list.readlines.map(&:chomp) : []
9-
remove = remove_gem_list.exist? ? remove_gem_list.readlines.map(&:chomp) : []
8+
add = StimulusReflex::Installer.add_gem_list.exist? ? StimulusReflex::Installer.add_gem_list.readlines.map(&:chomp) : []
9+
remove = StimulusReflex::Installer.remove_gem_list.exist? ? StimulusReflex::Installer.remove_gem_list.readlines.map(&:chomp) : []
1010

1111
if add.present? || remove.present?
12-
lines = gemfile_path.readlines
12+
lines = StimulusReflex::Installer.gemfile_path.readlines
1313

1414
remove.each do |name|
1515
index = lines.index { |line| line =~ /gem ['"]#{name}['"]/ }
@@ -40,17 +40,17 @@
4040
end
4141
end
4242

43-
gemfile_path.write lines.join
43+
StimulusReflex::Installer.gemfile_path.write lines.join
4444

45-
bundle_command("install --quiet", "BUNDLE_IGNORE_MESSAGES" => "1") if hash != gemfile_hash
45+
bundle_command("install --quiet", "BUNDLE_IGNORE_MESSAGES" => "1") if hash != StimulusReflex::Installer.gemfile_hash
4646
else
4747
say "⏩ No rubygems depedencies to install. Skipping."
4848
end
4949

50-
FileUtils.cp(development_working_path, development_path)
50+
FileUtils.cp(StimulusReflex::Installer.development_working_path, StimulusReflex::Installer.development_path)
5151
say "✅ development environment configuration installed"
5252

53-
FileUtils.cp(action_cable_initializer_working_path, action_cable_initializer_path)
53+
FileUtils.cp(StimulusReflex::Installer.action_cable_initializer_working_path, StimulusReflex::Installer.action_cable_initializer_path)
5454
say "✅ Action Cable initializer installed"
5555

56-
complete_step :bundle
56+
StimulusReflex::Installer.complete_step :bundle

lib/install/compression.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
require "stimulus_reflex/installer"
44

5-
initializer = action_cable_initializer_working_path.read
5+
initializer = StimulusReflex::Installer.action_cable_initializer_working_path.read
66

7-
if gemfile.match?(/gem ['"]permessage_deflate['"]/)
7+
if StimulusReflex::Installer.gemfile.match?(/gem ['"]permessage_deflate['"]/)
88
say "⏩ permessage_deflate already present in Gemfile. Skipping."
99
else
10-
add_gem "permessage_deflate@>= 0.1"
10+
StimulusReflex::Installer.add_gem "permessage_deflate@>= 0.1"
1111
end
1212

1313
# add permessage_deflate config to Action Cable initializer
1414
if initializer.exclude? "PermessageDeflate.configure"
15-
create_or_append(action_cable_initializer_working_path, verbose: false) do
15+
StimulusReflex::Installer.create_or_append(StimulusReflex::Installer.action_cable_initializer_working_path, verbose: false) do
1616
<<~RUBY
1717
module ActionCable
1818
module Connection
@@ -38,4 +38,4 @@ def initialize(env, event_target, event_loop, protocols)
3838
say "⏩ Action Cable initializer is already patched to deflate websocket traffic. Skipping."
3939
end
4040

41-
complete_step :compression
41+
StimulusReflex::Installer.complete_step :compression

0 commit comments

Comments
 (0)