Skip to content

Commit

Permalink
Add support for * symbol for preservesVectorData (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Subbotin authored May 14, 2022
1 parent ab1a582 commit 3483e2b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Sources/FigmaExportCore/AssetsFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public struct AssetsFilter {
.map { $0.trimmingCharacters(in: .whitespaces) }
}

public init(filters: [String]) {
self.filters = filters
}

/// Returns true if name matches with filter
/// - Parameters:
/// - name: Name of the asset
Expand Down
10 changes: 6 additions & 4 deletions Sources/XcodeExport/XcodeIconsExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ final public class XcodeIconsExporter: XcodeImagesExporterBase {
// Generate assets
let assetsFolderURL = output.assetsFolderURL
let preservesVectorRepresentation = output.preservesVectorRepresentation
let filter = AssetsFilter(filters: preservesVectorRepresentation ?? [])

// Filtering at suffixes
let renderMode = output.renderMode ?? .template
let defaultSuffix = renderMode == .template ? output.renderModeDefaultSuffix : nil
let originalSuffix = renderMode == .template ? output.renderModeOriginalSuffix : nil
let templateSuffix = renderMode != .template ? output.renderModeTemplateSuffix : nil

let imageAssetsFiles = try icons.flatMap { imagePack -> [FileContents] in
let preservesVector = preservesVectorRepresentation?.first(where: { $0 == imagePack.light.name }) != nil

let preservesVector = filter.match(name: imagePack.light.name)
if let defaultSuffix = defaultSuffix, imagePack.light.name.hasSuffix(defaultSuffix) {
return try imagePack.makeFileContents(to: assetsFolderURL, preservesVector: preservesVector, renderMode: .default)
} else if let originalSuffix = originalSuffix, imagePack.light.name.hasSuffix(originalSuffix) {
Expand All @@ -29,7 +31,7 @@ final public class XcodeIconsExporter: XcodeImagesExporterBase {

return try imagePack.makeFileContents(to: assetsFolderURL, preservesVector: preservesVector, renderMode: renderMode)
}

// Generate extensions
let imageNames = icons.map { normalizeName($0.light.name) }
let extensionFiles = try generateExtensions(names: imageNames, append: append)
Expand Down
91 changes: 90 additions & 1 deletion Tests/XcodeExportTests/XcodeIconsExporterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ final class XcodeIconsExporterTests: XCTestCase {
private let image2 = Image(name: "image2", url: URL(string: "2")!, format: "pdf")
private let image2Dark = Image(name: "image2", url: URL(string: "2_dark")!, format: "pdf")
private let imageWithKeyword = Image(name: "class", url: URL(string: "2")!, format: "pdf")

private let tabBarIcon = Image(name: "ic24TabBarHome", url: URL(string: "1")!, format: "pdf")

private let uiKitImageExtensionURL = FileManager.default
.temporaryDirectory
.appendingPathComponent("UIImage+extension.swift")
Expand Down Expand Up @@ -602,6 +603,94 @@ final class XcodeIconsExporterTests: XCTestCase {
"""
XCTAssertNoDifference(generatedCode, referenceCode)
}

func testExport_preservesVectorRepresentation() throws {
let output = XcodeImagesOutput(
assetsFolderURL: URL(string: "~/")!,
assetsInMainBundle: true,
preservesVectorRepresentation: ["ic24TabBar*"],
uiKitImageExtensionURL: uiKitImageExtensionURL
)
let exporter = XcodeIconsExporter(output: output)
let result = try exporter.export(
icons: [AssetPair(light: ImagePack(image: tabBarIcon), dark: nil)],
append: false
)

XCTAssertEqual(result.count, 4)
XCTAssertTrue(result[0].destination.url.absoluteString.hasSuffix("Contents.json"))
XCTAssertTrue(result[1].destination.url.absoluteString.hasSuffix("ic24TabBarHome.imageset/Contents.json"))
XCTAssertTrue(result[2].destination.url.absoluteString.hasSuffix("ic24TabBarHome.imageset/ic24TabBarHome.pdf"))
XCTAssertTrue(result[3].destination.url.absoluteString.hasSuffix("UIImage+extension.swift"))

let content = result[1].data
XCTAssertNotNil(content)

let generatedCode = String(data: content!, encoding: .utf8)
let referenceCode = """
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic24TabBarHome.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
},
"properties" : {
"template-rendering-intent" : "template",
"preserves-vector-representation" : true
}
}
"""
XCTAssertNoDifference(generatedCode, referenceCode)
}

func testExport_preservesVectorRepresentation2() throws {
let output = XcodeImagesOutput(
assetsFolderURL: URL(string: "~/")!,
assetsInMainBundle: true,
preservesVectorRepresentation: ["*"],
uiKitImageExtensionURL: uiKitImageExtensionURL
)
let exporter = XcodeIconsExporter(output: output)
let result = try exporter.export(
icons: [AssetPair(light: ImagePack(image: tabBarIcon), dark: nil)],
append: false
)

XCTAssertEqual(result.count, 4)
XCTAssertTrue(result[0].destination.url.absoluteString.hasSuffix("Contents.json"))
XCTAssertTrue(result[1].destination.url.absoluteString.hasSuffix("ic24TabBarHome.imageset/Contents.json"))
XCTAssertTrue(result[2].destination.url.absoluteString.hasSuffix("ic24TabBarHome.imageset/ic24TabBarHome.pdf"))
XCTAssertTrue(result[3].destination.url.absoluteString.hasSuffix("UIImage+extension.swift"))

let content = result[1].data
XCTAssertNotNil(content)

let generatedCode = String(data: content!, encoding: .utf8)
let referenceCode = """
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic24TabBarHome.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
},
"properties" : {
"template-rendering-intent" : "template",
"preserves-vector-representation" : true
}
}
"""
XCTAssertNoDifference(generatedCode, referenceCode)
}
}

private extension XcodeIconsExporterTests {
Expand Down

0 comments on commit 3483e2b

Please sign in to comment.