Skip to content

Commit

Permalink
Fix bin/setup modification failure in installation (shakacode#229)
Browse files Browse the repository at this point in the history
* Fix bin/setup modification failure

Due to different content of bin/setup in different Rails versions and
installation date, the earlier version of the generator couldn't modify
this file properly in all circumstances.

This commit handles the following scenarios:
- If there is a commented line for `system('bin/yarn')`, uncomment it.
- Otherwise, regardless of using single or double quotations in
  different Rails versions, add `system! 'bin/yarn'` after the intended
  line.
  • Loading branch information
ahangarha authored Jan 31, 2023
1 parent b9397de commit 9deec04
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Changes since last non-beta release.

_Please add entries here for your pull requests that are not yet released._
### Fixed
- Fixed failing to update `bin/setup` file due to different formats of the file in different versions of Rails. [PR229](https://github.com/shakacode/shakapacker/pull/229) by [ahangarha](https://github.com/ahangarha)

### Fixed
- Upgrade several JS dependencies to fix security issues. [PR 243](https://github.com/shakacode/shakapacker/pull/243) by [ahangarha](https://github.com/ahangarha).
Expand Down
24 changes: 22 additions & 2 deletions lib/install/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,33 @@
say %( Add <%= javascript_pack_tag "application" %> within the <head> tag in your custom layout.)
end

# Ensure there is `system!("bin/yarn")` command in `./bin/setup` file
if (setup_path = Rails.root.join("bin/setup")).exist?
say "Run bin/yarn during bin/setup"
insert_into_file setup_path.to_s, <<-RUBY, after: %( system("bundle check") || system!("bundle install")\n)

if File.read(setup_path).match? Regexp.escape(" # system('bin/yarn')\n")
gsub_file(setup_path, "# system('bin/yarn')", "system!('bin/yarn')")
else
# Due to the inconsistency of quotation usage in Rails 7 compared to
# earlier versions, we check both single and double quotations here.
pattern = /system\(['"]bundle check['"]\) \|\| system!\(['"]bundle install['"]\)\n/

string_to_add = <<-RUBY
# Install JavaScript dependencies
system! "bin/yarn"
system!("bin/yarn")
RUBY

if File.read(setup_path).match? pattern
insert_into_file(setup_path, string_to_add, after: pattern)
else
say <<~MSG, :red
It seems your `bin/setup` file doesn't have the expected content.
Please review the file and manually add `system!("bin/yarn")` before any
other command that requires JavaScript dependencies being already installed.
MSG
end
end
end

results = []
Expand Down

0 comments on commit 9deec04

Please sign in to comment.