Skip to content

Commit ce79651

Browse files
Juanpetruizlop
andauthored
Using the new swift-argument-parser library (#28)
* use new swift-argument-parser library to create the bow open api command * Suggested change: improving help message of output param Co-Authored-By: Tomás Ruiz-López <truizlop@gmail.com> * Suggested change: improving help message of schema param Co-Authored-By: Tomás Ruiz-López <truizlop@gmail.com> * Suggested change: improving help message of output param Co-Authored-By: Tomás Ruiz-López <truizlop@gmail.com> * update help message * update output param help message Co-authored-by: Tomás Ruiz-López <truizlop@gmail.com>
1 parent 2fa630b commit ce79651

File tree

6 files changed

+53
-55
lines changed

6 files changed

+53
-55
lines changed

BowOpenAPI.xcodeproj/project.pbxproj

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
/* End PBXAggregateTarget section */
2323

2424
/* Begin PBXBuildFile section */
25+
1E946BF92409121C005A3A4B /* BowOpenAPICommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E946BF82409121C005A3A4B /* BowOpenAPICommand.swift */; };
26+
1E946BFC24091347005A3A4B /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = 1E946BFB24091347005A3A4B /* ArgumentParser */; };
2527
8B017DA52347AC86003975CF /* APIClientSendTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BD085222345E9BD006D33DE /* APIClientSendTests.swift */; };
2628
8B30B8632348FBFD00E4D852 /* API+XCTest.swift in CopyFiles */ = {isa = PBXBuildFile; fileRef = 8B017D7D2347A373003975CF /* API+XCTest.swift */; };
2729
8B30B8642348FBFD00E4D852 /* API+Error.swift in CopyFiles */ = {isa = PBXBuildFile; fileRef = 8B30B8612348FB7600E4D852 /* API+Error.swift */; };
@@ -94,7 +96,6 @@
9496
8BD90F4223328DAF00281786 /* README.mustache in CopyFiles */ = {isa = PBXBuildFile; fileRef = 8BDE6913233280FF009342AD /* README.mustache */; };
9597
8BDE68DA233233CE009342AD /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BDE68D9233233CE009342AD /* main.swift */; };
9698
8BDE68ED23326E42009342AD /* Console.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BDE68EC23326E42009342AD /* Console.swift */; };
97-
8BDE691B23328538009342AD /* CommandLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BDE691A23328538009342AD /* CommandLine.swift */; };
9899
8BFE7E41234E10D4003E97BF /* APIClientModelsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BFE7E40234E10D4003E97BF /* APIClientModelsTests.swift */; };
99100
8BFE7E43234E11FA003E97BF /* URL+Paths.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BFE7E42234E11FA003E97BF /* URL+Paths.swift */; };
100101
8BFE7E4E234E1732003E97BF /* Bow in Frameworks */ = {isa = PBXBuildFile; productRef = 8BFE7E4D234E1732003E97BF /* Bow */; };
@@ -163,6 +164,7 @@
163164
/* End PBXCopyFilesBuildPhase section */
164165

