Skip to content

Commit 65ad81c

Browse files
committed
Introduce a 'tailwindcss:upgrade' task
- rename install/tailwindcss.rb to install/install_tailwindcss.rb - new script install/upgrade_tailwindcss.rb which: - removes the "inter-font" CSS tag from the application layout - comments out references to 'defaultTheme' in tailwind.config.js - runs "npx @tailwindcss/upgrade@next" if npx is available - new integration test
1 parent e507b0e commit 65ad81c

File tree

7 files changed

+155
-1
lines changed

7 files changed

+155
-1
lines changed

.github/workflows/ci.yml

+15
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,18 @@ jobs:
4646
bundler: latest
4747
- run: test/integration/user_install_test.sh
4848
shell: bash
49+
50+
user-upgrade:
51+
strategy:
52+
fail-fast: false
53+
matrix:
54+
plat: ["ubuntu", "windows", "macos"]
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
63+
shell: bash

.github/workflows/upstream.yml

+19
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,22 @@ jobs:
5252
bundler: latest
5353
- run: test/integration/user_install_test.sh
5454
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
73+
shell: bash
File renamed without changes.

lib/install/upgrade_tailwindcss.rb

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

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

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

test/integration/user_upgrade_test.sh

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
bin/rails tailwindcss:upgrade
61+
62+
# TEST: removal of inter-font CSS
63+
if grep -q inter-font app/views/layouts/application.html.erb ; then
64+
echo "FAIL: inter-font CSS not removed"
65+
exit 1
66+
fi
67+
68+
# generate CSS
69+
bin/rails tailwindcss:build[verbose]
70+
grep -q "py-2" app/assets/builds/tailwind.css
71+
72+
echo "OK"

0 commit comments

Comments
 (0)