Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Rename package and CLI to app-store-reviews and add page, reviews and…
Browse files Browse the repository at this point in the history
… territories commands
  • Loading branch information
jcoynel committed Feb 28, 2021
1 parent dcc2141 commit 3a000a7
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 58 deletions.
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import PackageDescription

let package = Package(
name: "AppStoreReviews",
name: "app-store-reviews",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
Expand All @@ -15,9 +15,9 @@ let package = Package(
"AppStoreReviews",
]),
.executable(
name: "AppStoreReviewsCLI",
name: "app-store-reviews",
targets: [
"AppStoreReviewsCLI",
"app-store-reviews",
])
],
dependencies: [
Expand All @@ -31,7 +31,7 @@ let package = Package(
.product(name: "Logging", package: "swift-log")
]),
.target(
name: "AppStoreReviewsCLI",
name: "app-store-reviews",
dependencies: [
"AppStoreReviews",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
Expand Down
51 changes: 0 additions & 51 deletions Sources/AppStoreReviewsCLI/Reviews.swift

This file was deleted.

3 changes: 0 additions & 3 deletions Sources/AppStoreReviewsCLI/main.swift

This file was deleted.

42 changes: 42 additions & 0 deletions Sources/app-store-reviews/Subcommands/Page.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Foundation
import ArgumentParser
import AppStoreReviews

/// Encapsulates `page` command behavior.
struct Page: ParsableCommand {
static var configuration = CommandConfiguration(
abstract: "Fetch the App Store Reviews feed page for the provided app ID, territory and page number."
)

@Argument(help: "The ID of the app.")
var appID: Int

@Argument(help: "App Store country or region.")
var territory: Territory

@Argument(help: "The page number.")
var page: Int

@Argument(help: "The reviews file output.")
var fileOutput: String

func run() throws {
let pageToFetch = try AppStoreReviews.Page(appID: appID, territory: territory, page: page)

let loader = Downloader()
loader.fetch(page: pageToFetch) { result in
switch result {
case .success(let feed):
do {
try Output.write(feed, to: fileOutput)
Self.exit()
} catch {
Self.exit(withError: error)
}
case .failure(let error):
Self.exit(withError: error)
}
}
dispatchMain()
}
}
37 changes: 37 additions & 0 deletions Sources/app-store-reviews/Subcommands/Reviews.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Foundation
import ArgumentParser
import AppStoreReviews

/// Encapsulates `reviews` command behavior.
struct Reviews: ParsableCommand {
static var configuration = CommandConfiguration(
abstract: "Fetch all the reviews from the App Store Reviews feed for the provided app ID and territory."
)

@Argument(help: "The ID of the app.")
var appID: Int

@Argument(help: "App Store country or region.")
var territory: Territory

@Argument(help: "The reviews file output.")
var fileOutput: String

func run() throws {
let loader = ReviewsLoader()
loader.fetchAll(appID: appID, territory: territory) { result in
switch result {
case .success(let reviews):
do {
try Output.write(reviews, to: fileOutput)
Self.exit()
} catch {
Self.exit(withError: error)
}
case .failure(let error):
Self.exit(withError: error)
}
}
dispatchMain()
}
}
22 changes: 22 additions & 0 deletions Sources/app-store-reviews/Subcommands/Territories.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Foundation
import ArgumentParser
import AppStoreReviews

/// Encapsulates `territories` command behavior.
struct Territories: ParsableCommand {
static var configuration = CommandConfiguration(
abstract: "Print the list of territories reviews can be fetched for."
)

func run() throws {
AppStoreReviews.Territory.allCases.forEach { territory in
/* e.g.
...
TON Tonga
TR Turkey
...
*/
print("\(territory.isoCode.padding(toLength: 3, withPad: " ", startingAt: 0)) \(territory.name)")
}
}
}
12 changes: 12 additions & 0 deletions Sources/app-store-reviews/Utilities/Output.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation
import ArgumentParser

enum Output {
static func write<Value: Encodable>(_ data: Value, to file: String) throws {
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let json = try encoder.encode(data)
let outputURL = URL(fileURLWithPath: file)
try json.write(to: outputURL)
}
}
4 changes: 4 additions & 0 deletions Sources/app-store-reviews/Utilities/TerritoryExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import ArgumentParser
import AppStoreReviews

extension Territory: ExpressibleByArgument {}
15 changes: 15 additions & 0 deletions Sources/app-store-reviews/VersionOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import ArgumentParser
import AppStoreReviews

/// Encapsulates `--version` flag behavior.
struct VersionOptions: ParsableArguments {
@Flag(name: .shortAndLong, help: "Print the version and exit")
var version: Bool = false

func validate() throws {
if version {
print(AppStoreReviews.version)
throw ExitCode.success
}
}
}
21 changes: 21 additions & 0 deletions Sources/app-store-reviews/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ArgumentParser

/// Collects the command line options that were passed to `app-store-reviews` and dispatches to the
/// appropriate subcommand.
struct AppStoreReviewsCommand: ParsableCommand {
static var configuration = CommandConfiguration(
commandName: "app-store-reviews",
abstract: "Fetch user reviews from the Apple App Stores",
subcommands: [
Reviews.self,
Page.self,
Territories.self,
],
defaultSubcommand: nil
)

@OptionGroup()
var versionOptions: VersionOptions
}

AppStoreReviewsCommand.main()

0 comments on commit 3a000a7

Please sign in to comment.