forked from rails/tailwindcss-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.rb
105 lines (84 loc) · 3.85 KB
/
commands.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require_relative "upstream"
module Tailwindcss
module Commands
DEFAULT_DIR = File.expand_path(File.join(__dir__, "..", "..", "exe"))
GEM_NAME = "tailwindcss-rails"
# raised when the host platform is not supported by upstream tailwindcss's binary releases
class UnsupportedPlatformException < StandardError
end
# raised when the tailwindcss executable could not be found where we expected it to be
class ExecutableNotFoundException < StandardError
end
# raised when TAILWINDCSS_INSTALL_DIR does not exist
class DirectoryNotFoundException < StandardError
end
class << self
def platform
[:cpu, :os].map { |m| Gem::Platform.local.send(m) }.join("-")
end
def executable(exe_path: DEFAULT_DIR)
tailwindcss_install_dir = ENV["TAILWINDCSS_INSTALL_DIR"]
if tailwindcss_install_dir
if File.directory?(tailwindcss_install_dir)
warn "NOTE: using TAILWINDCSS_INSTALL_DIR to find tailwindcss executable: #{tailwindcss_install_dir}"
exe_path = tailwindcss_install_dir
exe_file = File.expand_path(File.join(tailwindcss_install_dir, "tailwindcss"))
else
raise DirectoryNotFoundException, <<~MESSAGE
TAILWINDCSS_INSTALL_DIR is set to #{tailwindcss_install_dir}, but that directory does not exist.
MESSAGE
end
else
if Tailwindcss::Upstream::NATIVE_PLATFORMS.keys.none? { |p| Gem::Platform.match_gem?(Gem::Platform.new(p), GEM_NAME) }
raise UnsupportedPlatformException, <<~MESSAGE
tailwindcss-rails does not support the #{platform} platform
Please install tailwindcss following instructions at https://tailwindcss.com/docs/installation
MESSAGE
end
exe_file = Dir.glob(File.expand_path(File.join(exe_path, "*", "tailwindcss"))).find do |f|
Gem::Platform.match_gem?(Gem::Platform.new(File.basename(File.dirname(f))), GEM_NAME)
end
end
if exe_file.nil? || !File.exist?(exe_file)
raise ExecutableNotFoundException, <<~MESSAGE
Cannot find the tailwindcss executable for #{platform} in #{exe_path}
If you're using bundler, please make sure you're on the latest bundler version:
gem install bundler
bundle update --bundler
Then make sure your lock file includes this platform by running:
bundle lock --add-platform #{platform}
bundle install
See `bundle lock --help` output for details.
If you're still seeing this message after taking those steps, try running
`bundle config` and ensure `force_ruby_platform` isn't set to `true`. See
https://github.com/rails/tailwindcss-rails#check-bundle_force_ruby_platform
for more details.
MESSAGE
end
exe_file
end
def compile_command(debug: false, **kwargs)
command = [
executable(**kwargs),
"-i", Rails.root.join("app/assets/stylesheets/application.tailwind.css").to_s,
"-o", Rails.root.join("app/assets/builds/tailwind.css").to_s,
"-c", Rails.root.join("config/tailwind.config.js").to_s,
]
command << "--minify" unless (debug || rails_css_compressor?)
postcss_path = Rails.root.join("config/postcss.config.js")
command += ["--postcss", postcss_path.to_s] if File.exist?(postcss_path)
command
end
def watch_command(always: false, poll: false, **kwargs)
compile_command(**kwargs).tap do |command|
command << "-w"
command << "always" if always
command << "-p" if poll
end
end
def rails_css_compressor?
defined?(Rails) && Rails&.application&.config&.assets&.css_compressor.present?
end
end
end
end