-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace marmelroy/Zip module dependency (#170)
Adds an extension to Foundation.Process that executes a bash process with the `unzip` command and throws an error with the stderr as description in case the shell command returns an error code >= 1
- Loading branch information
1 parent
678f10a
commit 68497f8
Showing
4 changed files
with
39 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Foundation | ||
|
||
extension Process { | ||
/// Decompress the zip file to the destination folder. | ||
/// | ||
/// - Parameters: | ||
/// - file: The file path of the zip file to be decompressed. | ||
/// - destination: The folder path that will receive the decompressed files. | ||
/// | ||
/// Creates a `bash` process that calls the `unzip` command allowing overwrites | ||
/// and setting the destination path. | ||
/// | ||
/// Throws an error with `stderr` content in case the command fails. | ||
static func unzip(file: URL, destination: URL) throws { | ||
let task = Process() | ||
task.executableURL = URL(fileURLWithPath: "/bin/bash") | ||
task.arguments = [ | ||
"-c", | ||
"`which unzip` -o '\(file.relativePath)' -d '\(destination.relativePath)'" | ||
] | ||
|
||
let errorPipe = Pipe() | ||
task.standardError = errorPipe | ||
|
||
try task.run() | ||
task.waitUntilExit() | ||
|
||
let errorData = errorPipe.fileHandleForReading.readDataToEndOfFile() | ||
if !errorData.isEmpty, let description = String(data: errorData, encoding: .utf8) { | ||
throw ProcessError(description: description) | ||
} | ||
} | ||
|
||
private struct ProcessError: Error, CustomStringConvertible { | ||
var description: String | ||
} | ||
} |