Skip to content

Commit

Permalink
Allow TextStyle to be processable by an AssetsProcessor (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonlee2 authored Feb 26, 2021
1 parent efe4dac commit d651b8d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ common:
nameValidateRegexp: '^(img)_([a-z0-9_]+)$' # RegExp pattern for: img_image_name
# [optional] RegExp pattern for replacing. Supports only $n
nameReplaceRegexp: 'image_$2'
# [optional]
typography:
# [optional] RegExp pattern for text style name validation before exporting
nameValidateRegexp: '^[a-zA-Z0-9_]+$' # RegExp pattern for: h1_regular, h1_medium
# [optional] RegExp pattern for replacing. Supports only $n
nameReplaceRegexp: 'font_$1'

# [optional] iOS export parameters
ios:
Expand Down Expand Up @@ -116,6 +122,8 @@ ios:
generateLabels: true
# Relative or absolute path to directory where to place UILabel for each text style (font) (Requred if generateLabels = true)
labelsDirectory: "./Source/UIComponents/"
# Typography name style: camelCase or snake_case
nameStyle: camelCase

# [optional] Android export parameters
android:
Expand Down
7 changes: 7 additions & 0 deletions Sources/FigmaExport/Input/Params.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ struct Params: Decodable {
let nameReplaceRegexp: String?
}

struct Typography: Decodable {
let nameValidateRegexp: String?
let nameReplaceRegexp: String?
}

let colors: Colors?
let icons: Icons?
let images: Images?
let typography: Typography?
}

enum VectorFormat: String, Decodable {
Expand Down Expand Up @@ -78,6 +84,7 @@ struct Params: Decodable {
let swiftUIFontSwift: URL?
let generateLabels: Bool
let labelsDirectory: URL?
let nameStyle: NameStyle
}

let xcodeprojPath: String
Expand Down
10 changes: 9 additions & 1 deletion Sources/FigmaExport/Subcommands/ExportTypography.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ extension FigmaExportCommand {
let textStyles = try loader.load()

if let ios = options.params.ios {
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
)
let processedTextStyles = try processor.process(assets: textStyles).get()
logger.info("Saving text styles...")
try exportXcodeTextStyles(textStyles: textStyles, iosParams: ios, logger: logger)
try exportXcodeTextStyles(textStyles: processedTextStyles, iosParams: ios, logger: logger)
logger.info("Done!")
}
}
Expand Down
16 changes: 16 additions & 0 deletions Sources/FigmaExportCore/Processor/AssetsProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ public struct ColorsProcessor: AssetsProcessable {
}
}

public struct TypographyProcessor: AssetsProcessable {
public typealias AssetType = TextStyle

public let platform: Platform
public let nameValidateRegexp: String?
public let nameReplaceRegexp: String?
public let nameStyle: NameStyle?

public init(platform: Platform, nameValidateRegexp: String?, nameReplaceRegexp: String?, nameStyle: NameStyle?) {
self.platform = platform
self.nameValidateRegexp = nameValidateRegexp
self.nameReplaceRegexp = nameReplaceRegexp
self.nameStyle = nameStyle
}
}

public struct ImagesProcessor: AssetsProcessable {
public typealias AssetType = ImagePack

Expand Down
17 changes: 14 additions & 3 deletions Sources/FigmaExportCore/TextStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public enum DynamicTypeStyle: String, RawRepresentable {
}
}

public struct TextStyle {

public let name: String
public struct TextStyle: Asset {
public var name: String
public var platform: Platform?
public let fontName: String
public let fontSize: Double
public let fontStyle: DynamicTypeStyle?
Expand All @@ -50,6 +50,7 @@ public struct TextStyle {

public init(
name: String,
platform: Platform? = nil,
fontName: String,
fontSize: Double,
fontStyle: DynamicTypeStyle?,
Expand All @@ -63,4 +64,14 @@ public struct TextStyle {
self.lineHeight = lineHeight
self.letterSpacing = letterSpacing
}

// MARK: Hashable

public static func == (lhs: TextStyle, rhs: TextStyle) -> Bool {
return lhs.name == rhs.name
}

public func hash(into hasher: inout Hasher) {
hasher.combine(name)
}
}

0 comments on commit d651b8d

Please sign in to comment.