1
1
module 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.
2
6
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.
3
12
class ManifestEntry
4
13
attr_reader :logical_path , :digested_path , :integrity
5
14
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:
7
23
@logical_path = logical_path
8
24
@digested_path = digested_path
9
25
@integrity = integrity
10
26
end
11
27
28
+ # Converts the manifest entry to a hash representation.
29
+ #
30
+ # Returns a hash containing the +digested_path+ and +integrity+ keys.
12
31
def to_h
13
32
{ digested_path : digested_path , integrity : integrity }
14
33
end
15
34
end
16
35
17
36
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.
18
50
def from_path ( manifest_path )
19
51
manifest = Manifest . new
20
52
@@ -40,11 +72,31 @@ def from_path(manifest_path)
40
72
end
41
73
end
42
74
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.
43
82
def initialize ( integrity_hash_algorithm : nil )
44
83
@integrity_hash_algorithm = integrity_hash_algorithm
45
84
@entries = { }
46
85
end
47
86
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.
48
100
def push_asset ( asset )
49
101
entry = ManifestEntry . new (
50
102
logical_path : asset . logical_path . to_s ,
@@ -55,21 +107,59 @@ def push_asset(asset)
55
107
push ( entry )
56
108
end
57
109
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.
58
119
def push ( entry )
59
120
@entries [ entry . logical_path ] = entry
60
121
end
61
122
alias_method :<< , :push
62
123
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.
63
133
def []( logical_path )
64
134
@entries [ logical_path ]
65
135
end
66
136
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.
67
145
def to_json
68
146
@entries . transform_values do |manifest_entry |
69
147
manifest_entry . to_h
70
148
end . to_json
71
149
end
72
150
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.
73
163
def transform_values ( &block )
74
164
@entries . transform_values ( &block )
75
165
end
0 commit comments