RedMadRobot / figma-export

Command line utility to export colors, typography, icons and images from Figma to Xcode / Android Studio project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crash updating a UIImage extension file

NoFearJoe opened this issue · comments

We get a crash when trying to download new assets.

I found a line that crashes. It's in XcodeImagesExporterBase.swift, line 65:

private func appendContent(string: String, to fileURL: URL) throws -> String {
    var existingContents = try String(contentsOf: URL(fileURLWithPath: fileURL.path), encoding: .utf8)

    if let index = existingContents.lastIndex(of: "}") {
       let nextIndex = existingContents.index(after: index)
       existingContents.replaceSubrange(index...nextIndex, with: string)
    }
    return existingContents
}

You assume that there will always be a character (newline) after the last brace, but for some reason, we don't have this newline in the generated file (we haven't edited this file).

So, I'm going to fix this by removing the last newline everywhere and changing the function above to:

private func appendContent(string: String, to fileURL: URL) throws -> String {
    var existingContents = try String(contentsOf: URL(fileURLWithPath: fileURL.path), encoding: .utf8)

    if let index = existingContents.lastIndex(of: "}") {
       existingContents.replaceSubrange(index... index, with: string)
    }
    return existingContents
}

I found that you don't add a newline after adding new contents in a UIImage extension file. That is the reason why the newline is missing.

XcodeImagesExporterBase.swift, lines 27 and 49.

Thanks! I will fix it by the end of the week.