diff --git a/Sources/FigmaExportCore/AssetsFilter.swift b/Sources/FigmaExportCore/AssetsFilter.swift index f899ceaa..64e04c7c 100644 --- a/Sources/FigmaExportCore/AssetsFilter.swift +++ b/Sources/FigmaExportCore/AssetsFilter.swift @@ -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 diff --git a/Sources/XcodeExport/XcodeIconsExporter.swift b/Sources/XcodeExport/XcodeIconsExporter.swift index 79180ec5..08db1930 100644 --- a/Sources/XcodeExport/XcodeIconsExporter.swift +++ b/Sources/XcodeExport/XcodeIconsExporter.swift @@ -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) { @@ -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) diff --git a/Tests/XcodeExportTests/XcodeIconsExporterTests.swift b/Tests/XcodeExportTests/XcodeIconsExporterTests.swift index d4c90fc0..9e57a1ac 100644 --- a/Tests/XcodeExportTests/XcodeIconsExporterTests.swift +++ b/Tests/XcodeExportTests/XcodeIconsExporterTests.swift @@ -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") @@ -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 {