165166
/* Begin PBXFileReference section */
167+
1E946BF82409121C005A3A4B /* BowOpenAPICommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BowOpenAPICommand.swift; sourceTree = "<group>"; };
166168
8B017D7D2347A373003975CF /* API+XCTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "API+XCTest.swift"; sourceTree = "<group>"; };
167169
8B22DD6B2338D2000000CED9 /* APIClientError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIClientError.swift; sourceTree = "<group>"; };
168170
8B30B8612348FB7600E4D852 /* API+Error.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "API+Error.swift"; sourceTree = "<group>"; };
@@ -230,7 +232,6 @@
230232
8BDE6915233280FF009342AD /* modelArray.mustache */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = modelArray.mustache; sourceTree = "<group>"; };
231233
8BDE6916233280FF009342AD /* Configuration.mustache */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Configuration.mustache; sourceTree = "<group>"; };
232234
8BDE6917233280FF009342AD /* JSONEncodingHelper.mustache */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = JSONEncodingHelper.mustache; sourceTree = "<group>"; };
233-
8BDE691A23328538009342AD /* CommandLine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandLine.swift; sourceTree = "<group>"; };
234235
8BDE691D233286CC009342AD /* APIClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIClient.swift; sourceTree = "<group>"; };
235236
8BDEB0B32340D5850095A6E0 /* FileSystemError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileSystemError.swift; sourceTree = "<group>"; };
236237
8BDEB0B52340DAAF0095A6E0 /* GeneratorError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneratorError.swift; sourceTree = "<group>"; };
@@ -272,6 +273,7 @@
272273
isa = PBXFrameworksBuildPhase;
273274
buildActionMask = 2147483647;
274275
files = (
276+
1E946BFC24091347005A3A4B /* ArgumentParser in Frameworks */,
275277
8BC6CF922350DF0400163BD2 /* libOpenApiGenerator.a in Frameworks */,
276278
);
277279
runOnlyForDeploymentPostprocessing = 0;
@@ -462,7 +464,7 @@
462464
children = (
463465
8BDE68D9233233CE009342AD /* main.swift */,
464466
8BDE68EC23326E42009342AD /* Console.swift */,
465-
8BDE691A23328538009342AD /* CommandLine.swift */,
467+
1E946BF82409121C005A3A4B /* BowOpenAPICommand.swift */,
466468
);
467469
path = BowOpenAPI;
468470
sourceTree = "<group>";
@@ -598,6 +600,7 @@
598600
);
599601
name = BowOpenAPI;
600602
packageProductDependencies = (
603+
1E946BFB24091347005A3A4B /* ArgumentParser */,
601604
);
602605
productName = "Bow OpenAPI";
603606
productReference = 8BDE68D6233233CE009342AD /* bow-openapi */;
@@ -641,6 +644,7 @@
641644
8B8B1C1223435AC0009D195A /* XCRemoteSwiftPackageReference "SwiftCheck" */,
642645
8B631A54234CD05F0045BF05 /* XCRemoteSwiftPackageReference "swift-snapshot-testing" */,
643646
8BFE7E46234E1720003E97BF /* XCRemoteSwiftPackageReference "bow" */,
647+
1E946BFA24091347005A3A4B /* XCRemoteSwiftPackageReference "swift-argument-parser" */,
644648
);
645649
productRefGroup = 8BDE68D7233233CE009342AD /* Products */;
646650
projectDirPath = "";
@@ -739,7 +743,7 @@
739743
isa = PBXSourcesBuildPhase;
740744
buildActionMask = 2147483647;
741745
files = (
742-
8BDE691B23328538009342AD /* CommandLine.swift in Sources */,
746+
1E946BF92409121C005A3A4B /* BowOpenAPICommand.swift in Sources */,
743747
8BDE68ED23326E42009342AD /* Console.swift in Sources */,
744748
8BDE68DA233233CE009342AD /* main.swift in Sources */,
745749
);
@@ -1017,6 +1021,14 @@
10171021
/* End XCConfigurationList section */
10181022

