Skip to content

Commit 22e1a01

Browse files
authored
error messages on unsupported platforms or when bundler platforms aren't correct (#102)
* prefactor: expose constants related to upstream tailwindcss The following constants have been moved from rakelib/package.rake into lib/tailwindcss/upstream.rb to be accessible from within the installed gem: - TAILWINDCSS_VERSION → Tailwindcss::Upstream::VERSION - TAILWINDCSS_NATIVE_PLATFORMS -> Tailwindcss::Upstream::NATIVE_PLATFORMS * fix: provide more helpful error messages See #101 and comments in #96 for examples of what's confusing users.
1 parent 3fdd00f commit 22e1a01

File tree

6 files changed

+64
-24
lines changed

6 files changed

+64
-24
lines changed

.github/workflows/gem-install.yml

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jobs:
55
strategy:
66
fail-fast: false
77
matrix:
8-
platform: ["x64-mingw32", "x86_64-darwin", "x86_64-linux"]
8+
platform: ["ruby", "x64-mingw32", "x86_64-darwin", "x86_64-linux"]
99
runs-on: ubuntu-latest
1010
steps:
1111
- uses: actions/checkout@v2
@@ -21,6 +21,20 @@ jobs:
2121
path: pkg
2222
retention-days: 1
2323

24+
vanilla-install:
25+
needs: ["package"]
26+
runs-on: ubuntu-latest
27+
steps:
28+
- uses: ruby/setup-ruby@v1
29+
with:
30+
ruby-version: "3.0"
31+
- uses: actions/download-artifact@v2
32+
with:
33+
name: gem-ruby
34+
path: pkg
35+
- run: "gem install pkg/tailwindcss-rails-*.gem"
36+
- run: "tailwindcss 2>&1 | fgrep 'ERROR: Cannot find the tailwindcss executable'"
37+
2438
linux-install:
2539
needs: ["package"]
2640
runs-on: ubuntu-latest

exe/tailwindcss

+28-8
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,37 @@
22
# because rubygems shims assume a gem's executables are Ruby
33

44
require "shellwords"
5+
require "tailwindcss/upstream"
56

6-
platform_dir = Dir.glob(File.join(__dir__, "*")).select do |f|
7-
File.directory?(f) && Gem::Platform.match(File.basename(f))
8-
end.first
9-
if platform_dir.nil?
10-
raise "Cannot find the tailwindcss executable in #{__dir__} (1)"
7+
supported_platforms = Tailwindcss::Upstream::NATIVE_PLATFORMS.keys
8+
9+
if supported_platforms.none? { |supported_platform| Gem::Platform.match(supported_platform) }
10+
STDERR.puts(<<~ERRMSG)
11+
ERROR: tailwindcss-rails does not support the #{::Gem::Platform.local} platform
12+
Please install tailwindcss following instructions at https://tailwindcss.com/docs/installation
13+
ERRMSG
14+
exit 1
15+
end
16+
17+
exe_path = Dir.glob(File.join(__dir__, "*", "tailwindcss")).find do |f|
18+
Gem::Platform.match(File.basename(File.dirname(f)))
1119
end
20+
if exe_path.nil?
21+
STDERR.puts(<<~ERRMSG)
22+
ERROR: Cannot find the tailwindcss executable for #{::Gem::Platform.local} in #{__dir__}
23+
If you're using bundler, please make sure you're on the latest bundler version:
24+
25+
gem install bundler
26+
bundle update --bundler
27+
28+
Then make sure your lock file includes this platform by running:
29+
30+
bundle lock --add-platform #{::Gem::Platform.local}
31+
bundle install
1232
13-
exe_path = File.join(platform_dir, "tailwindcss")
14-
if !File.exist?(exe_path)
15-
raise "Cannot find the tailwindcss executable in #{__dir__} (2)"
33+
See `bundle lock --help` output for details.
34+
ERRMSG
35+
exit 1
1636
end
1737

