File: output_path.rb

package info (click to toggle)
ruby-propshaft 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 172 kB
  • sloc: ruby: 782; makefile: 4
file content (54 lines) | stat: -rw-r--r-- 1,547 bytes parent folder | download | duplicates (2)
1
2
3
4
5
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
51
52
53
54
require "propshaft/asset"

class Propshaft::OutputPath
  attr_reader :path, :manifest

  def initialize(path, manifest)
    @path, @manifest = path, manifest
  end

  def clean(count, age)
    asset_versions = files.group_by { |_, attrs| attrs[:logical_path] }
    asset_versions.each do |logical_path, versions|
      current = manifest[logical_path]

      versions
        .reject { |path, _| current && path == current }
        .sort_by { |_, attrs| attrs[:mtime] }
        .reverse
        .each_with_index
        .drop_while { |(_, attrs), index| fresh_version_within_limit(attrs[:mtime], count, expires_at: age, limit: index) }
        .each { |(path, _), _| remove(path) }
    end
  end

  def files
    Hash.new.tap do |files|
      all_files_from_tree(path).each do |file|
        digested_path = file.relative_path_from(path)
        logical_path, digest = Propshaft::Asset.extract_path_and_digest(digested_path.to_s)

        files[digested_path.to_s] = {
          logical_path: logical_path.to_s,
          digest: digest,
          mtime: File.mtime(file)
        }
      end
    end
  end

  private
    def fresh_version_within_limit(mtime, count, expires_at:, limit:)
      modified_at = [ 0, Time.now - mtime ].max
      modified_at < expires_at || limit < count
    end

    def remove(path)
      FileUtils.rm(@path.join(path))
      Propshaft.logger.info "Removed #{path}"
    end

    def all_files_from_tree(path)
      path.children.flat_map { |child| child.directory? ? all_files_from_tree(child) : child }
    end
end