-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathProject.swift
330 lines (310 loc) · 12.2 KB
/
Project.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import ProjectDescription
import Foundation
// MARK: - Project
let bundleId = "com.zenangst.Keyboard-Cowboy"
func xcconfig(_ targetName: String) -> String { "Configurations/\(targetName).xcconfig" }
func sources(_ folder: String) -> SourceFileGlob { "\(folder)/Sources/**" }
func xpcSources() -> SourceFileGlob { "XPC/Sources/**" }
func resources(_ folder: String) -> ResourceFileElements { "\(folder)/Resources/**" }
let rootPath = URL(fileURLWithPath: String(#filePath))
.deletingLastPathComponent()
.absoluteString
.replacingOccurrences(of: "file://", with: "")
let assetPath = rootPath.appending("Assets")
let envPath = rootPath.appending(".env")
let env = EnvHelper(envPath)
let shell = Shell(path: rootPath)
let buildNumber = ((try? shell.run("git rev-list --count HEAD")) ?? "x.x.x").trimmingCharacters(in: .whitespacesAndNewlines)
// Main application target
let mainAppTarget = Target.target(
name: "Keyboard-Cowboy",
destinations: .macOS,
product: .app,
bundleId: "com.zenangst.Keyboard-Cowboy",
deploymentTargets: .macOS("13.0"),
infoPlist: .file(path: .relativeToRoot("App/Info.plist")),
sources: SourceFilesList(arrayLiteral: sources("App"), xpcSources()),
resources: resources("App"),
entitlements: "App/Entitlements/com.zenangst.Keyboard-Cowboy.entitlements",
dependencies: [
.package(product: "AXEssibility"),
.package(product: "Apps"),
.package(product: "Bonzai"),
.package(product: "Dock"),
.package(product: "DynamicNotchKit"),
.package(product: "Inject"),
.package(product: "InputSources"),
.package(product: "Intercom"),
.package(product: "KeyCodes"),
.package(product: "LaunchArguments"),
.package(product: "MachPort"),
.package(product: "Sparkle"),
.package(product: "Windows"),
// .target(name: "LassoService")
],
settings:
Settings.settings(
base: [
"ASSETCATALOG_COMPILER_APPICON_NAME": "AppIcon",
"CODE_SIGN_IDENTITY": "Apple Development",
"CODE_SIGN_STYLE": "Automatic",
"CURRENT_PROJECT_VERSION": SettingValue(stringLiteral: buildNumber),
"DEVELOPMENT_TEAM": env["TEAM_ID"],
"ENABLE_HARDENED_RUNTIME": true,
"MARKETING_VERSION": "3.27.4",
"PRODUCT_NAME": "Keyboard Cowboy",
"SWIFT_STRICT_CONCURRENCY": "complete",
"SWIFT_VERSION": "6.0",
],
configurations: [
.debug(name: "Debug", xcconfig: "\(xcconfig("Debug"))"),
.release(name: "Release", xcconfig: "\(xcconfig("Release"))")
],
defaultSettings: .recommended)
)
// Unit Tests
let unitTestTarget = Target.target(
name: "UnitTests",
destinations: .macOS,
product: .unitTests,
bundleId: bundleId.appending(".unit-tests"),
deploymentTargets: .macOS("13.0"),
infoPlist: .file(path: .relativeToRoot("UnitTests/Info.plist")),
sources: SourceFilesList(arrayLiteral: sources("UnitTests")),
dependencies: [
.target(name: "Keyboard-Cowboy")
],
settings:
Settings.settings(base: [
"BUNDLE_LOADER": "$(TEST_HOST)",
"CODE_SIGN_IDENTITY": "Apple Development",
"CODE_SIGN_STYLE": "-",
"DEVELOPMENT_TEAM": env["TEAM_ID"],
"PRODUCT_BUNDLE_IDENTIFIER": "\(bundleId).UnitTests",
"TEST_HOST": "$(BUILT_PRODUCTS_DIR)/Keyboard Cowboy.app/Contents/MacOS/Keyboard Cowboy",
])
)
let assetGeneratorTarget = Target.target(
name: "AssetGenerator",
destinations: .macOS,
product: .unitTests,
bundleId: bundleId.appending(".asset-generator"),
deploymentTargets: .macOS("13.0"),
infoPlist: .file(path: .relativeToRoot("AssetGenerator/Info.plist")),
sources: SourceFilesList(arrayLiteral: sources("AssetGenerator")),
dependencies: [
.target(name: "Keyboard-Cowboy")
],
settings:
Settings.settings(base: [
"BUNDLE_LOADER": "$(TEST_HOST)",
"CODE_SIGN_IDENTITY": "Apple Development",
"CODE_SIGN_STYLE": "-",
"DEVELOPMENT_TEAM": env["TEAM_ID"],
"PRODUCT_BUNDLE_IDENTIFIER": "\(bundleId).AssetGenerator",
"TEST_HOST": "$(BUILT_PRODUCTS_DIR)/Keyboard Cowboy.app/Contents/MacOS/Keyboard Cowboy",
])
)
let xpcTarget = Target.target(
name: "LassoService",
destinations: .macOS,
product: .xpc,
bundleId: "com.zenangst.Keyboard-Cowboy.LassoService",
deploymentTargets: .macOS("13.0"),
infoPlist: .file(path: .relativeToRoot("LassoService/Info.plist")),
sources: SourceFilesList(arrayLiteral: sources("LassoService"), xpcSources()),
entitlements: "LassoService/Entitlements/com.zenangst.Keyboard-Cowboy.LassoService.entitlements",
dependencies: [
.package(product: "Apps"),
.package(product: "KeyCodes"),
.package(product: "InputSources"),
.package(product: "MachPort"),
],
settings: Settings.settings(base: [
"CODE_SIGN_IDENTITY": "Apple Development",
"CODE_SIGN_STYLE": "Automatic",
"DEVELOPMENT_TEAM": env["TEAM_ID"],
"ENABLE_HARDENED_RUNTIME": true,
"PRODUCT_NAME": "LassoService",
"SWIFT_STRICT_CONCURRENCY": "complete",
"SWIFT_VERSION": "6.0",
])
)
let project = Project(
name: "Keyboard Cowboy",
options: Project.Options.options(
textSettings: .textSettings(indentWidth: 2,
tabWidth: 2)),
packages: PackageResolver.packages(env),
settings: Settings.settings(configurations: [
.debug(name: "Debug", xcconfig: "\(xcconfig("Debug"))"),
.release(name: "Release", xcconfig: "\(xcconfig("Release"))")
], defaultSettings: .recommended),
targets: [
mainAppTarget,
unitTestTarget,
assetGeneratorTarget,
xpcTarget,
],
schemes: [
Scheme.scheme(
name: mainAppTarget.name,
shared: true,
hidden: false,
buildAction: .buildAction(targets: [.target(mainAppTarget.name)]),
testAction: .targets(
[.testableTarget(target: .target(unitTestTarget.name))],
arguments: .arguments(
environmentVariables: [
"ASSET_PATH": .environmentVariable(value: assetPath, isEnabled: true),
"SOURCE_ROOT": .environmentVariable(value: rootPath, isEnabled: true),
],
launchArguments: [
.launchArgument(name: "-running-unit-tests", isEnabled: true)
])
,
options: .options(
coverage: true,
codeCoverageTargets: [.target(mainAppTarget.name)]
)
),
runAction: .runAction(
executable: .target(mainAppTarget.name),
arguments: .arguments(
environmentVariables: [
"APP_ENVIRONMENT_OVERRIDE": .environmentVariable(value: "development", isEnabled: true),
"SOURCE_ROOT": .environmentVariable(value: rootPath, isEnabled: true),
],
launchArguments: [
.launchArgument(name: "-benchmark", isEnabled: false),
.launchArgument(name: "-debugEditing", isEnabled: false),
.launchArgument(name: "-injection", isEnabled: false),
.launchArgument(name: "-disableMachPorts", isEnabled: false),
.launchArgument(name: "-openWindowAtLaunch", isEnabled: true)
]
)
)
),
Scheme.scheme(
name: "AssetGenerator",
shared: true,
hidden: false,
testAction: .targets(
[.testableTarget(target: .target(assetGeneratorTarget.name))],
arguments: .arguments(
environmentVariables: [
"ASSET_PATH": .environmentVariable(value: assetPath, isEnabled: true),
"SOURCE_ROOT": .environmentVariable(value: rootPath, isEnabled: true),
],
launchArguments: [
.launchArgument(name: "-running-unit-tests", isEnabled: true)
])
)
)
],
additionalFiles: [
.glob(pattern: .path( ".env")),
.glob(pattern: .path( ".github/workflows")),
.glob(pattern: .path( ".gitignore")),
.glob(pattern: .path( "ci_scripts")),
.glob(pattern: .path( "Fixtures")),
.glob(pattern: .path( "Project.swift")),
.glob(pattern: .path( "README.md")),
.glob(pattern: .path( "Tuist/Dependencies.swift")),
.glob(pattern: .path( "appcast.xml")),
.glob(pattern: .path( "gh-pages")),
.glob(pattern: .path( "_RELEASE_NOTES.md")),
]
)
public enum PackageResolver {
public static func packages(_ env: EnvHelper) -> [Package] {
let packages: [Package]
if env["PACKAGE_DEVELOPMENT"] == "true" {
packages = [
.package(url: "https://github.com/krzysztofzablocki/Inject.git", from: "1.5.2"),
.package(url: "https://github.com/sparkle-project/Sparkle.git", from: "2.4.1"),
.package(path: "../AXEssibility"),
.package(path: "../Apps"),
.package(path: "../Bonzai"),
.package(path: "../Dock"),
.package(path: "../DynamicNotchKit"),
.package(path: "../InputSources"),
.package(path: "../Intercom"),
.package(path: "../KeyCodes"),
.package(path: "../LaunchArguments"),
.package(path: "../MachPort"),
.package(path: "../Windows"),
]
} else {
packages = [
.package(url: "https://github.com/krzysztofzablocki/Inject.git", from: "1.5.2"),
.package(url: "https://github.com/sparkle-project/Sparkle.git", from: "2.4.1"),
.package(url: "https://github.com/zenangst/AXEssibility.git", .revision("4a06484fd379c2eb34487c467aca043ac2048ee5")),
.package(url: "https://github.com/zenangst/Apps.git", .revision("98b33d6236cfe912d4accf4e0365fb327b9bca51")),
.package(url: "https://github.com/zenangst/Bonzai.git", .revision("da2e31025dd44f099058a59d33cd4519ca243761")),
.package(url: "https://github.com/zenangst/Dock.git", from: "1.0.1"),
.package(url: "https://github.com/zenangst/DynamicNotchKit", .revision("40abe91486627499783f470c4dedb5267df2f0be")),
.package(url: "https://github.com/zenangst/InputSources.git", from: "1.1.0"),
.package(url: "https://github.com/zenangst/Intercom.git", .revision("5a340e185e571d058c09ab8b8ad8716098282443")),
.package(url: "https://github.com/zenangst/KeyCodes.git", from: "5.0.0"),
.package(url: "https://github.com/zenangst/LaunchArguments.git", from: "1.0.2"),
.package(url: "https://github.com/zenangst/MachPort.git", .revision("6cc17710f7890cdda6cf197573c05a5e767d67af")),
.package(url: "https://github.com/zenangst/Windows.git", from: "1.2.2"),
]
}
return packages
}
}
public struct EnvHelper: Sendable {
private let dictionary: [String: String]
public subscript(key: String) -> SettingValue { SettingValue(stringLiteral: dictionary[key]!) }
public init(_ path: String) {
let fileManager = FileManager.default
guard fileManager.fileExists(atPath: path) else { fatalError("🌈 \(path) does not exist") }
guard let data = fileManager.contents(atPath: path) else { fatalError("🌈 .env files is missing at path: \(path)") }
guard let contents = String(data: data, encoding: .utf8) else { fatalError("🌈 Unable to read data at path: \(path)") }
var env = [String: String]()
let lines = contents
.components(separatedBy: .newlines)
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
.filter { $0.first != "#" }
.filter { $0.contains("=") }
lines.forEach { line in
let components = line.split(separator: "=", maxSplits: 1, omittingEmptySubsequences: true)
if components.count == 2 {
let name = String(components[0])
let value = String(components[1])
env[name] = value
}
}
self.dictionary = env
}
}
public final class Shell: Sendable {
private let path: String
public init(path: String) {
self.path = path
}
public func run(_ command: String) throws -> String {
let standardOutput = Pipe()
let standardError = Pipe()
let process = Process()
process.executableURL = URL(fileURLWithPath: "/bin/zsh")
process.currentDirectoryURL = URL(filePath: path)
process.arguments = ["-c", command]
process.standardOutput = standardOutput
process.standardError = standardError
try process.run()
let output: String
if let data = try standardOutput.fileHandleForReading.readToEnd() {
output = String(data: data, encoding: .utf8)!
} else if let data = try standardError.fileHandleForReading.readToEnd() {
output = String(data: data, encoding: .utf8)!
} else {
output = ""
}
process.waitUntilExit()
return output
}
}