-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathPlugin.swift
More file actions
159 lines (151 loc) · 6.09 KB
/
Copy pathPlugin.swift
File metadata and controls
159 lines (151 loc) · 6.09 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
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
import Foundation
import PackagePlugin
/// Provides `protocol` decoding/encoding syntax generation.
///
/// Creates build commands that produces syntax for `protocol`s
/// that indicate dynamic decoding/encoding with `Codable` macro.
@main
struct MetaProtocolCodable: BuildToolPlugin {
/// Fetches config data from file.
///
/// The alphanumeric characters of file name must case-insensitively
/// match `"metacodableconfig"`, and the data contained must be
/// either `plist` or `json` format, i.e. `metacodable-config.json`,
/// `metacodable_config.json`, `MetaCodableConfig.plist` are
/// all valid names.
///
/// - Parameter target: The target including plugin.
/// - Returns: The config if provided, otherwise default config.
func fetchConfig<Target: MetaProtocolCodableSourceTarget>(
for target: Target
) throws -> Config {
let path = try target.configPath(named: "metacodableconfig")
guard let path = path else { return .init(scan: .target) }
let conf = try Data(contentsOf: path)
let pConf = try? PropertyListDecoder().decode(Config.self, from: conf)
let config = try pConf ?? JSONDecoder().decode(Config.self, from: conf)
return config
}
/// Invoked by build systems to create build commands for a particular
/// target.
///
/// Creates build commands that produces intermediate files scanning
/// swift source files according to configuration. Final build command
/// generates syntax aggregating all intermediate files.
///
/// - Parameters:
/// - context: The package and environmental inputs context.
/// - target: The target including plugin.
///
/// - Returns: The commands to be executed during build.
func createBuildCommands<Context>(
in context: Context, for target: Context.Target
) throws -> [Command] where Context: MetaProtocolCodablePluginContext {
// Get config
let tool = try context.tool(named: "ProtocolGen")
let config = try fetchConfig(for: target)
let (allTargets, imports) = config.scanInput(for: target, in: context)
// Setup folder
let genFolder = Config.appending(
path: "ProtocolGen", to: context.pluginWorkDirectoryURL
)
try FileManager.default.createDirectory(
at: genFolder, withIntermediateDirectories: true
)
// Create source scan commands
var intermFiles: [URL] = []
var buildCommands = allTargets.flatMap { target in
return target.sourceFiles(withSuffix: "swift").map { file in
let moduleName = target.moduleName
let fileName = file.url.deletingPathExtension()
.lastPathComponent
let genFileName = "\(moduleName)-\(fileName)-gen.json"
let genFile = Config.appending(path: genFileName, to: genFolder)
intermFiles.append(genFile)
return Command.buildCommand(
displayName: """
Parse source file "\(fileName)" in module "\(moduleName)"
""",
executable: tool.url,
arguments: [
"parse",
Config.filePath(forURL: file.url),
"--output",
Config.filePath(forURL: genFile),
],
inputFiles: [file.url],
outputFiles: [genFile]
)
}
}
// Create syntax generation command
let moduleName = target.moduleName
let genFileName = "\(moduleName)+ProtocolHelperCoders.swift"
let genPath = Config.appending(path: genFileName, to: genFolder)
var genArgs = [
"generate", "--output", Config.filePath(forURL: genPath),
]
for `import` in imports {
genArgs.append(contentsOf: ["--module", `import`])
}
for file in intermFiles {
genArgs.append(Config.filePath(forURL: file))
}
buildCommands.append(
.buildCommand(
displayName: """
Generate protocol decoding/encoding syntax for "\(moduleName)"
""",
executable: tool.url,
arguments: genArgs,
inputFiles: intermFiles,
outputFiles: [genPath]
)
)
return buildCommands
}
}
extension MetaProtocolCodable {
/// Invoked by SwiftPM to create build commands for a particular target.
///
/// Creates build commands that produces intermediate files scanning
/// swift source files according to configuration. Final build command
/// generates syntax aggregating all intermediate files.
///
/// - Parameters:
/// - context: The package and environmental inputs context.
/// - target: The target including plugin.
///
/// - Returns: The commands to be executed during build.
func createBuildCommands(
context: PluginContext, target: Target
) async throws -> [Command] {
guard let target = target as? SourceModuleTarget else { return [] }
return try self.createBuildCommands(
in: context, for: SwiftPackageTarget(module: target)
)
}
}
#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin
extension MetaProtocolCodable: XcodeBuildToolPlugin {
/// Invoked by Xcode to create build commands for a particular target.
///
/// Creates build commands that produces intermediate files scanning
/// swift source files according to configuration. Final build command
/// generates syntax aggregating all intermediate files.
///
/// - Parameters:
/// - context: The package and environmental inputs context.
/// - target: The target including plugin.
///
/// - Returns: The commands to be executed during build.
func createBuildCommands(
context: XcodePluginContext, target: XcodeTarget
) throws -> [Command] {
try self.createBuildCommands(
in: context, for: target
)
}
}
#endif