From f6c5b90c03cd69d96c9f839576bf75aa70273343 Mon Sep 17 00:00:00 2001 From: Daniil Subbotin Date: Sat, 19 Feb 2022 12:30:51 +0300 Subject: [PATCH] Use original Stencil instead of fork (#150) --- Package.resolved | 6 ++--- Package.swift | 5 ++-- .../AndroidExport/AndroidColorExporter.swift | 4 +-- .../AndroidComposeIconExporter.swift | 2 +- Sources/AndroidExport/AndroidExporter.swift | 6 ++--- .../AndroidTypographyExporter.swift | 4 +-- .../AndroidExport/Resources/Colors.kt.stencil | 1 - .../Resources/colors.xml.stencil | 6 ++--- .../Resources/typography.xml.stencil | 6 ++--- .../Resources/Color+extension.swift.stencil | 13 ++-------- .../Resources/Image+extension.swift.stencil | 7 +----- .../Image+extension.swift.stencil.include | 7 +----- .../Resources/UIColor+extension.swift.stencil | 25 ++++--------------- .../Resources/UIImage+extension.swift.stencil | 7 +----- .../UIImage+extension.swift.stencil.include | 7 +----- Sources/XcodeExport/XcodeColorExporter.swift | 7 ++---- Sources/XcodeExport/XcodeExporterBase.swift | 6 ++--- .../XcodeExport/XcodeImagesExporterBase.swift | 13 +++++----- .../XcodeExport/XcodeTypographyExporter.swift | 10 ++++---- .../XcodeColorExporterTests.swift | 6 +---- 20 files changed, 43 insertions(+), 105 deletions(-) diff --git a/Package.resolved b/Package.resolved index a9964cb..1fc210c 100644 --- a/Package.resolved +++ b/Package.resolved @@ -30,11 +30,11 @@ }, { "package": "Stencil", - "repositoryURL": "https://github.com/subdan/Stencil.git", + "repositoryURL": "https://github.com/stencilproject/Stencil.git", "state": { "branch": null, - "revision": "0dc84149c32d8bfac23104202249ac3462869cb2", - "version": "0.14.5" + "revision": "ccd9402682f4c07dac9561befd207c8156e80e20", + "version": "0.14.2" } }, { diff --git a/Package.swift b/Package.swift index e582e0a..dfb52e2 100644 --- a/Package.swift +++ b/Package.swift @@ -15,10 +15,9 @@ let package = Package( .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"), .package(url: "https://github.com/jpsim/Yams.git", from: "4.0.0"), .package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"), - // Revert when PR https://github.com/stencilproject/Stencil/pull/287 will be merged - .package(url: "https://github.com/subdan/Stencil.git", from: "0.14.5"), + .package(url: "https://github.com/stencilproject/Stencil.git", from: "0.14.2"), .package(url: "https://github.com/tuist/XcodeProj.git", from: "8.5.0"), - .package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "0.3.0"), + .package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "0.3.0") ], targets: [ diff --git a/Sources/AndroidExport/AndroidColorExporter.swift b/Sources/AndroidExport/AndroidColorExporter.swift index d15a51e..1bcf7ca 100644 --- a/Sources/AndroidExport/AndroidColorExporter.swift +++ b/Sources/AndroidExport/AndroidColorExporter.swift @@ -61,7 +61,7 @@ final public class AndroidColorExporter: AndroidExporter { "colors": colors ] - let env = makeEnvironment(trimBehavior: .smart) + let env = makeEnvironment() return try env.renderTemplate(name: "colors.xml.stencil", context: context) } @@ -84,7 +84,7 @@ final public class AndroidColorExporter: AndroidExporter { "colors": colors ] - let env = makeEnvironment(trimBehavior: .smart) + let env = makeEnvironment() let string = try env.renderTemplate(name: "Colors.kt.stencil", context: context) let fileURL = URL(string: "Colors.kt")! diff --git a/Sources/AndroidExport/AndroidComposeIconExporter.swift b/Sources/AndroidExport/AndroidComposeIconExporter.swift index 0fbb2a2..558560e 100644 --- a/Sources/AndroidExport/AndroidComposeIconExporter.swift +++ b/Sources/AndroidExport/AndroidComposeIconExporter.swift @@ -37,7 +37,7 @@ final public class AndroidComposeIconExporter: AndroidExporter { "xmlResourcePackage": xmlResourcePackage, "icons": icons ] - let env = makeEnvironment(trimBehavior: .none) + let env = makeEnvironment() return try env.renderTemplate(name: "Icons.kt.stencil", context: context) } } diff --git a/Sources/AndroidExport/AndroidExporter.swift b/Sources/AndroidExport/AndroidExporter.swift index eeab017..0499104 100644 --- a/Sources/AndroidExport/AndroidExporter.swift +++ b/Sources/AndroidExport/AndroidExporter.swift @@ -11,7 +11,7 @@ public class AndroidExporter { self.templatesPath = templatesPath } - func makeEnvironment(trimBehavior: TrimBehavior) -> Environment { + func makeEnvironment() -> Environment { let loader: FileSystemLoader if let templateURL = templatesPath { loader = FileSystemLoader(paths: [Path(templateURL.path)]) @@ -21,9 +21,7 @@ public class AndroidExporter { Path(Bundle.module.resourcePath!) ]) } - var environment = Environment(loader: loader) - environment.trimBehavior = trimBehavior - return environment + return Environment(loader: loader) } func makeFileContents(for string: String, directory: URL, file: URL) throws -> FileContents { diff --git a/Sources/AndroidExport/AndroidTypographyExporter.swift b/Sources/AndroidExport/AndroidTypographyExporter.swift index b5b0d12..542913c 100644 --- a/Sources/AndroidExport/AndroidTypographyExporter.swift +++ b/Sources/AndroidExport/AndroidTypographyExporter.swift @@ -50,7 +50,7 @@ final public class AndroidTypographyExporter: AndroidExporter { let context: [String: Any] = [ "textStyles": fonts ] - let env = makeEnvironment(trimBehavior: .smart) + let env = makeEnvironment() let contents = try env.renderTemplate(name: "typography.xml.stencil", context: context) let directoryURL = output.xmlOutputDirectory.appendingPathComponent("values") @@ -81,7 +81,7 @@ final public class AndroidTypographyExporter: AndroidExporter { "package": package, "xmlResourcePackage": xmlResourcePackage ] - let env = makeEnvironment(trimBehavior: .none) + let env = makeEnvironment() let contents = try env.renderTemplate(name: "Typography.kt.stencil", context: context) let fileURL = URL(string: "Typography.kt")! diff --git a/Sources/AndroidExport/Resources/Colors.kt.stencil b/Sources/AndroidExport/Resources/Colors.kt.stencil index b874a92..7832f2b 100644 --- a/Sources/AndroidExport/Resources/Colors.kt.stencil +++ b/Sources/AndroidExport/Resources/Colors.kt.stencil @@ -8,7 +8,6 @@ import {{xmlResourcePackage}}.R object Colors {% for color in colors %} - @Composable @ReadOnlyComposable fun Colors.{{color.functionName}}(): Color = colorResource(id = R.color.{{color.name}}) diff --git a/Sources/AndroidExport/Resources/colors.xml.stencil b/Sources/AndroidExport/Resources/colors.xml.stencil index 93ed54e..98ecb04 100644 --- a/Sources/AndroidExport/Resources/colors.xml.stencil +++ b/Sources/AndroidExport/Resources/colors.xml.stencil @@ -1,6 +1,4 @@ - -{% for color in colors %} - {{color.hex}} -{% endfor %} +{% for color in colors %} + {{color.hex}}{% endfor %} \ No newline at end of file diff --git a/Sources/AndroidExport/Resources/typography.xml.stencil b/Sources/AndroidExport/Resources/typography.xml.stencil index 96119b3..0038fc6 100644 --- a/Sources/AndroidExport/Resources/typography.xml.stencil +++ b/Sources/AndroidExport/Resources/typography.xml.stencil @@ -1,10 +1,8 @@ - -{% for textStyle in textStyles %} +{% for textStyle in textStyles %} -{% endfor %} + {% endfor %} \ No newline at end of file diff --git a/Sources/XcodeExport/Resources/Color+extension.swift.stencil b/Sources/XcodeExport/Resources/Color+extension.swift.stencil index 6a11f29..977e573 100644 --- a/Sources/XcodeExport/Resources/Color+extension.swift.stencil +++ b/Sources/XcodeExport/Resources/Color+extension.swift.stencil @@ -1,24 +1,15 @@ {% include "header.stencil" %} - import SwiftUI - {% if assetsInSwiftPackage %} private class BundleProvider { static let bundle = Bundle.module } - {% elif not assetsInMainBundle %} private class BundleProvider { static let bundle = Bundle(for: BundleProvider.self) } {% endif %} -public extension Color { -{% for color in colors %} -{% if assetsInMainBundle %} - static var {{ color.name }}: Color { Color({% if useNamespace %}"{{ color.originalName }}"{% else %}#function{% endif %}) } -{% else %} - static var {{ color.name }}: Color { Color({% if useNamespace %}"{{ color.originalName }}"{% else %}#function{% endif %}, in: BundleProvider.bundle) } -{% endif %} -{% endfor %} +public extension Color {{ "{" }}{% for color in colors %} + static var {{ color.name }}: Color { Color({% if useNamespace %}"{{ color.originalName }}"{% else %}#function{% endif %}{% if not assetsInMainBundle %}, in: BundleProvider.bundle{% endif %}) }{% endfor %} } diff --git a/Sources/XcodeExport/Resources/Image+extension.swift.stencil b/Sources/XcodeExport/Resources/Image+extension.swift.stencil index 7be863e..ec5050f 100644 --- a/Sources/XcodeExport/Resources/Image+extension.swift.stencil +++ b/Sources/XcodeExport/Resources/Image+extension.swift.stencil @@ -1,18 +1,13 @@ {% include "header.stencil" %} - import SwiftUI - {% if assetsInSwiftPackage %} private class BundleProvider { static let bundle = Bundle.module } - {% elif not assetsInMainBundle %} private class BundleProvider { static let bundle = Bundle(for: BundleProvider.self) } - {% endif %} -public extension Image { -{% include "Image+extension.swift.stencil.include" %} +public extension Image {{ "{" }}{% include "Image+extension.swift.stencil.include" %} } diff --git a/Sources/XcodeExport/Resources/Image+extension.swift.stencil.include b/Sources/XcodeExport/Resources/Image+extension.swift.stencil.include index 2700eda..34c57d7 100644 --- a/Sources/XcodeExport/Resources/Image+extension.swift.stencil.include +++ b/Sources/XcodeExport/Resources/Image+extension.swift.stencil.include @@ -1,7 +1,2 @@ {% for image in images %} -{% if assetsInMainBundle %} - {% if addObjcPrefix %}@objc {% endif %}static var {{ image.name }}: Image { Image(#function) } -{% else %} - {% if addObjcPrefix %}@objc {% endif %}static var {{ image.name }}: Image { Image(#function, bundle: BundleProvider.bundle) } -{% endif %} -{% endfor %} + {% if addObjcPrefix %}@objc {% endif %}static var {{ image.name }}: Image { Image(#function{% if not assetsInMainBundle %}, bundle: BundleProvider.bundle{% endif %}) }{% endfor %} \ No newline at end of file diff --git a/Sources/XcodeExport/Resources/UIColor+extension.swift.stencil b/Sources/XcodeExport/Resources/UIColor+extension.swift.stencil index a8888a4..3401e40 100644 --- a/Sources/XcodeExport/Resources/UIColor+extension.swift.stencil +++ b/Sources/XcodeExport/Resources/UIColor+extension.swift.stencil @@ -1,28 +1,17 @@ {% include "header.stencil" %} - import UIKit - {% if assetsInSwiftPackage %} private class BundleProvider { static let bundle = Bundle.module } - {% elif not assetsInMainBundle %} private class BundleProvider { static let bundle = Bundle(for: BundleProvider.self) } - {% endif %} -public extension UIColor { -{% for color in colors %} -{% if colorFromAssetCatalog %} -{% if assetsInMainBundle %} - {% if addObjcPrefix %}@objc {% endif %}static var {{ color.name }}: UIColor { UIColor(named:{% if useNamespace %} "{{ color.originalName }}"{% else %} #function{% endif %})! } -{% else %} - {% if addObjcPrefix %}@objc {% endif %}static var {{ color.name }}: UIColor { UIColor(named:{% if useNamespace %} "{{ color.originalName }}"{% else %} #function{% endif %}, in: BundleProvider.bundle, compatibleWith: nil)! } -{% endif %} -{% else %} -{% if color.hasDarkVariant %} +public extension UIColor {{ "{" }}{% for color in colors %}{% if colorFromAssetCatalog %}{% if assetsInMainBundle %} + {% if addObjcPrefix %}@objc {% endif %}static var {{ color.name }}: UIColor { UIColor(named:{% if useNamespace %} "{{ color.originalName }}"{% else %} #function{% endif %})! }{% else %} + {% if addObjcPrefix %}@objc {% endif %}static var {{ color.name }}: UIColor { UIColor(named:{% if useNamespace %} "{{ color.originalName }}"{% else %} #function{% endif %}, in: BundleProvider.bundle, compatibleWith: nil)! }{% endif %}{% else %}{% if color.hasDarkVariant %} {% if addObjcPrefix %}@objc {% endif %}static var {{ color.light.name }}: UIColor { UIColor { traitCollection -> UIColor in if traitCollection.userInterfaceStyle == .dark { @@ -31,12 +20,8 @@ public extension UIColor { return UIColor(red: {{ color.light.red }}, green: {{ color.light.green }}, blue: {{ color.light.blue }}, alpha: {{ color.light.alpha }}) } } - } -{% else %} + }{% else %} {% if addObjcPrefix %}@objc {% endif %}static var {{ color.name }}: UIColor { UIColor(red: {{ color.red }}, green: {{ color.green }}, blue: {{ color.blue }}, alpha: {{ color.alpha }}) - } -{% endif %} -{% endif %} -{% endfor %} + }{% endif %}{% endif %}{% endfor %} } diff --git a/Sources/XcodeExport/Resources/UIImage+extension.swift.stencil b/Sources/XcodeExport/Resources/UIImage+extension.swift.stencil index 0e37ac8..a2a4540 100644 --- a/Sources/XcodeExport/Resources/UIImage+extension.swift.stencil +++ b/Sources/XcodeExport/Resources/UIImage+extension.swift.stencil @@ -1,18 +1,13 @@ {% include "header.stencil" %} - import UIKit - {% if assetsInSwiftPackage %} private class BundleProvider { static let bundle = Bundle.module } - {% elif not assetsInMainBundle %} private class BundleProvider { static let bundle = Bundle(for: BundleProvider.self) } - {% endif %} -public extension UIImage { -{% include "UIImage+extension.swift.stencil.include" %} +public extension UIImage {{ "{" }}{% include "UIImage+extension.swift.stencil.include" %} } diff --git a/Sources/XcodeExport/Resources/UIImage+extension.swift.stencil.include b/Sources/XcodeExport/Resources/UIImage+extension.swift.stencil.include index 5931484..b32baba 100644 --- a/Sources/XcodeExport/Resources/UIImage+extension.swift.stencil.include +++ b/Sources/XcodeExport/Resources/UIImage+extension.swift.stencil.include @@ -1,7 +1,2 @@ {% for image in images %} -{% if assetsInMainBundle %} - {% if addObjcPrefix %}@objc {% endif %}static var {{ image.name }}: UIImage { UIImage(named: #function)! } -{% else %} - {% if addObjcPrefix %}@objc {% endif %}static var {{ image.name }}: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! } -{% endif %} -{% endfor %} + {% if addObjcPrefix %}@objc {% endif %}static var {{ image.name }}: UIImage { UIImage(named: #function{% if not assetsInMainBundle %}, in: BundleProvider.bundle, compatibleWith: nil{% endif %})! }{% endfor %} \ No newline at end of file diff --git a/Sources/XcodeExport/XcodeColorExporter.swift b/Sources/XcodeExport/XcodeColorExporter.swift index d652e59..fcc87d4 100644 --- a/Sources/XcodeExport/XcodeColorExporter.swift +++ b/Sources/XcodeExport/XcodeColorExporter.swift @@ -60,7 +60,7 @@ final public class XcodeColorExporter: XcodeExporterBase { "colors": colors, ] - let env = makeEnvironment(templatesPath: output.templatesPath, trimBehavior: .smart) + let env = makeEnvironment(templatesPath: output.templatesPath) return try env.renderTemplate(name: "Color+extension.swift.stencil", context: context) } @@ -112,10 +112,7 @@ final public class XcodeColorExporter: XcodeExporterBase { "colors": colors, ] - let env = makeEnvironment( - templatesPath: output.templatesPath, - trimBehavior: TrimBehavior(leading: .none, trailing: .whitespaceAndOneNewLine) - ) + let env = makeEnvironment(templatesPath: output.templatesPath) return try env.renderTemplate(name: "UIColor+extension.swift.stencil", context: context) } diff --git a/Sources/XcodeExport/XcodeExporterBase.swift b/Sources/XcodeExport/XcodeExporterBase.swift index 48de5d4..0929ddf 100644 --- a/Sources/XcodeExport/XcodeExporterBase.swift +++ b/Sources/XcodeExport/XcodeExporterBase.swift @@ -24,7 +24,7 @@ public class XcodeExporterBase { } } - func makeEnvironment(templatesPath: URL?, trimBehavior: TrimBehavior) -> Environment { + func makeEnvironment(templatesPath: URL?) -> Environment { let loader: FileSystemLoader if let templateURL = templatesPath { loader = FileSystemLoader(paths: [Path(templateURL.path)]) @@ -34,9 +34,7 @@ public class XcodeExporterBase { Path(Bundle.module.resourcePath!) ]) } - var environment = Environment(loader: loader) - environment.trimBehavior = trimBehavior - return environment + return Environment(loader: loader) } func makeFileContents(for string: String, url: URL) throws -> FileContents { diff --git a/Sources/XcodeExport/XcodeImagesExporterBase.swift b/Sources/XcodeExport/XcodeImagesExporterBase.swift index 255fc52..f0063f0 100644 --- a/Sources/XcodeExport/XcodeImagesExporterBase.swift +++ b/Sources/XcodeExport/XcodeImagesExporterBase.swift @@ -69,10 +69,7 @@ public class XcodeImagesExporterBase: XcodeExporterBase { "assetsInMainBundle": output.assetsInMainBundle, "images": names.map { ["name": $0] }, ] - let env = makeEnvironment( - templatesPath: output.templatesPath, - trimBehavior: .init(leading: .none, trailing: .whitespaceAndOneNewLine) - ) + let env = makeEnvironment(templatesPath: output.templatesPath) return try env.renderTemplate(name: templateName, context: context) } @@ -81,10 +78,12 @@ public class XcodeImagesExporterBase: XcodeExporterBase { contentsOf: URL(fileURLWithPath: fileURL.path), encoding: .utf8 ) - let string = string + "}\n" - if let index = existingContents.lastIndex(of: "}") { + let string = string + "\n}\n" + + if let index = existingContents.dropLast(2).lastIndex(of: "}") { + let newIndex = existingContents.index(after: index) existingContents.replaceSubrange( - index.. FileContents { - let env = makeEnvironment(templatesPath: output.templatesPath, trimBehavior: .none) + let env = makeEnvironment(templatesPath: output.templatesPath) let labelStyleSwiftContents = try env.renderTemplate(name: "LabelStyle.swift.stencil") return try makeFileContents(for: labelStyleSwiftContents, directory: labelsDirectory, file: URL(string: "LabelStyle.swift")!) } diff --git a/Tests/XcodeExportTests/XcodeColorExporterTests.swift b/Tests/XcodeExportTests/XcodeColorExporterTests.swift index 9289cf0..2fffe51 100644 --- a/Tests/XcodeExportTests/XcodeColorExporterTests.swift +++ b/Tests/XcodeExportTests/XcodeColorExporterTests.swift @@ -229,10 +229,6 @@ final class XcodeColorExporterTests: XCTestCase { """) } - func testExport_swiftui_and_assets_in_separate_bundle() throws { - - } - func testExport_swiftui_and_assets_in_swift_package() throws { let output = XcodeColorsOutput( assetsColorsURL: colorsAssetCatalog, @@ -331,4 +327,4 @@ fileprivate func assertCodeEquals(_ data: Data?, _ referenceCode: String) throws let data = try XCTUnwrap(data) let generatedCode = String(data: data, encoding: .utf8) XCTAssertNoDifference(generatedCode, referenceCode) -} \ No newline at end of file +}