refactor: TemplateLoader to protocol, follow Swift API guidelines

This commit is contained in:
Kyle Fuller
2016-11-28 03:50:53 +00:00
parent 5ca1b78854
commit 429290e0b7
9 changed files with 65 additions and 34 deletions

View File

@@ -27,9 +27,8 @@ class IncludeNode : NodeType {
throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
}
guard let template = loader.loadTemplate(templateName) else {
let paths = loader.paths.map { $0.description }.joined(separator: ", ")
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
guard let template = try loader.loadTemplate(name: templateName) else {
throw TemplateSyntaxError("'\(templateName)' template not found")
}
return try template.render(context)

View File

@@ -67,9 +67,8 @@ class ExtendsNode : NodeType {
throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
}
guard let template = loader.loadTemplate(templateName) else {
let paths: String = loader.paths.map { $0.description }.joined(separator: ", ")
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
guard let template = try loader.loadTemplate(name: templateName) else {
throw TemplateSyntaxError("'\(templateName)' template not found")
}
let blockContext: BlockContext

View File

@@ -9,6 +9,12 @@ let NSFileNoSuchFileError = 4
public class Template: ExpressibleByStringLiteral {
let tokens: [Token]
/// Create a template with a template string
public init(templateString: String) {
let lexer = Lexer(templateString: templateString)
tokens = lexer.tokenize()
}
/// Create a template with the given name inside the given bundle
public convenience init(named:String, inBundle bundle:Bundle? = nil) throws {
let useBundle = bundle ?? Bundle.main
@@ -25,28 +31,22 @@ public class Template: ExpressibleByStringLiteral {
}
/// Create a template with a file found at the given path
public convenience init(path:Path) throws {
public convenience init(path: Path) throws {
self.init(templateString: try path.read())
}
/// Create a template with a template string
public init(templateString:String) {
let lexer = Lexer(templateString: templateString)
tokens = lexer.tokenize()
}
// Create a template with a template string literal
public convenience required init(stringLiteral value:String) {
public convenience required init(stringLiteral value: String) {
self.init(templateString: value)
}
// Create a template with a template string literal
public convenience required init(extendedGraphemeClusterLiteral value:StringLiteralType) {
public convenience required init(extendedGraphemeClusterLiteral value: StringLiteralType) {
self.init(stringLiteral: value)
}
// Create a template with a template string literal
public convenience required init(unicodeScalarLiteral value:StringLiteralType) {
public convenience required init(unicodeScalarLiteral value: StringLiteralType) {
self.init(stringLiteral: value)
}

View File

@@ -2,8 +2,29 @@ import Foundation
import PathKit
public protocol TemplateLoader {
func loadTemplate(name: String) throws -> Template?
func loadTemplate(names: [String]) throws -> Template?
}
extension TemplateLoader {
func loadTemplate(names: [String]) throws -> Template? {
for name in names {
let template = try loadTemplate(name: name)
if template != nil {
return template
}
}
return nil
}
}
// A class for loading a template from disk
public class TemplateLoader {
public class FileSystemLoader: TemplateLoader {
public let paths: [Path]
public init(paths: [Path]) {
@@ -16,19 +37,25 @@ public class TemplateLoader {
}
}
public func loadTemplate(_ templateName: String) -> Template? {
return loadTemplate([templateName])
public func loadTemplate(name: String) throws -> Template? {
for path in paths {
let templatePath = path + Path(name)
if templatePath.exists {
return try Template(path: templatePath)
}
}
return nil
}
public func loadTemplate(_ templateNames: [String]) -> Template? {
public func loadTemplate(names: [String]) throws -> Template? {
for path in paths {
for templateName in templateNames {
for templateName in names {
let templatePath = path + Path(templateName)
if templatePath.exists {
if let template = try? Template(path: templatePath) {
return template
}
return try Template(path: templatePath)
}
}
}