Skip to content

Commit 6f9d19b

Browse files
committed
generator: auto-install Shakapacker if not installed
1 parent e131192 commit 6f9d19b

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

lib/generators/react_on_rails/install_generator.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class InstallGenerator < Rails::Generators::Base
2727

2828
def run_generators
2929
if installation_prerequisites_met? || options.ignore_warnings?
30+
ensure_shakapacker_installed
3031
invoke_generators
3132
add_bin_scripts
3233
add_post_install_message
@@ -97,6 +98,27 @@ def add_post_install_message
9798
GeneratorMessages.add_info(GeneratorMessages.helpful_message_after_installation)
9899
end
99100

101+
def ensure_shakapacker_installed
102+
return if shakapacker_installed?
103+
104+
GeneratorMessages.add_info("Shakapacker not detected. Installing Shakapacker...")
105+
106+
result = system("rails shakapacker:install")
107+
unless result
108+
GeneratorMessages.add_error("Failed to install Shakapacker automatically. Please run 'rails shakapacker:install' manually.")
109+
return
110+
end
111+
112+
GeneratorMessages.add_info("Shakapacker installed successfully!")
113+
end
114+
115+
def shakapacker_installed?
116+
Gem::Specification.find_by_name("shakapacker")
117+
true
118+
rescue Gem::MissingSpecError
119+
false
120+
end
121+
100122
def using_shakapacker_7_or_above?
101123
shakapacker_gem = Gem::Specification.find_by_name("shakapacker")
102124
shakapacker_gem.version.segments.first >= 7

spec/react_on_rails/generators/install_generator_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,45 @@
130130
expect(install_generator.send(:missing_npm?)).to be true
131131
end
132132
end
133+
134+
context "when detecting Shakapacker installation" do
135+
let(:install_generator) { described_class.new }
136+
137+
context "shakapacker_installed?" do
138+
specify "when Shakapacker gem is installed" do
139+
mock_gem = double("gem_spec", version: double("version", segments: [7, 0, 0]))
140+
allow(Gem::Specification).to receive(:find_by_name).with("shakapacker").and_return(mock_gem)
141+
expect(install_generator.send(:shakapacker_installed?)).to be true
142+
end
143+
144+
specify "when Shakapacker gem is not installed" do
145+
allow(Gem::Specification).to receive(:find_by_name).with("shakapacker").and_raise(Gem::MissingSpecError.new("gem", "spec"))
146+
expect(install_generator.send(:shakapacker_installed?)).to be false
147+
end
148+
end
149+
150+
context "ensure_shakapacker_installed" do
151+
specify "when Shakapacker is already installed" do
152+
allow(install_generator).to receive(:shakapacker_installed?).and_return(true)
153+
expect(install_generator).not_to receive(:system)
154+
install_generator.send(:ensure_shakapacker_installed)
155+
end
156+
157+
specify "when Shakapacker is not installed and install succeeds" do
158+
allow(install_generator).to receive(:shakapacker_installed?).and_return(false)
159+
allow(install_generator).to receive(:system).with("rails shakapacker:install").and_return(true)
160+
expect(GeneratorMessages).to receive(:add_info).with("Shakapacker not detected. Installing Shakapacker...")
161+
expect(GeneratorMessages).to receive(:add_info).with("Shakapacker installed successfully!")
162+
install_generator.send(:ensure_shakapacker_installed)
163+
end
164+
165+
specify "when Shakapacker is not installed and install fails" do
166+
allow(install_generator).to receive(:shakapacker_installed?).and_return(false)
167+
allow(install_generator).to receive(:system).with("rails shakapacker:install").and_return(false)
168+
expect(GeneratorMessages).to receive(:add_info).with("Shakapacker not detected. Installing Shakapacker...")
169+
expect(GeneratorMessages).to receive(:add_error).with("Failed to install Shakapacker automatically. Please run 'rails shakapacker:install' manually.")
170+
install_generator.send(:ensure_shakapacker_installed)
171+
end
172+
end
173+
end
133174
end

0 commit comments

Comments
 (0)