Skip to content

Commit

Permalink
Set ios.colors, ios.icons, ios.images and ios.typography parameters a…
Browse files Browse the repository at this point in the history
…s optional

Closes #106
  • Loading branch information
subdan committed Jul 23, 2021
1 parent 5a38520 commit 66f65e0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 37 deletions.
8 changes: 4 additions & 4 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ios:
# [optional] Add @objc attribute to generated properties so that they are accessible in Objective-C. Defaults to false
addObjcAttribute: false

# Parameters for exporting colors
# [optional] Parameters for exporting colors
colors:
# How to export colors? Use .xcassets and UIColor extension (useColorAssets = true) or extension only (useColorAssets = false)
useColorAssets: true
Expand All @@ -81,7 +81,7 @@ ios:
# [optional] Absolute or relative path to swift file where to export SwiftUI colors (Color) for accessing from the code (e.g. Color.backgroundPrimary)
swiftuiColorSwift: "./Source/Color+extension.swift"

# Parameters for exporting icons
# [optional] Parameters for exporting icons
icons:
# Image file format: pdf or svg
format: pdf
Expand All @@ -101,7 +101,7 @@ ios:
# Asset render mode: "template", "orignal" or "default". Default value is "template".
renderMode: default

# Parameters for exporting images
# [optional] Parameters for exporting images
images:
# Name of the folder inside Assets.xcassets where to place images (.imageset directories)
assetsFolder: Illustrations
Expand All @@ -114,7 +114,7 @@ ios:
# [optional] Absolute or relative path to swift file where to generate extension for UIImage for accessing illustrations from the code (e.g. UIImage.illZeroNoInternet)
imageSwift: "./Example/Source/UIImage+extension_illustrations.swift"

# Parameters for exporting typography
# [optional] Parameters for exporting typography
typography:
# [optional] Absolute or relative path to swift file where to export UIKit fonts (UIFont extension).
fontSwift: "./Source/UIComponents/UIFont+extension.swift"
Expand Down
8 changes: 4 additions & 4 deletions Sources/FigmaExport/Input/Params.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ struct Params: Decodable {
let xcassetsInMainBundle: Bool
let xcassetsInSwiftPackage: Bool?
let addObjcAttribute: Bool?
let colors: Colors
let icons: Icons
let images: Images
let typography: Typography
let colors: Colors?
let icons: Icons?
let images: Images?
let typography: Typography?
}

