Change attributes syntax input.

This commit is contained in:
Marek Fořt 2020-02-23 19:16:46 +01:00
parent 34f79b3271
commit a9907e3caf
4 changed files with 21 additions and 11 deletions

View File

@ -13,6 +13,10 @@ public func getAttribute(for name: String) throws -> String {
return value
}
public func generate(_ content: String) {
print(content)
}
public struct ParsedAttribute: Codable {
public init(name: String, value: String) {
self.name = name

View File

@ -5,6 +5,8 @@ import TuistCore
import TuistGenerator
import TuistLoader
import TuistSupport
import TuistTemplate
import struct TemplateDescription.ParsedAttribute
private typealias Platform = TuistCore.Platform
private typealias Product = TuistCore.Product
@ -48,15 +50,19 @@ class InitCommand: NSObject, Command {
let pathArgument: OptionArgument<String>
let nameArgument: OptionArgument<String>
let playgroundGenerator: PlaygroundGenerating
private let templateLoader: TemplateLoading
// MARK: - Init
public required convenience init(parser: ArgumentParser) {
self.init(parser: parser, playgroundGenerator: PlaygroundGenerator())
self.init(parser: parser,
playgroundGenerator: PlaygroundGenerator(),
templateLoader: TemplateLoader())
}
init(parser: ArgumentParser,
playgroundGenerator: PlaygroundGenerating) {
playgroundGenerator: PlaygroundGenerating,
templateLoader: TemplateLoading) {
let subParser = parser.add(subparser: InitCommand.command, overview: InitCommand.overview)
platformArgument = subParser.add(option: "--platform",
shortName: nil,
@ -78,6 +84,7 @@ class InitCommand: NSObject, Command {
usage: "The name of the project. If it's not passed (Default: Name of the directory).",
completion: nil)
self.playgroundGenerator = playgroundGenerator
self.templateLoader = templateLoader
}
func run(with arguments: ArgumentParser.Result) throws {

View File

@ -2,6 +2,7 @@ import Foundation
import TuistSupport
import TuistLoader
import TuistTemplate
import struct TemplateDescription.ParsedAttribute
import SPMUtility
import Basic
@ -40,7 +41,7 @@ class ScaffoldCommand: NSObject, Command {
attributesArgument = subParser.add(option: "--attributes",
shortName: "-a",
kind: [String].self,
strategy: .upToNextOption,
strategy: .remaining,
usage: "Attributes for a given template",
completion: nil)
self.templateLoader = templateLoader

View File

@ -140,14 +140,12 @@ public class TemplateLoader: TemplateLoading {
}
private func parseAttributes(_ attributes: [String]) -> [ParsedAttribute] {
attributes.map {
let splitAttributes = $0.components(separatedBy: "=")
// TODO: Error with proper format
guard splitAttributes.count == 2 else { fatalError() }
let name = splitAttributes[0]
let value = splitAttributes[1]
return ParsedAttribute(name: name, value: value)
}
let (options, values): ([String], [String]) = attributes
.reduce(([], [])) {
$1.starts(with: "--") ? ($0.0 + [String($1.dropFirst(2))], $0.1) : ($0.0, $0.1 + [$1])
}
return zip(options, values).map(ParsedAttribute.init)
}
}