@@ -17,28 +17,20 @@ struct MetaProtocolCodable: BuildToolPlugin {
1717 ///
1818 /// - Parameter target: The target including plugin.
1919 /// - Returns: The config if provided, otherwise default config.
20- func fetchConfig( for target: SourceModuleTarget ) async throws -> Config {
21- let fileManager = FileManager . default
22- let directory = target. directory. string
23- let contents = try fileManager. contentsOfDirectory ( atPath: directory)
24- let file = contents. first { file in
25- let path = Path ( file)
26- let name = path. stem
27- . components ( separatedBy: . alphanumerics. inverted)
28- . joined ( separator: " " )
29- . lowercased ( )
30- return name == " metacodableconfig "
31- }
32- guard let file else { return . init( scan: . target) }
33- let pathStr = target. directory. appending ( [ file] ) . string
20+ func fetchConfig< Target: MetaProtocolCodableSourceTarget > (
21+ for target: Target
22+ ) throws -> Config {
23+ let pathStr = try target. configPath ( named: " metacodableconfig " )
24+ guard let pathStr else { return . init( scan: . target) }
3425 let path = Config . url ( forFilePath: pathStr)
3526 let conf = try Data ( contentsOf: path)
3627 let pConf = try ? PropertyListDecoder ( ) . decode ( Config . self, from: conf)
3728 let config = try pConf ?? JSONDecoder ( ) . decode ( Config . self, from: conf)
3829 return config
3930 }
4031
41- /// Invoked by SwiftPM to create build commands for a particular target.
32+ /// Invoked by build systems to create build commands for a particular
33+ /// target.
4234 ///
4335 /// Creates build commands that produces intermediate files scanning
4436 /// swift source files according to configuration. Final build command
@@ -49,14 +41,14 @@ struct MetaProtocolCodable: BuildToolPlugin {
4941 /// - target: The target including plugin.
5042 ///
5143 /// - Returns: The commands to be executed during build.
52- func createBuildCommands(
53- context: PluginContext , target: Target
54- ) async throws -> [ Command ] {
55- guard let target = target as? SourceModuleTarget else { return [ ] }
44+ func createBuildCommands< Context > (
45+ in context: Context , for target: Context . Target
46+ ) throws -> [ Command ] where Context : MetaProtocolCodablePluginContext {
47+ // Get config
5648 let tool = try context. tool ( named: " ProtocolGen " )
57- // Get Config
58- let config = try await fetchConfig ( for: target)
59- let ( allTargets , imports ) = config . scanInput ( for : target )
49+ let config = try fetchConfig ( for : target )
50+ let ( allTargets , imports ) = config . scanInput ( for: target, in : context )
51+
6052 // Setup folder
6153 let genFolder = context. pluginWorkDirectory. appending ( [ " ProtocolGen " ] )
6254 try FileManager . default. createDirectory (
@@ -115,45 +107,49 @@ struct MetaProtocolCodable: BuildToolPlugin {
115107 }
116108}
117109
118- extension Config {
119- /// Returns targets to scan and import modules based on current
120- /// configuration.
110+ extension MetaProtocolCodable {
111+ /// Invoked by SwiftPM to create build commands for a particular target.
121112 ///
122- /// Based on configuration, the targets for which source files need
123- /// to be checked and the modules that will be imported in final syntax
124- /// generated is returned .
113+ /// Creates build commands that produces intermediate files scanning
114+ /// swift source files according to configuration. Final build command
115+ /// generates syntax aggregating all intermediate files .
125116 ///
126- /// - Parameter target: The target including plugin.
127- /// - Returns: The targets to scan and modules to import.
128- func scanInput(
129- for target: SourceModuleTarget
130- ) -> ( targets: [ SourceModuleTarget ] , modules: [ String ] ) {
131- let allTargets : [ SourceModuleTarget ]
132- let modules : [ String ]
133- switch scan {
134- case . target:
135- allTargets = [ target]
136- modules = [ ]
137- case . local:
138- var targets = target. dependencies. compactMap { dependency in
139- return switch dependency {
140- case . target( let target) :
141- target. sourceModule
142- default :
143- nil
144- }
145- }
146- modules = targets. map ( \. moduleName)
147- targets. append ( target)
148- allTargets = targets
149- case . recursive:
150- var targets = target. recursiveTargetDependencies. compactMap {
151- return $0 as? SourceModuleTarget
152- }
153- modules = targets. map ( \. moduleName)
154- targets. append ( target)
155- allTargets = targets
156- }
157- return ( allTargets, modules)
117+ /// - Parameters:
118+ /// - context: The package and environmental inputs context.
119+ /// - target: The target including plugin.
120+ ///
121+ /// - Returns: The commands to be executed during build.
122+ func createBuildCommands(
123+ context: PluginContext , target: Target
124+ ) async throws -> [ Command ] {
125+ guard let target = target as? SourceModuleTarget else { return [ ] }
126+ return try self . createBuildCommands (
127+ in: context, for: SwiftPackageTarget ( module: target)
128+ )
129+ }
130+ }
131+
132+ #if canImport(XcodeProjectPlugin)
133+ @_implementationOnly import XcodeProjectPlugin
134+
135+ extension MetaProtocolCodable : XcodeBuildToolPlugin {
136+ /// Invoked by Xcode to create build commands for a particular target.
137+ ///
138+ /// Creates build commands that produces intermediate files scanning
139+ /// swift source files according to configuration. Final build command
140+ /// generates syntax aggregating all intermediate files.
141+ ///
142+ /// - Parameters:
143+ /// - context: The package and environmental inputs context.
144+ /// - target: The target including plugin.
145+ ///
146+ /// - Returns: The commands to be executed during build.
147+ func createBuildCommands(
148+ context: XcodePluginContext , target: XcodeTarget
149+ ) throws -> [ Command ] {
150+ return try self . createBuildCommands (
151+ in: context, for: target
152+ )
158153 }
159154}
155+ #endif
0 commit comments