Skip to content

Commit ef29a22

Browse files
authored
Merge pull request #11 from bow-swift/tomas/project-name
Custom module name
2 parents 2813552 + 7b28f14 commit ef29a22

19 files changed

+101
-54
lines changed

BowOpenAPI.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@
670670
);
671671
runOnlyForDeploymentPostprocessing = 0;
672672
shellPath = /bin/sh;
673-
shellScript = "cd ${BUILT_PRODUCTS_DIR}\ntests=${SRCROOT}/Tests/Fixtures\n\n./bow-openapi --schema \"$tests/petstore.yaml\" --output \"$tests/api\"\n";
673+
shellScript = "cd ${BUILT_PRODUCTS_DIR}\ntests=${SRCROOT}/Tests/Fixtures\n\n./bow-openapi --name Tests --schema \"$tests/petstore.yaml\" --output \"$tests/api\"\n";
674674
};
675675
/* End PBXShellScriptBuildPhase section */
676676

BowOpenAPI/CommandLine.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@ let SCRIPT_NAME = "bow-openapi"
77
extension CommandLine {
88
struct Arguments {
99
let script: String
10+
let name: String
1011
let schema: String
1112
let output: String
1213
}
1314

1415
static var input: Arguments? {
15-
guard CommandLine.arguments.count == 5,
16-
CommandLine.arguments[1] == "--schema",
17-
CommandLine.arguments[3] == "--output" else { return nil }
16+
guard CommandLine.arguments.count == 7,
17+
CommandLine.arguments[1] == "--name",
18+
CommandLine.arguments[3] == "--schema",
19+
CommandLine.arguments[5] == "--output" else { return nil }
1820

1921
let scriptName = CommandLine.arguments.first?.components(separatedBy: "/").last ?? SCRIPT_NAME
2022
return Arguments(script: scriptName,
21-
schema: CommandLine.arguments[2].expandingTildeInPath,
22-
output: CommandLine.arguments[4].expandingTildeInPath)
23+
name: CommandLine.arguments[2],
24+
schema: CommandLine.arguments[4].expandingTildeInPath,
25+
output: CommandLine.arguments[6].expandingTildeInPath)
2326
}
2427
}

BowOpenAPI/Console.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import Foundation
44

