Skip to content

Commit

Permalink
Template Image and Original Image for Xcode
Browse files Browse the repository at this point in the history
Closes #54
  • Loading branch information
subdan committed Dec 25, 2020
1 parent 7a32264 commit e6c88ec
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ ios:
swiftUIImageSwift: "./Source/Image+extension_icons.swift"
# [optional] Absolute or relative path to swift file where to generate extension for UIImage for accessing icons from the code (e.g. UIImage.ic24ArrowRight)
imageSwift: "./Example/Source/UIImage+extension_icons.swift"
# Asset render mode: "template", "orignal" or "default". Default value is "template".
renderMode: default

# Parameters for exporting images
images:
Expand Down
2 changes: 2 additions & 0 deletions Sources/FigmaExport/Input/Params.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ struct Params: Decodable {

let imageSwift: URL?
let swiftUIImageSwift: URL?

let renderMode: XcodeRenderMode?
}

struct Images: Decodable {
Expand Down
3 changes: 2 additions & 1 deletion Sources/FigmaExport/Subcommands/ExportIcons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ extension FigmaExportCommand {
assetsInMainBundle: ios.xcassetsInMainBundle,
preservesVectorRepresentation: ios.icons.preservesVectorRepresentation,
uiKitImageExtensionURL: ios.icons.imageSwift,
swiftUIImageExtensionURL: ios.icons.swiftUIImageSwift)
swiftUIImageExtensionURL: ios.icons.swiftUIImageSwift,
renderMode: ios.icons.renderMode)

let exporter = XcodeIconsExporter(output: output)
let localAndRemoteFiles = try exporter.export(icons: icons, append: filter != nil)
Expand Down
7 changes: 7 additions & 0 deletions Sources/FigmaExportCore/XcodeRenderMode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

public enum XcodeRenderMode: String, Decodable {
case `default`
case template
case original
}
32 changes: 20 additions & 12 deletions Sources/XcodeExport/Model/XcodeAssetContents.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import FigmaExportCore

