-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid polluting Object with admin's tailwind rake task helper methods
Methods defined within the `namespace` block of a rake task are defined on `Object` and are thus available everywhere. We fix it by extracting them to a module. We're also renaming the `#compile_file` method to `#compile_to_tempfile`.
- Loading branch information
1 parent
37080f3
commit 01779d0
Showing
2 changed files
with
76 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require "tailwindcss-rails" | ||
|
||
module SolidusAdmin | ||
# @api private | ||
module Tailwindcss | ||
module_function | ||
|
||
def run(args = "") | ||
config_file = compile_to_tempfile( | ||
[config_app_path, config_engine_path].find(&:exist?), | ||
"tailwind.config.js" | ||
) | ||
stylesheet_file = compile_to_tempfile( | ||
[stylesheet_app_path, stylesheet_engine_path].find(&:exist?), | ||
"application.tailwind.css" | ||
) | ||
|
||
system "#{::Tailwindcss::Engine.root.join("exe/tailwindcss")} \ | ||
-i #{stylesheet_file.path} \ | ||
-o #{Rails.root.join("app/assets/builds/solidus_admin/tailwind.css")} \ | ||
-c #{config_file.path} \ | ||
#{args}" | ||
ensure | ||
config_file&.close && config_file&.unlink | ||
stylesheet_file&.close && stylesheet_file&.unlink | ||
end | ||
|
||
def config_app_path | ||
Rails.root.join("config", "solidus_admin", "tailwind.config.js.erb") | ||
end | ||
|
||
def config_engine_path | ||
SolidusAdmin::Engine.root.join("config", "solidus_admin", "tailwind.config.js.erb") | ||
end | ||
|
||
def stylesheet_app_path | ||
Rails.root.join("app", "assets", "stylesheets", "solidus_admin", "application.tailwind.css.erb") | ||
end | ||
|
||
def stylesheet_engine_path | ||
SolidusAdmin::Engine.root.join("app", "assets", "stylesheets", "solidus_admin", "application.tailwind.css.erb") | ||
end | ||
|
||
def compile_to_tempfile(path, name) | ||
Tempfile.new(name).tap do |file| | ||
path | ||
.then { |path| File.read(path) } | ||
.then { |content| ERB.new(content) } | ||
.then { |erb| erb.result } | ||
.then { |compiled_content| file.write(compiled_content) && file.rewind } | ||
end | ||
end | ||
|
||
def copy_file(src, dst) | ||
FileUtils.mkdir_p(File.dirname(dst)) | ||
FileUtils.cp(src, dst) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters