Skip to content

Commit

Permalink
allow array option to be a string with commas
Browse files Browse the repository at this point in the history
  • Loading branch information
yonaskolb committed Jun 30, 2023
1 parent b293adb commit fe00b6c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ Options are structured input for the `Stencil` templates. They serve as document
- `string` a simple string
- `boolean` a boolean
- `choice` a string from a list of choices. Requires `choices` to be defined
- `array` an array of other options. Requires `options` to be defined.
- `array` an array of other options. If child `options` are defined, if will be an array of options, otherwise it will split a string by command, removing any whitespace between
- **options**: An array of child options. Used if the `array` type contains objects
#### Files
Expand Down
6 changes: 3 additions & 3 deletions Sources/GenesisKit/Option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct Option: Equatable, Decodable {
public var set: String
public var choices: [String]
public var childType: OptionType?
public var options: [Option]
public var options: [Option]?

public enum OptionType: String, Decodable {
case string
Expand All @@ -20,7 +20,7 @@ public struct Option: Equatable, Decodable {
case array
}

public init(name: String, description: String? = nil, value: String? = nil, type: OptionType = .string, set: String? = nil, question: String? = nil, required: Bool = false, choices: [String] = [], branch: [String: TemplateSection] = [:], options: [Option] = []) {
public init(name: String, description: String? = nil, value: String? = nil, type: OptionType = .string, set: String? = nil, question: String? = nil, required: Bool = false, choices: [String] = [], branch: [String: TemplateSection] = [:], options: [Option]? = nil) {
self.name = name
self.value = value
self.description = description
Expand Down Expand Up @@ -61,6 +61,6 @@ public struct Option: Equatable, Decodable {
required = try container.decodeIfPresent(Bool.self, forKey: .required) ?? false
question = try container.decodeIfPresent(String.self, forKey: .question)
branch = try container.decodeIfPresent([String: TemplateSection].self, forKey: .branch) ?? [:]
options = try container.decodeIfPresent([Option].self, forKey: .options) ?? []
options = try container.decodeIfPresent([Option].self, forKey: .options)
}
}
36 changes: 26 additions & 10 deletions Sources/GenesisKit/TemplateGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ public class TemplateGenerator {
}

fileprivate func getOptionValue(_ option: Option, context: inout Context) throws {
defer {
switch option.type {
case .array:
// split string into array
if let value = context[option.name] as? String, option.options == nil {
context[option.name] = value.split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }
}
default:
break
}
}

if context[option.name] != nil {
// found existing option
return
Expand All @@ -58,19 +70,23 @@ public class TemplateGenerator {
case .string: context[option.name] = Input.readLine(prompt: question)
case .boolean: context[option.name] = Input.readBool(prompt: question)
case .array:
var array: [Context] = []
func addItem() throws {
if Input.readBool(prompt: question) {
var childContext = Context()
for childOption in option.options {
try getOptionValue(childOption, context: &childContext)
if let options = option.options {
var array: [Context] = []
func addItem() throws {
if Input.readBool(prompt: question) {
var childContext = Context()
for childOption in options {
try getOptionValue(childOption, context: &childContext)
}
array.append(childContext)
context[option.name] = array
try addItem()
}
array.append(childContext)
context[option.name] = array
try addItem()
}
try addItem()
} else {
context[option.name] = Input.readLine(prompt: question)
}
try addItem()
}

answers.append(Answer(question: question, answer: context[option.name] as Any))
Expand Down
3 changes: 3 additions & 0 deletions Tests/Fixtures/Project.stencil
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

Name: {{project}}
Some content
{% for name in names %}
- {{ name }}
{% endfor %}
2 changes: 2 additions & 0 deletions Tests/Fixtures/generated/MyProject.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

Name: MyProject
Some content
- Sarah
- Stephen
2 changes: 2 additions & 0 deletions Tests/Fixtures/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ options:
required: true
type: string
value: Parent/Child
- name: names
type: array
files:
- template: Project.stencil
path: "{{ project }}.swift"
Expand Down
1 change: 1 addition & 0 deletions Tests/GenesisKitTests/TemplateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class TemplateTests: XCTestCase {
Option(name: "generate", value: "true", type: .boolean, question: "Should this be generated?"),
]),
Option(name: "path", description: "The name of the path to App directory", value: "Parent/Child", type: .string, question: "What is the name of the path for App directory?", required: true),
Option(name: "names", type: .array),
]
let expectedTemplate = GenesisTemplate(
path: templateFixture,
Expand Down
1 change: 1 addition & 0 deletions Tests/GenesisKitTests/WritingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class WritingTests: XCTestCase {
]
],
"path": "App/Child",
"names": "Sarah, Stephen"
]
let result = try generator.generate(context: context, interactive: false)
try result.writeFiles(path: fixturePath + "generated")
Expand Down

0 comments on commit fe00b6c

Please sign in to comment.