-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathCLIDownloader.swift
More file actions
60 lines (48 loc) · 2.48 KB
/
CLIDownloader.swift
File metadata and controls
60 lines (48 loc) · 2.48 KB
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
import Foundation
// Only available on macOS
#if os(macOS)
/// Helper for downloading the CLI Zip file so we don't have to include it in the repo.
struct CLIDownloader {
/// The URL string for getting the current version of the CLI
static let downloadURLString = "https://install.apollographql.com/legacy-cli/darwin/2.33.9"
/// Downloads the appropriate Apollo CLI in a zip file.
///
/// - Parameters:
/// - cliFolderURL: The folder URL to download the zip file to.
/// - timeout: The maximum time to wait before indicating that the download timed out, in seconds.
static func downloadIfNeeded(to cliFolderURL: URL, timeout: Double) throws {
let zipFileURL = ApolloFilePathHelper.zipFileURL(fromCLIFolder: cliFolderURL)
guard !FileManager.default.apollo.fileExists(at: zipFileURL) else {
CodegenLogger.log("Zip file with the CLI is already downloaded!")
return
}
try self.download(to: zipFileURL, timeout: timeout)
}
/// Deletes any existing version of the zip file and re-downloads a new version.
///
/// - Parameters:
/// - cliFolderURL: The folder where the zip file lives.
/// - timeout: The maximum time to wait before indicating that the download timed out, in seconds.
static func forceRedownload(to cliFolderURL: URL, timeout: Double) throws {
let zipFileURL = ApolloFilePathHelper.zipFileURL(fromCLIFolder: cliFolderURL)
try FileManager.default.apollo.deleteFile(at: zipFileURL)
let apolloFolderURL = ApolloFilePathHelper.apolloFolderURL(fromCLIFolder: cliFolderURL)
try FileManager.default.apollo.deleteFolder(at: apolloFolderURL)
try self.download(to: zipFileURL, timeout: timeout)
}
/// Downloads the zip file of the Apollo CLI synchronously.
///
/// - Parameters:
/// - zipFileURL: The URL where downloaded data should be saved.
/// - timeout: The maximum time to wait before indicating that the download timed out, in seconds.
private static func download(to zipFileURL: URL, timeout: Double) throws {
try FileManager.default.apollo.createContainingFolderIfNeeded(for: zipFileURL)
CodegenLogger.log("Downloading zip file with the CLI...")
let urlRequest = URLRequest(url: URL(string: CLIDownloader.downloadURLString)!)
try URLDownloader().downloadSynchronously(with: urlRequest,
to: zipFileURL,
timeout: timeout)
CodegenLogger.log("CLI zip file successfully downloaded!")
}
}
#endif