-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Package.swift
161 lines (148 loc) · 5.92 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
// swift-tools-version: 6.0
import Foundation
import PackageDescription
import CompilerPluginSupport
let package = Package(
name: "MetaCodable",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13),
.watchOS(.v6),
.macCatalyst(.v13),
],
products: [
.library(name: "MetaCodable", targets: ["MetaCodable"]),
.library(name: "HelperCoders", targets: ["HelperCoders"]),
.plugin(name: "MetaProtocolCodable", targets: ["MetaProtocolCodable"]),
],
dependencies: [
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.1.0"..<"601.0.0"),
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"),
.package(url: "https://github.com/swiftlang/swift-format", from: "600.0.0"),
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.0.0"),
],
targets: [
// MARK: Core
.target(
name: "PluginCore",
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftOperators", package: "swift-syntax"),
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "OrderedCollections", package: "swift-collections"),
]
),
// MARK: Macro
.macro(
name: "MacroPlugin",
dependencies: [
"PluginCore",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
]
),
.target(name: "MetaCodable", dependencies: ["MacroPlugin"]),
.target(name: "HelperCoders", dependencies: ["MetaCodable"]),
// MARK: Build Tool
.executableTarget(
name: "ProtocolGen",
dependencies: [
"PluginCore", "MetaCodable",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacroExpansion", package: "swift-syntax"),
]
),
.plugin(
name: "MetaProtocolCodable", capability: .buildTool(),
dependencies: ["ProtocolGen"]
),
// MARK: Test
.testTarget(
name: "MetaCodableTests",
dependencies: [
"PluginCore", "MacroPlugin", "MetaCodable", "HelperCoders",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacrosGenericTestSupport", package: "swift-syntax"),
],
plugins: ["MetaProtocolCodable"]
),
],
swiftLanguageModes: [.v6, .v5]
)
extension Package.Dependency.Kind {
var repoName: String? {
guard
case let .sourceControl(
name: _, location: location, requirement: _
) = self,
let location = URL(string: location),
let name = location.lastPathComponent.split(separator: ".").first
else { return nil }
return String(name)
}
}
var unusedDeps: Set<String> = []
var includeTargets: Set<String> = []
var includeProducts: Set<String> = []
if Context.environment["METACODABLE_BEING_USED_FROM_COCOAPODS"] != nil { // CocoaPods specific
unusedDeps.formUnion(["swift-format", "swift-docc-plugin"])
includeTargets.formUnion(["PluginCore", "MacroPlugin"])
includeProducts.insert("MacroPlugin")
package.products.append(.executable(name: "MacroPlugin", targets: ["MacroPlugin"]))
package.targets = package.targets.compactMap { target in
guard target.type == .macro else { return target }
return .executableTarget(
name: target.name,
dependencies: target.dependencies,
path: target.path,
exclude: target.exclude,
sources: target.sources,
resources: target.resources,
publicHeadersPath: target.publicHeadersPath,
cSettings: target.cSettings,
cxxSettings: target.cxxSettings,
swiftSettings: target.swiftSettings,
linkerSettings: target.linkerSettings,
plugins: target.plugins
)
}
if Context.environment["METACODABLE_COCOAPODS_PROTOCOL_PLUGIN"] != nil {
includeTargets.insert("ProtocolGen")
includeProducts.insert("ProtocolGen")
package.products.append(
.executable(name: "ProtocolGen", targets: ["ProtocolGen"])
)
} else {
unusedDeps.insert("swift-argument-parser")
}
} else if Context.environment["METACODABLE_CI"] == nil { // SPM specific
unusedDeps.insert("swift-format")
package.targets.removeAll { $0.name == "MetaCodableTests" }
package.targets.append(
.testTarget(
name: "MetaCodableTests",
dependencies: [
"PluginCore", "MacroPlugin", "MetaCodable", "HelperCoders",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
],
plugins: ["MetaProtocolCodable"]
)
)
if Context.environment["SPI_GENERATE_DOCS"] == nil {
unusedDeps.insert("swift-docc-plugin")
}
}
package.dependencies.removeAll { unusedDeps.contains($0.kind.repoName ?? "") }
if !includeTargets.isEmpty {
package.targets.removeAll { !includeTargets.contains($0.name) }
}
if !includeProducts.isEmpty {
package.products.removeAll { !includeProducts.contains($0.name) }
}