11module Propshaft
2+ # Manages the manifest file that maps logical asset paths to their digested counterparts.
3+ #
4+ # The manifest is used to track assets that have been processed and digested, storing
5+ # their logical paths, digested paths, and optional integrity hashes.
26 class Manifest
7+ # Represents a single entry in the asset manifest.
8+ #
9+ # Each entry contains information about an asset including its logical path
10+ # (the original path), digested path (the path with content hash), and
11+ # optional integrity hash for security verification.
312 class ManifestEntry
413 attr_reader :logical_path , :digested_path , :integrity
514
6- def initialize ( logical_path :, digested_path :, integrity :)
15+ # Creates a new manifest entry.
16+ #
17+ # ==== Parameters
18+ #
19+ # * +logical_path+ - The logical path of the asset
20+ # * +digested_path+ - The digested path of the asset
21+ # * +integrity+ - The integrity hash of the asset (optional)
22+ def initialize ( logical_path :, digested_path :, integrity :) # :nodoc:
723 @logical_path = logical_path
824 @digested_path = digested_path
925 @integrity = integrity
1026 end
1127
28+ # Converts the manifest entry to a hash representation.
29+ #
30+ # Returns a hash containing the +digested_path+ and +integrity+ keys.
1231 def to_h
1332 { digested_path : digested_path , integrity : integrity }
1433 end
1534 end
1635
1736 class << self
37+ # Creates a new Manifest instance from a manifest file.
38+ #
39+ # Reads and parses a manifest file, supporting both the current format
40+ # (with +digested_path+ and +integrity+ keys) and the legacy format
41+ # (simple string values for backwards compatibility).
42+ #
43+ # ==== Parameters
44+ #
45+ # * +manifest_path+ - The path to the manifest file
46+ #
47+ # ==== Returns
48+ #
49+ # A new manifest instance populated with entries from the file.
1850 def from_path ( manifest_path )
1951 manifest = Manifest . new
2052
@@ -40,11 +72,31 @@ def from_path(manifest_path)
4072 end
4173 end
4274
75+ # Creates a new Manifest instance.
76+ #
77+ # ==== Parameters
78+ #
79+ # * +integrity_hash_algorithm+ - The algorithm to use for generating
80+ # integrity hashes (e.g., 'sha256', 'sha384', 'sha512'). If +nil+, integrity hashes
81+ # will not be generated.
4382 def initialize ( integrity_hash_algorithm : nil )
4483 @integrity_hash_algorithm = integrity_hash_algorithm
4584 @entries = { }
4685 end
4786
87+ # Adds an asset to the manifest.
88+ #
89+ # Creates a manifest entry from the given asset and adds it to the manifest.
90+ # The entry will include the asset's logical path, digested path, and optionally
91+ # an integrity hash if an integrity hash algorithm is configured.
92+ #
93+ # ==== Parameters
94+ #
95+ # * +asset+ - The asset to add to the manifest
96+ #
97+ # ==== Returns
98+ #
99+ # The manifest entry that was added.
48100 def push_asset ( asset )
49101 entry = ManifestEntry . new (
50102 logical_path : asset . logical_path . to_s ,
@@ -55,21 +107,59 @@ def push_asset(asset)
55107 push ( entry )
56108 end
57109
110+ # Adds a manifest entry to the manifest.
111+ #
112+ # ==== Parameters
113+ #
114+ # * +entry+ - The manifest entry to add
115+ #
116+ # ==== Returns
117+ #
118+ # The entry that was added.
58119 def push ( entry )
59120 @entries [ entry . logical_path ] = entry
60121 end
61122 alias_method :<< , :push
62123
124+ # Retrieves a manifest entry by its logical path.
125+ #
126+ # ==== Parameters
127+ #
128+ # * +logical_path+ - The logical path of the asset to retrieve
129+ #
130+ # ==== Returns
131+ #
132+ # The manifest entry, or +nil+ if not found.
63133 def []( logical_path )
64134 @entries [ logical_path ]
65135 end
66136
137+ # Converts the manifest to JSON format.
138+ #
139+ # The JSON representation maps logical paths to hash representations of
140+ # manifest entries, containing +digested_path+ and +integrity+ information.
141+ #
142+ # ==== Returns
143+ #
144+ # The JSON representation of the manifest.
67145 def to_json
68146 @entries . transform_values do |manifest_entry |
69147 manifest_entry . to_h
70148 end . to_json
71149 end
72150
151+ # Transforms the values of all manifest entries using the given block.
152+ #
153+ # This method is useful for applying transformations to all manifest entries
154+ # while preserving the logical path keys.
155+ #
156+ # ==== Parameters
157+ #
158+ # * +block+ - A block that will receive each manifest entry
159+ #
160+ # ==== Returns
161+ #
162+ # A new hash with the same keys but transformed values.
73163 def transform_values ( &block )
74164 @entries . transform_values ( &block )
75165 end
0 commit comments