Module: Polyrun::Prepare::Assets

Defined in:
lib/polyrun/prepare/assets.rb

Overview

Asset digest and optional Rails assets:precompile, stdlib only.

Class Method Summary collapse

Class Method Details

.digest_sources(*paths) ⇒ Object

Stable digest of a list of files (sorted). Directories are expanded to all files recursively.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/polyrun/prepare/assets.rb', line 12

def digest_sources(*paths)
  files = []
  paths.flatten.compact.each do |p|
    next unless p

    path = p.to_s
    if File.directory?(path)
      Dir.glob(File.join(path, "**", "*"), File::FNM_DOTMATCH).each do |f|
        files << f if File.file?(f)
      end
    elsif File.file?(path)
      files << path
    end
  end
  files.sort!
  combined = files.map { |f| "#{f}:#{Digest::MD5.file(f).hexdigest}" }.join("|")
  Digest::MD5.hexdigest(combined)
end

.precompile!(rails_root:, silent: true) ⇒ Object

Shells out to bin/rails assets:precompile when rails_root contains bin/rails.

Raises:



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/polyrun/prepare/assets.rb', line 44

def precompile!(rails_root:, silent: true)
  exe = File.join(rails_root, "bin", "rails")
  raise Polyrun::Error, "Prepare::Assets: no #{exe}" unless File.executable?(exe)

  cmd = [exe, "assets:precompile"]
  _out, err, st = Open3.capture3(*cmd, chdir: rails_root)
  Polyrun::Log.warn err if !silent && !err.empty?
  raise Polyrun::Error, "assets:precompile failed: #{err}" unless st.success?

  true
end

.stale?(marker_path, *digest_paths) ⇒ Boolean

Writes digest to marker_path if missing or content differs (caller runs compile when needed).

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/polyrun/prepare/assets.rb', line 32

def stale?(marker_path, *digest_paths)
  return true unless File.file?(marker_path)

  File.read(marker_path).strip != digest_sources(*digest_paths)
end

.write_marker!(marker_path, *digest_paths) ⇒ Object



38
39
40
41
# File 'lib/polyrun/prepare/assets.rb', line 38

def write_marker!(marker_path, *digest_paths)
  FileUtils.mkdir_p(File.dirname(marker_path))
  File.write(marker_path, digest_sources(*digest_paths))
end