From c60a484b8ef5e358f0e4105fd9e41ee748d77ac0 Mon Sep 17 00:00:00 2001 From: stevapple Date: Mon, 25 May 2020 12:04:27 +0800 Subject: [PATCH 1/3] Update RoutesCommand to align with RoutingKit --- Sources/Vapor/Commands/RoutesCommand.swift | 38 ++++++++++++---------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/Sources/Vapor/Commands/RoutesCommand.swift b/Sources/Vapor/Commands/RoutesCommand.swift index 0cfa41f007..aa85ee85fb 100644 --- a/Sources/Vapor/Commands/RoutesCommand.swift +++ b/Sources/Vapor/Commands/RoutesCommand.swift @@ -10,7 +10,7 @@ /// A colon preceding a path component indicates a variable parameter. A colon with no text following /// is a parameter whose result will be discarded. /// -/// An asterisk indicates a catch-all. Any path components after a catch-all will be discarded and ignored. +/// The path will be displayed with the same syntax that is used to register a route. public final class RoutesCommand: Command { public struct Signature: CommandSignature { public init() { } @@ -25,25 +25,16 @@ public final class RoutesCommand: Command { public func run(using context: CommandContext, signature: Signature) throws { let routes = context.application.routes let includeDescription = !routes.all.filter { $0.userInfo["description"] != nil }.isEmpty + let pathSeparator = "/".consoleText() context.console.outputASCIITable(routes.all.map { route -> [ConsoleText] in - var pathText: ConsoleText = "" - if route.path.isEmpty { - pathText += "/".consoleText(.info) - } - for path in route.path { - pathText += "/".consoleText(.info) - switch path { - case .constant(let string): - pathText += string.consoleText() - case .parameter(let name): - pathText += ":".consoleText(.info) - pathText += name.consoleText() - case .anything: - pathText += ":".consoleText(.info) - case .catchall: - pathText += "*".consoleText(.info) + let pathText = pathSeparator + route.path.map { + switch $0 { + case .constant: + return $0.description.consoleText() + default: + return $0.description.consoleText(.info) } - } + }.joined(separator: pathSeparator) var column = [route.method.string.consoleText(), pathText] if includeDescription { let desc = route.userInfo["description"] @@ -56,6 +47,17 @@ public final class RoutesCommand: Command { } } +extension Collection where Element == ConsoleText { + func joined(separator: ConsoleText) -> ConsoleText { + guard let result: ConsoleText = self.first else { + return "" + } + return self.dropFirst().reduce(into: result) { + $0 += separator + $1 + } + } +} + extension Console { func outputASCIITable(_ rows: [[ConsoleText]]) { var columnWidths: [Int] = [] From 5c711ab92cbd665c150afe27699389e2a9be9e98 Mon Sep 17 00:00:00 2001 From: stevapple Date: Tue, 26 May 2020 17:15:32 +0800 Subject: [PATCH 2/3] Simplify the implementation --- Sources/Vapor/Commands/RoutesCommand.swift | 27 ++++++++-------------- 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/Sources/Vapor/Commands/RoutesCommand.swift b/Sources/Vapor/Commands/RoutesCommand.swift index aa85ee85fb..a5eacd87c6 100644 --- a/Sources/Vapor/Commands/RoutesCommand.swift +++ b/Sources/Vapor/Commands/RoutesCommand.swift @@ -27,14 +27,16 @@ public final class RoutesCommand: Command { let includeDescription = !routes.all.filter { $0.userInfo["description"] != nil }.isEmpty let pathSeparator = "/".consoleText() context.console.outputASCIITable(routes.all.map { route -> [ConsoleText] in - let pathText = pathSeparator + route.path.map { - switch $0 { - case .constant: - return $0.description.consoleText() - default: - return $0.description.consoleText(.info) + let pathText = route.path.isEmpty ? pathSeparator : route.path.map { + switch $0 { + case .constant: + return $0.description.consoleText() + default: + return $0.description.consoleText(.info) + } } - }.joined(separator: pathSeparator) + .map { pathSeparator + $0 } + .reduce("".consoleText(), +) var column = [route.method.string.consoleText(), pathText] if includeDescription { let desc = route.userInfo["description"] @@ -47,17 +49,6 @@ public final class RoutesCommand: Command { } } -extension Collection where Element == ConsoleText { - func joined(separator: ConsoleText) -> ConsoleText { - guard let result: ConsoleText = self.first else { - return "" - } - return self.dropFirst().reduce(into: result) { - $0 += separator + $1 - } - } -} - extension Console { func outputASCIITable(_ rows: [[ConsoleText]]) { var columnWidths: [Int] = [] From 2e409069b2da180aff6265b81d4743bc8a4140cd Mon Sep 17 00:00:00 2001 From: stevapple Date: Tue, 26 May 2020 17:28:16 +0800 Subject: [PATCH 3/3] Move consoleText() conversion to PathComponent --- Sources/Vapor/Commands/RoutesCommand.swift | 31 ++++++++++++++-------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/Sources/Vapor/Commands/RoutesCommand.swift b/Sources/Vapor/Commands/RoutesCommand.swift index a5eacd87c6..f8c6191ee9 100644 --- a/Sources/Vapor/Commands/RoutesCommand.swift +++ b/Sources/Vapor/Commands/RoutesCommand.swift @@ -27,17 +27,15 @@ public final class RoutesCommand: Command { let includeDescription = !routes.all.filter { $0.userInfo["description"] != nil }.isEmpty let pathSeparator = "/".consoleText() context.console.outputASCIITable(routes.all.map { route -> [ConsoleText] in - let pathText = route.path.isEmpty ? pathSeparator : route.path.map { - switch $0 { - case .constant: - return $0.description.consoleText() - default: - return $0.description.consoleText(.info) - } - } - .map { pathSeparator + $0 } - .reduce("".consoleText(), +) - var column = [route.method.string.consoleText(), pathText] + var column = [route.method.string.consoleText()] + if route.path.isEmpty { + column.append(pathSeparator) + } else { + column.append(route.path + .map { pathSeparator + $0.consoleText() } + .reduce("".consoleText(), +) + ) + } if includeDescription { let desc = route.userInfo["description"] .flatMap { $0 as? String } @@ -49,6 +47,17 @@ public final class RoutesCommand: Command { } } +extension PathComponent { + func consoleText() -> ConsoleText { + switch self { + case .constant: + return description.consoleText() + default: + return description.consoleText(.info) + } + } +} + extension Console { func outputASCIITable(_ rows: [[ConsoleText]]) { var columnWidths: [Int] = []