@@ -31,6 +31,33 @@ struct SwiftGenPlugin: BuildToolPlugin {
31
31
}
32
32
}
33
33
34
+ #if canImport(XcodeProjectPlugin)
35
+ import XcodeProjectPlugin
36
+
37
+ extension SwiftGenPlugin : XcodeBuildToolPlugin {
38
+ func createBuildCommands( context: XcodePluginContext , target: XcodeTarget ) throws -> [ Command ] {
39
+ let fileManager = FileManager . default
40
+
41
+ // Possible paths where there may be a config file (root of package, target dir.)
42
+ let configurations : [ Path ] = [ context . xcodeProject . directory ]
43
+ . map { $0. appending ( " swiftgen.yml " ) }
44
+ . filter { fileManager. fileExists ( atPath: $0. string) }
45
+
46
+ // Validate paths list
47
+ guard validate ( configurations: configurations, target: target) else {
48
+ return [ ]
49
+ }
50
+
51
+ // Clear the SwiftGen plugin's directory (in case of dangling files)
52
+ fileManager. forceClean ( directory: context. pluginWorkDirectory)
53
+
54
+ return try configurations. map { configuration in
55
+ try . swiftgen( using: configuration, context: context, target: target)
56
+ }
57
+ }
58
+ }
59
+ #endif
60
+
34
61
// MARK: - Helpers
35
62
36
63
private extension SwiftGenPlugin {
@@ -47,6 +74,21 @@ private extension SwiftGenPlugin {
47
74
48
75
return true
49
76
}
77
+
78
+ #if canImport(XcodeProjectPlugin)
79
+ func validate( configurations: [ Path ] , target: XcodeTarget ) -> Bool {
80
+ guard !configurations. isEmpty else {
81
+ Diagnostics . error ( """
82
+ No SwiftGen configurations found for target \( target. displayName) . If you would like to generate sources for this \
83
+ target include a `swiftgen.yml` in the target's source directory, or include a shared `swiftgen.yml` at the \
84
+ package's root.
85
+ """ )
86
+ return false
87
+ }
88
+
89
+ return true
90
+ }
91
+ #endif
50
92
}
51
93
52
94
private extension Command {
@@ -69,6 +111,27 @@ private extension Command {
69
111
outputFilesDirectory: context. pluginWorkDirectory
70
112
)
71
113
}
114
+
115
+ #if canImport(XcodeProjectPlugin)
116
+ static func swiftgen( using configuration: Path , context: XcodePluginContext , target: XcodeTarget ) throws -> Command {
117
+ . prebuildCommand(
118
+ displayName: " SwiftGen BuildTool Plugin " ,
119
+ executable: try context. tool ( named: " swiftgen " ) . path,
120
+ arguments: [
121
+ " config " ,
122
+ " run " ,
123
+ " --verbose " ,
124
+ " --config " , " \( configuration) "
125
+ ] ,
126
+ environment: [
127
+ " PROJECT_DIR " : context. xcodeProject. directory,
128
+ " TARGET_NAME " : target. displayName,
129
+ " DERIVED_SOURCES_DIR " : context. pluginWorkDirectory
130
+ ] ,
131
+ outputFilesDirectory: context. pluginWorkDirectory
132
+ )
133
+ }
134
+ #endif
72
135
}
73
136
74
137
private extension FileManager {
0 commit comments