Module: Spree::Admin::TailwindHelper

Defined in:
lib/spree/admin/tailwind_helper.rb

Class Method Summary collapse

Class Method Details

.engine_css_pathObject



17
18
19
# File 'lib/spree/admin/tailwind_helper.rb', line 17

def engine_css_path
  Spree::Admin::Engine.root.join("app/assets/tailwind")
end

.engine_sources(engine) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/spree/admin/tailwind_helper.rb', line 50

def engine_sources(engine)
  root = engine.root.to_s
  source_paths = [
    ["app/views/spree/admin", "**/*.erb"],
    ["app/helpers/spree/admin", "**/*.rb"],
    ["app/javascript/spree/admin", "**/*.js"]
  ]

  source_paths.filter_map do |dir, pattern|
    full_dir = File.join(root, dir)
    %(@source "#{full_dir}/#{pattern}";) if File.directory?(full_dir)
  end
end

.input_pathObject



5
6
7
# File 'lib/spree/admin/tailwind_helper.rb', line 5

def input_path
  Rails.root.join("app/assets/tailwind/spree_admin.css")
end

.output_pathObject



9
10
11
# File 'lib/spree/admin/tailwind_helper.rb', line 9

def output_path
  Rails.root.join("app/assets/builds/spree/admin/application.css")
end

.resolved_input_cssObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spree/admin/tailwind_helper.rb', line 21

def resolved_input_css
  css = File.read(input_path)
  css = css.gsub("$SPREE_ADMIN_PATH", Spree::Admin::Engine.root.to_s)
  css = css.gsub("/* $SPREE_ENGINE_SOURCES */", spree_engine_sources)

  # Resolve relative @source paths to absolute paths since the resolved
  # CSS is written to tmp/ which changes the relative path base
  source_base = input_path.dirname
  css = css.gsub(%r{@source\s+"(\.\./[^"]+)"}) do |_match|
    relative = Regexp.last_match(1)
    absolute = File.expand_path(relative, source_base)
    %(@source "#{absolute}")
  end

  css
end

.resolved_input_pathObject



13
14
15
# File 'lib/spree/admin/tailwind_helper.rb', line 13

def resolved_input_path
  Rails.root.join("tmp/tailwind/spree_admin_resolved.css")
end

.spree_engine_sourcesObject



38
39
40
41
42
# File 'lib/spree/admin/tailwind_helper.rb', line 38

def spree_engine_sources
  spree_engines.flat_map do |engine|
    engine_sources(engine)
  end.join("\n")
end

.spree_enginesObject



44
45
46
47
48
# File 'lib/spree/admin/tailwind_helper.rb', line 44

def spree_engines
  Rails::Engine.subclasses.select do |engine|
    engine.name&.start_with?("Spree::")
  end
end

.write_resolved_cssObject



64
65
66
67
68
# File 'lib/spree/admin/tailwind_helper.rb', line 64

def write_resolved_css
  FileUtils.mkdir_p(resolved_input_path.dirname)
  File.write(resolved_input_path, resolved_input_css)
  resolved_input_path
end