Skip to content

Commit e20def8

Browse files
authored
Implement the new arguments parser framework (#14)
* Make use of the new argument parser * Add configuration description * Fix tests and add extra options for arguments * Add verbose logging * Updated the readme * Fix Danger error for trailing comma
1 parent d19112f commit e20def8

File tree

12 files changed

+1531
-1878
lines changed

12 files changed

+1531
-1878
lines changed

Package.resolved

Lines changed: 4 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ let package = Package(
1616
dependencies: [
1717
// dev .package(url: "https://github.com/danger/swift", from: "3.0.0"),
1818
// dev .package(path: "Submodules/WeTransfer-iOS-CI/Danger-Swift"),
19-
.package(url: "https://github.com/apple/swift-package-manager.git", from: "0.1.0")
19+
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.0.1")
2020
],
2121
targets: [
2222
// dev .testTarget(name: "PoesTests", dependencies: ["PoesCore"]),
2323
// dev .target(name: "DangerDependencies", dependencies: ["Danger", "WeTransferPRLinter"], path: "Submodules/WeTransfer-iOS-CI/Danger-Swift", sources: ["DangerFakeSource.swift"]),
2424
.target(name: "Poes", dependencies: ["PoesCore"]),
25-
.target(name: "PoesCore", dependencies: ["SPMUtility"])
25+
.target(name: "PoesCore", dependencies: ["ArgumentParser"])
2626
]
2727
)

Poes.xcodeproj/project.pbxproj

Lines changed: 1419 additions & 1753 deletions
Large diffs are not rendered by default.

Poes.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Poes.xcodeproj/xcshareddata/xcschemes/Poes-Package.xcscheme

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
buildForAnalyzing = "YES">
1515
<BuildableReference
1616
BuildableIdentifier = "primary"
17-
BlueprintIdentifier = "Poes::PoesCore"
18-
BuildableName = "PoesCore.framework"
19-
BlueprintName = "PoesCore"
17+
BlueprintIdentifier = "Poes::Poes"
18+
BuildableName = "Poes"
19+
BlueprintName = "Poes"
2020
ReferencedContainer = "container:Poes.xcodeproj">
2121
</BuildableReference>
2222
</BuildActionEntry>
@@ -28,9 +28,9 @@
2828
buildForAnalyzing = "YES">
2929
<BuildableReference
3030
BuildableIdentifier = "primary"
31-
BlueprintIdentifier = "Poes::Poes"
32-
BuildableName = "Poes"
33-
BlueprintName = "Poes"
31+
BlueprintIdentifier = "Poes::PoesCore"
32+
BuildableName = "PoesCore.framework"
33+
BlueprintName = "PoesCore"
3434
ReferencedContainer = "container:Poes.xcodeproj">
3535
</BuildableReference>
3636
</BuildActionEntry>

README.md

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,48 @@ Poes helps you with:
2020
- Xcode 11.4 beta 1 and up
2121

2222
### Usage
23+
24+
```
25+
$ poes --help
26+
OVERVIEW: A Swift command-line tool to easily test push notifications to the
27+
iOS simulator
28+
29+
USAGE: poes <subcommand>
30+
31+
OPTIONS:
32+
-h, --help Show help information.
33+
34+
SUBCOMMANDS:
35+
send Send a push notification to an app installed on the
36+
iOS Simulator
2337
```
24-
$ Poes --help
25-
OVERVIEW: A Swift command-line tool to easily send push notifications to the iOS simulator
2638

27-
USAGE: Poes <options>
39+
40+
41+
```
42+
$ poes send --help
43+
OVERVIEW: Send a push notification to an app installed on the iOS Simulator
44+
45+
USAGE: poes send <bundle-identifier> [--title <title>] [--body <body>] [--badge <badge>] [--is-mutable] [--verbose]
46+
47+
ARGUMENTS:
48+
<bundle-identifier> The bundle identifier of the app to push to
2849
2950
OPTIONS:
30-
--body, -b The body of the Push Notification
31-
--bundle-identifier The bundle identifier to push to
32-
--mutable, -m Adds the mutable-content key to the payload
33-
--title, -t The title of the Push Notification
34-
--badge The number to display in a badge on your app’s icon
35-
--verbose Show extra logging for debugging purposes
36-
--help Display available options
51+
-t, --title <title> The title of the Push notification (default: Default
52+
Title)
53+
-b, --body <body> The body of the Push notification (default: Default
54+
Body)
55+
-b, --badge <badge> The number to display in a badge on your app’s icon
56+
-i, --is-mutable Adds the mutable-content key to the payload
57+
--verbose Show extra logging for debugging purposes
58+
-h, --help Show help information.
3759
```
3860

3961
The bundle identifier is mandatory, all others have a default value. The following command can be enough to send out a notification:
4062

4163
```
42-
$ Poes --bundle-identifier com.wetransfer.app --verbose
64+
$ poes send com.wetransfer.app --verbose
4365
Generated payload:
4466
4567
{

Sources/Poes/main.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
import PoesCore
22

3-
do {
4-
try Poes.run()
5-
} catch {
6-
print("Pushing failed: \(error)")
7-
}
3+
Poes.main()

Sources/PoesCore/PayloadFactory.swift

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

Sources/PoesCore/Poes.swift

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,12 @@
77
//
88

99
import Foundation
10-
import SPMUtility
10+
import ArgumentParser
1111

12-
public struct Poes: ShellInjectable {
13-
enum Error: Swift.Error, CustomStringConvertible {
14-
case missingBundleIdentifier
12+
public struct Poes: ParsableCommand {
13+
public static let configuration = CommandConfiguration(
14+
abstract: "A Swift command-line tool to easily test push notifications to the iOS simulator",
15+
subcommands: [Send.self])
1516

16-
var description: String {
17-
switch self {
18-
case .missingBundleIdentifier:
19-
return "Pass in the bundle identifier with the --bundle-identifier argument"
20-
}
21-
}
22-
}
23-
24-
static var usage: String = "<options>"
25-
static var overview: String = "A Swift command-line tool to easily send push notifications to the iOS simulator"
26-
27-
public static func run(arguments: [String] = ProcessInfo.processInfo.arguments) throws {
28-
Log.isVerbose = arguments.contains("--verbose")
29-
let parser = ArgumentParser(usage: usage, overview: overview)
30-
_ = parser.add(option: "--verbose", kind: Bool.self, usage: "Show extra logging for debugging purposes")
31-
32-
let bundleIdentifierArgument = parser.add(option: "--bundle-identifier", kind: String.self, usage: "The bundle identifier to push to")
33-
let payloadFactory = PayloadFactory(parser: parser)
34-
35-
let parsedArguments = try parser.process(arguments: arguments)
36-
37-
guard let bundleIdentifier = parsedArguments.get(bundleIdentifierArgument) else {
38-
throw Error.missingBundleIdentifier
39-
}
40-
41-
let payload = payloadFactory.make(using: parsedArguments)
42-
let jsonData = try JSONEncoder().encode(payload)
43-
44-
if Log.isVerbose, let object = try? JSONSerialization.jsonObject(with: jsonData, options: []), let jsonString = String(data: try! JSONSerialization.data(withJSONObject: object, options: .prettyPrinted), encoding: .utf8) {
45-
Log.debug("Generated payload:\n\n\(jsonString)\n")
46-
}
47-
48-
let url = Foundation.URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent("payload.json")
49-
FileManager.default.createFile(atPath: url.path, contents: jsonData, attributes: nil)
50-
51-
Log.message("Sending push notification...")
52-
shell.execute(.push(bundleIdentifier: bundleIdentifier, payloadPath: url.path))
53-
Log.message("Push notification sent successfully")
54-
}
55-
}
56-
57-
private extension ArgumentParser {
58-
@discardableResult func process(arguments: [String]) throws -> ArgumentParser.Result {
59-
// We drop the first argument as this is always the execution path. In our case: "gitbuddy"
60-
return try parse(Array(arguments.dropFirst()))
61-
}
17+
public init() { }
6218
}

Sources/PoesCore/Send.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Send.swift
3+
// PoesCore
4+
//
5+
// Created by Antoine van der Lee on 08/02/2020.
6+
// Copyright © 2020 AvdLee. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import ArgumentParser
11+
12+
struct Send: ParsableCommand, ShellInjectable {
13+
14+
public static let configuration = CommandConfiguration(abstract: "Send a push notification to an app installed on the iOS Simulator")
15+
16+
@Argument(help: "The bundle identifier of the app to push to")
17+
private var bundleIdentifier: String
18+
19+
@Option(name: .shortAndLong, default: "Default Title", help: "The title of the Push notification")
20+
private var title: String
21+
22+
@Option(name: .shortAndLong, default: "Default Body", help: "The body of the Push notification")
23+
private var body: String
24+
25+
@Option(name: .shortAndLong, help: "The number to display in a badge on your app’s icon")
26+
private var badge: Int?
27+
28+
@Flag(name: .shortAndLong, help: "Adds the mutable-content key to the payload")
29+
private var isMutable: Bool
30+
31+
@Flag(name: .long, help: "Show extra logging for debugging purposes")
32+
private var verbose: Bool
33+
34+
func run() throws {
35+
Log.isVerbose = verbose
36+
37+
let payload = Payload(title: title, body: body, isMutable: isMutable, badge: badge)
38+
let jsonData = try JSONEncoder().encode(payload)
39+
40+
if Log.isVerbose, let object = try? JSONSerialization.jsonObject(with: jsonData, options: []), let jsonString = String(data: try! JSONSerialization.data(withJSONObject: object, options: .prettyPrinted), encoding: .utf8) {
41+
Log.debug("Generated payload:\n\n\(jsonString)\n")
42+
}
43+
44+
let url = Foundation.URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent("payload.json")
45+
FileManager.default.createFile(atPath: url.path, contents: jsonData, attributes: nil)
46+
47+
Log.message("Sending push notification...")
48+
Self.shell.execute(.push(bundleIdentifier: bundleIdentifier, payloadPath: url.path))
49+
Log.message("Push notification sent successfully")
50+
}
51+
}

0 commit comments

Comments
 (0)