-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Package.swift
366 lines (329 loc) · 9.21 KB
/
Package.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// swift-tools-version: 5.8
import PackageDescription
var package = Package(name: "swift-utilities")
// MARK: 💫 Package Customization
package.defaultLocalization = "en"
package.platforms = [
.macOS(.v12),
.iOS(.v14),
.tvOS(.v14),
.watchOS(.v7),
]
// MARK: - 🧸 Module Names
let AssertionExtras = "AssertionExtras"
let Cache = "Cache"
let Extensions = "Extensions"
let FileManagerClient = "FileManagerClient"
let Protected = "Protected"
let Reachability = "Reachability"
let ShortID = "ShortID"
let Utilities = "Utilities"
// MARK: - 🔑 Builders
let 📦 = Module.builder(
withDefaults: .init(
name: "Basic Module",
dependsOn: [],
defaultWith: [],
unitTestsDependsOn: []
)
)
// MARK: - 🎯 Targets
AssertionExtras
<+ 📦 {
$0.createProduct = .library
$0.createUnitTests = false
$0.dependsOn = [
Extensions
]
$0.with = [
.customDump,
.xcTestDynamicOverlay,
]
}
#if !os(Linux)
Cache
<+ 📦 {
$0.createProduct = .library
$0.dependsOn = [
Extensions
]
$0.with = [
.asyncAlgorithms,
.deque,
.dependencies,
.orderedCollections,
]
}
#endif
Extensions
<+ 📦 {
$0.createProduct = .library
$0.with = [
.dependencies
]
}
FileManagerClient
<+ 📦 {
$0.createProduct = .library
$0.createUnitTests = false
$0.with = [
.xcTestDynamicOverlay
]
}
Protected
<+ 📦 {
$0.createProduct = .library
}
Reachability
<+ 📦 {
$0.createProduct = .library
$0.createUnitTests = false
$0.with = [
.asyncAlgorithms,
.dependencies,
.xcTestDynamicOverlay,
]
}
ShortID
<+ 📦 {
$0.createProduct = .library
$0.dependsOn = [
Protected
]
$0.with = [
.dependencies
]
}
/// 👜 Define 3rd party dependencies. Associate these dependencies
/// with modules using `$0.with = [ ]` property
/// ------------------------------------------------------------
// MARK: - 👜 3rd Party Dependencies
package.dependencies = [
.package(url: "https://github.com/apple/swift-async-algorithms", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.2"),
.package(url: "https://github.com/apple/swift-collections", from: "1.0.2"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.2.0"),
.package(url: "http://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.1.2"),
]
extension Target.Dependency {
static let asyncAlgorithms: Self = .product(
name: "AsyncAlgorithms", package: "swift-async-algorithms"
)
static let customDump: Self = .product(
name: "CustomDump", package: "swift-custom-dump"
)
static let dependencies: Self = .product(
name: "Dependencies", package: "swift-dependencies"
)
static let deque: Target.Dependency = .product(
name: "DequeModule", package: "swift-collections"
)
static let orderedCollections: Target.Dependency = .product(
name: "OrderedCollections", package: "swift-collections"
)
static let tagged: Self = .product(
name: "Tagged", package: "swift-tagged"
)
static let xcTestDynamicOverlay: Self = .product(
name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"
)
}
/// ✂️ Copy everything below this into other Package.swift files
/// to re-create the same DSL capabilities.
/// ------------------------------------------------------------
// MARK: - 🪄 Package Helpers
extension String {
var dependency: Target.Dependency {
Target.Dependency.target(name: self)
}
var snapshotTests: String { "\(self)SnapshotTests" }
var tests: String { "\(self)Tests" }
}
struct Module {
enum ProductType {
case library
}
typealias Builder = (inout Self) -> Void
static func builder(withDefaults defaults: Module) -> (Builder?) -> Module {
{ block in
var module = Self(
name: "TO BE REPLACED",
defaultWith: defaults.defaultWith,
swiftSettings: defaults.swiftSettings,
plugins: defaults.plugins
)
block?(&module)
return module.merged(with: defaults)
}
}
var name: String
var group: String?
var dependsOn: [String]
let defaultWith: [Target.Dependency]
var with: [Target.Dependency]
var createProduct: ProductType?
var createTarget: Bool
var createUnitTests: Bool
var unitTestsDependsOn: [String]
var unitTestsWith: [Target.Dependency]
var createSnapshotTests: Bool
var snapshotTestsDependsOn: [String]
var resources: [Resource]?
var swiftSettings: [SwiftSetting]
var plugins: [Target.PluginUsage]
var dependencies: [Target.Dependency] {
defaultWith + with + dependsOn.map { $0.dependency }
}
var productTargets: [String] {
createTarget ? [name] : dependsOn
}
init(
name: String,
group: String? = nil,
dependsOn: [String] = [],
defaultWith: [Target.Dependency] = [],
with: [Target.Dependency] = [],
createProduct: ProductType? = nil,
createTarget: Bool = true,
createUnitTests: Bool = true,
unitTestsDependsOn: [String] = [],
unitTestsWith: [Target.Dependency] = [],
createSnapshotTests: Bool = false,
snapshotTestsDependsOn: [String] = [],
resources: [Resource]? = nil,
swiftSettings: [SwiftSetting] = [],
plugins: [Target.PluginUsage] = []
) {
self.name = name
self.group = group
self.dependsOn = dependsOn
self.defaultWith = defaultWith
self.with = with
self.createProduct = createProduct
self.createTarget = createTarget
self.createUnitTests = createUnitTests
self.unitTestsDependsOn = unitTestsDependsOn
self.unitTestsWith = unitTestsWith
self.createSnapshotTests = createSnapshotTests
self.snapshotTestsDependsOn = snapshotTestsDependsOn
self.resources = resources
self.swiftSettings = swiftSettings
self.plugins = plugins
}
private func merged(with other: Self) -> Self {
var copy = self
copy.dependsOn = Set(dependsOn).union(other.dependsOn).sorted()
copy.unitTestsDependsOn = Set(unitTestsDependsOn).union(other.unitTestsDependsOn).sorted()
copy.snapshotTestsDependsOn = Set(snapshotTestsDependsOn).union(other.snapshotTestsDependsOn).sorted()
return copy
}
func group(by group: String) -> Self {
var copy = self
if let existingGroup = self.group {
copy.group = "\(group)/\(existingGroup)"
} else {
copy.group = group
}
return copy
}
}
extension Package {
func add(module: Module) {
// Check should create a product
if case .library = module.createProduct {
products.append(
.library(
name: module.name,
targets: module.productTargets
)
)
}
// Check should create a target
if module.createTarget {
let path = module.group.map { "\($0)/Sources/\(module.name)" }
targets.append(
.target(
name: module.name,
dependencies: module.dependencies,
path: path,
resources: module.resources,
swiftSettings: module.swiftSettings,
plugins: module.plugins
)
)
}
// Check should add unit tests
if module.createUnitTests {
let path = module.group.map { "\($0)/Tests/\(module.name.tests)" }
targets.append(
.testTarget(
name: module.name.tests,
dependencies: [module.name.dependency] + module.unitTestsDependsOn.map { $0.dependency }
+ module.unitTestsWith + [.customDump],
path: path,
plugins: module.plugins
)
)
}
// Check should add snapshot tests
if module.createSnapshotTests {
let path = module.group.map { "\($0)/Tests/\(module.name.snapshotTests)" }
targets.append(
.testTarget(
name: module.name.snapshotTests,
dependencies: [module.name.dependency] + module.snapshotTestsDependsOn.map { $0.dependency } + [
.customDump
],
path: path,
plugins: module.plugins
)
)
}
}
}
protocol ModuleGroupConvertible {
func makeGroup() -> [Module]
}
extension Module: ModuleGroupConvertible {
func makeGroup() -> [Module] { [self] }
}
struct ModuleGroup {
var name: String
var modules: [Module]
init(_ name: String, @ModuleBuilder builder: () -> [Module]) {
self.name = name
self.modules = builder()
}
}
extension ModuleGroup: ModuleGroupConvertible {
func makeGroup() -> [Module] {
modules.map { $0.group(by: name) }
}
}
@resultBuilder
struct ModuleBuilder {
static func buildBlock() -> [Module] { [] }
static func buildBlock(_ modules: ModuleGroupConvertible...) -> [Module] {
modules.flatMap { $0.makeGroup() }
}
}
infix operator <>
extension String {
/// Adds the string as a module to the package, using the provided module
static func <+ (lhs: String, rhs: Module) {
var module = rhs
module.name = lhs
package.add(module: module)
}
}
infix operator <+
extension String {
/// Adds the string as a module to the package, allowing for inline customization
static func <> (lhs: String, rhs: Module.Builder) {
var module = Module(name: lhs)
rhs(&module)
package.add(module: module)
}
}