Skip to content

Commit

Permalink
Allow dynamic file names in template manifest (#385)
Browse files Browse the repository at this point in the history
* Allow dynamic file names in template manifest

By adding `dynamic_name` parameters to files or folders in
manifest.yml, their destination names can depend on variables or the
name of the new package.

* Only initialize one MustacheRenderer
  • Loading branch information
s-k authored May 20, 2022
1 parent 7c0b3b3 commit dbccf23
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions Sources/VaporToolbox/New/TemplateScaffolder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,37 @@ struct TemplateScaffolder {
}
}
}

let renderer = MustacheRenderer()

let destinationFileName: String
if let dynamicName = file.dynamicName {
destinationFileName = try renderer.render(template: dynamicName, data: context)
} else {
destinationFileName = file.name
}
let destinationPath = destination.appendingPathComponents(destinationFileName)

switch file.type {
case .file(let dynamic):
self.console.output("+ " + file.name.consoleText())
if dynamic {
let template = try String(contentsOf: source.appendingPathComponents(file.name).asFileURL, encoding: .utf8)
try MustacheRenderer().render(template: template, data: context)
.write(to: URL(fileURLWithPath: destination.appendingPathComponents(file.name)), atomically: true, encoding: .utf8)
try renderer.render(template: template, data: context)
.write(to: URL(fileURLWithPath: destinationPath), atomically: true, encoding: .utf8)
} else {
try FileManager.default.moveItem(
atPath: source.appendingPathComponents(file.name),
toPath: destination.appendingPathComponents(file.name))
toPath: destinationPath)
}
case .folder(let files):
let folder = file
try FileManager.default.createDirectory(atPath: destination.appendingPathComponents(folder.name), withIntermediateDirectories: false)
try FileManager.default.createDirectory(atPath: destinationPath, withIntermediateDirectories: false)
for file in files {
try self.scaffold(
file: file,
from: source.appendingPathComponents(folder.name).trailingSlash,
to: destination.appendingPathComponents(folder.name).trailingSlash,
to: destinationPath.trailingSlash,
context: context
)
}
Expand Down Expand Up @@ -221,6 +231,7 @@ struct TemplateManifest: Decodable {
case exists(variable: String)
}
var name: String
var dynamicName: String?
var condition: Condition?
var type: Kind

Expand All @@ -231,6 +242,7 @@ struct TemplateManifest: Decodable {
case dynamic
case condition
case `if`
case dynamic_name
}

init(from decoder: Decoder) throws {
Expand All @@ -249,6 +261,7 @@ struct TemplateManifest: Decodable {
if let variable = try container.decodeIfPresent(String.self, forKey: .if) {
self.condition = .exists(variable: variable)
}
self.dynamicName = try container.decodeIfPresent(String.self, forKey: .dynamic_name)
}
}
}
Expand Down

0 comments on commit dbccf23

Please sign in to comment.