struct Android: Decodable {
Expand Down
4 changes: 2 additions & 2 deletions Sources/FigmaExport/Loaders/ImagesLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class ImagesLoader {
func loadIcons(filter: String? = nil) throws -> [ImagePack] {
let formatParams: FormatParams

switch (platform, params.ios?.icons.format) {
switch (platform, params.ios?.icons?.format) {
case (.android, _),
(.ios, .svg):
formatParams = SVGParams()
Expand Down Expand Up @@ -182,7 +182,7 @@ final class ImagesLoader {
customScales = filterScales(params.android?.images?.scales)
} else {
validScales = [1, 2, 3]
customScales = filterScales(params.ios?.images.scales)
customScales = filterScales(params.ios?.images?.scales)
}
return customScales.isEmpty ? validScales : customScales
}
Expand Down
17 changes: 11 additions & 6 deletions Sources/FigmaExport/Subcommands/ExportColors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension FigmaExportCommand {
platform: .ios,
nameValidateRegexp: options.params.common?.colors?.nameValidateRegexp,
nameReplaceRegexp: options.params.common?.colors?.nameReplaceRegexp,
nameStyle: options.params.ios?.colors.nameStyle,
nameStyle: options.params.ios?.colors?.nameStyle,
useSingleFile: options.params.common?.colors?.useSingleFile,
darkModeSuffix: options.params.common?.colors?.darkModeSuffix
)
Expand Down Expand Up @@ -76,9 +76,14 @@ extension FigmaExportCommand {
}

private func exportXcodeColors(colorPairs: [AssetPair<Color>], iosParams: Params.iOS, logger: Logger) throws {
guard let colorParams = iosParams.colors else {
logger.error("Nothing to do. Add ios.colors parameters to the config file.")
return
}

var colorsURL: URL?
if iosParams.colors.useColorAssets {
if let folder = iosParams.colors.assetsFolder {
if colorParams.useColorAssets {
if let folder = colorParams.assetsFolder {
colorsURL = iosParams.xcassetsPath.appendingPathComponent(folder)
} else {
throw FigmaExportError.colorsAssetsFolderNotSpecified
Expand All @@ -90,13 +95,13 @@ extension FigmaExportCommand {
assetsInMainBundle: iosParams.xcassetsInMainBundle,
assetsInSwiftPackage: iosParams.xcassetsInSwiftPackage,
addObjcAttribute: iosParams.addObjcAttribute,
colorSwiftURL: iosParams.colors.colorSwift,
swiftuiColorSwiftURL: iosParams.colors.swiftuiColorSwift)
colorSwiftURL: colorParams.colorSwift,
swiftuiColorSwiftURL: colorParams.swiftuiColorSwift)

let exporter = XcodeColorExporter(output: output)
let files = exporter.export(colorPairs: colorPairs)

if iosParams.colors.useColorAssets, let url = colorsURL {
if colorParams.useColorAssets, let url = colorsURL {
try? FileManager.default.removeItem(atPath: url.path)
}

Expand Down
17 changes: 9 additions & 8 deletions Sources/FigmaExport/Subcommands/ExportIcons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ extension FigmaExportCommand {
}

private func exportiOSIcons(client: Client, params: Params, logger: Logger) throws {
guard let ios = params.ios else {
logger.info("Nothing to do. You haven’t specified ios parameter in the config file.")
guard let ios = params.ios,
let iconsParams = ios.icons else {
logger.info("Nothing to do. You haven’t specified ios.icons parameters in the config file.")
return
}

Expand All @@ -55,20 +56,20 @@ extension FigmaExportCommand {
platform: .ios,
nameValidateRegexp: params.common?.icons?.nameValidateRegexp,
nameReplaceRegexp: params.common?.icons?.nameReplaceRegexp,
nameStyle: params.ios?.icons.nameStyle
nameStyle: iconsParams.nameStyle
)
let icons = try processor.process(assets: images).get()

let assetsURL = ios.xcassetsPath.appendingPathComponent(ios.icons.assetsFolder)
let assetsURL = ios.xcassetsPath.appendingPathComponent(iconsParams.assetsFolder)
let output = XcodeImagesOutput(
assetsFolderURL: assetsURL,
assetsInMainBundle: ios.xcassetsInMainBundle,
assetsInSwiftPackage: ios.xcassetsInSwiftPackage,
addObjcAttribute: ios.addObjcAttribute,
preservesVectorRepresentation: ios.icons.preservesVectorRepresentation,
uiKitImageExtensionURL: ios.icons.imageSwift,
swiftUIImageExtensionURL: ios.icons.swiftUIImageSwift,
renderMode: ios.icons.renderMode)
preservesVectorRepresentation: iconsParams.preservesVectorRepresentation,
uiKitImageExtensionURL: iconsParams.imageSwift,
swiftUIImageExtensionURL: iconsParams.swiftUIImageSwift,
renderMode: iconsParams.renderMode)

let exporter = XcodeIconsExporter(output: output)
let localAndRemoteFiles = try exporter.export(icons: icons, append: filter != nil)
Expand Down
13 changes: 7 additions & 6 deletions Sources/FigmaExport/Subcommands/ExportImages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ extension FigmaExportCommand {
}

private func exportiOSImages(client: Client, params: Params, logger: Logger) throws {
guard let ios = params.ios else {
logger.info("Nothing to do. You haven’t specified ios parameter in the config file.")
guard let ios = params.ios,
let imagesParams = ios.images else {
logger.info("Nothing to do. You haven’t specified ios.images parameters in the config file.")
return
}

Expand All @@ -55,22 +56,22 @@ extension FigmaExportCommand {
platform: .ios,
nameValidateRegexp: params.common?.images?.nameValidateRegexp,
nameReplaceRegexp: params.common?.images?.nameReplaceRegexp,
nameStyle: params.ios?.images.nameStyle
nameStyle: imagesParams.nameStyle
)
let images = processor.process(light: imagesTuple.light, dark: imagesTuple.dark)
if let warning = images.warning?.errorDescription {
logger.warning("\(warning)")
}

let assetsURL = ios.xcassetsPath.appendingPathComponent(ios.images.assetsFolder)
let assetsURL = ios.xcassetsPath.appendingPathComponent(imagesParams.assetsFolder)

let output = XcodeImagesOutput(
assetsFolderURL: assetsURL,
assetsInMainBundle: ios.xcassetsInMainBundle,
assetsInSwiftPackage: ios.xcassetsInSwiftPackage,
addObjcAttribute: ios.addObjcAttribute,
uiKitImageExtensionURL: ios.images.imageSwift,
swiftUIImageExtensionURL: ios.images.swiftUIImageSwift)
uiKitImageExtensionURL: imagesParams.imageSwift,
swiftUIImageExtensionURL: imagesParams.swiftUIImageSwift)

let exporter = XcodeImagesExporter(output: output)
let localAndRemoteFiles = try exporter.export(assets: images.get(), append: filter != nil)
Expand Down
16 changes: 9 additions & 7 deletions Sources/FigmaExport/Subcommands/ExportTypography.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ extension FigmaExportCommand {
logger.info("Fetching text styles. Please wait...")
let loader = TextStylesLoader(client: client, params: options.params.figma)
let textStyles = try loader.load()

if let ios = options.params.ios {

if let ios = options.params.ios,
let typographyParams = ios.typography {

logger.info("Processing typography...")
let processor = TypographyProcessor(
platform: .ios,
nameValidateRegexp: options.params.common?.typography?.nameValidateRegexp,
nameReplaceRegexp: options.params.common?.typography?.nameReplaceRegexp,
nameStyle: options.params.ios?.typography.nameStyle
nameStyle: typographyParams.nameStyle
)
let processedTextStyles = try processor.process(assets: textStyles).get()
logger.info("Saving text styles...")
Expand All @@ -59,10 +61,10 @@ extension FigmaExportCommand {

private func exportXcodeTextStyles(textStyles: [TextStyle], iosParams: Params.iOS, logger: Logger) throws {
let output = XcodeTypographyOutput(
fontExtensionURL: iosParams.typography.fontSwift,
swiftUIFontExtensionURL: iosParams.typography.swiftUIFontSwift,
generateLabels: iosParams.typography.generateLabels,
labelsDirectory: iosParams.typography.labelsDirectory,
fontExtensionURL: iosParams.typography?.fontSwift,
swiftUIFontExtensionURL: iosParams.typography?.swiftUIFontSwift,
generateLabels: iosParams.typography?.generateLabels,
labelsDirectory: iosParams.typography?.labelsDirectory,
addObjcAttribute: iosParams.addObjcAttribute
)
let exporter = XcodeTypographyExporter(output: output)
Expand Down

0 comments on commit 66f65e0

Please sign in to comment.