55
enum Console {
66
static func help() -> Never {
7-
print("\(SCRIPT_NAME) --schema <schema json|yaml> --output <output path>")
7+
print("\(SCRIPT_NAME) --name <name> --schema <schema json|yaml> --output <output path>")
88
print("""
99
10+
name: name for the output module.
1011
schema: path to schema open api. ex. `/home/schema-openapi.json`
1112
output: path where bow client will be generate. ex. `/home`
1213

BowOpenAPI/main.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
import Foundation
44
import OpenApiGenerator
55

6+
let prodEnv = Environment(logPath: "/tmp/bow-openapi.log",
7+
fileSystem: MacFileSystem(),
8+
generator: SwaggerClientGenerator())
69

710
func main() {
811
guard let arguments = CommandLine.input else { Console.help() }
912
guard FileManager.default.fileExists(atPath: arguments.schema) else { Console.exit(failure: "received invalid schema path") }
1013

11-
APIClient.bow(scheme: arguments.schema, output: arguments.output)
12-
.provide(Environment(logPath: "/tmp/bow-openapi.log", fileSystem: MacFileSystem(), generator: SwaggerClientGenerator()))
14+
APIClient.bow(moduleName: arguments.name, scheme: arguments.schema, output: arguments.output)
15+
.provide(prodEnv)
1316
.unsafeRunSyncEither()
1417
.mapLeft { apiError in "could not generate api client for schema '\(arguments.schema)'\ninformation: \(apiError)" }
15-
.fold({ failure in Console.exit(failure: failure) },
16-
{ success in Console.exit(success: success) })
18+
.fold(Console.exit(failure:), Console.exit(success:))
1719
}
1820

1921
// #: - MAIN <launcher>

OpenApiGenerator/APIClient.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,28 @@ import Bow
55
import BowEffects
66

77
public enum APIClient {
8-
public static func bow(scheme: String, output: String) -> EnvIO<Environment, APIClientError, String> {
8+
public static func bow(moduleName: String, scheme: String, output: String) -> EnvIO<Environment, APIClientError, String> {
99
EnvIO { env in
1010
let template = IO<APIClientError, String>.var()
1111
return binding(
1212
template <- getTemplatePath(),
13-
|<-bow(scheme: scheme, output: output, templatePath: template.get).provide(env),
13+
|<-bow(moduleName: moduleName, scheme: scheme, output: output, templatePath: template.get).provide(env),
1414
yield: "RENDER SUCCEEDED")^
1515
}
1616
}
1717

18-
public static func bow(scheme: String, output: String, templatePath: String) -> EnvIO<Environment, APIClientError, String> {
18+
public static func bow(moduleName: String, scheme: String, output: String, templatePath: String) -> EnvIO<Environment, APIClientError, String> {
1919
EnvIO { env in
2020
let outputPath = OutputPath(sources: "\(output)/Sources", tests: "\(output)/XCTest")
2121

2222
return binding(
2323
|<-createStructure(outputPath: outputPath).provide(env.fileSystem),
24-
|<-env.generator.generate(schemePath: scheme,
24+
|<-env.generator.generate(moduleName: moduleName,
25+
schemePath: scheme,
2526
outputPath: outputPath,
2627
templatePath: templatePath,
2728
logPath: env.logPath).provide(env.fileSystem),
28-
|<-createSwiftPackage(outputPath: output, templatePath: templatePath).provide(env.fileSystem),
29+
|<-createSwiftPackage(moduleName: moduleName, outputPath: output, templatePath: templatePath).provide(env.fileSystem),
2930
yield: "RENDER SUCCEEDED")^
3031
}
3132
}
@@ -57,9 +58,25 @@ public enum APIClient {
5758
}
5859
}
5960

60-
internal static func createSwiftPackage(outputPath: String, templatePath: String) -> EnvIO<FileSystem, APIClientError, ()> {
61+
internal static func createSwiftPackage(moduleName: String, outputPath: String, templatePath: String) -> EnvIO<FileSystem, APIClientError, ()> {
6162
EnvIO { fileSystem in
6263
fileSystem.copy(item: "Package.swift", from: templatePath, to: outputPath)^
63-
}.mapError(FileSystemError.toAPIClientError)
64+
}.followedBy(package(moduleName: moduleName, outputPath: outputPath))^
65+
.mapError(FileSystemError.toAPIClientError)
66+
}
67+
68+
internal static func package(moduleName: String, outputPath: String) -> EnvIO<FileSystem, FileSystemError, ()> {
69+
EnvIO { fileSystem in
70+
let content = IO<FileSystemError, String>.var()
71+
let fixedContent = IO<FileSystemError, String>.var()
72+
let path = outputPath + "/Package.swift"
73+
74+
return binding(
75+
content <- fileSystem.readFile(atPath: path),
76+
fixedContent <- IO.pure(content.get.replacingOccurrences(of: "{{ moduleName }}", with: moduleName)),
77+
|<-fileSystem.write(content: fixedContent.get, toFile: path),
78+
yield: ()
79+
)^
80+
}
6481
}
6582
}

OpenApiGenerator/Algebra/ClientGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ public struct OutputPath {
1010
}
1111

1212
public protocol ClientGenerator {
13-
func generate(schemePath: String, outputPath: OutputPath, templatePath: String, logPath: String) -> EnvIO<FileSystem, APIClientError, ()>
13+
func generate(moduleName: String, schemePath: String, outputPath: OutputPath, templatePath: String, logPath: String) -> EnvIO<FileSystem, APIClientError, ()>
1414
}

OpenApiGenerator/SwaggerClientGenerator.swift

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Templates/API+Error.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright © 2019 The Bow Authors.
22

33
import Foundation
4+
import {{ moduleName }}
45

56
public enum HTTPErrorTest {
67
case malformedURL
@@ -11,8 +12,7 @@ public enum HTTPErrorTest {
1112
case serverError
1213
case serviceUnavailable
1314
case unknown
14-
case otherError(Error)
15-
case other
15+
case other(error: Error? = nil)
1616
}
1717

1818
extension HTTPErrorTest {
@@ -28,8 +28,7 @@ extension HTTPErrorTest {
2828
case .serverError: return .serverError(response: dataError!.response, data: dataError!.data)
2929
case .serviceUnavailable: return .serviceUnavailable(response: dataError!.response, data: dataError!.data)
3030
case .unknown: return .unknown(response: dataError!.response, data: dataError!.data)
31-
case .otherError(let error): return .other(error: error)
32-
case .other: return .other(error: apiError.error)
31+
case .other(let error): return .other(error: error ?? apiError.error)
3332
}
3433
}
3534
}

Templates/API+XCTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import XCTest
44
import Bow
55
import BowEffects
6-
6+
import {{ moduleName }}
77

88
public extension XCTest {
99

Templates/APIConfigTesting.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright © 2019 The Bow Authors.
22

33
import Foundation
4+
import {{ moduleName }}
45

56
public extension API.Config {
67

0 commit comments

Comments
 (0)