1838
command = Shellwords.join([exe_path, ARGV].flatten)

lib/tailwindcss-rails.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Tailwindcss
22
end
33

4+
require "tailwindcss/upstream"
45
require "tailwindcss/version"
56
require "tailwindcss/engine"

lib/tailwindcss/upstream.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Tailwindcss
2+
# constants describing the upstream tailwindcss project
3+
module Upstream
4+
VERSION = "v3.0.5"
5+
6+
# rubygems platform name => upstream release filename
7+
NATIVE_PLATFORMS = {
8+
"arm64-darwin" => "tailwindcss-macos-arm64",
9+
"x64-mingw32" => "tailwindcss-windows-x64.exe",
10+
"x86_64-darwin" => "tailwindcss-macos-x64",
11+
"x86_64-linux" => "tailwindcss-linux-x64",
12+
}
13+
end
14+
end

rakelib/package.rake

+5-15
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#
55
# TL;DR: run "rake package"
66
#
7-
# The native platform gems (defined by TAILWINDCSS_NATIVE_PLATFORMS below) will each contain two
8-
# files in addition to what the vanilla ruby gem contains:
7+
# The native platform gems (defined by Tailwindcss::Upstream::NATIVE_PLATFORMS) will each contain
8+
# two files in addition to what the vanilla ruby gem contains:
99
#
1010
# exe/
1111
# ├── tailwindcss # generic ruby script to find and run the binary
@@ -56,19 +56,10 @@
5656
#
5757
require "rubygems/package_task"
5858
require "open-uri"
59-
60-
TAILWINDCSS_VERSION = "v3.0.5" # string used to generate the download URL
61-
62-
# rubygems platform name => upstream release filename
63-
TAILWINDCSS_NATIVE_PLATFORMS = {
64-
"arm64-darwin" => "tailwindcss-macos-arm64",
65-
"x64-mingw32" => "tailwindcss-windows-x64.exe",
66-
"x86_64-darwin" => "tailwindcss-macos-x64",
67-
"x86_64-linux" => "tailwindcss-linux-x64",
68-
}
59+
require_relative "../lib/tailwindcss/upstream"
6960

7061
def tailwindcss_download_url(filename)
71-
"https://github.com/tailwindlabs/tailwindcss/releases/download/#{TAILWINDCSS_VERSION}/#{filename}"
62+
"https://github.com/tailwindlabs/tailwindcss/releases/download/#{Tailwindcss::Upstream::VERSION}/#{filename}"
7263
end
7364

7465
TAILWINDCSS_RAILS_GEMSPEC = Bundler.load_gemspec("tailwindcss-rails.gemspec")
@@ -78,15 +69,14 @@ desc "Build the ruby gem"
7869
task "gem:ruby" => [gem_path]
7970

8071
exepaths = []
81-
TAILWINDCSS_NATIVE_PLATFORMS.each do |platform, filename|
72+
Tailwindcss::Upstream::NATIVE_PLATFORMS.each do |platform, filename|
8273
TAILWINDCSS_RAILS_GEMSPEC.dup.tap do |gemspec|
8374
exedir = File.join(gemspec.bindir, platform) # "exe/x86_64-linux"
8475
exepath = File.join(exedir, "tailwindcss") # "exe/x86_64-linux/tailwindcss"
8576
exepaths << exepath
8677

8778
# modify a copy of the gemspec to include the native executable
8879
gemspec.platform = platform
89-
gemspec.executables << "tailwindcss"
9080
gemspec.files += [exepath, "LICENSE-DEPENDENCIES"]
9181

9282
# create a package task

tailwindcss-rails.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Gem::Specification.new do |spec|
1616

1717
spec.files = Dir["{app,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]
1818
spec.bindir = "exe"
19+
spec.executables << "tailwindcss"
1920

2021
spec.add_dependency "railties", ">= 6.0.0"
2122
end

0 commit comments

Comments
 (0)