From 0439437bb324308f8b57960dbe06937c5a24d03c Mon Sep 17 00:00:00 2001 From: Alex Guretzki Date: Thu, 14 Nov 2024 11:52:34 +0100 Subject: [PATCH] Moving ExpressibleByArgument extensions to the same file --- ...ommandLineTool+ExpressibleByArgument.swift | 78 +++++++++++++++++++ .../CommandLineTool+Extensions.swift | 32 -------- .../ProjectToOutputCommand.swift | 19 ----- 3 files changed, 78 insertions(+), 51 deletions(-) create mode 100644 Sources/ExecutableTargets/CommandLineTool/CommandLineTool+ExpressibleByArgument.swift delete mode 100644 Sources/ExecutableTargets/CommandLineTool/CommandLineTool+Extensions.swift diff --git a/Sources/ExecutableTargets/CommandLineTool/CommandLineTool+ExpressibleByArgument.swift b/Sources/ExecutableTargets/CommandLineTool/CommandLineTool+ExpressibleByArgument.swift new file mode 100644 index 0000000..fdc4829 --- /dev/null +++ b/Sources/ExecutableTargets/CommandLineTool/CommandLineTool+ExpressibleByArgument.swift @@ -0,0 +1,78 @@ +// +// Copyright (c) 2024 Adyen N.V. +// +// This file is open source and available under the MIT license. See the LICENSE file for more info. +// + +import ArgumentParser + +import PADLogging +import PADSwiftInterfaceFileLocator +import PADProjectBuilder + +extension SwiftInterfaceType: ExpressibleByArgument { + public init?(argument: String) { + let mapping: [String: Self] = [ + "public": .package, + "private": .private, + "package": .package + ] + + if let match = mapping.value(forArgument: argument) { + self = match + } else { + return nil + } + } +} + +extension LogLevel: ExpressibleByArgument { + public init?(argument: String) { + let mapping: [String: Self] = [ + "quiet": .quiet, + "default": .default, + "debug": .debug + ] + + if let match = mapping.value(forArgument: argument) { + self = match + } else { + return nil + } + } +} + +extension ProjectPlatform: ExpressibleByArgument { + public init?(argument: String) { + let mapping: [String: Self] = [ + "iOS": .iOS, + "macOS": .macOS + ] + + if let match = mapping.value(forArgument: argument) { + self = match + } else { + return nil + } + } +} + +// MARK: - Convenience + +fileprivate extension Dictionary where Key == String { + func value(forArgument argument: String) -> Value? { + for (key, value) in self { + if argument.caseInsensitiveEquals(key) { + return value + } + } + + return nil + } +} + +fileprivate extension String { + func caseInsensitiveEquals(_ other: String) -> Bool { + self.compare(other, options: .caseInsensitive) == .orderedSame + } +} diff --git a/Sources/ExecutableTargets/CommandLineTool/CommandLineTool+Extensions.swift b/Sources/ExecutableTargets/CommandLineTool/CommandLineTool+Extensions.swift deleted file mode 100644 index 77ca052..0000000 --- a/Sources/ExecutableTargets/CommandLineTool/CommandLineTool+Extensions.swift +++ /dev/null @@ -1,32 +0,0 @@ -// -// Copyright (c) 2024 Adyen N.V. -// -// This file is open source and available under the MIT license. See the LICENSE file for more info. -// - -import ArgumentParser - -import PADLogging -import PADSwiftInterfaceFileLocator - -extension SwiftInterfaceType: ExpressibleByArgument { - public init?(argument: String) { - switch argument { - case "public": self = .public - case "private": self = .private - case "package": self = .package - default: return nil - } - } -} - -extension LogLevel: ExpressibleByArgument { - public init?(argument: String) { - switch argument { - case "quiet": self = .quiet - case "default": self = .default - case "debug": self = .debug - default: return nil - } - } -} diff --git a/Sources/ExecutableTargets/CommandLineTool/ProjectToOutputCommand.swift b/Sources/ExecutableTargets/CommandLineTool/ProjectToOutputCommand.swift index 3002b24..5815c6f 100644 --- a/Sources/ExecutableTargets/CommandLineTool/ProjectToOutputCommand.swift +++ b/Sources/ExecutableTargets/CommandLineTool/ProjectToOutputCommand.swift @@ -213,22 +213,3 @@ private extension ProjectToOutputCommand { ) } } - -extension ProjectPlatform: ExpressibleByArgument { - - static var mapping: [String: ProjectPlatform] = [ - "iOS": .iOS, - "macOS": .macOS - ] - - public init?(argument: String) { - for (key, value) in Self.mapping { - if argument.compare(key, options: .caseInsensitive) == .orderedSame { - self = value - return - } - } - - return nil - } -}