-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-generator.swift
executable file
·162 lines (135 loc) · 4.99 KB
/
config-generator.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env swift
import Foundation
func log(_ input: String, prefix: String = "✅", separator: String = " ") {
print([prefix, input].joined(separator: separator))
}
func error(_ input: String) {
log(input, prefix: "❌")
}
let licenseText = """
// Copyright (c) 2021 D4L data4life gGmbH
// All rights reserved.
//
// D4L owns all legal rights, title and interest in and to the Software Development Kit (SDK),
// including any intellectual property rights that subsist in the SDK.
//
// The SDK and its documentation may be accessed and used for viewing/review purposes only.
// Any usage of the SDK for other purposes, including usage for the development of
// applications/third-party applications shall require the conclusion of a license agreement
// between you and D4L.
//
// If you are interested in licensing the SDK for your own applications/third-party
// applications and/or if you’d like to contribute to the development of the SDK, please
// contact D4L by email to [email protected].
//
"""
func makeXcconfig(
clientIdentifier: String,
clientSecret: String,
redirectScheme: String,
environment: String,
platform: String,
licenseText: String = licenseText
) -> String {
"""
\(licenseText)
D4L_PLATFORM = \(platform)
D4L_ID = \(clientIdentifier)
D4L_SECRET = \(clientSecret)
D4L_REDIRECT_SCHEME = \(redirectScheme)
D4L_ENVIRONMENT = \(environment)
"""
}
func generateFile(
content: String,
outputDirectoryPath: String,
filename: String,
fileManager: FileManager = .default
) {
do {
if fileManager.fileExists(atPath: outputDirectoryPath) {
log("removing existing files @ \(outputDirectoryPath)")
try fileManager.removeItem(atPath: outputDirectoryPath)
}
log("creating output directory @ \(outputDirectoryPath)")
try fileManager.createDirectory(
atPath: outputDirectoryPath,
withIntermediateDirectories: false,
attributes: nil
)
let url = URL(string: "file://" + outputDirectoryPath + "/\(filename)")!
try content.data(using: .utf8)?.write(to: url)
log("created file @ \(url.relativePath)")
} catch {
log("generating files failed due to \(String(describing: error))")
}
}
struct Configuration: Codable {
let id: String
let secret: String
let redirectScheme: String
}
enum Environment: String, Codable, CaseIterable {
case local
case development
case staging
case sandbox
case production
var name: String { return rawValue.uppercased() }
}
enum Platform: String, Codable, CaseIterable {
case d4l
case s4h
var name: String { return rawValue.uppercased() }
}
struct EnvironmentConfigurations: Codable {
let platform: String?
let configs: [String: Configuration]
}
func parseEnvConfiguration(
atPath path: String,
fileManager: FileManager = .default,
jsonDecoder: JSONDecoder = JSONDecoder()
) -> EnvironmentConfigurations? {
guard let data = fileManager.contents(atPath: path) else { return nil}
return try? jsonDecoder.decode(EnvironmentConfigurations.self, from: data)
}
func main() {
guard CommandLine.arguments.count == 3 else {
error("usage: config-generator.swift target-platform [d4l|s4h] target-environment [development|staging|production|sandbox]")
exit(EXIT_FAILURE)
}
let targetPlatformName = CommandLine.arguments[1]
guard let targetPlatform = Platform(rawValue: targetPlatformName) else {
error("could not find `\(targetPlatformName)` platform")
error("supported platform: \(Platform.allCases.map(\.rawValue).joined(separator: ", "))")
exit(EXIT_FAILURE)
}
let targetEnvironmentName = CommandLine.arguments[2]
guard let targetEnvironment = Environment(rawValue: targetEnvironmentName) else {
error("could not find `\(targetEnvironmentName)` environment")
error("supported envs: \(Environment.allCases.map(\.rawValue).joined(separator: ", "))")
exit(EXIT_FAILURE)
}
let fileManager = FileManager.default
let configurationJsonFilePath = fileManager.currentDirectoryPath.appending("/\(targetPlatform.rawValue)-example-app-config.json")
let environments = parseEnvConfiguration(atPath: configurationJsonFilePath)
guard let config = environments?.configs[targetEnvironment.name] else {
error("could not find target environment \(targetEnvironment)")
exit(EXIT_FAILURE)
}
let xcconfig = makeXcconfig(
clientIdentifier: config.id,
clientSecret: config.secret,
redirectScheme: config.redirectScheme,
environment: targetEnvironment.name,
platform: targetPlatform.name
)
generateFile(
content: xcconfig,
outputDirectoryPath: fileManager.currentDirectoryPath.appending("/generated"),
filename: "d4l-example.xcconfig"
)
}
main()
exit(EXIT_SUCCESS)