Skip to content

Implement Engines support #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: feature/engines-convention-over-configuration-support
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions lib/tailwindcss/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
module Tailwindcss
module Commands
class << self
def compile_command(debug: false, **kwargs)
def rails_root
defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd)
end

def compile_command(input = rails_root.join("app/assets/tailwind/application.css").to_s, debug: false, **kwargs)
debug = ENV["TAILWINDCSS_DEBUG"].present? if ENV.key?("TAILWINDCSS_DEBUG")
rails_root = defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd)

command = [
Tailwindcss::Ruby.executable(**kwargs),
"-i", rails_root.join("app/assets/tailwind/application.css").to_s,
"-i", input,
"-o", rails_root.join("app/assets/builds/tailwind.css").to_s,
]

Expand All @@ -21,8 +24,8 @@ def compile_command(debug: false, **kwargs)
command
end

def watch_command(always: false, poll: false, **kwargs)
compile_command(**kwargs).tap do |command|
def watch_command(input = rails_root.join("app/assets/tailwind/application.css").to_s, always: false, poll: false, **kwargs)
compile_command(input, **kwargs).tap do |command|
command << "-w"
command << "always" if always
command << "-p" if poll
Expand Down Expand Up @@ -57,19 +60,17 @@ def engines_tailwindcss_roots
end.compact
end

def enhance_command(command)
def with_dynamic_input
engine_roots = Tailwindcss::Commands.engines_tailwindcss_roots
if engine_roots.any?
Tempfile.create('tailwind.css') do |file|
file.write(engine_roots.map { |root| "@import \"#{root}\";" }.join("\n"))
file.write("\n@import \"#{Rails.root.join('app/assets/tailwind/application.css')}\";\n")
file.rewind
transformed_command = command.dup
transformed_command[2] = file.path
yield transformed_command if block_given?
yield file.path if block_given?
end
else
yield command if block_given?
yield rails_root.join("app/assets/tailwind/application.css").to_s if block_given?
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/tasks/build.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace :tailwindcss do
debug = args.extras.include?("debug")
verbose = args.extras.include?("verbose")

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

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

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

Expand Down
34 changes: 11 additions & 23 deletions test/lib/tailwindcss/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setup

test ".compile_command" do
Rails.stub(:root, File) do # Rails.root won't work in this test suite
actual = Tailwindcss::Commands.compile_command
actual = Tailwindcss::Commands.compile_command("app/assets/tailwind/application.css")
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
assert_includes(actual, "-i")
Expand Down Expand Up @@ -212,29 +212,25 @@ def setup
end
end

test ".enhance_command when there are no engines" do
test ".with_dynamic_input when there are no engines" do
Dir.mktmpdir do |tmpdir|
root = Pathname.new(tmpdir)
input_path = root.join("app/assets/tailwind/application.css")
output_path = root.join("app/assets/builds/tailwind.css")

command = ["tailwindcss", "-i", input_path.to_s, "-o", output_path.to_s]
input_path = root.join("app/assets/tailwind/application.css").to_s

Rails.stub(:root, root) do
Tailwindcss::Commands.stub(:engines_tailwindcss_roots, []) do
Tailwindcss::Commands.enhance_command(command) do |actual|
assert_equal command, actual
Tailwindcss::Commands.with_dynamic_input do |actual|
assert_equal input_path, actual
end
end
end
end
end

test ".enhance_command when there are engines" do
test ".with_dynamic_input when there are engines" do
Dir.mktmpdir do |tmpdir|
root = Pathname.new(tmpdir)
input_path = root.join("app/assets/tailwind/application.css")
output_path = root.join("app/assets/builds/tailwind.css")
input_path = root.join("app/assets/tailwind/application.css").to_s

# Create necessary files
FileUtils.mkdir_p(File.dirname(input_path))
Expand All @@ -245,24 +241,16 @@ def setup
FileUtils.mkdir_p(File.dirname(engine_css_path))
FileUtils.touch(engine_css_path)

command = ["tailwindcss", "-i", input_path.to_s, "-o", output_path.to_s]

Rails.stub(:root, root) do
Tailwindcss::Commands.stub(:engines_tailwindcss_roots, [engine_css_path.to_s]) do
Tailwindcss::Commands.enhance_command(command) do |actual|
# Command should be modified to use a temporary file
assert_equal command[0], actual[0] # executable
assert_equal command[1], actual[1] # -i flag
assert_equal command[3], actual[3] # -o flag
assert_equal command[4], actual[4] # output path

temp_path = Pathname.new(actual[2])
refute_equal command[2], temp_path.to_s # input path should be different
Tailwindcss::Commands.with_dynamic_input do |actual|
temp_path = Pathname.new(actual)
refute_equal input_path, temp_path.to_s # input path should be different
assert_match(/tailwind\.css/, temp_path.basename.to_s) # should use temp file
assert_includes [Dir.tmpdir, '/tmp'], temp_path.dirname.to_s # should be in temp directory

# Check temp file contents
temp_content = File.read(temp_path)
temp_content = File.read(actual)
expected_content = <<~CSS
@import "#{engine_css_path}";
@import "#{input_path}";
Expand Down