-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor template generation to use common use case
- Loading branch information
Showing
8 changed files
with
168 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
Sources/FigmaAssetsFetch/Data/Templates/TemplateFileSystemRender.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Foundation | ||
import PathKit | ||
import Stencil | ||
|
||
public enum TemplateFileSystemRenderError: Error { | ||
case invalidTemplatePath | ||
case invalidOutputPath | ||
} | ||
|
||
public struct TemplateFileSystemRender { | ||
private let templatePath: String | ||
private let fileWriter: FileWriterType | ||
|
||
public init( | ||
templatePath: String, | ||
fileWriter: FileWriterType = FileWriter() | ||
) { | ||
self.templatePath = templatePath | ||
self.fileWriter = fileWriter | ||
} | ||
} | ||
|
||
extension TemplateFileSystemRender: ColorsRender { | ||
public func render(colors: [NamedColor], output: String) throws { | ||
guard let templateURL = URL(string: "file://\(templatePath)"), | ||
templateURL.lastPathComponent.contains(".stencil") | ||
else { | ||
throw TemplateFileSystemRenderError.invalidTemplatePath | ||
} | ||
|
||
guard let outputURL = URL(string: "file://\(output)") else { | ||
throw TemplateFileSystemRenderError.invalidOutputPath | ||
} | ||
|
||
let context = ["colors": colors] | ||
|
||
let environment = Environment( | ||
loader: FileSystemLoader( | ||
paths: [ | ||
Path(templateURL.deletingLastPathComponent().path), | ||
] | ||
) | ||
) | ||
|
||
let rendered = try environment.renderTemplate( | ||
name: templateURL.lastPathComponent, | ||
context: context | ||
) | ||
|
||
try fileWriter.write(content: rendered, at: outputURL) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Tests/FigmaAssetsFetchTests/DataTests/Templates/TemplateFileSystemRenderTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import XCTest | ||
|
||
@testable import FigmaAssetsFetch | ||
|
||
class TemplateFileSystemRenderTests: XCTestCase { | ||
func testTemplateRendering() throws { | ||
let mockFileWriter = FileWriterMock() | ||
let render = TemplateFileSystemRender( | ||
templatePath: Bundle.module.url(forResource: "TestTemplate", withExtension: "stencil")!.path, | ||
fileWriter: mockFileWriter | ||
) | ||
let namedColor = NamedColor( | ||
name: .init(name: "Bg / Primary"), | ||
value: .init(r: 0.424, g: 0.459, b: 0.49, a: 1) | ||
) | ||
|
||
try render.render(colors: [namedColor], output: "/Test.swift") | ||
|
||
assert(mockFileWriter.paths == ["/Test.swift"]) | ||
assert(mockFileWriter.contents == ["\nbgPrimary 0.424 0.459 0.49 1.0 6C757D\n\n"]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% for color in colors %} | ||
{{ color.name.camelCased }} {{ color.value.r }} {{ color.value.g }} {{ color.value.b }} {{ color.value.a }} {{ color.value.hexValue }} | ||
{% endfor %} |