Skip to content

Commit 6889a0c

Browse files
Update makefile (#34)
* update makefile minor changes minor changes minor changes * get templates from prefix_bin minor changes minor changes * add linux test auto-generation in macos-step (makefile) minor changes * decouple fixtures from install - makefile minor changes valid Makefile
1 parent 9e47f37 commit 6889a0c

File tree

8 files changed

+94
-74
lines changed

8 files changed

+94
-74
lines changed

.github/workflows/swift.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
export PATH=${JAVA_HOME}/bin:$PATH
1919
brew install swagger-codegen
2020
make macos
21+
make fixtures
2122
swift test
2223
- name: Generate linux tests
2324
run: |
@@ -48,4 +49,5 @@ jobs:
4849
- name: Run tests
4950
run: |
5051
sudo make linux
52+
sudo make fixtures
5153
swift test

Makefile

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,54 @@
1-
#/bin/bash
1+
prefix ?= /usr/local
22

33
TOOL_NAME = bow-openapi
4-
PREFIX_BIN = /usr/local/bin
5-
BASE_TEMPLATES_PATH = $(PREFIX_BIN)/bow
6-
TEMPLATES_PATH = $(BASE_TEMPLATES_PATH)/openapi/templates
4+
PREFIX_BIN = $(prefix)/bin
5+
RESOURCES_PATH = $(prefix)/lib/bowopenapi
76
BUILD_PATH = /tmp/$(TOOL_NAME)
8-
BINARIES_PATH = $(BUILD_PATH)/release
97
SWAGGER_JAR = "https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.19/swagger-codegen-cli-3.0.19.jar"
108

119
.PHONY: linux
12-
linux: clean dependencies basic fixtures
10+
linux: clean structure dependencies install
1311

1412
.PHONY: macos
15-
macos: clean basic fixtures
13+
macos: clean structure install
14+
swift test --generate-linuxmain
15+
16+
.PHONY: fixtures
17+
fixtures:
18+
@rm -rf ./Tests/Fixtures/FixturesAPI
19+
$(TOOL_NAME) --name FixturesAPI --schema ./Tests/Fixtures/petstore.yaml --output ./Tests/Fixtures/FixturesAPI --verbose
1620

1721
.PHONY: xcode
18-
xcode: macos
22+
xcode: macos fixtures
1923
swift package generate-xcodeproj
2024

21-
.PHONY: basic
22-
basic:
23-
tar -xvf ./Tests/Fixtures/FixturesAPI.tar.gz -C ./Tests/Fixtures/
24-
swift build -c release --build-path $(BUILD_PATH)
25-
mkdir -p $(TEMPLATES_PATH)
26-
@install $(BINARIES_PATH)/bow-openapi $(PREFIX_BIN)/bow-openapi
27-
@install ./Templates/* $(TEMPLATES_PATH)
28-
29-
.PHONY: fixtures
30-
fixtures:
25+
.PHONY: install
26+
install:
3127
@rm -rf ./Tests/Fixtures/FixturesAPI
32-
bow-openapi --name FixturesAPI --schema ./Tests/Fixtures/petstore.yaml --output ./Tests/Fixtures/FixturesAPI --verbose
28+
@tar -xvf ./Tests/Fixtures/FixturesAPI.tar.gz -C ./Tests/Fixtures/
29+
@swift build --disable-sandbox --configuration release --build-path $(BUILD_PATH)/build
30+
@install $(BUILD_PATH)/build/release/$(TOOL_NAME) $(PREFIX_BIN)/$(TOOL_NAME)
31+
@cp -R ./Templates $(RESOURCES_PATH)
32+
@cp ./Tests/Fixtures/petstore.yaml $(RESOURCES_PATH)
3333

3434
.PHONY: dependencies
3535
dependencies:
3636
apt update && apt install openjdk-8-jre-headless
37-
mkdir -p $(BUILD_PATH)
3837
wget $(SWAGGER_JAR) --output-document $(BUILD_PATH)/swagger-codegen-cli.jar
39-
@install $(BUILD_PATH)/swagger-codegen-cli.jar $(PREFIX_BIN)/swagger-codegen-cli.jar
38+
install $(BUILD_PATH)/swagger-codegen-cli.jar $(PREFIX_BIN)/swagger-codegen-cli.jar
39+
40+
.PHONY: structure
41+
structure:
42+
@install -d $(BUILD_PATH)/
43+
@install -d $(PREFIX_BIN)/
44+
@install -d $(RESOURCES_PATH)/
45+
46+
.PHONY: uninstall
47+
uninstall:
48+
@rm -rf $(PREFIX_BIN)/swagger-codegen-cli.jar
49+
@rm -rf $(PREFIX_BIN)/$(TOOL_NAME)
50+
@rm -rf $(RESOURCES_PATH)
4051

4152
.PHONY: clean
42-
clean:
43-
@rm -rf $(PREFIX_BIN)/swagger-codegen-cli.jar
44-
@rm -rf $(PREFIX_BIN)/bow-openapi
45-
@rm -rf $(BASE_TEMPLATES_PATH)
46-
@rm -rf $(BUILD_PATH)
53+
clean: uninstall
54+
@rm -rf $(BUILD_PATH)

OpenApiGenerator/APIClient.swift

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@ import Bow
55
import BowEffects
66

77
public enum APIClient {
8+
89
public static func bow(moduleName: String, schema: String, output: String) -> EnvIO<Environment, APIClientError, String> {
9-
EnvIO { env in
10-
let template = IO<APIClientError, String>.var()
11-
let schema = schema.expandingTildeInPath
12-
let output = output.expandingTildeInPath
13-
14-
return binding(
15-
|<-validate(schema: schema),
16-
template <- getTemplatePath(),
17-
|<-bow(moduleName: moduleName, scheme: schema, output: output, templatePath: template.get).provide(env),
18-
yield: "RENDER SUCCEEDED")^
19-
}
10+
let env = EnvIO<Environment, APIClientError, Environment>.var()
11+
let validated = EnvIO<Environment, APIClientError, String>.var()
12+
let template = EnvIO<Environment, APIClientError, URL>.var()
13+
let schema = schema.expandingTildeInPath
14+
let output = output.expandingTildeInPath
15+
16+
return binding(
17+
env <- .ask(),
18+
validated <- validate(schema: schema),
19+
template <- env.get.generator.getTemplates().contramap(\.fileSystem),
20+
|<-bow(moduleName: moduleName, scheme: validated.get, output: output, template: template.get),
21+
yield: "RENDER SUCCEEDED")^
2022
}
2123

22-
public static func bow(moduleName: String, scheme: String, output: String, templatePath: String) -> EnvIO<Environment, APIClientError, String> {
24+
public static func bow(moduleName: String, scheme: String, output: String, template: URL) -> EnvIO<Environment, APIClientError, String> {
2325
EnvIO { env in
2426
let outputPath = OutputPath(sources: "\(output)/Sources",
2527
tests: "\(output)/XCTest")
@@ -29,33 +31,25 @@ public enum APIClient {
2931
|<-env.generator.generate(moduleName: moduleName,
3032
schemePath: scheme,
3133
outputPath: outputPath,
32-
templatePath: templatePath,
34+
template: template,
3335
logPath: env.logPath).provide(env.fileSystem),
34-
|<-createSwiftPackage(moduleName: moduleName, outputPath: output, templatePath: templatePath).provide(env.fileSystem),
36+
|<-createSwiftPackage(moduleName: moduleName, outputPath: output, template: template).provide(env.fileSystem),
3537
yield: "RENDER SUCCEEDED")^
3638
}
3739
}
3840

3941
// MARK: attributes
40-
private static func validate(schema: String) -> IO<APIClientError, Void> {
41-
IO.invoke {
42+
private static func validate(schema: String) -> EnvIO<Environment, APIClientError, String> {
43+
EnvIO.invoke { _ in
4244
guard FileManager.default.fileExists(atPath: schema) else {
4345
throw APIClientError(operation: "validate(schema:output:)",
4446
error: GeneratorError.invalidParameters)
4547
}
48+
49+
return schema
4650
}
4751
}
4852

49-
private static func getTemplatePath() -> IO<APIClientError, String> {
50-
let libPath = "/usr/local/bin"
51-
guard let bundle = Bundle(path: "\(libPath)/bow/openapi/templates/"),
52-
let template = bundle.resourcePath else {
53-
return IO.raiseError(APIClientError(operation: "getTemplatePath()", error: GeneratorError.templateNotFound))^
54-
}
55-
56-
return IO.pure(template)^
57-
}
58-
5953
// MARK: steps
6054
internal static func createStructure(outputPath: OutputPath) -> EnvIO<FileSystem, APIClientError, ()> {
6155
EnvIO { fileSystem in
@@ -69,9 +63,9 @@ public enum APIClient {
6963
}
7064
}
7165

72-
internal static func createSwiftPackage(moduleName: String, outputPath: String, templatePath: String) -> EnvIO<FileSystem, APIClientError, ()> {
66+
internal static func createSwiftPackage(moduleName: String, outputPath: String, template: URL) -> EnvIO<FileSystem, APIClientError, ()> {
7367
EnvIO { fileSystem in
74-
fileSystem.copy(item: "Package.swift", from: templatePath, to: outputPath)^
68+
fileSystem.copy(item: "Package.swift", from: template.path, to: outputPath)^
7569
}.followedBy(package(moduleName: moduleName, outputPath: outputPath))^
7670
.mapError(FileSystemError.toAPIClientError)
7771
}

OpenApiGenerator/Algebra/ClientGenerator.swift

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

1212
public protocol ClientGenerator {
13-
func generate(moduleName: String, schemePath: String, outputPath: OutputPath, templatePath: String, logPath: String) -> EnvIO<FileSystem, APIClientError, ()>
13+
func getTemplates() -> EnvIO<FileSystem, APIClientError, URL>
14+
func generate(moduleName: String, schemePath: String, outputPath: OutputPath, template: URL, logPath: String) -> EnvIO<FileSystem, APIClientError, Void>
1415
}

OpenApiGenerator/SwaggerClientGenerator.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,35 @@ public class SwaggerClientGenerator: ClientGenerator {
99

1010
public init() { }
1111

12-
public func generate(moduleName: String, schemePath: String, outputPath: OutputPath, templatePath: String, logPath: String) -> EnvIO<FileSystem, APIClientError, ()> {
12+
public func generate(moduleName: String, schemePath: String, outputPath: OutputPath, template: URL, logPath: String) -> EnvIO<FileSystem, APIClientError, ()> {
1313
return binding(
14-
|<-self.swaggerGenerator(scheme: schemePath, output: outputPath.sources, template: templatePath, logPath: logPath),
15-
|<-self.reorganizeFiles(moduleName: moduleName, in: outputPath, fromTemplate: templatePath),
14+
|<-self.swaggerGenerator(scheme: schemePath, output: outputPath.sources, template: template, logPath: logPath),
15+
|<-self.reorganizeFiles(moduleName: moduleName, in: outputPath, fromTemplate: template),
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"),
1919
yield: ())^
2020
}
2121

22-
internal func swaggerGenerator(scheme: String, output: String, template: String, logPath: String) -> EnvIO<FileSystem, APIClientError, ()> {
22+
public func getTemplates() -> EnvIO<FileSystem, APIClientError, URL> {
23+
EnvIO.invoke { _ in
24+
let libPath = "/usr/local/lib"
25+
guard let bundle = Bundle(path: "\(libPath)/bowopenapi/Templates"),
26+
let template = bundle.resourcePath else {
27+
throw APIClientError(operation: "getTemplatePath()", error: GeneratorError.templateNotFound)
28+
}
29+
30+
return URL(fileURLWithPath: template)
31+
}
32+
}
33+
34+
internal func swaggerGenerator(scheme: String, output: String, template: URL, logPath: String) -> EnvIO<FileSystem, APIClientError, ()> {
2335
func runSwagger() -> IO<APIClientError, ()> {
2436
IO.invoke {
2537
#if os(Linux)
26-
let result = run("java", args: ["-jar", "/usr/local/bin/swagger-codegen-cli.jar"] + ["generate", "--lang", "swift4", "--input-spec", "\(scheme)", "--output", "\(output)", "--template-dir", "\(template)"])
38+
let result = run("java", args: ["-jar", "/usr/local/bin/swagger-codegen-cli.jar"] + ["generate", "--lang", "swift4", "--input-spec", "\(scheme)", "--output", "\(output)", "--template-dir", "\(template.path)"])
2739
#else
28-
let result = run("/usr/local/bin/swagger-codegen", args: ["generate", "--lang", "swift4", "--input-spec", "\(scheme)", "--output", "\(output)", "--template-dir", "\(template)"]) { settings in
40+
let result = run("/usr/local/bin/swagger-codegen", args: ["generate", "--lang", "swift4", "--input-spec", "\(scheme)", "--output", "\(output)", "--template-dir", "\(template.path)"]) { settings in
2941
settings.execution = .log(file: logPath)
3042
}
3143
#endif
@@ -38,13 +50,13 @@ public class SwaggerClientGenerator: ClientGenerator {
3850
return EnvIO { _ in runSwagger() }
3951
}
4052

41-
internal func reorganizeFiles(moduleName: String, in outputPath: OutputPath, fromTemplate templatePath: String) -> EnvIO<FileSystem, APIClientError, ()> {
53+
internal func reorganizeFiles(moduleName: String, in outputPath: OutputPath, fromTemplate template: URL) -> EnvIO<FileSystem, APIClientError, ()> {
4254
EnvIO { fileSystem in
4355
binding(
4456
|<-fileSystem.moveFiles(in: "\(outputPath.sources)/SwaggerClient/Classes/Swaggers", to: outputPath.sources),
4557
|<-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"),
4658
|<-fileSystem.rename("APIConfiguration.swift", itemAt: "\(outputPath.sources)/APIHelper.swift"),
47-
|<-self.copyTestFiles(moduleName: moduleName, templatePath: templatePath, outputPath: outputPath.tests).provide(fileSystem),
59+
|<-self.copyTestFiles(moduleName: moduleName, template: template, outputPath: outputPath.tests).provide(fileSystem),
4860
yield: ())^.mapError(FileSystemError.toAPIClientError)
4961
}
5062
}
@@ -163,12 +175,12 @@ public class SwaggerClientGenerator: ClientGenerator {
163175
}
164176
}
165177

166-
internal func copyTestFiles(moduleName: String, templatePath: String, outputPath: String) -> EnvIO<FileSystem, FileSystemError, ()> {
178+
internal func copyTestFiles(moduleName: String, template: URL, outputPath: String) -> EnvIO<FileSystem, FileSystemError, ()> {
167179
let files = ["API+XCTest.swift", "API+Error.swift", "APIConfigTesting.swift", "StubURL.swift"]
168180

169181
return EnvIO { fileSystem in
170182
binding(
171-
|<-fileSystem.copy(items: files, from: templatePath, to: outputPath),
183+
|<-fileSystem.copy(items: files, from: template.path, to: outputPath),
172184
|<-files.traverse { file in self.fixTestFile(moduleName: moduleName, fileName: file, outputPath: outputPath).provide(fileSystem) },
173185
yield: ())
174186
}^

Tests/GeneratorAPIClientTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class GeneratorAPIClientTests: XCTestCase {
2525

2626

2727
func testCreateSwiftPackage() {
28-
try? APIClient.createSwiftPackage(moduleName: moduleName, outputPath: output.path, templatePath: template.path)
28+
try? APIClient.createSwiftPackage(moduleName: moduleName, outputPath: output.path, template: template)
2929
.provide(fileSystem)
3030
.unsafeRunSync()
3131

@@ -47,7 +47,7 @@ class GeneratorAPIClientTests: XCTestCase {
4747
let clientGeneratorMock = ClientGeneratorMock(shouldFail: false)
4848
let environmentMock = Environment(logPath: "\(output.path)/log.1.txt", fileSystem: fileSystem, generator: clientGeneratorMock)
4949

50-
_ = try? APIClient.bow(moduleName: moduleName, scheme: URL.schemas.file(.model).path, output: output.path, templatePath: template.path)
50+
_ = try? APIClient.bow(moduleName: moduleName, scheme: URL.schemas.file(.model).path, output: output.path, template: template)
5151
.provide(environmentMock)
5252
.unsafeRunSync()
5353

@@ -59,7 +59,7 @@ class GeneratorAPIClientTests: XCTestCase {
5959
let clientGeneratorMock = ClientGeneratorMock(shouldFail: false)
6060
let environmentMock = Environment(logPath: "\(output.path)/log.1.txt", fileSystem: fileSystem, generator: clientGeneratorMock)
6161

62-
let either = APIClient.bow(moduleName: moduleName, scheme: URL.schemas.file(.model).path, output: output.path, templatePath: template.path)
62+
let either = APIClient.bow(moduleName: moduleName, scheme: URL.schemas.file(.model).path, output: output.path, template: template)
6363
.provide(environmentMock)
6464
.unsafeRunSyncEither()
6565

@@ -71,7 +71,7 @@ class GeneratorAPIClientTests: XCTestCase {
7171
let clientGeneratorMock = ClientGeneratorMock(shouldFail: true)
7272
let environmentMock = Environment(logPath: "\(output.path)/log.1.txt", fileSystem: fileSystem, generator: clientGeneratorMock)
7373

74-
let either = APIClient.bow(moduleName: moduleName, scheme: URL.schemas.file(.model).path, output: output.path, templatePath: template.path)
74+
let either = APIClient.bow(moduleName: moduleName, scheme: URL.schemas.file(.model).path, output: output.path, template: template)
7575
.provide(environmentMock)
7676
.unsafeRunSyncEither()
7777

@@ -84,7 +84,7 @@ class GeneratorAPIClientTests: XCTestCase {
8484
let fileSystemMock = FileSystemMock(shouldFail: false)
8585
let environmentMock = Environment(logPath: "\(output.path)/log.1.txt", fileSystem: fileSystemMock, generator: clientGeneratorMock)
8686

87-
let either = APIClient.bow(moduleName: moduleName, scheme: URL.schemas.file(.model).path, output: output.path, templatePath: template.path)
87+
let either = APIClient.bow(moduleName: moduleName, scheme: URL.schemas.file(.model).path, output: output.path, template: template)
8888
.provide(environmentMock)
8989
.unsafeRunSyncEither()
9090

@@ -96,7 +96,7 @@ class GeneratorAPIClientTests: XCTestCase {
9696
let fileSystemMock = FileSystemMock(shouldFail: true)
9797
let environmentMock = Environment(logPath: "\(output.path)/log.1.txt", fileSystem: fileSystemMock, generator: clientGeneratorMock)
9898

99-
let either = APIClient.bow(moduleName: moduleName, scheme: URL.schemas.file(.model).path, output: output.path, templatePath: template.path)
99+
let either = APIClient.bow(moduleName: moduleName, scheme: URL.schemas.file(.model).path, output: output.path, template: template)
100100
.provide(environmentMock)
101101
.unsafeRunSyncEither()
102102

Tests/Mocks/ClientGeneratorMock.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ import BowEffects
66
import OpenApiGenerator
77

88
class ClientGeneratorMock: ClientGenerator {
9-
109
private(set) var generateInvoked = false
1110
private let shouldFail: Bool
1211

1312
init(shouldFail: Bool) {
1413
self.shouldFail = shouldFail
1514
}
1615

17-
func generate(moduleName: String, schemePath: String, outputPath: OutputPath, templatePath: String, logPath: String) -> EnvIO<FileSystem, APIClientError, ()> {
16+
func generate(moduleName: String, schemePath: String, outputPath: OutputPath, template: URL, logPath: String) -> EnvIO<FileSystem, APIClientError, ()> {
1817
EnvIO { _ in
1918
self.generateInvoked = true
2019
let error = APIClientError(operation: "Testing", error: GeneratorError.structure)
2120
return self.shouldFail ? IO.raiseError(error): IO.pure(())^
2221
}
2322
}
23+
24+
func getTemplates() -> EnvIO<FileSystem, APIClientError, URL> {
25+
EnvIO.pure(URL.templates)^
26+
}
2427
}

Tests/Utils/Snapshotting+OpenAPI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension Snapshotting where Value == URL, Format == String {
1515
var strategy = Snapshotting<String, String>.lines.pullback { (url: URL) -> String in
1616
let testName = "\(file.string.filename.removeExtension)-focusIn\(focus.removeExtension)"
1717
let directory = URL.temp(subfolder: testName)
18-
let either = APIClient.bow(moduleName: module, scheme: url.path, output: directory.path, templatePath: URL.templates.path)
18+
let either = APIClient.bow(moduleName: module, scheme: url.path, output: directory.path, template: URL.templates)
1919
.provide(environment(named: testName))
2020
.unsafeRunSyncEither()
2121

0 commit comments

Comments
 (0)