Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Commander library by ArgumentParser #70

Merged
merged 3 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@
"object": {
"pins": [
{
"package": "Commander",
"repositoryURL": "https://github.com/kylef/Commander",
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser",
"state": {
"branch": null,
"revision": "e5b50ad7b2e91eeb828393e89b03577b16be7db9",
"version": "0.8.0"
}
},
{
"package": "Spectre",
"repositoryURL": "https://github.com/kylef/Spectre.git",
"state": {
"branch": null,
"revision": "f14ff47f45642aa5703900980b014c2e9394b6e5",
"version": "0.9.0"
"revision": "92646c0cdbaca076c8d3d0207891785b3379cbff",
"version": "0.3.1"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let package = Package(
.executable(name: "AppIcon", targets: ["AppIcon"])
],
dependencies: [
.package(url: "https://github.com/kylef/Commander", from: "0.8.0"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.3.1"),
.package(url: "https://github.com/kareman/SwiftShell.git", from: "5.1.0"),
],
targets: [
Expand All @@ -21,7 +21,7 @@ let package = Package(
.target(
name: "AppIconCore",
dependencies: [
"Commander",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"SwiftShell",
]
),
Expand Down
77 changes: 49 additions & 28 deletions Sources/AppIcon/main.swift
Original file line number Diff line number Diff line change
@@ -1,35 +1,56 @@
import AppIconCore
import Commander

let main = command(
Argument<String>("base image (1024x1024.png)", description: "The path of the base image"),
Option("icon-name", default: "AppIcon", description: "The name of the generated image"),
Option("output-path", default: "AppIcon", description: "The path of the generated appiconset"),
Flag("ipad", description: "Whether or not to generate iPad icons"),
Flag("mac", description: "Whether or not to generate Mac icons"),
Flag("watch", description: "Whether or not to generate Apple Watch icons")
) { base, iconName, path, ipad, mac, watch in
guard base.hasSuffix(".png") else {
throw ArgumentError.missingValue(argument: "base image (1024x1024.png)")
}
import ArgumentParser

let outputExpansion = ".appiconset"
let outputPath = path.hasSuffix(outputExpansion) ? path : "\(path)\(outputExpansion)"
let platforms = Platform.platforms(ipad: ipad, mac: mac, watch: watch)
struct AppIcon: ParsableCommand {
static let configuration = CommandConfiguration(
commandName: "appicon",
abstract: "AppIcon generates *.appiconset contains each resolution image for iOS",
version: "1.0.5"
)

do {
try ImageExtractor.extract(input: (base, platforms), output: (iconName, outputPath))
} catch {
print("Image Extraction Error has occured 😱")
}
@Argument(help: "The path to the base image (1024x1024.png)")
var image: String

do {
try JSONExtractor.extract(input: platforms, output: (iconName, outputPath))
} catch {
print("JSON Extraction Error has occured 😱")
}
@Option(help: "The name of the generated image")
var iconName = "AppIcon"

@Option(help: "The path of the generated appiconset")
var outputPath = "AppIcon"

@Flag(help: "Whether or not to generate iPad icons")
var ipad = false

@Flag(help: "Whether or not to generate Mac icons")
var mac = false

print("\(outputPath) has been generated 🎉")
@Flag(help: "Whether or not to generate Apple Watch icons")
var watch = false

func run() throws {
guard image.hasSuffix(".png") else {
throw ValidationError("image path should have .png extension")
}

let outputExpansion = ".appiconset"
let outputPath = self.outputPath.hasSuffix(outputExpansion) ? self.outputPath : "\(self.outputPath)\(outputExpansion)"
let platforms = Platform.platforms(ipad: ipad, mac: mac, watch: watch)

do {
try ImageExtractor.extract(input: (image, platforms), output: (iconName, outputPath))
} catch {
print("Image Extraction Error has occured 😱")
throw ExitCode(1)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think in that and L48 cases program flow should be aborted to prevent wrong success info printing

}

do {
try JSONExtractor.extract(input: platforms, output: (iconName, outputPath))
} catch {
print("JSON Extraction Error has occured 😱")
throw ExitCode(1)
}

print("\(outputPath) has been generated 🎉")
}
}

main.run("1.0.4")
AppIcon.main()