RedMadRobot / figma-export

Command line utility to export colors, typography, icons and images from Figma to Xcode / Android Studio project
MIT License
719 stars 115 forks source link

Crash updating a UIImage extension file #91

Closed NoFearJoe closed 3 years ago

NoFearJoe commented 3 years ago

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
}
NoFearJoe commented 3 years ago

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.

subdan commented 3 years ago

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