Skip to content

Commit

Permalink
[BUG] Can't export images to Android. (#38)
Browse files Browse the repository at this point in the history
* Fixed export images for android

* Removed unused individualScales case
  • Loading branch information
yyjim authored Nov 13, 2020
1 parent 4a39844 commit b1f1394
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 47 deletions.
25 changes: 15 additions & 10 deletions Sources/FigmaExport/Loaders/ImagesLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]))
}

}
14 changes: 6 additions & 8 deletions Sources/FigmaExport/Subcommands/ExportImages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
}
}
12 changes: 0 additions & 12 deletions Sources/FigmaExportCore/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
17 changes: 0 additions & 17 deletions Sources/XcodeExport/XcodeImagesExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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) })
}
Expand Down

0 comments on commit b1f1394

Please sign in to comment.