Class: Avo::TailwindVariablesPreparer

Inherits:
Object
  • Object
show all
Defined in:
lib/avo/tailwind_variables_preparer.rb

Class Method Summary collapse

Class Method Details

.copy_unless_identical(source, target) ⇒ Object



73
74
75
76
77
# File 'lib/avo/tailwind_variables_preparer.rb', line 73

def self.copy_unless_identical(source, target)
  return if File.exist?(target) && File.identical?(source, target)

  FileUtils.cp(source, target)
end

.prepare(target:, label:, avo_gem_path: nil, cwd: Dir.pwd) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/avo/tailwind_variables_preparer.rb', line 6

def self.prepare(target:, label:, avo_gem_path: nil, cwd: Dir.pwd)
  source = resolve_source(avo_gem_path:, cwd:)
  unless source
    raise "[#{label}] Could not resolve Avo variables.css source."
  end

  target_dir = File.dirname(target)
  begin
    FileUtils.mkdir_p(target_dir)
  rescue Errno::EACCES
    warn "[#{label}] prebuild_css: cannot create #{target_dir} (permission denied). Skipping."
    return true
  end

  if File.exist?(target) || File.symlink?(target)
    begin
      return true if File.identical?(source, target)
    rescue
      # ignore and continue
    end

    begin
      File.delete(target)
    rescue Errno::EACCES
      warn "[#{label}] prebuild_css: cannot update #{target} (permission denied). Skipping."
      return true
    end
  end

  begin
    File.symlink(source, target)
  rescue Errno::EEXIST
    begin
      return true if File.identical?(source, target)
      File.delete(target)
      File.symlink(source, target)
    rescue
      copy_unless_identical(source, target)
    end
  rescue
    copy_unless_identical(source, target)
  end

  true
end

.resolve_source(avo_gem_path: nil, cwd: Dir.pwd) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/avo/tailwind_variables_preparer.rb', line 52

def self.resolve_source(avo_gem_path: nil, cwd: Dir.pwd)
  roots = []
  roots << avo_gem_path if avo_gem_path
  roots << File.expand_path("../avo", cwd)
  roots << `bundle show avo 2>/dev/null`.strip

  begin
    roots << Gem::Specification.find_by_name("avo").full_gem_path
  rescue Gem::LoadError
    # ignore
  end

  roots
    .compact
    .map(&:strip)
    .reject(&:empty?)
    .uniq
    .map { |root| File.join(root, "app/assets/stylesheets/css/variables.css") }
    .find { |path| File.exist?(path) }
end