@@ -9,10 +9,10 @@ public class SwaggerClientGenerator: ClientGenerator {
99
1010 public init ( ) { }
1111
12- public func generate( schemePath: String , outputPath: OutputPath , templatePath: String , logPath: String ) -> EnvIO < FileSystem , APIClientError , ( ) > {
12+ public func generate( moduleName : String , schemePath: String , outputPath: OutputPath , templatePath: String , logPath: String ) -> EnvIO < FileSystem , APIClientError , ( ) > {
1313 return binding (
1414 |<- self . swaggerGenerator ( scheme: schemePath, output: outputPath. sources, template: templatePath, logPath: logPath) ,
15- |<- self . reorganizeFiles ( in: outputPath, fromTemplate: templatePath) ,
15+ |<- self . reorganizeFiles ( moduleName : moduleName , in: outputPath, fromTemplate: templatePath) ,
1616 |<- self . fixSignatureParameters ( filesAt: " \( outputPath. sources) /APIs " ) ,
1717 |<- self . renderHelpersForHeaders ( filesAt: " \( outputPath. sources) /APIs " , inFile: " \( outputPath. sources) /APIs.swift " ) ,
1818 |<- self . removeHeadersDefinition ( filesAt: " \( outputPath. sources) /APIs " ) ,
@@ -34,13 +34,13 @@ public class SwaggerClientGenerator: ClientGenerator {
3434 return EnvIO { _ in runSwagger ( ) }
3535 }
3636
37- internal func reorganizeFiles( in outputPath: OutputPath , fromTemplate templatePath: String ) -> EnvIO < FileSystem , APIClientError , ( ) > {
37+ internal func reorganizeFiles( moduleName : String , in outputPath: OutputPath , fromTemplate templatePath: String ) -> EnvIO < FileSystem , APIClientError , ( ) > {
3838 EnvIO { fileSystem in
3939 binding (
4040 |<- fileSystem. moveFiles ( in: " \( outputPath. sources) /SwaggerClient/Classes/Swaggers " , to: outputPath. sources) ,
4141 |<- fileSystem. remove ( from: outputPath. sources, files: " Cartfile " , " AlamofireImplementations.swift " , " Models.swift " , " git_push.sh " , " SwaggerClient.podspec " , " SwaggerClient " , " .swagger-codegen " , " .swagger-codegen-ignore " , " JSONEncodableEncoding.swift " , " JSONEncodingHelper.swift " ) ,
4242 |<- fileSystem. rename ( " APIConfiguration.swift " , itemAt: " \( outputPath. sources) /APIHelper.swift " ) ,
43- |<- fileSystem . copy ( items : [ " API+XCTest.swift " , " API+Error.swift " , " APIConfigTesting.swift " , " StubURL.swift " ] , from : templatePath, to : outputPath. tests) ,
43+ |<- self . copyTestFiles ( moduleName : moduleName , templatePath : templatePath, outputPath : outputPath. tests) . provide ( fileSystem ) ,
4444 yield: ( ) ) ^. mapLeft( FileSystemError . toAPIClientError)
4545 }
4646 }
@@ -158,4 +158,29 @@ public class SwaggerClientGenerator: ClientGenerator {
158158 yield: ( ) ) ^. mapLeft( FileSystemError . toAPIClientError)
159159 }
160160 }
161+
162+ internal func copyTestFiles( moduleName: String , templatePath: String , outputPath: String ) -> EnvIO < FileSystem , FileSystemError , ( ) > {
163+ let files = [ " API+XCTest.swift " , " API+Error.swift " , " APIConfigTesting.swift " , " StubURL.swift " ]
164+
165+ return EnvIO { fileSystem in
166+ binding (
167+ |<- fileSystem. copy ( items: files, from: templatePath, to: outputPath) ,
168+ |<- files. traverse { file in self . fixTestFile ( moduleName: moduleName, fileName: file, outputPath: outputPath) . provide ( fileSystem) } ,
169+ yield: ( ) )
170+ } ^
171+ }
172+
173+ internal func fixTestFile( moduleName: String , fileName: String , outputPath: String ) -> EnvIO < FileSystem , FileSystemError , ( ) > {
174+ let content = IO < FileSystemError , String > . var ( )
175+ let fixedContent = IO < FileSystemError , String > . var ( )
176+ let path = outputPath + " / " + fileName
177+
178+ return EnvIO { fileSystem in
179+ binding (
180+ content <- fileSystem. readFile ( atPath: path) ,
181+ fixedContent <- IO . pure ( content. get. replacingOccurrences ( of: " {{ moduleName }} " , with: moduleName) ) ,
182+ |<- fileSystem. write ( content: fixedContent. get, toFile: path) ,
183+ yield: ( ) )
184+ }
185+ }
161186}
0 commit comments