Skip to content

Commit d86e74b

Browse files
authored
Merge pull request #466 from rails/flavorjones-v4-upgrade
Introduce a 'tailwindcss:upgrade' task
2 parents 629da53 + fef615c commit d86e74b

File tree

9 files changed

+184
-9
lines changed

9 files changed

+184
-9
lines changed

Diff for: .github/workflows/ci.yml

+17-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
- name: Run tests
3333
run: bin/test
3434

35-
user-journey:
35+
user-install:
3636
strategy:
3737
fail-fast: false
3838
matrix:
@@ -44,5 +44,20 @@ jobs:
4444
with:
4545
ruby-version: "3.4"
4646
bundler: latest
47-
- run: test/integration/user_journey_test.sh
47+
- run: test/integration/user_install_test.sh
48+
shell: bash
49+
50+
user-upgrade:
51+
strategy:
52+
fail-fast: false
53+
matrix:
54+
plat: ["ubuntu", "macos"] # TODO: on windows the tailwind upgrader tests are failing
55+
runs-on: ${{matrix.plat}}-latest
56+
steps:
57+
- uses: actions/checkout@v4
58+
- uses: ruby/setup-ruby@v1
59+
with:
60+
ruby-version: "3.4"
61+
bundler: latest
62+
- run: test/integration/user_upgrade_test.sh
4863
shell: bash

Diff for: .github/workflows/upstream.yml

+22-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ jobs:
3434
- name: Run tests
3535
run: bin/test
3636

37-
user-journey:
38-
name: "user-journey (rails ${{ matrix.ref }})"
37+
user-install:
38+
name: "user-install (rails ${{ matrix.ref }})"
3939
runs-on: ${{matrix.plat}}-latest
4040
strategy:
4141
fail-fast: false
@@ -50,5 +50,24 @@ jobs:
5050
with:
5151
ruby-version: "3.3"
5252
bundler: latest
53-
- run: test/integration/user_journey_test.sh
53+
- run: test/integration/user_install_test.sh
54+
shell: bash
55+
56+
user-upgrade:
57+
name: "user-upgrade (rails ${{ matrix.ref }})"
58+
runs-on: ${{matrix.plat}}-latest
59+
strategy:
60+
fail-fast: false
61+
matrix:
62+
plat: ["ubuntu"]
63+
ref: ["7-2-stable", "8-0-stable", "main"]
64+
env:
65+
RAILSOPTS: --git=https://github.com/rails/rails --ref=${{ matrix.ref }}
66+
steps:
67+
- uses: actions/checkout@v4
68+
- uses: ruby/setup-ruby@v1
69+
with:
70+
ruby-version: "3.3"
71+
bundler: latest
72+
- run: test/integration/user_upgrade_test.sh
5473
shell: bash

Diff for: CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This doc is a brief introduction on modifying and maintaining this gem.
99

1010
The unit tests are run with `bundle exec rake test`
1111

12-
There is an additional integration test which runs in CI, `test/integration/user_journey_test.sh` which you may also want to run.
12+
There is an additional integration test which runs in CI, `test/integration/user_install_test.sh` which you may also want to run.
1313

1414

1515
### Testing in a Rails app
File renamed without changes.