enum XcodeAssetIdiom: String, Encodable {
case universal
Expand All @@ -14,7 +15,7 @@ struct XcodeAssetContents: Encodable {
let version = 1
let author = "xcode"
}
struct DarkAppeareance: Encodable {
struct DarkAppearance: Encodable {
let appearance = "luminosity"
let value = "dark"
}
Expand All @@ -34,49 +35,56 @@ struct XcodeAssetContents: Encodable {
}
struct ColorData: Encodable {
let idiom = "universal"
var appearances: [DarkAppeareance]?
var appearances: [DarkAppearance]?
var color: ColorInfo
}
struct ImageData: Encodable {
let idiom: XcodeAssetIdiom
var scale: String?
var appearances: [DarkAppeareance]?
var appearances: [DarkAppearance]?
let filename: String
}

struct TemplateProperties: Encodable {
let templateRenderingIntent = "template"
struct Properties: Encodable {
let templateRenderingIntent: String?
let preservesVectorRepresentation: Bool?

enum CodingKeys: String, CodingKey {
case templateRenderingIntent = "template-rendering-intent"
case preservesVectorRepresentation = "preserves-vector-representation"
}

init?(preservesVectorRepresentation: Bool?) {
guard let preservesVectorRepresentation = preservesVectorRepresentation else {
init?(preserveVectorData: Bool?, renderMode: XcodeRenderMode?) {
preservesVectorRepresentation = preserveVectorData == true ? true : nil

if let renderMode = renderMode, (renderMode == .original || renderMode == .template) {
templateRenderingIntent = renderMode.rawValue
} else {
templateRenderingIntent = nil
}

if preservesVectorRepresentation == nil && templateRenderingIntent == nil {
return nil
}
self.preservesVectorRepresentation = preservesVectorRepresentation ? true : nil
}

}

let info = Info()
let colors: [ColorData]?
let images: [ImageData]?
let properties: TemplateProperties?
let properties: Properties?

init(colors: [ColorData]) {
self.colors = colors
self.images = nil
self.properties = nil
}

init(icons: [ImageData], preservesVectorRepresentation: Bool = false) {
init(icons: [ImageData], preserveVectorData: Bool = false, renderMode: XcodeRenderMode) {
self.colors = nil
self.images = icons
self.properties = TemplateProperties(preservesVectorRepresentation: preservesVectorRepresentation)
self.properties = Properties(preserveVectorData: preserveVectorData, renderMode: renderMode)
}

init(images: [ImageData]) {
Expand All @@ -85,7 +93,7 @@ struct XcodeAssetContents: Encodable {
self.properties = nil
}

init(images: [ImageData], properties: TemplateProperties? = nil) {
init(images: [ImageData], properties: Properties? = nil) {
self.colors = nil
self.images = images
self.properties = properties
Expand Down
15 changes: 10 additions & 5 deletions Sources/XcodeExport/Model/XcodeExportExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ extension ImagePack {
return xcodeImagePack
}

func makeFileContents(to directory: URL, preservesVector: Bool?, appearance: Appearance? = nil) throws -> [FileContents] {
let properties = XcodeAssetContents.TemplateProperties(preservesVectorRepresentation: preservesVector)
func makeFileContents(
to directory: URL,
preservesVector: Bool?,
renderMode: XcodeRenderMode,
appearance: Appearance? = nil) throws -> [FileContents] {

let properties = XcodeAssetContents.Properties(preserveVectorData: preservesVector, renderMode: renderMode)

return try packForXcode()
.flatMap { imagePack -> [FileContents] in
Expand Down Expand Up @@ -106,7 +111,7 @@ extension ImagePack {

extension AssetPair where AssetType == ImagePack {

func makeFileContents(to directory: URL, preservesVector: Bool?) throws -> [FileContents] {
func makeFileContents(to directory: URL, preservesVector: Bool?, renderMode: XcodeRenderMode? = nil) throws -> [FileContents] {
let name = light.name
let dirURL = directory.appendingPathComponent("\(name).imageset")

Expand All @@ -119,7 +124,7 @@ extension AssetPair where AssetType == ImagePack {
let lightAssetContents = lightPack?.makeXcodeAssetContentsImageData(appearance: .light) ?? []
let darkAssetContents = darkPack?.makeXcodeAssetContentsImageData(appearance: .dark) ?? []

let properties = XcodeAssetContents.TemplateProperties(preservesVectorRepresentation: preservesVector)
let properties = XcodeAssetContents.Properties(preserveVectorData: preservesVector, renderMode: renderMode)

let contentsFileContents = try XcodeAssetContents(
images: lightAssetContents + darkAssetContents,
Expand All @@ -144,7 +149,7 @@ extension Image {
func makeXcodeAssetContentsImageData(scale: Scale, appearance: Appearance? = nil) -> XcodeAssetContents.ImageData {
let filename = makeFileURL(scale: scale, appearance: appearance).absoluteString
let xcodeIdiom = idiom.flatMap { XcodeAssetIdiom(rawValue: $0) } ?? .universal
let appearances = appearance.flatMap { $0 == .dark ? [XcodeAssetContents.DarkAppeareance()] : nil }
let appearances = appearance.flatMap { $0 == .dark ? [XcodeAssetContents.DarkAppearance()] : nil }
let scaleString = scale.string

return XcodeAssetContents.ImageData(
Expand Down
6 changes: 5 additions & 1 deletion Sources/XcodeExport/Model/XcodeImagesOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public struct XcodeImagesOutput {
let assetsFolderURL: URL
let assetsInMainBundle: Bool
let preservesVectorRepresentation: [String]?
let renderMode: XcodeRenderMode?

let uiKitImageExtensionURL: URL?
let swiftUIImageExtensionURL: URL?
Expand All @@ -15,17 +16,20 @@ public struct XcodeImagesOutput {
/// - preservesVectorRepresentation: A list of image names which should preserve vector data
/// - uiKitImageExtensionURL: URL of the swift file where to generate extension for UIImage class
/// - swiftUIImageExtensionURL: URL of the swift file where to generate extension for Image struct
/// - renderMode: Xcode Asset Catalog render mode
public init(
assetsFolderURL: URL,
assetsInMainBundle: Bool,
preservesVectorRepresentation: [String]? = nil,
uiKitImageExtensionURL: URL? = nil,
swiftUIImageExtensionURL: URL? = nil) {
swiftUIImageExtensionURL: URL? = nil,
renderMode: XcodeRenderMode? = nil) {

self.assetsFolderURL = assetsFolderURL
self.assetsInMainBundle = assetsInMainBundle
self.preservesVectorRepresentation = preservesVectorRepresentation
self.uiKitImageExtensionURL = uiKitImageExtensionURL
self.swiftUIImageExtensionURL = swiftUIImageExtensionURL
self.renderMode = renderMode
}
}
2 changes: 1 addition & 1 deletion Sources/XcodeExport/XcodeColorExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final public class XcodeColorExporter {
if let darkColor = colorPair.dark {
colors.append(
XcodeAssetContents.ColorData(
appearances: [XcodeAssetContents.DarkAppeareance()],
appearances: [XcodeAssetContents.DarkAppearance()],
color: XcodeAssetContents.ColorInfo(
components: darkColor.toHexComponents())
)
Expand Down
3 changes: 2 additions & 1 deletion Sources/XcodeExport/XcodeIconsExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ final public class XcodeIconsExporter: XcodeImagesExporterBase {
// Generate assets
let assetsFolderURL = output.assetsFolderURL
let preservesVectorRepresentation = output.preservesVectorRepresentation
let renderMode = output.renderMode ?? .template

let imageAssetsFiles = try icons.flatMap { imagePack -> [FileContents] in
let preservesVector = preservesVectorRepresentation?.first(where: { $0 == imagePack.name }) != nil
return try imagePack.makeFileContents(to: assetsFolderURL, preservesVector: preservesVector)
return try imagePack.makeFileContents(to: assetsFolderURL, preservesVector: preservesVector, renderMode: renderMode)
}

// Generate extensions
Expand Down
2 changes: 1 addition & 1 deletion Sources/XcodeExport/XcodeImagesExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final public class XcodeImagesExporter: XcodeImagesExporterBase {
let contentsFile = XcodeEmptyContents().makeFileContents(to: assetsFolderURL)

let imageAssetsFiles = try assets.flatMap { pair -> [FileContents] in
try pair.makeFileContents(to: assetsFolderURL, preservesVector: nil)
try pair.makeFileContents(to: assetsFolderURL, preservesVector: nil, renderMode: nil)
}

// Generate extensions
Expand Down

0 comments on commit e6c88ec

Please sign in to comment.