Skip to content

Commit

Permalink
Merge pull request #43 from hhru/group-by-component-set
Browse files Browse the repository at this point in the history
Добавлен флаг groupByComponentSet
  • Loading branch information
timbaev authored Sep 5, 2023
2 parents 2ed2815 + 45fc913 commit b06a02d
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 40 deletions.
1 change: 1 addition & 0 deletions Demo/.figmagen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ images:
assets: FigmaGenDemo/Resources/Images.xcassets/Generated
destination: FigmaGenDemo/Generated/Images.swift
groupByFrame: true
groupByComponentSet: true
templateOptions:
publicAccess: true

Expand Down
13 changes: 12 additions & 1 deletion Sources/FigmaGen/Commands/ImagesCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,17 @@ final class ImagesCommand: AsyncExecutableCommand, GenerationConfigurableCommand
let groupByFrame = Flag(
"--groupByFrame",
description: """
Groupу generated assets and resources into folders with name of parent frame.
Group generated assets and resources into folders with name of parent frame.
By default without grouping.
"""
)

let groupByComponentSet = Flag(
"--groupByComponentSet",
description: """
Group generated assets and resources into folders with name of component set.
Only for components with variants.
By default without grouping.
"""
)

Expand Down Expand Up @@ -226,6 +236,7 @@ final class ImagesCommand: AsyncExecutableCommand, GenerationConfigurableCommand
useAbsoluteBounds: useAbsoluteBounds.value,
preserveVectorData: preserveVectorData.value,
groupByFrame: groupByFrame.value,
groupByComponentSet: groupByComponentSet.value,
namingStyle: resolveNamingStyle()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ extension ImagesConfiguration {
useAbsoluteBounds: useAbsoluteBounds,
preserveVectorData: preserveVectorData,
groupByFrame: groupByFrame,
groupByComponentSet: groupByComponentSet,
namingStyle: namingStyle
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct ImagesConfiguration: Decodable {
case useAbsoluteBounds
case preserveVectorData
case groupByFrame
case groupByComponentSet
case namingStyle
}

Expand All @@ -29,6 +30,7 @@ struct ImagesConfiguration: Decodable {
let useAbsoluteBounds: Bool
let preserveVectorData: Bool
let groupByFrame: Bool
let groupByComponentSet: Bool
let namingStyle: ImageNamingStyle

// MARK: - Initializers
Expand All @@ -44,6 +46,7 @@ struct ImagesConfiguration: Decodable {
useAbsoluteBounds: Bool,
preserveVectorData: Bool,
groupByFrame: Bool,
groupByComponentSet: Bool,
namingStyle: ImageNamingStyle
) {
self.generatation = generatation
Expand All @@ -56,6 +59,7 @@ struct ImagesConfiguration: Decodable {
self.useAbsoluteBounds = useAbsoluteBounds
self.preserveVectorData = preserveVectorData
self.groupByFrame = groupByFrame
self.groupByComponentSet = groupByComponentSet
self.namingStyle = namingStyle
}

Expand All @@ -72,6 +76,7 @@ struct ImagesConfiguration: Decodable {
useAbsoluteBounds = try container.decodeIfPresent(forKey: .useAbsoluteBounds) ?? false
preserveVectorData = try container.decodeIfPresent(forKey: .preserveVectorData) ?? false
groupByFrame = try container.decodeIfPresent(forKey: .groupByFrame) ?? false
groupByComponentSet = try container.decodeIfPresent(forKey: .groupByComponentSet) ?? false
namingStyle = try container.decodeIfPresent(forKey: .namingStyle) ?? .camelCase

generatation = try GenerationConfiguration(from: decoder)
Expand All @@ -91,6 +96,7 @@ struct ImagesConfiguration: Decodable {
useAbsoluteBounds: useAbsoluteBounds,
preserveVectorData: preserveVectorData,
groupByFrame: groupByFrame,
groupByComponentSet: groupByComponentSet,
namingStyle: namingStyle
)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/FigmaGen/Models/Parameters/ImagesParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ struct ImagesParameters {
let useAbsoluteBounds: Bool
let preserveVectorData: Bool
let groupByFrame: Bool
let groupByComponentSet: Bool
let namingStyle: ImageNamingStyle
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ final class DefaultImageAssetsProvider: ImageAssetsProvider, ImagesFolderPathRes

let folderPath = resolveFolderPath(
groupByFrame: parameters.groupByFrame,
groupByComponentSet: parameters.groupByComponentSet,
setNode: setNode,
folderPath: folderPath
)
Expand Down Expand Up @@ -113,6 +114,7 @@ final class DefaultImageAssetsProvider: ImageAssetsProvider, ImagesFolderPathRes
private func saveAssetFolders(
assets: [ImageComponentSetAsset: AssetFolder],
groupByFrame: Bool,
groupByComponentSet: Bool,
in folderPath: String
) throws -> Promise<Void> {
let folderPath = Path(folderPath)
Expand All @@ -124,7 +126,12 @@ final class DefaultImageAssetsProvider: ImageAssetsProvider, ImagesFolderPathRes
let promises = assets.map { asset, folder in
assetsProvider.saveAssetFolder(
folder,
in: resolveFolderPath(groupByFrame: groupByFrame, setAsset: asset, folderPath: folderPath).string
in: resolveFolderPath(
groupByFrame: groupByFrame,
groupByComponentSet: groupByComponentSet,
setAsset: asset,
folderPath: folderPath
).string
)
}

Expand Down Expand Up @@ -163,7 +170,12 @@ final class DefaultImageAssetsProvider: ImageAssetsProvider, ImagesFolderPathRes
)
}
}.then { assets in
try self.saveAssetFolders(assets: assets, groupByFrame: parameters.groupByFrame, in: folderPath)
try self.saveAssetFolders(
assets: assets,
groupByFrame: parameters.groupByFrame,
groupByComponentSet: parameters.groupByComponentSet,
in: folderPath
)
}.then {
self.saveImageFiles(assets: assets)
}
Expand Down
15 changes: 3 additions & 12 deletions Sources/FigmaGen/Providers/Images/DefaultImagesProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,13 @@ final class DefaultImagesProvider: ImagesProvider {

private func saveResourceImagesIfNeeded(
nodes: [ImageComponentSetRenderedNode],
groupByFrame: Bool,
format: ImageFormat,
postProcessor: String?,
namingStyle: ImageNamingStyle,
parameters: ImagesParameters,
in resources: String?
) -> Promise<[ImageRenderedNode: ImageResource]> {
resources.map { folderPath in
imageResourcesProvider.saveImages(
nodes: nodes,
groupByFrame: groupByFrame,
format: format,
postProcessor: postProcessor,
namingStyle: namingStyle,
parameters: parameters,
in: folderPath
)
} ?? .value([:])
Expand All @@ -193,10 +187,7 @@ final class DefaultImagesProvider: ImagesProvider {
),
self.saveResourceImagesIfNeeded(
nodes: nodes,
groupByFrame: parameters.groupByFrame,
format: parameters.format,
postProcessor: parameters.postProcessor,
namingStyle: parameters.namingStyle,
parameters: parameters,
in: parameters.resources
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ protocol ImagesFolderPathResolving {

func resolveFolderPath(
groupByFrame: Bool,
groupByComponentSet: Bool,
parentNodeName: String?,
isSingleComponent: Bool,
nodeName: String,
Expand All @@ -20,6 +21,7 @@ extension ImagesFolderPathResolving {

func resolveFolderPath(
groupByFrame: Bool,
groupByComponentSet: Bool,
parentNodeName: String?,
isSingleComponent: Bool,
nodeName: String,
Expand All @@ -31,7 +33,7 @@ extension ImagesFolderPathResolving {
folderPath = folderPath.appending(name.camelized)
}

if !isSingleComponent {
if groupByComponentSet, !isSingleComponent {
folderPath = folderPath.appending(nodeName.camelized)
}

Expand All @@ -40,11 +42,13 @@ extension ImagesFolderPathResolving {

func resolveFolderPath(
groupByFrame: Bool,
groupByComponentSet: Bool,
setNode: ImageComponentSetRenderedNode,
folderPath: Path
) -> Path {
resolveFolderPath(
groupByFrame: groupByFrame,
groupByComponentSet: groupByComponentSet,
parentNodeName: setNode.parentName,
isSingleComponent: setNode.isSingleComponent,
nodeName: setNode.name,
Expand All @@ -54,11 +58,13 @@ extension ImagesFolderPathResolving {

func resolveFolderPath(
groupByFrame: Bool,
groupByComponentSet: Bool,
setAsset: ImageComponentSetAsset,
folderPath: Path
) -> Path {
resolveFolderPath(
groupByFrame: groupByFrame,
groupByComponentSet: groupByComponentSet,
parentNodeName: setAsset.parentName,
isSingleComponent: setAsset.isSingleComponent,
nodeName: setAsset.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ final class DefaultImageResourcesProvider: ImageResourcesProvider, ImagesFolderP
private func makeResource(
for node: ImageRenderedNode,
setNode: ImageComponentSetRenderedNode,
groupByFrame: Bool,
format: ImageFormat,
namingStyle: ImageNamingStyle,
parameters: ImagesParameters,
folderPath: Path
) -> ImageResource {
let fileName = resolveFileName(for: node, setNode: setNode, namingStyle: namingStyle)
let folderPath = resolveFolderPath(groupByFrame: groupByFrame, setNode: setNode, folderPath: folderPath)
let fileExtension = format.fileExtension
let fileName = resolveFileName(for: node, setNode: setNode, namingStyle: parameters.namingStyle)

let folderPath = resolveFolderPath(
groupByFrame: parameters.groupByFrame,
groupByComponentSet: parameters.groupByComponentSet,
setNode: setNode,
folderPath: folderPath
)

let fileExtension = parameters.format.fileExtension

let filePaths = node.urls.keys.reduce(into: [:]) { result, scale in
result[scale] = folderPath
Expand All @@ -58,9 +63,7 @@ final class DefaultImageResourcesProvider: ImageResourcesProvider, ImagesFolderP

private func makeResources(
for nodes: [ImageComponentSetRenderedNode],
groupByFrame: Bool,
format: ImageFormat,
namingStyle: ImageNamingStyle,
parameters: ImagesParameters,
folderPath: Path
) -> [ImageRenderedNode: ImageResource] {
var resources: [ImageRenderedNode: ImageResource] = [:]
Expand All @@ -70,9 +73,7 @@ final class DefaultImageResourcesProvider: ImageResourcesProvider, ImagesFolderP
resources[node] = makeResource(
for: node,
setNode: setNode,
groupByFrame: groupByFrame,
format: format,
namingStyle: namingStyle,
parameters: parameters,
folderPath: folderPath
)
}
Expand Down Expand Up @@ -121,22 +122,17 @@ final class DefaultImageResourcesProvider: ImageResourcesProvider, ImagesFolderP

func saveImages(
nodes: [ImageComponentSetRenderedNode],
groupByFrame: Bool,
format: ImageFormat,
postProcessor: String?,
namingStyle: ImageNamingStyle,
parameters: ImagesParameters,
in folderPath: String
) -> Promise<[ImageRenderedNode: ImageResource]> {
perform(on: DispatchQueue.global(qos: .userInitiated)) {
self.makeResources(
for: nodes,
groupByFrame: groupByFrame,
format: format,
namingStyle: namingStyle,
parameters: parameters,
folderPath: Path(folderPath)
)
}.nest { resources in
try self.saveImageFiles(resources: resources, postProcessor: postProcessor, in: Path(folderPath))
try self.saveImageFiles(resources: resources, postProcessor: parameters.postProcessor, in: Path(folderPath))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ protocol ImageResourcesProvider {

func saveImages(
nodes: [ImageComponentSetRenderedNode],
groupByFrame: Bool,
format: ImageFormat,
postProcessor: String?,
namingStyle: ImageNamingStyle,
parameters: ImagesParameters,
in folderPath: String
) -> Promise<[ImageRenderedNode: ImageResource]>
}

0 comments on commit b06a02d

Please sign in to comment.