Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a 'tailwindcss:upgrade' task #466

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Run tests
run: bin/test

user-journey:
user-install:
strategy:
fail-fast: false
matrix:
Expand All @@ -44,5 +44,20 @@ jobs:
with:
ruby-version: "3.4"
bundler: latest
- run: test/integration/user_journey_test.sh
- run: test/integration/user_install_test.sh
shell: bash

user-upgrade:
strategy:
fail-fast: false
matrix:
plat: ["ubuntu", "macos"] # TODO: on windows the tailwind upgrader tests are failing
runs-on: ${{matrix.plat}}-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: "3.4"
bundler: latest
- run: test/integration/user_upgrade_test.sh
shell: bash
25 changes: 22 additions & 3 deletions .github/workflows/upstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ jobs:
- name: Run tests
run: bin/test

user-journey:
name: "user-journey (rails ${{ matrix.ref }})"
user-install:
name: "user-install (rails ${{ matrix.ref }})"
runs-on: ${{matrix.plat}}-latest
strategy:
fail-fast: false
Expand All @@ -50,5 +50,24 @@ jobs:
with:
ruby-version: "3.3"
bundler: latest
- run: test/integration/user_journey_test.sh
- run: test/integration/user_install_test.sh
shell: bash

user-upgrade:
name: "user-upgrade (rails ${{ matrix.ref }})"
runs-on: ${{matrix.plat}}-latest
strategy:
fail-fast: false
matrix:
plat: ["ubuntu"]
ref: ["7-2-stable", "8-0-stable", "main"]
env:
RAILSOPTS: --git=https://github.com/rails/rails --ref=${{ matrix.ref }}
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: "3.3"
bundler: latest
- run: test/integration/user_upgrade_test.sh
shell: bash
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This doc is a brief introduction on modifying and maintaining this gem.

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

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


### Testing in a Rails app
Expand Down
File renamed without changes.
48 changes: 48 additions & 0 deletions lib/install/upgrade_tailwindcss.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
TAILWIND_CONFIG_PATH = Rails.root.join("config/tailwind.config.js")
APPLICATION_LAYOUT_PATH = Rails.root.join("app/views/layouts/application.html.erb")
POSTCSS_CONFIG_PATH = Rails.root.join("config/postcss.config.js")

unless TAILWIND_CONFIG_PATH.exist?
say "Default tailwind.config.js is missing!", :red
abort
end

if File.read(TAILWIND_CONFIG_PATH).match?(/defaultTheme/)
say "Removing references to 'defaultTheme' from #{TAILWIND_CONFIG_PATH}"
gsub_file TAILWIND_CONFIG_PATH.to_s, /^(.*defaultTheme)/, "// \\1"
end

if POSTCSS_CONFIG_PATH.exist?
say "Moving PostCSS configuration to application root directory"
FileUtils.mv(POSTCSS_CONFIG_PATH, Rails.root, verbose: true) || abort
end

if system("npx --version")
say "Running the upstream Tailwind CSS upgrader"
command = Shellwords.join(["npx", "@tailwindcss/upgrade@next", "--force", "--config", TAILWIND_CONFIG_PATH.to_s])
success = run(command, abort_on_failure: false)
unless success
say "The upgrade tool failed!", :red
say %( You probably need to update your configuration. Please read the error messages,)
say %( and check the Tailwind CSS upgrade guide at https://tailwindcss.com/docs/upgrade-guide.)
abort
end
else
say "Could not run the Tailwind upgrade tool. Please see https://tailwindcss.com/docs/upgrade-guide for manual instructions.", :red
abort
end

if APPLICATION_LAYOUT_PATH.exist?
if File.read(APPLICATION_LAYOUT_PATH).match?(/"inter-font"/)
say "Strip Inter font CSS from application layout"
gsub_file APPLICATION_LAYOUT_PATH.to_s, %r{, "inter-font"}, ""
else
say "Inter font CSS not detected.", :green
end
else
say "Default application.html.erb is missing!", :red
say %( Please check your layouts and remove any "inter-font" stylesheet links.)
end

say "Compile initial Tailwind build"
run "rails tailwindcss:build"
2 changes: 1 addition & 1 deletion lib/tasks/install.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace :tailwindcss do
desc "Install Tailwind CSS into the app"
task :install do
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/tailwindcss.rb", __dir__)}"
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/install_tailwindcss.rb", __dir__)}"
end
end
6 changes: 6 additions & 0 deletions lib/tasks/upgrade.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace :tailwindcss do
desc "Upgrade app from Tailwind CSS v3 to v4"
task :upgrade do
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/upgrade_tailwindcss.rb", __dir__)}"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pushd "My Workspace"

# create a rails app
bundle exec rails -v
bundle exec rails new test-app --skip-bundle
pushd test-app
bundle exec rails new test-install --skip-bundle
pushd test-install

# make sure to use the same version of rails (e.g., install from git source if necessary)
bundle remove rails --skip-install
Expand Down
87 changes: 87 additions & 0 deletions test/integration/user_upgrade_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#! /usr/bin/env bash
# reproduce the documented user journey for installing and running tailwindcss-rails
# this is run in the CI pipeline, non-zero exit code indicates a failure

set -o pipefail
set -eux

# set up dependencies
rm -f Gemfile.lock
bundle remove actionmailer
bundle add rails --skip-install ${RAILSOPTS:-}
bundle install --prefer-local

# do our work a directory with spaces in the name (#176, #184)
rm -rf "My Workspace"
mkdir "My Workspace"
pushd "My Workspace"

# create a rails app
bundle exec rails -v
bundle exec rails new test-upgrade --skip-bundle
pushd test-upgrade

# make sure to use the same version of rails (e.g., install from git source if necessary)
bundle remove rails --skip-install
bundle add rails --skip-install ${RAILSOPTS:-}

# set up app with tailwindcss-rails v3 and tailwindcss-ruby v3
bundle add tailwindcss-rails --skip-install --version 3.3.0
bundle add tailwindcss-ruby --skip-install --version 3.4.17
bundle install --prefer-local
bundle show --paths
bundle binstubs --all

# install tailwindcss
bin/rails tailwindcss:install
grep -q inter-font app/views/layouts/application.html.erb

if [[ $(rails -v) > "Rails 8.0.0.beta" ]] ; then
# install auth templates
bin/rails generate authentication
grep -q PasswordsController app/controllers/passwords_controller.rb
fi

# install scaffold templates
bin/rails generate scaffold post title:string body:text published:boolean
grep -q "Show this post" app/views/posts/index.html.erb

# upgrade time!
bundle remove tailwindcss-rails --skip-install
bundle remove tailwindcss-ruby --skip-install

bundle add tailwindcss-rails --skip-install --path="../.."
bundle add tailwindcss-ruby --skip-install --version 4.0.0

bundle install --prefer-local
bundle show --paths
bundle binstubs --all

# create a postcss file
cat <<EOF > config/postcss.config.js
module.exports = {
plugins: {
autoprefixer: {},
},
}
EOF

bin/rails tailwindcss:upgrade

# TEST: removal of inter-font CSS
if grep -q inter-font app/views/layouts/application.html.erb ; then
echo "FAIL: inter-font CSS not removed"
exit 1
fi

# TEST: moving the postcss file
if [ ! -f postcss.config.js ] ; then
echo "FAIL: postcss.config.js not moved"
exit 1
fi

# generate CSS
bin/rails tailwindcss:build[verbose]
grep -q "py-2" app/assets/builds/tailwind.css

echo "OK"
Loading