Skip to content

Commit

Permalink
Default to use aria2 for downloads if installed
Browse files Browse the repository at this point in the history
  • Loading branch information
interstateone committed Oct 21, 2020
1 parent 1da3d5c commit e870ae8
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 40 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ macOS User Password:
Xcode 11.2.0 has been installed to /Applications/Xcode-11.2.0.app
```

If you have [aria2](https://aria2.github.io) installed (it's available in Homebrew, `brew install aria2`), then xcodes will default to use it for downloads. It uses up to 16 connections to download Xcode 3-5x faster than URLSession.

### Commands

- `install <version>`: Download and install a specific version of Xcode
Expand Down
125 changes: 125 additions & 0 deletions Sources/XcodesKit/Aria2CError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import Foundation

/// A LocalizedError that represents a non-zero exit code from running aria2c.
struct Aria2CError: LocalizedError {
var code: Code

init?(exitStatus: Int32) {
guard let code = Code(rawValue: exitStatus) else { return nil }
self.code = code
}

var errorDescription: String? {
"aria2c error: \(code.description)"
}

// https://github.com/aria2/aria2/blob/master/src/error_code.h
enum Code: Int32, CustomStringConvertible {
case undefined = -1
// Ignoring, not an error
// case finished = 0
case unknownError = 1
case timeOut
case resourceNotFound
case maxFileNotFound
case tooSlowDownloadSpeed
case networkProblem
case inProgress
case cannotResume
case notEnoughDiskSpace
case pieceLengthChanged
case duplicateDownload
case duplicateInfoHash
case fileAlreadyExists
case fileRenamingFailed
case fileOpenError
case fileCreateError
case fileIoError
case dirCreateError
case nameResolveError
case metalinkParseError
case ftpProtocolError
case httpProtocolError
case httpTooManyRedirects
case httpAuthFailed
case bencodeParseError
case bittorrentParseError
case magnetParseError
case optionError
case httpServiceUnavailable
case jsonParseError
case removed
case checksumError

var description: String {
switch self {
case .undefined:
return "Undefined"
case .unknownError:
return "Unknown error"
case .timeOut:
return "Timed out"
case .resourceNotFound:
return "Resource not found"
case .maxFileNotFound:
return "Maximum number of file not found errors reached"
case .tooSlowDownloadSpeed:
return "Download speed too slow"
case .networkProblem:
return "Network problem"
case .inProgress:
return "Unfinished downloads in progress"
case .cannotResume:
return "Remote server did not support resume when resume was required to complete download"
case .notEnoughDiskSpace:
return "Not enough disk space available"
case .pieceLengthChanged:
return "Piece length was different from one in .aria2 control file"
case .duplicateDownload:
return "Duplicate download"
case .duplicateInfoHash:
return "Duplicate info hash torrent"
case .fileAlreadyExists:
return "File already exists"
case .fileRenamingFailed:
return "Renaming file failed"
case .fileOpenError:
return "Could not open existing file"
case .fileCreateError:
return "Could not create new file or truncate existing file"
case .fileIoError:
return "File I/O error"
case .dirCreateError:
return "Could not create directory"
case .nameResolveError:
return "Name resolution failed"
case .metalinkParseError:
return "Could not parse Metalink document"
case .ftpProtocolError:
return "FTP command failed"
case .httpProtocolError:
return "HTTP response header was bad or unexpected"
case .httpTooManyRedirects:
return "Too many redirects occurred"
case .httpAuthFailed:
return "HTTP authorization failed"
case .bencodeParseError:
return "Could not parse bencoded file (usually \".torrent\" file)"
case .bittorrentParseError:
return "\".torrent\" file was corrupted or missing information"
case .magnetParseError:
return "Magnet URI was bad"
case .optionError:
return "Bad/unrecognized option was given or unexpected option argument was given"
case .httpServiceUnavailable:
return "HTTP service unavailable"
case .jsonParseError:
return "Could not parse JSON-RPC request"
case .removed:
return "Reserved. Not used."
case .checksumError:
return "Checksum validation failed"
}
}
}
}
75 changes: 75 additions & 0 deletions Sources/XcodesKit/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,81 @@ public struct Shell {
public func xcodeSelectSwitch(password: String?, path: String) -> Promise<ProcessOutput> {
xcodeSelectSwitch(password, path)
}

public var downloadWithAria2: (Path, URL, Path, [HTTPCookie]) -> (Progress, Promise<Void>) = { aria2Path, url, destination, cookies in
let process = Process()
process.executableURL = aria2Path.url
process.arguments = [
"--header=Cookie: \(cookies.map { "\($0.name)=\($0.value)" }.joined(separator: "; "))",
"--max-connection-per-server=16",
"--split=16",
"--summary-interval=1",
"--stop-with-process=\(ProcessInfo.processInfo.processIdentifier)",
"--dir=\(destination.parent.string)",
"--out=\(destination.basename())",
url.absoluteString,
]
let stdOutPipe = Pipe()
process.standardOutput = stdOutPipe
let stdErrPipe = Pipe()
process.standardError = stdErrPipe

var progress = Progress(totalUnitCount: 100)

let observer = NotificationCenter.default.addObserver(
forName: .NSFileHandleDataAvailable,
object: nil,
queue: OperationQueue.main
) { note in
guard
// This should always be the case for Notification.Name.NSFileHandleDataAvailable
let handle = note.object as? FileHandle,
handle === stdOutPipe.fileHandleForReading || handle === stdErrPipe.fileHandleForReading
else { return }

defer { handle.waitForDataInBackgroundAndNotify() }

let string = String(decoding: handle.availableData, as: UTF8.self)
let regex = try! NSRegularExpression(pattern: #"((?<percent>\d+)%\))"#)
let range = NSRange(location: 0, length: string.utf16.count)

guard
let match = regex.firstMatch(in: string, options: [], range: range),
let matchRange = Range(match.range(withName: "percent"), in: string),
let percentCompleted = Int64(string[matchRange])
else { return }

progress.completedUnitCount = percentCompleted
}

stdOutPipe.fileHandleForReading.waitForDataInBackgroundAndNotify()
stdErrPipe.fileHandleForReading.waitForDataInBackgroundAndNotify()

do {
try process.run()
} catch {
return (progress, Promise(error: error))
}

let promise = Promise<Void> { seal in
DispatchQueue.global(qos: .default).async {
process.waitUntilExit()

NotificationCenter.default.removeObserver(observer, name: .NSFileHandleDataAvailable, object: nil)

guard process.terminationReason == .exit, process.terminationStatus == 0 else {
if let aria2cError = Aria2CError(exitStatus: process.terminationStatus) {
return seal.reject(aria2cError)
} else {
return seal.reject(Process.PMKError.execution(process: process, standardOutput: "", standardError: ""))
}
}
seal.fulfill(())
}
}

return (progress, promise)
}

public var readLine: (String) -> String? = { prompt in
print(prompt, terminator: "")
Expand Down
40 changes: 40 additions & 0 deletions Sources/XcodesKit/Promise+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Foundation
import PromiseKit

/// Attempt and retry a task that fails with resume data up to `maximumRetryCount` times
func attemptResumableTask<T>(
maximumRetryCount: Int = 3,
delayBeforeRetry: DispatchTimeInterval = .seconds(2),
_ body: @escaping (Data?) -> Promise<T>
) -> Promise<T> {
var attempts = 0
func attempt(with resumeData: Data? = nil) -> Promise<T> {
attempts += 1
return body(resumeData).recover { error -> Promise<T> in
guard
attempts < maximumRetryCount,
let resumeData = (error as NSError).userInfo[NSURLSessionDownloadTaskResumeData] as? Data
else { throw error }

return after(delayBeforeRetry).then(on: nil) { attempt(with: resumeData) }
}
}
return attempt()
}

/// Attempt and retry a task up to `maximumRetryCount` times
func attemptRetryableTask<T>(
maximumRetryCount: Int = 3,
delayBeforeRetry: DispatchTimeInterval = .seconds(2),
_ body: @escaping () -> Promise<T>
) -> Promise<T> {
var attempts = 0
func attempt() -> Promise<T> {
attempts += 1
return body().recover { error -> Promise<T> in
guard attempts < maximumRetryCount else { throw error }
return after(delayBeforeRetry).then(on: nil) { attempt() }
}
}
return attempt()
}
Loading

0 comments on commit e870ae8

Please sign in to comment.