Skip to content

Commit 588b6a4

Browse files
committed
Implement with_dynamic_input
1 parent 1025445 commit 588b6a4

File tree

3 files changed

+26
-37
lines changed

3 files changed

+26
-37
lines changed

lib/tailwindcss/commands.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
module Tailwindcss
44
module Commands
55
class << self
6-
def compile_command(debug: false, **kwargs)
6+
def rails_root
7+
defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd)
8+
end
9+
10+
def compile_command(input = rails_root.join("app/assets/tailwind/application.css").to_s, debug: false, **kwargs)
711
debug = ENV["TAILWINDCSS_DEBUG"].present? if ENV.key?("TAILWINDCSS_DEBUG")
8-
rails_root = defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd)
912

1013
command = [
1114
Tailwindcss::Ruby.executable(**kwargs),
12-
"-i", rails_root.join("app/assets/tailwind/application.css").to_s,
15+
"-i", input,
1316
"-o", rails_root.join("app/assets/builds/tailwind.css").to_s,
1417
]
1518

@@ -21,8 +24,8 @@ def compile_command(debug: false, **kwargs)
2124
command
2225
end
2326

24-
def watch_command(always: false, poll: false, **kwargs)
25-
compile_command(**kwargs).tap do |command|
27+
def watch_command(input = rails_root.join("app/assets/tailwind/application.css").to_s, always: false, poll: false, **kwargs)
28+
compile_command(input, **kwargs).tap do |command|
2629
command << "-w"
2730
command << "always" if always
2831
command << "-p" if poll
@@ -57,19 +60,17 @@ def engines_tailwindcss_roots
5760
end.compact
5861
end
5962

60-
def enhance_command(command)
63+
def with_dynamic_input
6164
engine_roots = Tailwindcss::Commands.engines_tailwindcss_roots
6265
if engine_roots.any?
6366
Tempfile.create('tailwind.css') do |file|
6467
file.write(engine_roots.map { |root| "@import \"#{root}\";" }.join("\n"))
6568
file.write("\n@import \"#{Rails.root.join('app/assets/tailwind/application.css')}\";\n")
6669
file.rewind
67-
transformed_command = command.dup
68-
transformed_command[2] = file.path
69-
yield transformed_command if block_given?
70+
yield file.path if block_given?
7071
end
7172
else
72-
yield command if block_given?
73+
yield rails_root.join("app/assets/tailwind/application.css").to_s if block_given?
7374
end
7475
end
7576
end

lib/tasks/build.rake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ namespace :tailwindcss do
44
debug = args.extras.include?("debug")
55
verbose = args.extras.include?("verbose")
66

7-
command = Tailwindcss::Commands.compile_command(debug: debug)
8-
Tailwindcss::Commands.enhance_command(command) do |transformed_command|
7+
Tailwindcss::Commands.with_dynamic_input do |input|
8+
command = Tailwindcss::Commands.compile_command(input, debug: debug)
99
env = Tailwindcss::Commands.command_env(verbose: verbose)
1010
puts "Running: #{Shellwords.join(command)}" if verbose
1111

@@ -20,8 +20,8 @@ namespace :tailwindcss do
2020
always = args.extras.include?("always")
2121
verbose = args.extras.include?("verbose")
2222

23-
command = Tailwindcss::Commands.watch_command(always: always, debug: debug, poll: poll)
24-
Tailwindcss::Commands.enhance_command(command) do |transformed_command|
23+
Tailwindcss::Commands.with_dynamic_input do |input|
24+
command = Tailwindcss::Commands.watch_command(input, always: always, debug: debug, poll: poll)
2525
env = Tailwindcss::Commands.command_env(verbose: verbose)
2626
puts "Running: #{Shellwords.join(command)}" if verbose
2727

test/lib/tailwindcss/commands_test.rb

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def setup
1111

1212
test ".compile_command" do
1313
Rails.stub(:root, File) do # Rails.root won't work in this test suite
14-
actual = Tailwindcss::Commands.compile_command
14+
actual = Tailwindcss::Commands.compile_command("app/assets/tailwind/application.css")
1515
assert_kind_of(Array, actual)
1616
assert_equal(executable, actual.first)
1717
assert_includes(actual, "-i")
@@ -212,29 +212,25 @@ def setup
212212
end
213213
end
214214

215-
test ".enhance_command when there are no engines" do
215+
test ".with_dynamic_input when there are no engines" do
216216
Dir.mktmpdir do |tmpdir|
217217
root = Pathname.new(tmpdir)
218-
input_path = root.join("app/assets/tailwind/application.css")
219-
output_path = root.join("app/assets/builds/tailwind.css")
220-
221-
command = ["tailwindcss", "-i", input_path.to_s, "-o", output_path.to_s]
218+
input_path = root.join("app/assets/tailwind/application.css").to_s
222219

223220
Rails.stub(:root, root) do
224221
Tailwindcss::Commands.stub(:engines_tailwindcss_roots, []) do
225-
Tailwindcss::Commands.enhance_command(command) do |actual|
226-
assert_equal command, actual
222+
Tailwindcss::Commands.with_dynamic_input do |actual|
223+
assert_equal input_path, actual
227224
end
228225
end
229226
end
230227
end
231228
end
232229

233-
test ".enhance_command when there are engines" do
230+
test ".with_dynamic_input when there are engines" do
234231
Dir.mktmpdir do |tmpdir|
235232
root = Pathname.new(tmpdir)
236-
input_path = root.join("app/assets/tailwind/application.css")
237-
output_path = root.join("app/assets/builds/tailwind.css")
233+
input_path = root.join("app/assets/tailwind/application.css").to_s
238234

239235
# Create necessary files
240236
FileUtils.mkdir_p(File.dirname(input_path))
@@ -245,24 +241,16 @@ def setup
245241
FileUtils.mkdir_p(File.dirname(engine_css_path))
246242
FileUtils.touch(engine_css_path)
247243

248-
command = ["tailwindcss", "-i", input_path.to_s, "-o", output_path.to_s]
249-
250244
Rails.stub(:root, root) do
251245
Tailwindcss::Commands.stub(:engines_tailwindcss_roots, [engine_css_path.to_s]) do
252-
Tailwindcss::Commands.enhance_command(command) do |actual|
253-
# Command should be modified to use a temporary file
254-
assert_equal command[0], actual[0] # executable
255-
assert_equal command[1], actual[1] # -i flag
256-
assert_equal command[3], actual[3] # -o flag
257-
assert_equal command[4], actual[4] # output path
258-
259-
temp_path = Pathname.new(actual[2])
260-
refute_equal command[2], temp_path.to_s # input path should be different
246+
Tailwindcss::Commands.with_dynamic_input do |actual|
247+
temp_path = Pathname.new(actual)
248+
refute_equal input_path, temp_path.to_s # input path should be different
261249
assert_match(/tailwind\.css/, temp_path.basename.to_s) # should use temp file
262250
assert_includes [Dir.tmpdir, '/tmp'], temp_path.dirname.to_s # should be in temp directory
263251

264252
# Check temp file contents
265-
temp_content = File.read(temp_path)
253+
temp_content = File.read(actual)
266254
expected_content = <<~CSS
267255
@import "#{engine_css_path}";
268256
@import "#{input_path}";

0 commit comments

Comments
 (0)