This PR contains the work done to support input parameters for the `create` command of the executable target, and to render content dynamically for the newly-generated project. Reviewed-on: #4 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
51 lines
1.2 KiB
Swift
51 lines
1.2 KiB
Swift
import Foundation
|
|
import Mustache
|
|
|
|
public struct TemplateService {
|
|
|
|
// MARK: Properties
|
|
|
|
private let mustacheRenderer: MustacheLibrary
|
|
|
|
// MARK: Initialisers
|
|
|
|
public init(
|
|
bundle: Bundleable? = nil,
|
|
templateFolder: String
|
|
) async throws (TemplateServiceError) {
|
|
guard let pathResources = (bundle ?? Bundle.module).resourcePath else {
|
|
throw .resourcePathNotFound
|
|
}
|
|
|
|
let pathTemplates = pathResources + "/" + templateFolder
|
|
|
|
do {
|
|
self.mustacheRenderer = try await MustacheLibrary(directory: pathTemplates)
|
|
} catch {
|
|
throw .serviceNotInitialized
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - TemplateServicing
|
|
|
|
extension TemplateService: TemplateServicing {
|
|
|
|
// MARK: Functions
|
|
|
|
public func render(_ object: Any, on template: String) async throws (TemplateServiceError) -> String {
|
|
guard mustacheRenderer.getTemplate(named: template) != nil else {
|
|
throw .templateNotFound
|
|
}
|
|
|
|
guard let content = mustacheRenderer.render(object, withTemplate: template) else {
|
|
throw .contentNotRendered
|
|
}
|
|
|
|
return content
|
|
}
|
|
|
|
}
|
|
|