Skip to content

Commit

Permalink
Merge pull request #1306 from G-Rath/use-package_json
Browse files Browse the repository at this point in the history
feat: support other js package managers
  • Loading branch information
justin808 authored Jan 6, 2024
2 parents 7f124c7 + d29a7a4 commit 4aa7fb1
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 25 deletions.
18 changes: 16 additions & 2 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
matrix:
ruby: [2.7]
runs-on: ubuntu-latest
env:
PACKAGE_JSON_FALLBACK_MANAGER: yarn_classic
steps:
- uses: actions/checkout@v4
with:
Expand Down Expand Up @@ -56,6 +58,15 @@ jobs:
strategy:
fail-fast: false
matrix:
js_package_manager:
- name: npm
installer: npm
- name: yarn_classic
installer: yarn
- name: pnpm
installer: pnpm
- name: bun
installer: bun
ruby: [2.7]
gemfile:
# These have shakapacker:
Expand All @@ -71,12 +82,14 @@ jobs:
# Workaround b/c upgrading Minitest broke some mocking expectations
# having to do with automatic kwarg splatting
MT_KWARGS_HACK: 1
PACKAGE_JSON_FALLBACK_MANAGER: ${{ matrix.js_package_manager.name }}
SHAKAPACKER_USE_PACKAGE_JSON_GEM: true
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: actions/setup-node@v3
- run: npm -g install yalc
- run: npm -g install yalc ${{ matrix.js_package_manager.installer }}
- run: yalc publish
- name: Save root node_modules to cache
uses: actions/cache@v3
Expand All @@ -92,14 +105,15 @@ jobs:
with:
bundler: 2.4.9
ruby-version: ${{ matrix.ruby }}
- run: ./test/bin/create-fake-js-package-managers ${{ matrix.js_package_manager.installer }}
- name: Save dummy app ruby gems to cache
uses: actions/cache@v3
with:
path: test/dummy/vendor/bundle
key: dummy-app-gem-cache-${{ hashFiles('${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile.lock') }}
- name: Install Ruby Gems for dummy app
run: bundle lock --add-platform 'x86_64-linux' && bundle check --path=test/dummy/vendor/bundle || bundle _2.4.9_ install --frozen --path=test/dummy/vendor/bundle --jobs=4 --retry=3
- run: cd test/dummy && yalc add react_ujs && yarn
- run: cd test/dummy && yalc add react_ujs && ${{ matrix.js_package_manager.installer }} install
- run: bundle exec rake test
env:
NODE_OPTIONS: --openssl-legacy-provider
2 changes: 1 addition & 1 deletion Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ end

appraise 'shakapacker' do
gem 'rails', '~> 7.0.x'
gem 'shakapacker', '7.0.2'
gem 'shakapacker', '7.2.0'
end

appraise 'base' do
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ _Please add entries here for your pull requests that are not yet released._

#### Changed
- Make es6 and ts usable at same time. #1299
- Support other JS package managers using `package_json` gem #1306

## [3.1.1] - 2023-08-16

Expand Down
7 changes: 2 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,16 @@ GEM
matrix (0.4.2)
method_source (1.0.0)
mini_mime (1.0.2)
mini_portile2 (2.8.4)
minitest (5.17.0)
nenv (0.3.0)
nokogiri (1.14.3)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
nokogiri (1.14.3-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.14.3-x86_64-linux)
racc (~> 1.4)
notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
package_json (0.1.0)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -166,7 +163,6 @@ GEM
zeitwerk (2.6.6)

PLATFORMS
ruby
x86_64-darwin-20
x86_64-linux

Expand All @@ -182,6 +178,7 @@ DEPENDENCIES
guard-minitest
jbuilder
listen (~> 3.0.0)
package_json
pry-byebug
react-rails!
selenium-webdriver
Expand Down
19 changes: 7 additions & 12 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,26 @@ end

Bundler::GemHelper.install_tasks

require "package_json"

def copy_react_asset(webpack_file, destination_file)
full_webpack_path = File.expand_path("../react-builds/build/#{webpack_file}", __FILE__)
full_destination_path = File.expand_path("../lib/assets/react-source/#{destination_file}", __FILE__)
FileUtils.cp(full_webpack_path, full_destination_path)
end

# Move to `dirname` and execute `yarn {cmd}`
def yarn_run_in(dirname, cmd)
Dir.chdir(dirname) do
`yarn #{cmd}`
end
end

namespace :react do
desc "Run the JS build process to put files in the gem source"
task update: %i[install build copy]

desc "Install the JavaScript dependencies"
task :install do
yarn_run_in("react-builds", "install")
PackageJson.read("react-builds").manager.install
end

desc "Build the JS bundles with Webpack"
task :build do
yarn_run_in("react-builds", "build")
PackageJson.read("react-builds").manager.run("build")
end

