-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathArgumentSetup.swift
More file actions
84 lines (71 loc) · 2.95 KB
/
ArgumentSetup.swift
File metadata and controls
84 lines (71 loc) · 2.95 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import Foundation
import ApolloCodegenLib
import TSCUtility
enum Target {
case starWars
case gitHub
init?(name: String) {
switch name {
case "StarWars":
self = .starWars
case "GitHub":
self = .gitHub
default:
return nil
}
}
func targetRootURL(fromSourceRoot sourceRootURL: Foundation.URL) -> Foundation.URL {
switch self {
case .gitHub:
return sourceRootURL
.appendingPathComponent("Sources")
.appendingPathComponent("GitHubAPI")
case .starWars:
return sourceRootURL
.appendingPathComponent("Sources")
.appendingPathComponent("StarWarsAPI")
}
}
func options(fromSourceRoot sourceRootURL: Foundation.URL) -> ApolloCodegenOptions {
let targetRootURL = self.targetRootURL(fromSourceRoot: sourceRootURL)
switch self {
case .starWars:
return ApolloCodegenOptions(targetRootURL: targetRootURL)
case .gitHub:
let json = targetRootURL.appendingPathComponent("schema.json")
let outputFileURL = targetRootURL.appendingPathComponent("API.swift")
let operationIDsURL = targetRootURL.appendingPathComponent("operationIDs.json")
return ApolloCodegenOptions(mergeInFieldsFromFragmentSpreads: true,
operationIDsURL: operationIDsURL,
outputFormat: .singleFile(atFileURL: outputFileURL),
suppressSwiftMultilineStringLiterals: true,
urlToSchemaFile: json)
}
}
}
struct ArgumentSetup {
enum ArgumentError: Error, LocalizedError {
case targetNotProvided(args: [String])
var errorDescription: String? {
switch self {
case .targetNotProvided(let args):
return "No valid was provided to generate code for. Args were: \(args)"
}
}
}
static func parse(arguments: [String] = Array(ProcessInfo.processInfo.arguments.dropFirst())) throws -> Target {
let parser = ArgumentParser(usage: "Codegen <options>", overview: "This is what this tool is for")
let target: OptionArgument<String> = parser.add(option: "--target",
shortName: "-t",
kind: String.self,
usage: "The target to generate code for")
let parsedArguments = try parser.parse(arguments)
if
let targetName = parsedArguments.get(target),
let target = Target(name: targetName) {
return target
} else {
throw ArgumentError.targetNotProvided(args: arguments)
}
}
}