From d6b4d559bddc156b602eb6afa16a28e5de4e5112 Mon Sep 17 00:00:00 2001 From: Piper McCorkle Date: Wed, 29 Mar 2023 20:15:14 -0500 Subject: [PATCH] feat: allow overriding executable with environment variable --- lib/tailwindcss/commands.rb | 14 ++++++++++---- test/lib/tailwindcss/commands_test.rb | 7 +++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/tailwindcss/commands.rb b/lib/tailwindcss/commands.rb index 4c91428c..d303ea91 100644 --- a/lib/tailwindcss/commands.rb +++ b/lib/tailwindcss/commands.rb @@ -25,11 +25,17 @@ def executable( 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)))) + # Let the user override the executable path with an environment variable + # Useful for when running under Nix + exe_file = ENV["TAILWINDCSS_RAILS_EXECUTABLE_PATH"] + + if exe_file.nil? || exe_file.empty? + exe_file = 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 end - if exe_path.nil? + if exe_file.nil? || exe_file.empty? raise ExecutableNotFoundException, <<~MESSAGE Cannot find the tailwindcss executable for #{platform} in #{exe_path} @@ -52,7 +58,7 @@ def executable( MESSAGE end - exe_path + exe_file end def compile_command(debug: false, **kwargs) diff --git a/test/lib/tailwindcss/commands_test.rb b/test/lib/tailwindcss/commands_test.rb index ac8f9d05..2fd83b27 100644 --- a/test/lib/tailwindcss/commands_test.rb +++ b/test/lib/tailwindcss/commands_test.rb @@ -26,6 +26,13 @@ def mock_exe_directory(platform) end end + test ".executable accepts an override through the TAILWINDCSS_RAILS_EXECUTABLE_PATH environment variable" do + filename = "/nix/store/li4jwsd16y68yjyznbffaf98jccipg36-tailwindcss-3.2.7/bin/tailwindcss" + ENV["TAILWINDCSS_RAILS_EXECUTABLE_PATH"] = filename + assert_equal(filename, Tailwindcss::Commands.executable) + ENV["TAILWINDCSS_RAILS_EXECUTABLE_PATH"] = "" + end + test ".executable raises UnsupportedPlatformException when we're not on a supported platform" do Gem::Platform.stub(:match, false) do # nothing is supported assert_raises(Tailwindcss::Commands::UnsupportedPlatformException) do