|
| 1 | +// |
| 2 | +// Command.swift |
| 3 | +// CommandLineKit |
| 4 | +// |
| 5 | +// Created by Matthias Zenger on 20/08/2023. |
| 6 | +// Copyright © 2023 Matthias Zenger. All rights reserved. |
| 7 | +// |
| 8 | +// Redistribution and use in source and binary forms, with or without |
| 9 | +// modification, are permitted provided that the following conditions are met: |
| 10 | +// |
| 11 | +// * Redistributions of source code must retain the above copyright notice, |
| 12 | +// this list of conditions and the following disclaimer. |
| 13 | +// |
| 14 | +// * Redistributions in binary form must reproduce the above copyright notice, |
| 15 | +// this list of conditions and the following disclaimer in the documentation |
| 16 | +// and/or other materials provided with the distribution. |
| 17 | +// |
| 18 | +// * Neither the name of the copyright holder nor the names of its contributors |
| 19 | +// may be used to endorse or promote products derived from this software without |
| 20 | +// specific prior written permission. |
| 21 | +// |
| 22 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 23 | +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 24 | +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 26 | +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 27 | +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 29 | +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +// |
| 33 | + |
| 34 | +import Foundation |
| 35 | + |
| 36 | +/// |
| 37 | +/// The command protocol implements an API for command-line tools. A `Flags` object |
| 38 | +/// gets automatically instantiated and flags, declared with flag property wrappers, |
| 39 | +/// are automatically registered with the `Flags` object. If flag parsing results in |
| 40 | +/// an error, the `fail(with: String)` method is called. Otherwise, `run()` gets |
| 41 | +/// executed. |
| 42 | +/// |
| 43 | +public protocol Command { |
| 44 | + static var name: String { get } |
| 45 | + static var arguments: [String] { get } |
| 46 | + init() |
| 47 | + mutating func run() throws |
| 48 | + mutating func fail(with: String) throws |
| 49 | +} |
| 50 | + |
| 51 | +extension Command { |
| 52 | + |
| 53 | + public mutating func fail(with reason: String) { |
| 54 | + print(reason) |
| 55 | + exit(1) |
| 56 | + } |
| 57 | + |
| 58 | + public static var name: String { |
| 59 | + if let toolPath = CommandLine.arguments.first { |
| 60 | + return URL(fileURLWithPath: toolPath).lastPathComponent |
| 61 | + } else { |
| 62 | + let str = String(describing: self) |
| 63 | + if let i = str.firstIndex(of: "("), i > str.startIndex, i < str.endIndex { |
| 64 | + return String(str[str.startIndex..<i]) |
| 65 | + } else { |
| 66 | + return str |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static var arguments: [String] { |
| 72 | + var args = CommandLine.arguments |
| 73 | + args.removeFirst() |
| 74 | + return args |
| 75 | + } |
| 76 | + |
| 77 | + public static func newFlags() -> Flags { |
| 78 | + return Flags(toolName: Self.name, arguments: Self.arguments) |
| 79 | + } |
| 80 | + |
| 81 | + public static func argumentNameConverter() -> ArgumentNameConverter { |
| 82 | + return ArgumentNameConverter(Self.argumentNamingStrategy()) |
| 83 | + } |
| 84 | + |
| 85 | + public static func argumentNamingStrategy() -> ArgumentNameConverter.Strategy { |
| 86 | + return .lowercase |
| 87 | + } |
| 88 | + |
| 89 | + public static func main() throws { |
| 90 | + var command = Self() |
| 91 | + let flags = Self.newFlags() |
| 92 | + let converter = Self.argumentNameConverter() |
| 93 | + let children = Mirror(reflecting: command).children |
| 94 | + for child in children { |
| 95 | + if let wrapper = child.value as? FlagWrapper { |
| 96 | + if let label = child.label { |
| 97 | + if label.hasPrefix("_") { |
| 98 | + wrapper.register(as: converter.convert(String(label.dropFirst())), with: flags) |
| 99 | + } else { |
| 100 | + wrapper.register(as: converter.convert(label), with: flags) |
| 101 | + } |
| 102 | + } else { |
| 103 | + wrapper.register(as: nil, with: flags) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + if let reason = flags.parsingFailure() { |
| 108 | + try command.fail(with: reason) |
| 109 | + } else { |
| 110 | + try command.run() |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +public class ArgumentNameConverter { |
| 116 | + |
| 117 | + public enum Strategy { |
| 118 | + case camelcase |
| 119 | + case lowercase |
| 120 | + case separate(Character) |
| 121 | + } |
| 122 | + |
| 123 | + public let strategy: Strategy |
| 124 | + public let prefix: String |
| 125 | + |
| 126 | + public init(_ strategy: Strategy = .lowercase, prefix: String = "") { |
| 127 | + self.strategy = strategy |
| 128 | + self.prefix = prefix |
| 129 | + } |
| 130 | + |
| 131 | + public func convert(_ name: String) -> String { |
| 132 | + guard !name.isEmpty else { |
| 133 | + return "" |
| 134 | + } |
| 135 | + switch self.strategy { |
| 136 | + case .camelcase: |
| 137 | + if prefix.isEmpty { |
| 138 | + return name |
| 139 | + } else { |
| 140 | + return prefix + name.prefix(1).capitalized + name.dropFirst() |
| 141 | + } |
| 142 | + case .lowercase: |
| 143 | + return (prefix + name).lowercased() |
| 144 | + case .separate(let separator): |
| 145 | + var index = name.startIndex |
| 146 | + var separate = true |
| 147 | + var res = "" |
| 148 | + while index < name.endIndex { |
| 149 | + let character = name[index] |
| 150 | + if character.isUppercase { |
| 151 | + if separate && !res.isEmpty { |
| 152 | + res.append(separator) |
| 153 | + } |
| 154 | + let next = name.index(after: index) |
| 155 | + separate = next < name.endIndex && |
| 156 | + name[next].isUppercase && |
| 157 | + name.index(after: next) < name.endIndex && |
| 158 | + name[name.index(after: next)].isLowercase |
| 159 | + } else { |
| 160 | + separate = character != separator |
| 161 | + } |
| 162 | + res += character.lowercased() |
| 163 | + index = name.index(after: index) |
| 164 | + } |
| 165 | + return res |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments