diff --git a/CONFIG.md b/CONFIG.md index 1b58a41..d831b01 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -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 @@ -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 @@ -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 @@ -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" diff --git a/Sources/FigmaExport/Input/Params.swift b/Sources/FigmaExport/Input/Params.swift index bb00f0b..a36226a 100644 --- a/Sources/FigmaExport/Input/Params.swift +++ b/Sources/FigmaExport/Input/Params.swift @@ -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 { diff --git a/Sources/FigmaExport/Loaders/ImagesLoader.swift b/Sources/FigmaExport/Loaders/ImagesLoader.swift index 1253bad..1b456ee 100644 --- a/Sources/FigmaExport/Loaders/ImagesLoader.swift +++ b/Sources/FigmaExport/Loaders/ImagesLoader.swift @@ -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() @@ -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 } diff --git a/Sources/FigmaExport/Subcommands/ExportColors.swift b/Sources/FigmaExport/Subcommands/ExportColors.swift index 0140332..61a7f20 100644 --- a/Sources/FigmaExport/Subcommands/ExportColors.swift +++ b/Sources/FigmaExport/Subcommands/ExportColors.swift @@ -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 ) @@ -76,9 +76,14 @@ extension FigmaExportCommand { } private func exportXcodeColors(colorPairs: [AssetPair], 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 @@ -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) } diff --git a/Sources/FigmaExport/Subcommands/ExportIcons.swift b/Sources/FigmaExport/Subcommands/ExportIcons.swift index 3dfe6c6..e3b5d05 100644 --- a/Sources/FigmaExport/Subcommands/ExportIcons.swift +++ b/Sources/FigmaExport/Subcommands/ExportIcons.swift @@ -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 } @@ -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) diff --git a/Sources/FigmaExport/Subcommands/ExportImages.swift b/Sources/FigmaExport/Subcommands/ExportImages.swift index 8f6c11e..880d932 100644 --- a/Sources/FigmaExport/Subcommands/ExportImages.swift +++ b/Sources/FigmaExport/Subcommands/ExportImages.swift @@ -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 } @@ -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) diff --git a/Sources/FigmaExport/Subcommands/ExportTypography.swift b/Sources/FigmaExport/Subcommands/ExportTypography.swift index 7b9ea19..11e8f34 100644 --- a/Sources/FigmaExport/Subcommands/ExportTypography.swift +++ b/Sources/FigmaExport/Subcommands/ExportTypography.swift @@ -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...") @@ -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)