@@ -29,6 +29,10 @@ public enum RegistryError: Error {
29
29
}
30
30
31
31
public final class RegistryManager {
32
+ internal static var archiverFactory : ( FileSystem ) -> Archiver = { fileSystem in
33
+ return ZipArchiver ( fileSystem: fileSystem)
34
+ }
35
+
32
36
internal static var clientFactory : ( ) -> HTTPClientProtocol = {
33
37
var configuration = HTTPClientConfiguration ( )
34
38
configuration. followRedirects = false
@@ -169,7 +173,7 @@ public final class RegistryManager {
169
173
for version: Version ,
170
174
of package : PackageReference ,
171
175
into fileSystem: FileSystem ,
172
- at path : AbsolutePath ,
176
+ at destinationPath : AbsolutePath ,
173
177
expectedChecksum: ByteString ? = nil ,
174
178
completion: @escaping ( Result < Void , Error > ) -> Void
175
179
) {
@@ -184,12 +188,14 @@ public final class RegistryManager {
184
188
method: . get,
185
189
url: url,
186
190
headers: [
187
- " Accept " : " application/vnd.swift.registry.v1+zip "
191
+ " Accept " : " application/vnd.swift.registry.v1+zip " ,
188
192
]
189
193
)
190
194
195
+ let client = Self . clientFactory ( )
191
196
client. execute ( request) { result in
192
- completion ( result. tryMap { response in
197
+ switch result {
198
+ case . success( let response) :
193
199
if response. statusCode == 200 ,
194
200
response. headers. get ( " Content-Version " ) . first == " 1 " ,
195
201
response. headers. get ( " Content-Type " ) . first? . hasPrefix ( " application/zip " ) == true ,
@@ -210,24 +216,26 @@ public final class RegistryManager {
210
216
)
211
217
}
212
218
213
- let archivePath = path . withExtension ( " zip " )
219
+ let archivePath = destinationPath . withExtension ( " zip " )
214
220
try fileSystem. writeFileContents ( archivePath, bytes: contents)
215
221
216
- try fileSystem. createDirectory ( path , recursive: true )
222
+ try fileSystem. createDirectory ( destinationPath , recursive: true )
217
223
218
- let archiver = ZipArchiver ( fileSystem: fileSystem)
219
- archiver. extract ( from: archivePath, to: path) { result in
220
- try ? fileSystem. removeFileTree ( archivePath)
224
+ let archiver = Self . archiverFactory ( fileSystem)
225
+ archiver. extract ( from: archivePath, to: destinationPath) { result in
221
226
completion ( result)
227
+ try ? fileSystem. removeFileTree ( archivePath)
222
228
}
223
229
} catch {
224
- try ? fileSystem. removeFileTree ( path )
225
- throw error
230
+ try ? fileSystem. removeFileTree ( destinationPath )
231
+ completion ( . failure ( error) )
226
232
}
227
233
} else {
228
- throw RegistryError . invalidResponse
234
+ completion ( . failure ( RegistryError . invalidResponse) )
229
235
}
230
- } )
236
+ case . failure( let error) :
237
+ completion ( . failure( error) )
238
+ }
231
239
}
232
240
}
233
241
}
0 commit comments