From dbccf23e87e855a6d5a01b8c000192dd818ab4cc Mon Sep 17 00:00:00 2001 From: Stephen Kockentiedt Date: Fri, 20 May 2022 11:30:02 +0200 Subject: [PATCH] Allow dynamic file names in template manifest (#385) * 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 --- .../VaporToolbox/New/TemplateScaffolder.swift | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Sources/VaporToolbox/New/TemplateScaffolder.swift b/Sources/VaporToolbox/New/TemplateScaffolder.swift index 3f7673ff..b3fde682 100644 --- a/Sources/VaporToolbox/New/TemplateScaffolder.swift +++ b/Sources/VaporToolbox/New/TemplateScaffolder.swift @@ -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 ) } @@ -221,6 +231,7 @@ struct TemplateManifest: Decodable { case exists(variable: String) } var name: String + var dynamicName: String? var condition: Condition? var type: Kind @@ -231,6 +242,7 @@ struct TemplateManifest: Decodable { case dynamic case condition case `if` + case dynamic_name } init(from decoder: Decoder) throws { @@ -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) } } }