desc "Copy browser-ready JS files to the gem's asset paths"
Expand All @@ -51,12 +46,12 @@ namespace :ujs do

desc "Install the JavaScript dependencies"
task :install do
`yarn install`
PackageJson.read.manager.install
end

desc "Build the JS bundles with Webpack"
task :build do
`yarn build`
PackageJson.read.manager.run("build")
end

desc "Copy browser-ready JS files to the gem's asset paths"
Expand Down Expand Up @@ -87,7 +82,7 @@ task default: :test

task :test_setup do
Dir.chdir("./test/dummy") do
`yarn install`
PackageJson.read.manager.install
end
end

Expand Down
2 changes: 2 additions & 0 deletions gemfiles/base.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ GEM
notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
package_json (0.1.0)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -258,6 +259,7 @@ DEPENDENCIES
guard-minitest
jbuilder
listen (~> 3.0.0)
package_json
pry-byebug
rails (~> 7.0.x)
react-rails!
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/shakapacker.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
source "http://rubygems.org"

gem "rails", "~> 7.0.x"
gem "shakapacker", "7.0.2"
gem "shakapacker", "7.2.0"

gemspec path: "../"
9 changes: 6 additions & 3 deletions gemfiles/shakapacker.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ GEM
notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
package_json (0.1.0)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
Expand All @@ -185,7 +186,7 @@ GEM
public_suffix (5.0.1)
racc (1.6.2)
rack (2.2.7)
rack-proxy (0.7.6)
rack-proxy (0.7.7)
rack
rack-test (2.1.0)
rack (>= 1.3)
Expand Down Expand Up @@ -227,8 +228,9 @@ GEM
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
semantic_range (3.0.0)
shakapacker (7.0.2)
shakapacker (7.2.0)
activesupport (>= 5.2)
package_json
rack-proxy (>= 0.6.1)
railties (>= 5.2)
semantic_range (>= 2.3.0)
Expand Down Expand Up @@ -268,11 +270,12 @@ DEPENDENCIES
guard-minitest
jbuilder
listen (~> 3.0.0)
package_json
pry-byebug
rails (~> 7.0.x)
react-rails!
selenium-webdriver
shakapacker (= 7.0.2)
shakapacker (= 7.2.0)
test-unit (~> 2.5)

BUNDLED WITH
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/sprockets_3.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ GEM
notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
package_json (0.1.0)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
Expand Down Expand Up @@ -270,6 +271,7 @@ DEPENDENCIES
jbuilder
listen (~> 3.0.0)
mini_racer
package_json
pry-byebug
rails (~> 7.0.x)
react-rails!
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/sprockets_4.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ GEM
notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
package_json (0.1.0)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
Expand Down Expand Up @@ -270,6 +271,7 @@ DEPENDENCIES
jbuilder
listen (~> 3.0.0)
mini_racer
package_json
pry-byebug
rails (~> 7.0.x)
react-rails!
Expand Down
13 changes: 12 additions & 1 deletion lib/generators/react/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,19 @@ def setup_react_sprockets
ReactRailsUJS.useContext(componentRequireContext);
JS

def require_package_json_gem
require "bundler/inline"

gemfile(true) { gem "package_json" }

puts "using package_json v#{PackageJson::VERSION}"
end

def setup_react_shakapacker
`yarn add react_ujs`
require_package_json_gem

PackageJson.read.manager.add(["react_ujs"])

if manifest.exist?
append_file(manifest, SHAKAPACKER_SETUP_UJS)
else
Expand Down
1 change: 1 addition & 0 deletions react-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'selenium-webdriver'
s.add_development_dependency 'test-unit', '~> 2.5'
s.add_development_dependency 'pry-byebug'
s.add_development_dependency 'package_json'


s.add_dependency 'connection_pool'
Expand Down
36 changes: 36 additions & 0 deletions test/bin/create-fake-js-package-managers
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# creates a set of fake JavaScript package managers in a temporary bin
# directory for GitHub Actions, _excluding_ the one passed in as an
# argument in order to assert that only that package manager is used

require "tmpdir"

# setup the bin directory we want to use
bin_dir = Dir.mktmpdir("react-rails-")

if ENV["GITHUB_ACTIONS"]
puts "adding #{bin_dir} to GITHUB_PATH..."
File.write(ENV.fetch("GITHUB_PATH"), "#{bin_dir}\n", mode: "a+")
end

managers = %w[npm yarn pnpm bun]
manager_in_use = ARGV[0]

Dir.chdir(bin_dir) do
managers.each do |manager|
next if manager == manager_in_use

puts "creating #{bin_dir}/#{manager}..."
File.write(
manager,
<<~CONTENTS
#!/usr/bin/env node
throw new Error("(#{manager}) this is not the package manager you're looking...");
CONTENTS
)
File.chmod(0o755, manager)
end
end

0 comments on commit 4aa7fb1

Please sign in to comment.