forked from rails/tailwindcss-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.rb
104 lines (86 loc) · 3.55 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
require_relative "upstream"
module Tailwindcss
module Commands
# 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
class << self
def platform
[:cpu, :os].map { |m| Gem::Platform.local.send(m) }.join("-")
end
def executable(
exe_path: File.expand_path(File.join(__dir__, "..", "..", "exe"))
)
if Tailwindcss::Upstream::NATIVE_PLATFORMS.keys.none? { |p| Gem::Platform.match(Gem::Platform.new(p)) }
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_path = Dir.glob(File.expand_path(File.join(exe_path, "*", "tailwindcss"))).find do |f|
Gem::Platform.match(Gem::Platform.new(File.basename(File.dirname(f))))
end
if exe_path.nil?
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_path
end
def compile_command(debug: false, **kwargs)
[
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,
].tap do |command|
command << "--minify" unless (debug || rails_css_compressor?)
end
end
def watch_command(poll: false, **kwargs)
compile_command(**kwargs).tap do |command|
command << "-w"
command << "-p" if poll
end
end
def compile_file_command(file:, debug: false, **kwargs)
file = Pathname.new(file.to_s)
input = file
output = Rails.root.join("app/assets/builds", file.basename.sub("tailwind.", ""))
config = input.open(&:readline).start_with?("@config") ? nil : Rails.root.join("config/tailwind.config.js")
[
executable(**kwargs),
"-i", input.to_s,
"-o", output.to_s
].tap do |command|
command << "-c" if config
command << config.to_s if config
command << "--minify" unless debug
end
end
def watch_file_command(poll: false, **kwargs)
compile_file_command(**kwargs).tap do |command|
command << "-w"
command << "-p" if poll
end
end
def rails_css_compressor?
defined?(Rails) && Rails&.application&.config&.assets&.css_compressor.present?
end
end
end
end