From b1f139411edcf753b1843b0c1884088d6be93e1a Mon Sep 17 00:00:00 2001 From: Jim Wang Date: Sat, 14 Nov 2020 03:41:17 +0800 Subject: [PATCH] [BUG] Can't export images to Android. (#38) * Fixed export images for android * Removed unused individualScales case --- .../FigmaExport/Loaders/ImagesLoader.swift | 25 +++++++++++-------- .../Subcommands/ExportImages.swift | 14 +++++------ Sources/FigmaExportCore/Image.swift | 12 --------- Sources/XcodeExport/XcodeImagesExporter.swift | 17 ------------- 4 files changed, 21 insertions(+), 47 deletions(-) diff --git a/Sources/FigmaExport/Loaders/ImagesLoader.swift b/Sources/FigmaExport/Loaders/ImagesLoader.swift index 5790302..dc3c33b 100644 --- a/Sources/FigmaExport/Loaders/ImagesLoader.swift +++ b/Sources/FigmaExport/Loaders/ImagesLoader.swift @@ -138,12 +138,12 @@ final class ImagesLoader { } // Group images by name - let groups = Dictionary(grouping: imagesDict) { $1.name.parseNameAndIdiom().name } + let groups = Dictionary(grouping: imagesDict) { $1.name.parseNameAndIdiom(platform: platform).name } // Create image packs for groups let imagePacks = groups.compactMap { _, components -> ImagePack? in let packImages = components.flatMap { nodeId, component -> [Image] in - let (name, idiom) = component.name.parseNameAndIdiom() + let (name, idiom) = component.name.parseNameAndIdiom(platform: platform) return scales.compactMap { scale -> Image? in guard let urlString = images[scale]?[nodeId], let url = URL(string: urlString) else { return nil @@ -173,16 +173,21 @@ final class ImagesLoader { private extension String { - func parseNameAndIdiom() -> (name: String, idiom: String) { - guard let regex = try? NSRegularExpression(pattern: "(.*)~(.*)$") else { - return (self, "") - } - guard let match = regex.firstMatch(in: self, range: NSRange(startIndex..., in: self)), - let name = Range(match.range(at: 1), in: self), - let idiom = Range(match.range(at: 2), in: self) else { + func parseNameAndIdiom(platform: Platform) -> (name: String, idiom: String) { + switch platform { + case .android: return (self, "") + case .ios: + guard let regex = try? NSRegularExpression(pattern: "(.*)~(.*)$") else { + return (self, "") + } + guard let match = regex.firstMatch(in: self, range: NSRange(startIndex..., in: self)), + let name = Range(match.range(at: 1), in: self), + let idiom = Range(match.range(at: 2), in: self) else { + return (self, "") + } + return (String(self[name]), String(self[idiom])) } - return (String(self[name]), String(self[idiom])) } } diff --git a/Sources/FigmaExport/Subcommands/ExportImages.swift b/Sources/FigmaExport/Subcommands/ExportImages.swift index 08a9ec6..c179d40 100644 --- a/Sources/FigmaExport/Subcommands/ExportImages.swift +++ b/Sources/FigmaExport/Subcommands/ExportImages.swift @@ -225,10 +225,10 @@ extension FigmaExportCommand { logger.info("Downloading remote files...") let remoteFiles = images.flatMap { asset -> [FileContents] in var result = [FileContents]() - if case ImagePack.individualScales(let images) = asset.light { + if case ImagePack.images(let images) = asset.light { result.append(contentsOf: makeRemoteFiles(images: images, dark: false, outputDirectory: tempDirectoryURL)) } - if let darkImages = asset.dark, case ImagePack.individualScales(let images) = darkImages { + if let darkImages = asset.dark, case ImagePack.images(let images) = darkImages { result.append(contentsOf: makeRemoteFiles(images: images, dark: true, outputDirectory: tempDirectoryURL)) } return result @@ -284,11 +284,10 @@ extension FigmaExportCommand { /// - images: Dictionary of images. Key = scale, value = image info /// - dark: Dark mode? /// - outputDirectory: URL of the output directory - private func makeRemoteFiles(images: [Double: Image], dark: Bool, outputDirectory: URL) -> [FileContents] { - var result: [FileContents] = [] - for scale in images.keys { - guard let image = images[scale] else { continue } + private func makeRemoteFiles(images: [Image], dark: Bool, outputDirectory: URL) -> [FileContents] { + images.map { image -> FileContents in let fileURL = URL(string: "\(image.name).\(image.format)")! + let scale = image.scale let dest = Destination( directory: outputDirectory .appendingPathComponent(dark ? "dark" : "light") @@ -297,9 +296,8 @@ extension FigmaExportCommand { var file = FileContents(destination: dest, sourceURL: image.url) file.scale = scale file.dark = dark - result.append(file) + return file } - return result } } } diff --git a/Sources/FigmaExportCore/Image.swift b/Sources/FigmaExportCore/Image.swift index 0e86219..ab53cce 100644 --- a/Sources/FigmaExportCore/Image.swift +++ b/Sources/FigmaExportCore/Image.swift @@ -35,15 +35,12 @@ public enum ImagePack: Asset { public typealias Scale = Double case singleScale(Image) - case individualScales([Scale: Image]) case images([Image]) public var single: Image { switch self { case .singleScale(let image): return image - case .individualScales: - fatalError("Unable to extract image from image pack") case .images: fatalError("Unable to extract image from image pack") } @@ -54,8 +51,6 @@ public enum ImagePack: Asset { switch self { case .singleScale(let image): return image.name - case .individualScales(let images): - return images.first!.value.name case .images(let images): return images.first!.name } @@ -65,11 +60,6 @@ public enum ImagePack: Asset { case .singleScale(var image): image.name = newValue self = .singleScale(image) - case .individualScales(var images): - for key in images.keys { - images[key]?.name = newValue - } - self = .individualScales(images) case .images(let images): let image = images.map { image -> Image in var newImage = image @@ -85,8 +75,6 @@ public enum ImagePack: Asset { switch self { case .singleScale(let image): return image.platform - case .individualScales(let images): - return images.first?.value.platform case .images(let images): return images.first?.platform } diff --git a/Sources/XcodeExport/XcodeImagesExporter.swift b/Sources/XcodeExport/XcodeImagesExporter.swift index 4fa6c2c..574839a 100644 --- a/Sources/XcodeExport/XcodeImagesExporter.swift +++ b/Sources/XcodeExport/XcodeImagesExporter.swift @@ -89,10 +89,6 @@ final public class XcodeImagesExporter: XcodeImagesExporterBase { switch pack { case .singleScale(let image): return [saveImage(image, to: directory, dark: dark)] - case .individualScales(let images): - return images.map { scale, image -> FileContents in - saveImage(image, to: directory, scale: scale, dark: dark) - } case .images(let images): return images.map { saveImage($0, to: directory, scale: $0.scale, dark: dark) } } @@ -125,10 +121,6 @@ final public class XcodeImagesExporter: XcodeImagesExporterBase { switch pack { case .singleScale(let image): return [imageDataForImage(image, dark: dark)] - case .individualScales(let images): - return images.map { scale, image -> XcodeAssetContents.ImageData in - imageDataForImage(image, scale: scale, dark: dark) - } case .images(let images): return images.map { imageDataForImage($0, scale: $0.scale, dark: dark) } } @@ -188,15 +180,6 @@ private extension ImagePack { return nil } return self - case .individualScales(let images): - let validImages = images.reduce(into: [Scale: Image]()) { result, info in - let (scale, image) = info - guard image.isValidForXcode(scale: scale) else { - return - } - result[scale] = image - } - return .individualScales(validImages) case .images(let images): return .images(images.filter { $0.isValidForXcode(scale: $0.scale) }) }