forked from Carthage/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArchive.swift
63 lines (53 loc) · 2.43 KB
/
Archive.swift
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
55
56
57
58
59
60
61
62
63
//
// Archive.swift
// Carthage
//
// Created by Justin Spahr-Summers on 2014-12-26.
// Copyright (c) 2014 Carthage. All rights reserved.
//
import Foundation
import Result
import ReactiveCocoa
import ReactiveTask
/// Zips the given input items (recursively) into an archive that will be
/// located at the given URL.
public func zipIntoArchive(destinationArchiveURL: NSURL, workingDirectory: String, inputPaths: [String]) -> SignalProducer<(), CarthageError> {
precondition(destinationArchiveURL.fileURL)
precondition(!inputPaths.isEmpty)
let task = Task("/usr/bin/env", workingDirectoryPath: workingDirectory, arguments: [ "zip", "-q", "-r", "--symlinks", destinationArchiveURL.path! ] + inputPaths)
return launchTask(task)
.mapError(CarthageError.TaskError)
.then(.empty)
}
/// Unzips the archive at the given file URL, extracting into the given
/// directory URL (which must already exist).
public func unzipArchiveToDirectory(fileURL: NSURL, _ destinationDirectoryURL: NSURL) -> SignalProducer<(), CarthageError> {
precondition(fileURL.fileURL)
precondition(destinationDirectoryURL.fileURL)
let task = Task("/usr/bin/env", arguments: [ "unzip", "-qq", "-d", destinationDirectoryURL.path!, fileURL.path! ])
return launchTask(task)
.mapError(CarthageError.TaskError)
.then(.empty)
}
/// Unzips the archive at the given file URL into a temporary directory, then
/// sends the file URL to that directory.
public func unzipArchiveToTemporaryDirectory(fileURL: NSURL) -> SignalProducer<NSURL, CarthageError> {
return SignalProducer.attempt { () -> Result<String, CarthageError> in
var temporaryDirectoryTemplate: [CChar] = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("carthage-archive.XXXXXX").nulTerminatedUTF8.map { CChar($0) }
let result = temporaryDirectoryTemplate.withUnsafeMutableBufferPointer { (inout template: UnsafeMutableBufferPointer<CChar>) -> UnsafeMutablePointer<CChar> in
return mkdtemp(template.baseAddress)
}
if result == nil {
return .Failure(.TaskError(.POSIXError(errno)))
}
let temporaryPath = temporaryDirectoryTemplate.withUnsafeBufferPointer { (ptr: UnsafeBufferPointer<CChar>) -> String in
return String.fromCString(ptr.baseAddress)!
}
return .Success(temporaryPath)
}
.map { NSURL.fileURLWithPath($0, isDirectory: true) }
.flatMap(.Merge) { directoryURL in
return unzipArchiveToDirectory(fileURL, directoryURL)
.then(SignalProducer(value: directoryURL))
}
}