10191023
/* Begin XCRemoteSwiftPackageReference section */
1024+
1E946BFA24091347005A3A4B /* XCRemoteSwiftPackageReference "swift-argument-parser" */ = {
1025+
isa = XCRemoteSwiftPackageReference;
1026+
repositoryURL = "https://github.com/apple/swift-argument-parser.git";
1027+
requirement = {
1028+
kind = upToNextMajorVersion;
1029+
minimumVersion = 0.0.1;
1030+
};
1031+
};
10201032
8B631A54234CD05F0045BF05 /* XCRemoteSwiftPackageReference "swift-snapshot-testing" */ = {
10211033
isa = XCRemoteSwiftPackageReference;
10221034
repositoryURL = "https://github.com/pointfreeco/swift-snapshot-testing.git";
@@ -1052,6 +1064,11 @@
10521064
/* End XCRemoteSwiftPackageReference section */
10531065

10541066
/* Begin XCSwiftPackageProductDependency section */
1067+
1E946BFB24091347005A3A4B /* ArgumentParser */ = {
1068+
isa = XCSwiftPackageProductDependency;
1069+
package = 1E946BFA24091347005A3A4B /* XCRemoteSwiftPackageReference "swift-argument-parser" */;
1070+
productName = ArgumentParser;
1071+
};
10551072
8B631A55234CD05F0045BF05 /* SnapshotTesting */ = {
10561073
isa = XCSwiftPackageProductDependency;
10571074
package = 8B631A54234CD05F0045BF05 /* XCRemoteSwiftPackageReference "swift-snapshot-testing" */;

BowOpenAPI/BowOpenAPICommand.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright © 2019 The Bow Authors.
2+
3+
import ArgumentParser
4+
5+
struct BowOpenAPICommand: ParsableCommand {
6+
static var configuration = CommandConfiguration(commandName: "bow-openapi",
7+
abstract: "Generate a Swift network client from an OpenAPI / Swagger specification file." )
8+
9+
@Option(help: "Name for the output module.")
10+
var name: String
11+
12+
@Option(help: ArgumentHelp("Path to the OpenAPI/Swagger schema. ex. `/home/schema-openapi.json`.", valueName: "json|yaml"))
13+
var schema: String
14+
15+
@Option(help: ArgumentHelp("Path where the Swift package containing the network client will be generated. ex. `/home`.", valueName: "output path"))
16+
var output: String
17+
}

BowOpenAPI/CommandLine.swift

Lines changed: 0 additions & 27 deletions
This file was deleted.

BowOpenAPI/Console.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@
33
import Foundation
44

55
enum Console {
6-
static func help() -> Never {
7-
print("\(SCRIPT_NAME) --name <name> --schema <schema json|yaml> --output <output path>")
8-
print("""
9-
10-
name: name for the output module.
11-
schema: path to schema open api. ex. `/home/schema-openapi.json`
12-
output: path where bow client will be generate. ex. `/home`
13-
14-
""")
15-
Darwin.exit(0)
16-
}
17-
186
static func exit(failure: String) -> Never {
197
print("☠️ error: \(failure)")
208
Darwin.exit(-1)

BowOpenAPI/main.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ let prodEnv = Environment(logPath: "/tmp/bow-openapi.log",
77
fileSystem: MacFileSystem(),
88
generator: SwaggerClientGenerator())
99

10-
func main() {
11-
guard let arguments = CommandLine.input else { Console.help() }
12-
guard FileManager.default.fileExists(atPath: arguments.schema) else { Console.exit(failure: "received invalid schema path") }
13-
14-
APIClient.bow(moduleName: arguments.name, scheme: arguments.schema, output: arguments.output)
15-
.provide(prodEnv)
16-
.unsafeRunSyncEither()
17-
.mapLeft { apiError in "could not generate api client for schema '\(arguments.schema)'\ninformation: \(apiError)" }
18-
.fold(Console.exit(failure:), Console.exit(success:))
10+
extension BowOpenAPICommand {
11+
func run() throws {
12+
guard FileManager.default.fileExists(atPath: schema) else {
13+
Console.exit(failure: "received invalid schema path")
14+
}
15+
16+
APIClient.bow(moduleName: name, scheme: schema, output: output)
17+
.provide(prodEnv)
18+
.unsafeRunSyncEither()
19+
.mapLeft { apiError in "could not generate api client for schema '\(schema)'\ninformation: \(apiError)" }
20+
.fold(Console.exit(failure:), Console.exit(success:))
21+
}
1922
}
2023

21-
// #: - MAIN <launcher>
22-
main()
24+
BowOpenAPICommand.main()

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ let package = Package(
1010

1111
dependencies: [
1212
.package(url: "https://github.com/bow-swift/bow.git", .exact("0.6.0")),
13+
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "0.0.1"),
1314
.package(url: "https://github.com/bow-swift/Swiftline.git", .exact("0.5.3"))
1415
],
1516

@@ -19,7 +20,7 @@ let package = Package(
1920
path: "OpenApiGenerator"),
2021

2122
.target(name: "CLI",
22-
dependencies: ["OpenApiGenerator"],
23+
dependencies: ["OpenApiGenerator", "ArgumentParser"],
2324
path: "BowOpenAPI")
2425
]
2526
)

0 commit comments

Comments
 (0)