From 2447b0b9e2af6c659485285f331c1c706da0534e Mon Sep 17 00:00:00 2001 From: Geoffrey Foster Date: Mon, 10 Dec 2018 00:44:41 -0500 Subject: [PATCH] switch to using CommandRegistry --- Package.resolved | 18 +++++ Package.swift | 8 +-- ...Command.swift => Command+Extensions.swift} | 11 +-- .../stringray/Commands/CommandRegistry.swift | 67 ------------------- Sources/stringray/Commands/CopyCommand.swift | 1 + Sources/stringray/Commands/LintCommand.swift | 1 + Sources/stringray/Commands/MoveCommand.swift | 1 + .../stringray/Commands/RenameCommand.swift | 1 + Sources/stringray/Commands/SortCommand.swift | 1 + Sources/stringray/main.swift | 3 +- 10 files changed, 31 insertions(+), 81 deletions(-) rename Sources/stringray/Commands/{Command.swift => Command+Extensions.swift} (86%) delete mode 100644 Sources/stringray/Commands/CommandRegistry.swift diff --git a/Package.resolved b/Package.resolved index 6ad7724..a0130cc 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,6 +1,15 @@ { "object": { "pins": [ + { + "package": "CommandRegistry", + "repositoryURL": "https://github.com/g-Off/CommandRegistry.git", + "state": { + "branch": "master", + "revision": "142aa27445e7998c5201b5ec9682698195d6701a", + "version": null + } + }, { "package": "SwiftPM", "repositoryURL": "https://github.com/apple/swift-package-manager.git", @@ -19,6 +28,15 @@ "version": "0.8.2" } }, + { + "package": "XcodeProject", + "repositoryURL": "https://github.com/g-Off/XcodeProject.git", + "state": { + "branch": null, + "revision": "a1e12a8659684039258b79761c65abb9ec98fc94", + "version": "0.4.0" + } + }, { "package": "Yams", "repositoryURL": "https://github.com/jpsim/Yams.git", diff --git a/Package.swift b/Package.swift index f626298..b82ab0a 100644 --- a/Package.swift +++ b/Package.swift @@ -4,19 +4,19 @@ import PackageDescription let package = Package( name: "stringray", dependencies: [ - .package(url: "https://github.com/apple/swift-package-manager.git", from: "0.3.0"), .package(url: "https://github.com/jpsim/Yams.git", from: "1.0.1"), .package(url: "https://github.com/scottrhoyt/SwiftyTextTable.git", from: "0.5.0"), - .package(url: "https://github.com/g-Off/XcodeProject.git", .branch("master")) + .package(url: "https://github.com/g-Off/XcodeProject.git", from: "0.4.0"), + .package(url: "https://github.com/g-Off/CommandRegistry.git", .branch("master")) ], targets: [ .target( name: "stringray", dependencies: [ - "Utility", "Yams", "SwiftyTextTable", - "XcodeProject" + "XcodeProject", + "CommandRegistry" ] ), .testTarget( diff --git a/Sources/stringray/Commands/Command.swift b/Sources/stringray/Commands/Command+Extensions.swift similarity index 86% rename from Sources/stringray/Commands/Command.swift rename to Sources/stringray/Commands/Command+Extensions.swift index f90730a..a3b8d6c 100644 --- a/Sources/stringray/Commands/Command.swift +++ b/Sources/stringray/Commands/Command+Extensions.swift @@ -1,5 +1,5 @@ // -// Command.swift +// Command+Extensions.swift // stringray // // Created by Geoffrey Foster on 2018-11-04. @@ -7,14 +7,7 @@ import Foundation import Utility - -protocol Command { - var command: String { get } - var overview: String { get } - - init(parser: ArgumentParser) - func run(with arguments: ArgumentParser.Result) throws -} +import CommandRegistry extension Command { func write(to url: Foundation.URL, table: StringsTable) throws { diff --git a/Sources/stringray/Commands/CommandRegistry.swift b/Sources/stringray/Commands/CommandRegistry.swift deleted file mode 100644 index 22b898a..0000000 --- a/Sources/stringray/Commands/CommandRegistry.swift +++ /dev/null @@ -1,67 +0,0 @@ -// -// CommandRegistry.swift -// stringray -// -// Created by Geoffrey Foster on 2018-11-07. -// - -import Foundation -import Utility -import Basic - -struct CommandRegistry { - private let parser: ArgumentParser - private var commands: [Command] = [] - - private let versionOption: OptionArgument? - private let version: Version? - - init(usage: String, overview: String, version: Version? = nil) { - self.parser = ArgumentParser(usage: usage, overview: overview) - self.version = version - if version != nil { - self.versionOption = parser.add(option: "--version", shortName: "-v", kind: Bool.self, usage: "Version", completion: nil) - } else { - self.versionOption = nil - } - } - - mutating func register(command: Command.Type) { - commands.append(command.init(parser: parser)) - } - - func run() { - do { - let parsedArguments = try parse() - try process(arguments: parsedArguments) - exit(EXIT_SUCCESS) - } - catch let error as ArgumentParserError { - print(error.description) - exit(EXIT_FAILURE) - } - catch let error { - print(error.localizedDescription) - exit(EXIT_FAILURE) - } - } - - private func parse() throws -> ArgumentParser.Result { - let arguments = Array(ProcessInfo.processInfo.arguments.dropFirst()) - return try parser.parse(arguments) - } - - private func process(arguments: ArgumentParser.Result) throws { - if let versionOption = versionOption, arguments.get(versionOption) == true, let version = version { - stdoutStream.write("\(version)\n") - stdoutStream.flush() - return - } - guard let subparser = arguments.subparser(parser), - let command = commands.first(where: { $0.command == subparser }) else { - parser.printUsage(on: stdoutStream) - return - } - try command.run(with: arguments) - } -} diff --git a/Sources/stringray/Commands/CopyCommand.swift b/Sources/stringray/Commands/CopyCommand.swift index 72e86e7..fd0a81d 100644 --- a/Sources/stringray/Commands/CopyCommand.swift +++ b/Sources/stringray/Commands/CopyCommand.swift @@ -7,6 +7,7 @@ import Foundation import Utility +import CommandRegistry struct CopyCommand: Command { private struct Arguments { diff --git a/Sources/stringray/Commands/LintCommand.swift b/Sources/stringray/Commands/LintCommand.swift index 04376cf..d14e4e9 100644 --- a/Sources/stringray/Commands/LintCommand.swift +++ b/Sources/stringray/Commands/LintCommand.swift @@ -10,6 +10,7 @@ import Utility import Basic import SwiftyTextTable import XcodeProject +import CommandRegistry struct LintCommand: Command { private struct Arguments { diff --git a/Sources/stringray/Commands/MoveCommand.swift b/Sources/stringray/Commands/MoveCommand.swift index 9a5c52d..23fb359 100644 --- a/Sources/stringray/Commands/MoveCommand.swift +++ b/Sources/stringray/Commands/MoveCommand.swift @@ -7,6 +7,7 @@ import Foundation import Utility +import CommandRegistry struct MoveCommand: Command { private struct Arguments { diff --git a/Sources/stringray/Commands/RenameCommand.swift b/Sources/stringray/Commands/RenameCommand.swift index 830efc5..25c4499 100644 --- a/Sources/stringray/Commands/RenameCommand.swift +++ b/Sources/stringray/Commands/RenameCommand.swift @@ -7,6 +7,7 @@ import Foundation import Utility +import CommandRegistry struct RenameCommand: Command { private struct Arguments { diff --git a/Sources/stringray/Commands/SortCommand.swift b/Sources/stringray/Commands/SortCommand.swift index 4e8a949..0b58770 100644 --- a/Sources/stringray/Commands/SortCommand.swift +++ b/Sources/stringray/Commands/SortCommand.swift @@ -7,6 +7,7 @@ import Foundation import Utility +import CommandRegistry struct SortCommand: Command { private struct Arguments { diff --git a/Sources/stringray/main.swift b/Sources/stringray/main.swift index 820570b..c5e5bfa 100644 --- a/Sources/stringray/main.swift +++ b/Sources/stringray/main.swift @@ -8,8 +8,9 @@ import Foundation import Utility +import CommandRegistry -var registry = CommandRegistry(usage: " ", overview: "", version: Version(0, 1, 1)) +var registry = Registry(usage: " ", overview: "", version: Version(0, 1, 1)) registry.register(command: MoveCommand.self) registry.register(command: CopyCommand.self) registry.register(command: SortCommand.self)