Skip to content

Commit

Permalink
Use original Stencil instead of fork (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
subdan authored Feb 19, 2022
1 parent f0b6686 commit f6c5b90
Show file tree
Hide file tree
Showing 20 changed files with 43 additions and 105 deletions.
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down
5 changes: 2 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [

Expand Down
4 changes: 2 additions & 2 deletions Sources/AndroidExport/AndroidColorExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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")!
Expand Down
2 changes: 1 addition & 1 deletion Sources/AndroidExport/AndroidComposeIconExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
6 changes: 2 additions & 4 deletions Sources/AndroidExport/AndroidExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions Sources/AndroidExport/AndroidTypographyExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")!
Expand Down
1 change: 0 additions & 1 deletion Sources/AndroidExport/Resources/Colors.kt.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -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}})
Expand Down
6 changes: 2 additions & 4 deletions Sources/AndroidExport/Resources/colors.xml.stencil
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
{% for color in colors %}
<color name="{{color.name}}">{{color.hex}}</color>
{% endfor %}
<resources>{% for color in colors %}
<color name="{{color.name}}">{{color.hex}}</color>{% endfor %}
</resources>
6 changes: 2 additions & 4 deletions Sources/AndroidExport/Resources/typography.xml.stencil
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
{% for textStyle in textStyles %}
<resources>{% for textStyle in textStyles %}
<style name="{{ textStyle.name }}">
<item name="android:fontFamily">{{ textStyle.fontFamily }}</item>
<item name="android:textSize">{{ textStyle.textSize }}sp</item>
<item name="android:letterSpacing">{{ textStyle.letterSpacing }}</item>
</style>
{% endfor %}
</style>{% endfor %}
</resources>
13 changes: 2 additions & 11 deletions Sources/XcodeExport/Resources/Color+extension.swift.stencil
Original file line number Diff line number Diff line change
@@ -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 %}
}
7 changes: 1 addition & 6 deletions Sources/XcodeExport/Resources/Image+extension.swift.stencil
Original file line number Diff line number Diff line change
@@ -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" %}
}
Original file line number Diff line number Diff line change
@@ -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 %}
25 changes: 5 additions & 20 deletions Sources/XcodeExport/Resources/UIColor+extension.swift.stencil
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 %}
}
Original file line number Diff line number Diff line change
@@ -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" %}
}
Original file line number Diff line number Diff line change
@@ -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 %}
7 changes: 2 additions & 5 deletions Sources/XcodeExport/XcodeColorExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 2 additions & 4 deletions Sources/XcodeExport/XcodeExporterBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand All @@ -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 {
Expand Down
13 changes: 6 additions & 7 deletions Sources/XcodeExport/XcodeImagesExporterBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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..<existingContents.endIndex,
newIndex..<existingContents.endIndex,
with: string
)
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/XcodeExport/XcodeTypographyExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final public class XcodeTypographyExporter: XcodeExporterBase {
"type": $0.fontStyle?.textStyleName ?? ""
]
}
let env = makeEnvironment(templatesPath: output.templatesPath, trimBehavior: .none)
let env = makeEnvironment(templatesPath: output.templatesPath)
let contents = try env.renderTemplate(name: "UIFont+extension.swift.stencil", context: [
"textStyles": textStyles,
"addObjcPrefix": output.addObjcAttribute
Expand All @@ -74,7 +74,7 @@ final public class XcodeTypographyExporter: XcodeExporterBase {
"type": $0.fontStyle?.textStyleName ?? ""
]
}
let env = makeEnvironment(templatesPath: output.templatesPath, trimBehavior: .none)
let env = makeEnvironment(templatesPath: output.templatesPath)
let contents = try env.renderTemplate(name: "Font+extension.swift.stencil", context: [
"textStyles": textStyles,
])
Expand All @@ -94,7 +94,7 @@ final public class XcodeTypographyExporter: XcodeExporterBase {
"lineHeight": style.lineHeight ?? 0,
"textCase": style.textCase.rawValue
]}
let env = makeEnvironment(templatesPath: output.templatesPath, trimBehavior: .none)
let env = makeEnvironment(templatesPath: output.templatesPath)
let contents = try env.renderTemplate(name: "LabelStyle+extension.swift.stencil", context: ["styles": dict])

let labelStylesSwiftExtension = try makeFileContents(for: contents, url: labelStyleExtensionURL)
Expand All @@ -114,7 +114,7 @@ final public class XcodeTypographyExporter: XcodeExporterBase {
"lineHeight": style.lineHeight ?? 0,
"textCase": style.textCase.rawValue
]}
let env = makeEnvironment(templatesPath: output.templatesPath, trimBehavior: .none)
let env = makeEnvironment(templatesPath: output.templatesPath)
let contents = try env.renderTemplate(name: "Label.swift.stencil", context: [
"styles": dict,
"separateStyles": separateStyles
Expand All @@ -123,7 +123,7 @@ final public class XcodeTypographyExporter: XcodeExporterBase {
}

private func makeLabelStyle(labelsDirectory: URL) throws -> 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")!)
}
Expand Down
6 changes: 1 addition & 5 deletions Tests/XcodeExportTests/XcodeColorExporterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

0 comments on commit f6c5b90

Please sign in to comment.