From e507b0ee1b89f90b31f5d9f0b7cfd929c540d5ff Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Thu, 23 Jan 2025 12:26:15 -0500 Subject: [PATCH 1/3] prefactor: rename user_journey_test to user_install_test --- .github/workflows/ci.yml | 4 ++-- .github/workflows/upstream.yml | 6 +++--- CONTRIBUTING.md | 2 +- .../{user_journey_test.sh => user_install_test.sh} | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) rename test/integration/{user_journey_test.sh => user_install_test.sh} (96%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 07fb8285..b53627e4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,7 +32,7 @@ jobs: - name: Run tests run: bin/test - user-journey: + user-install: strategy: fail-fast: false matrix: @@ -44,5 +44,5 @@ jobs: with: ruby-version: "3.4" bundler: latest - - run: test/integration/user_journey_test.sh + - run: test/integration/user_install_test.sh shell: bash diff --git a/.github/workflows/upstream.yml b/.github/workflows/upstream.yml index e491a957..e38eac50 100644 --- a/.github/workflows/upstream.yml +++ b/.github/workflows/upstream.yml @@ -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 @@ -50,5 +50,5 @@ jobs: with: ruby-version: "3.3" bundler: latest - - run: test/integration/user_journey_test.sh + - run: test/integration/user_install_test.sh shell: bash diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a4040dfa..c2e8746f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 diff --git a/test/integration/user_journey_test.sh b/test/integration/user_install_test.sh similarity index 96% rename from test/integration/user_journey_test.sh rename to test/integration/user_install_test.sh index 985b72c4..dd36af25 100755 --- a/test/integration/user_journey_test.sh +++ b/test/integration/user_install_test.sh @@ -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 From 25010f3aecff8a2335e23749cd02143d29fa538c Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Thu, 23 Jan 2025 11:18:23 -0500 Subject: [PATCH 2/3] 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 --- .github/workflows/ci.yml | 15 ++++ .github/workflows/upstream.yml | 19 +++++ ...{tailwindcss.rb => install_tailwindcss.rb} | 0 lib/install/upgrade_tailwindcss.rb | 42 +++++++++++ lib/tasks/install.rake | 2 +- lib/tasks/upgrade.rake | 6 ++ test/integration/user_upgrade_test.sh | 72 +++++++++++++++++++ 7 files changed, 155 insertions(+), 1 deletion(-) rename lib/install/{tailwindcss.rb => install_tailwindcss.rb} (100%) create mode 100644 lib/install/upgrade_tailwindcss.rb create mode 100644 lib/tasks/upgrade.rake create mode 100755 test/integration/user_upgrade_test.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b53627e4..3822e414 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,3 +46,18 @@ jobs: bundler: latest - 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 diff --git a/.github/workflows/upstream.yml b/.github/workflows/upstream.yml index e38eac50..e1b12dea 100644 --- a/.github/workflows/upstream.yml +++ b/.github/workflows/upstream.yml @@ -52,3 +52,22 @@ jobs: bundler: latest - 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 diff --git a/lib/install/tailwindcss.rb b/lib/install/install_tailwindcss.rb similarity index 100% rename from lib/install/tailwindcss.rb rename to lib/install/install_tailwindcss.rb diff --git a/lib/install/upgrade_tailwindcss.rb b/lib/install/upgrade_tailwindcss.rb new file mode 100644 index 00000000..24dd4551 --- /dev/null +++ b/lib/install/upgrade_tailwindcss.rb @@ -0,0 +1,42 @@ +TAILWIND_CONFIG_PATH = Rails.root.join("config/tailwind.config.js") +APPLICATION_LAYOUT_PATH = Rails.root.join("app/views/layouts/application.html.erb") + +if TAILWIND_CONFIG_PATH.exist? + 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 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 +else + say "Default tailwind.config.js is missing!", :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" diff --git a/lib/tasks/install.rake b/lib/tasks/install.rake index 2a571cad..f94ec2a1 100644 --- a/lib/tasks/install.rake +++ b/lib/tasks/install.rake @@ -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 diff --git a/lib/tasks/upgrade.rake b/lib/tasks/upgrade.rake new file mode 100644 index 00000000..05047523 --- /dev/null +++ b/lib/tasks/upgrade.rake @@ -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 diff --git a/test/integration/user_upgrade_test.sh b/test/integration/user_upgrade_test.sh new file mode 100755 index 00000000..d2cc9d8f --- /dev/null +++ b/test/integration/user_upgrade_test.sh @@ -0,0 +1,72 @@ +#! /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 + +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 + +# generate CSS +bin/rails tailwindcss:build[verbose] +grep -q "py-2" app/assets/builds/tailwind.css + +echo "OK" From fef615c401e54cd9ae1a089c8f4c9381ab5e2bea Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Thu, 23 Jan 2025 14:06:58 -0500 Subject: [PATCH 3/3] If present, move config/postcss.config.js to root This is where the tailwind upgrade tool expects it to be, because that's the convention, apparently. --- lib/install/upgrade_tailwindcss.rb | 42 +++++++++++++++------------ test/integration/user_upgrade_test.sh | 15 ++++++++++ 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/lib/install/upgrade_tailwindcss.rb b/lib/install/upgrade_tailwindcss.rb index 24dd4551..b5389c73 100644 --- a/lib/install/upgrade_tailwindcss.rb +++ b/lib/install/upgrade_tailwindcss.rb @@ -1,28 +1,34 @@ 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") -if TAILWIND_CONFIG_PATH.exist? - 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 +unless TAILWIND_CONFIG_PATH.exist? + say "Default tailwind.config.js is missing!", :red + 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 +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 "Default tailwind.config.js is missing!", :red + say "Could not run the Tailwind upgrade tool. Please see https://tailwindcss.com/docs/upgrade-guide for manual instructions.", :red abort end diff --git a/test/integration/user_upgrade_test.sh b/test/integration/user_upgrade_test.sh index d2cc9d8f..e4912818 100755 --- a/test/integration/user_upgrade_test.sh +++ b/test/integration/user_upgrade_test.sh @@ -57,6 +57,15 @@ bundle install --prefer-local bundle show --paths bundle binstubs --all +# create a postcss file +cat < config/postcss.config.js +module.exports = { + plugins: { + autoprefixer: {}, + }, +} +EOF + bin/rails tailwindcss:upgrade # TEST: removal of inter-font CSS @@ -65,6 +74,12 @@ if grep -q inter-font app/views/layouts/application.html.erb ; then 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