forked from apollographql/apollo-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApolloCodegen.swift
More file actions
48 lines (40 loc) · 1.76 KB
/
ApolloCodegen.swift
File metadata and controls
48 lines (40 loc) · 1.76 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
import Foundation
// Only available on macOS
#if os(macOS)
/// A class to facilitate running code generation
public class ApolloCodegen {
/// Errors which can happen with code generation
public enum CodegenError: Error, LocalizedError {
case folderDoesNotExist(_ url: URL)
public var errorDescription: String? {
switch self {
case .folderDoesNotExist(let url):
return "Can't run codegen trying to run the command from \(url) because there is no folder there! This should be the folder which, at some depth, contains all your `.graphql` files."
}
}
}
/// Runs code generation from the given folder with the passed-in options
///
/// - Parameters:
/// - folder: The folder to run the script from. Should be the folder that at some depth, contains all `.graphql` files.
/// - cliFolderURL: The folder where the Apollo CLI is/should be downloaded.
/// - options: The options object to use to run the code generation.
/// - Returns: Output from a successful run
@discardableResult
public static func run(from folder: URL,
with cliFolderURL: URL,
options: ApolloCodegenOptions) throws -> String {
guard FileManager.default.apollo.folderExists(at: folder) else {
throw CodegenError.folderDoesNotExist(folder)
}
switch options.outputFormat {
case .multipleFiles(let folderURL):
try FileManager.default.apollo.createFolderIfNeeded(at: folderURL)
case .singleFile(let fileURL):
try FileManager.default.apollo.createContainingFolderIfNeeded(for: fileURL)
}
let cli = try ApolloCLI.createCLI(cliFolderURL: cliFolderURL, timeout: options.downloadTimeout)
return try cli.runApollo(with: options.arguments, from: folder)
}
}
#endif