Diff for: lib/install/upgrade_tailwindcss.rb

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
TAILWIND_CONFIG_PATH = Rails.root.join("config/tailwind.config.js")
2+
APPLICATION_LAYOUT_PATH = Rails.root.join("app/views/layouts/application.html.erb")
3+
POSTCSS_CONFIG_PATH = Rails.root.join("config/postcss.config.js")
4+
5+
unless TAILWIND_CONFIG_PATH.exist?
6+
say "Default tailwind.config.js is missing!", :red
7+
abort
8+
end
9+
10+
if File.read(TAILWIND_CONFIG_PATH).match?(/defaultTheme/)
11+
say "Removing references to 'defaultTheme' from #{TAILWIND_CONFIG_PATH}"
12+
gsub_file TAILWIND_CONFIG_PATH.to_s, /^(.*defaultTheme)/, "// \\1"
13+
end
14+
15+
if POSTCSS_CONFIG_PATH.exist?
16+
say "Moving PostCSS configuration to application root directory"
17+
FileUtils.mv(POSTCSS_CONFIG_PATH, Rails.root, verbose: true) || abort
18+
end
19+
20+
if system("npx --version")
21+
say "Running the upstream Tailwind CSS upgrader"
22+
command = Shellwords.join(["npx", "@tailwindcss/upgrade@next", "--force", "--config", TAILWIND_CONFIG_PATH.to_s])
23+
success = run(command, abort_on_failure: false)
24+
unless success
25+
say "The upgrade tool failed!", :red
26+
say %( You probably need to update your configuration. Please read the error messages,)
27+
say %( and check the Tailwind CSS upgrade guide at https://tailwindcss.com/docs/upgrade-guide.)
28+
abort
29+
end
30+
else
31+
say "Could not run the Tailwind upgrade tool. Please see https://tailwindcss.com/docs/upgrade-guide for manual instructions.", :red
32+
abort
33+
end
34+
35+
if APPLICATION_LAYOUT_PATH.exist?
36+
if File.read(APPLICATION_LAYOUT_PATH).match?(/"inter-font"/)
37+
say "Strip Inter font CSS from application layout"
38+
gsub_file APPLICATION_LAYOUT_PATH.to_s, %r{, "inter-font"}, ""
39+
else
40+
say "Inter font CSS not detected.", :green
41+
end
42+
else
43+
say "Default application.html.erb is missing!", :red
44+
say %( Please check your layouts and remove any "inter-font" stylesheet links.)
45+
end
46+
47+
say "Compile initial Tailwind build"
48+
run "rails tailwindcss:build"

Diff for: lib/tasks/install.rake

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace :tailwindcss do
22
desc "Install Tailwind CSS into the app"
33
task :install do
4-
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/tailwindcss.rb", __dir__)}"
4+
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/install_tailwindcss.rb", __dir__)}"
55
end
66
end

Diff for: lib/tasks/upgrade.rake

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace :tailwindcss do
2+
desc "Upgrade app from Tailwind CSS v3 to v4"
3+
task :upgrade do
4+
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/upgrade_tailwindcss.rb", __dir__)}"
5+
end
6+
end

Diff for: test/integration/user_journey_test.sh renamed to test/integration/user_install_test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ pushd "My Workspace"
1818

1919
# create a rails app
2020
bundle exec rails -v
21-
bundle exec rails new test-app --skip-bundle
22-
pushd test-app
21+
bundle exec rails new test-install --skip-bundle
22+
pushd test-install
2323

2424
# make sure to use the same version of rails (e.g., install from git source if necessary)
2525
bundle remove rails --skip-install

Diff for: test/integration/user_upgrade_test.sh

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#! /usr/bin/env bash
2+
# reproduce the documented user journey for installing and running tailwindcss-rails
3+
# this is run in the CI pipeline, non-zero exit code indicates a failure
4+
5+
set -o pipefail
6+
set -eux
7+
8+
# set up dependencies
9+
rm -f Gemfile.lock
10+
bundle remove actionmailer
11+
bundle add rails --skip-install ${RAILSOPTS:-}
12+
bundle install --prefer-local
13+
14+
# do our work a directory with spaces in the name (#176, #184)
15+
rm -rf "My Workspace"
16+
mkdir "My Workspace"
17+
pushd "My Workspace"
18+
19+
# create a rails app
20+
bundle exec rails -v
21+
bundle exec rails new test-upgrade --skip-bundle
22+
pushd test-upgrade
23+
24+
# make sure to use the same version of rails (e.g., install from git source if necessary)
25+
bundle remove rails --skip-install
26+
bundle add rails --skip-install ${RAILSOPTS:-}
27+
28+
# set up app with tailwindcss-rails v3 and tailwindcss-ruby v3
29+
bundle add tailwindcss-rails --skip-install --version 3.3.0
30+
bundle add tailwindcss-ruby --skip-install --version 3.4.17
31+
bundle install --prefer-local
32+
bundle show --paths
33+
bundle binstubs --all
34+
35+
# install tailwindcss
36+
bin/rails tailwindcss:install
37+
grep -q inter-font app/views/layouts/application.html.erb
38+
39+
if [[ $(rails -v) > "Rails 8.0.0.beta" ]] ; then
40+
# install auth templates
41+
bin/rails generate authentication
42+
grep -q PasswordsController app/controllers/passwords_controller.rb
43+
fi
44+
45+
# install scaffold templates
46+
bin/rails generate scaffold post title:string body:text published:boolean
47+
grep -q "Show this post" app/views/posts/index.html.erb
48+
49+
# upgrade time!
50+
bundle remove tailwindcss-rails --skip-install
51+
bundle remove tailwindcss-ruby --skip-install
52+
53+
bundle add tailwindcss-rails --skip-install --path="../.."
54+
bundle add tailwindcss-ruby --skip-install --version 4.0.0
55+
56+
bundle install --prefer-local
57+
bundle show --paths
58+
bundle binstubs --all
59+
60+
# create a postcss file
61+
cat <<EOF > config/postcss.config.js
62+
module.exports = {
63+
plugins: {
64+
autoprefixer: {},
65+
},
66+
}
67+
EOF
68+
69+
bin/rails tailwindcss:upgrade
70+
71+
# TEST: removal of inter-font CSS
72+
if grep -q inter-font app/views/layouts/application.html.erb ; then
73+
echo "FAIL: inter-font CSS not removed"
74+
exit 1
75+
fi
76+
77+
# TEST: moving the postcss file
78+
if [ ! -f postcss.config.js ] ; then
79+
echo "FAIL: postcss.config.js not moved"
80+
exit 1
81+
fi
82+
83+
# generate CSS
84+
bin/rails tailwindcss:build[verbose]
85+
grep -q "py-2" app/assets/builds/tailwind.css
86+
87+
echo "OK"

0 commit comments

Comments
 (0)