Skip to content

Commit

Permalink
Add Kebab case support to TemplateScaffolder context (#353)
Browse files Browse the repository at this point in the history
* Add name_lower_kebab to context

* Use name_kebab and correct kebab case requirements

* Undo Update Sources/VaporToolbox/New/TemplateScaffolder.swift

Co-authored-by: Gwynne Raskind <[email protected]>

* Use flatmap

Use flatmap and renamed to kababcased to keep in line with swift naming convention (like lowercased)

Co-authored-by: Gwynne Raskind <[email protected]>
  • Loading branch information
marzvrover and gwynne authored Apr 6, 2021
1 parent 1d858e3 commit 4a2b9ed
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions Sources/VaporToolbox/New/TemplateScaffolder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Mustache
struct TemplateScaffolder {
let console: Console
let manifest: TemplateManifest

init(console: Console, manifest: TemplateManifest) {
self.console = console
self.manifest = manifest
Expand All @@ -17,6 +17,7 @@ struct TemplateScaffolder {
assert(destination.hasPrefix("/"))
var context: [String: MustacheData] = [:]
context["name"] = .string(name)
context["name_kebab"] = .string(name.kebabcased())
self.console.output(key: "name", value: name)
for variable in self.manifest.variables {
try self.ask(variable: variable, to: &context, using: &input)
Expand All @@ -29,7 +30,7 @@ struct TemplateScaffolder {

private func ask(
variable: TemplateManifest.Variable,
to context: inout [String: MustacheData],
to context: inout [String: MustacheData],
using input: inout CommandInput,
prefix: String = ""
) throws {
Expand All @@ -50,7 +51,7 @@ struct TemplateScaffolder {
} else {
confirm = self.console.confirm("\(variable.description) \("(--\(optionName)/--no-\(optionName))", style: .info)")
}

if confirm {
context[variable.name] = .string(true.description)
self.console.output(key: variable.name, value: "Yes")
Expand All @@ -67,7 +68,7 @@ struct TemplateScaffolder {
let name = input.arguments[next]
input.arguments.remove(at: next)
input.arguments.remove(at: index)
guard let found = options.filter({
guard let found = options.filter({
$0.name.lowercased().hasPrefix(name.lowercased())
}).first else {
throw "No --\(optionName) option matching '\(name)'"
Expand Down Expand Up @@ -123,7 +124,7 @@ struct TemplateScaffolder {
}
}
}

switch file.type {
case .file(let dynamic):
self.console.output("+ " + file.name.consoleText())
Expand Down Expand Up @@ -151,6 +152,15 @@ struct TemplateScaffolder {
}
}

fileprivate extension StringProtocol {
func kebabcased() -> String {
return .init(self
.flatMap { $0.isWhitespace ? "-" : "\($0)" }
.enumerated().flatMap { $0 > 0 && $1.isUppercase ? "-\($1.lowercased())" : "\($1.lowercased())" }
)
}
}

extension Console {
func output(key: String, value: String) {
self.output(key.consoleText() + ": " + value.consoleText(.info))
Expand Down

0 comments on commit 4a2b9ed

Please sign in to comment.