Skip to content

Commit

Permalink
Merge pull request #58 from hhru/MOB-35468_support_several_files_gene…
Browse files Browse the repository at this point in the history
…ration_from_images

Mob 35468 support several files generation from images
  • Loading branch information
timbaev authored Dec 19, 2023
2 parents 17bdef0 + 0065b81 commit efd4f50
Show file tree
Hide file tree
Showing 18 changed files with 357 additions and 259 deletions.
12 changes: 9 additions & 3 deletions Sources/FigmaGen/Commands/GenerationConfigurableCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ extension GenerationConfigurableCommand {

// MARK: - Instance Properties

// !!! Important note !!!
// For CLI usage of FigmaGen we don't support multiple templates for any token type.
var generationConfiguration: GenerationConfiguration {
GenerationConfiguration(
file: resolveFileConfiguration(),
accessToken: resolveAccessTokenConfiguration(),
template: template.value,
templateOptions: resolveTemplateOptions(),
destination: destination.value
templates: [
TemplateConfiguration(
template: template.value,
templateOptions: resolveTemplateOptions(),
destination: destination.value
)
]
)
}

Expand Down
14 changes: 7 additions & 7 deletions Sources/FigmaGen/Commands/TokensCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,49 +231,49 @@ extension TokensCommand {
accessToken: resolveAccessTokenConfiguration(),
templates: TokensTemplateConfiguration(
colors: [
TokensTemplateConfiguration.Template(
TemplateConfiguration(
template: colorsTemplate.value,
templateOptions: resolveTemplateOptions(colorsTemplateOptions.value),
destination: colorsDestination.value
)
],
baseColors: [
TokensTemplateConfiguration.Template(
TemplateConfiguration(
template: baseColorsTemplate.value,
templateOptions: resolveTemplateOptions(baseColorsTemplateOptions.value),
destination: baseColorsDestination.value
)
],
fontFamilies: [
TokensTemplateConfiguration.Template(
TemplateConfiguration(
template: fontFamiliesTemplate.value,
templateOptions: resolveTemplateOptions(fontFamiliesTemplateOptions.value),
destination: fontFamiliesDestination.value
)
],
typographies: [
TokensTemplateConfiguration.Template(
TemplateConfiguration(
template: typographiesTemplate.value,
templateOptions: resolveTemplateOptions(typographiesTemplateOptions.value),
destination: typographiesDestination.value
)
],
boxShadows: [
TokensTemplateConfiguration.Template(
TemplateConfiguration(
template: boxShadowsTemplate.value,
templateOptions: resolveTemplateOptions(boxShadowsTemplateOptions.value),
destination: boxShadowsDestination.value
)
],
theme: [
TokensTemplateConfiguration.Template(
TemplateConfiguration(
template: themeTemplate.value,
templateOptions: resolveTemplateOptions(themeTemplateOptions.value),
destination: themeDestination.value
)
],
spacing: [
TokensTemplateConfiguration.Template(
TemplateConfiguration(
template: spacingTemplate.value,
templateOptions: resolveTemplateOptions(spacingTemplateOptions.value),
destination: spacingDestination.value
Expand Down
25 changes: 19 additions & 6 deletions Sources/FigmaGen/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ enum Dependencies {
// MARK: -

static let tokensResolver: TokensResolver = DefaultTokensResolver()
static let renderParametersResolver: RenderParametersResolver = DefaultRenderParametersResolver()
static let accessTokenResolver: AccessTokenResolver = DefaultAccessTokenResolver()

static let tokensGenerationParametersResolver: TokensGenerationParametersResolver
= DefaultTokensGenerationParametersResolver()
static let tokensGenerationParametersResolver: TokensGenerationParametersResolver =
DefaultTokensGenerationParametersResolver(
renderParametersResolver: renderParametersResolver,
accessTokenResolver: accessTokenResolver
)

static let colorTokensContextProvider: ColorTokensContextProvider = DefaultColorTokensContextProvider(
tokensResolver: tokensResolver
Expand Down Expand Up @@ -108,22 +113,30 @@ enum Dependencies {

static let colorStylesGenerator: ColorStylesGenerator = DefaultColorStylesGenerator(
colorStylesProvider: colorStylesProvider,
templateRenderer: templateRenderer
templateRenderer: templateRenderer,
accessTokenResolver: accessTokenResolver,
renderParametersResolver: renderParametersResolver
)

static let textStylesGenerator: TextStylesGenerator = DefaultTextStylesGenerator(
textStylesProvider: textStylesProvider,
templateRenderer: templateRenderer
templateRenderer: templateRenderer,
accessTokenResolver: accessTokenResolver,
renderParametersResolver: renderParametersResolver
)

static let imagesGenerator: ImagesGenerator = DefaultImagesGenerator(
imagesProvider: imagesProvider,
templateRenderer: templateRenderer
templateRenderer: templateRenderer,
accessTokenResolver: accessTokenResolver,
renderParametersResolver: renderParametersResolver
)

static let shadowStylesGenerator: ShadowStylesGenerator = DefaultShadowStylesGenerator(
shadowStylesProvider: shadowStylesProvider,
templateRenderer: templateRenderer
templateRenderer: templateRenderer,
accessTokenResolver: accessTokenResolver,
renderParametersResolver: renderParametersResolver
)

static let colorTokensGenerator: ColorTokensGenerator = DefaultColorTokensGenerator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,45 @@ final class DefaultColorStylesGenerator: ColorStylesGenerator, GenerationParamet

let colorStylesProvider: ColorStylesProvider
let templateRenderer: TemplateRenderer
let accessTokenResolver: AccessTokenResolver
let renderParametersResolver: RenderParametersResolver

let defaultTemplateType = RenderTemplateType.native(name: "ColorStyles")
let defaultDestination = RenderDestination.console

// MARK: - Initializers

init(colorStylesProvider: ColorStylesProvider, templateRenderer: TemplateRenderer) {
init(
colorStylesProvider: ColorStylesProvider,
templateRenderer: TemplateRenderer,
accessTokenResolver: AccessTokenResolver,
renderParametersResolver: RenderParametersResolver
) {
self.colorStylesProvider = colorStylesProvider
self.templateRenderer = templateRenderer
self.accessTokenResolver = accessTokenResolver
self.renderParametersResolver = renderParametersResolver
}

// MARK: - Instance Methods

private func generate(parameters: GenerationParameters, assets: String?) -> Promise<Void> {
firstly {
self.colorStylesProvider.fetchColorStyles(
colorStylesProvider.fetchColorStyles(
from: parameters.file,
nodes: parameters.nodes,
assets: assets
)
}.map { colorStyles in
ColorStylesContext(colorStyles: colorStyles)
}.done { context in
try self.templateRenderer.renderTemplate(
parameters.render.template,
to: parameters.render.destination,
context: context
)
try parameters.renderParameters.forEach { params in
try self.templateRenderer.renderTemplate(
params.template,
to: params.destination,
context: context
)
}
}
}

Expand Down
58 changes: 17 additions & 41 deletions Sources/FigmaGen/Generators/GenerationParametersResolving.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ protocol GenerationParametersResolving {

// MARK: - Instance Properties

var accessTokenResolver: AccessTokenResolver { get }
var renderParametersResolver: RenderParametersResolver { get }

// MARK: - Instance Properties

var defaultTemplateType: RenderTemplateType { get }
var defaultDestination: RenderDestination { get }

Expand All @@ -14,45 +19,16 @@ protocol GenerationParametersResolving {

extension GenerationParametersResolving {

// MARK: - Instance Methods

private func resolveAccessToken(configuration: GenerationConfiguration) -> String? {
switch configuration.accessToken {
case let .value(accessToken):
return accessToken

case let .environmentVariable(environmentVariable):
return ProcessInfo.processInfo.environment[environmentVariable]

case nil:
return nil
}
}

private func resolveTemplateType(configuration: GenerationConfiguration) -> RenderTemplateType {
if let templatePath = configuration.template {
return .custom(path: templatePath)
}

return defaultTemplateType
}

private func resolveDestination(configuration: GenerationConfiguration) -> RenderDestination {
if let destinationPath = configuration.destination {
return .file(path: destinationPath)
}

return defaultDestination
}

// MARK: -

func resolveGenerationParameters(from configuration: GenerationConfiguration) throws -> GenerationParameters {
guard let fileConfiguration = configuration.file else {
throw GenerationParametersError.invalidFileConfiguration
}

guard let accessToken = resolveAccessToken(configuration: configuration), !accessToken.isEmpty else {
let accessToken = accessTokenResolver.resolveAccessToken(from: configuration.accessToken)

guard let accessToken, !accessToken.isEmpty else {
throw GenerationParametersError.invalidAccessToken
}

Expand All @@ -67,16 +43,16 @@ extension GenerationParametersResolving {
excludedIDs: fileConfiguration.excludedNodes
)

let templateType = resolveTemplateType(configuration: configuration)
let destination = resolveDestination(configuration: configuration)

let template = RenderTemplate(
type: templateType,
options: configuration.templateOptions ?? [:]
let renderParametersList = renderParametersResolver.resolveRenderParameters(
templates: configuration.templates,
defaultTemplateType: defaultTemplateType,
defaultDestination: defaultDestination
)

let render = RenderParameters(template: template, destination: destination)

return GenerationParameters(file: file, nodes: nodes, render: render)
return GenerationParameters(
file: file,
nodes: nodes,
renderParameters: renderParametersList
)
}
}
23 changes: 17 additions & 6 deletions Sources/FigmaGen/Generators/Images/DefaultImagesGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ final class DefaultImagesGenerator: ImagesGenerator, GenerationParametersResolvi

let imagesProvider: ImagesProvider
let templateRenderer: TemplateRenderer
let accessTokenResolver: AccessTokenResolver
let renderParametersResolver: RenderParametersResolver

let defaultTemplateType = RenderTemplateType.native(name: "Images")
let defaultDestination = RenderDestination.console

// MARK: - Initializers

init(imagesProvider: ImagesProvider, templateRenderer: TemplateRenderer) {
init(
imagesProvider: ImagesProvider,
templateRenderer: TemplateRenderer,
accessTokenResolver: AccessTokenResolver,
renderParametersResolver: RenderParametersResolver
) {
self.imagesProvider = imagesProvider
self.templateRenderer = templateRenderer
self.accessTokenResolver = accessTokenResolver
self.renderParametersResolver = renderParametersResolver
}

// MARK: - Instance Methods
Expand All @@ -33,11 +42,13 @@ final class DefaultImagesGenerator: ImagesGenerator, GenerationParametersResolvi
imageSets: imageSets.sorted { $0.name.lowercased() < $1.name.lowercased() }
)
}.done { context in
try self.templateRenderer.renderTemplate(
parameters.render.template,
to: parameters.render.destination,
context: context
)
try parameters.renderParameters.forEach { params in
try self.templateRenderer.renderTemplate(
params.template,
to: params.destination,
context: context
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ final class DefaultShadowStylesGenerator: ShadowStylesGenerator, GenerationParam

let shadowStylesProvider: ShadowStylesProvider
let templateRenderer: TemplateRenderer
let accessTokenResolver: AccessTokenResolver
let renderParametersResolver: RenderParametersResolver

let defaultTemplateType: RenderTemplateType = .native(name: "ShadowStyles")
let defaultDestination: RenderDestination = .console

// MARK: - Initializers

init(shadowStylesProvider: ShadowStylesProvider, templateRenderer: TemplateRenderer) {
init(
shadowStylesProvider: ShadowStylesProvider,
templateRenderer: TemplateRenderer,
accessTokenResolver: AccessTokenResolver,
renderParametersResolver: RenderParametersResolver
) {
self.shadowStylesProvider = shadowStylesProvider
self.templateRenderer = templateRenderer
self.accessTokenResolver = accessTokenResolver
self.renderParametersResolver = renderParametersResolver
}

// MARK: - Instance Methods
Expand All @@ -27,11 +36,13 @@ final class DefaultShadowStylesGenerator: ShadowStylesGenerator, GenerationParam
}.map { shadowStyles in
ShadowStylesContext(shadowStyles: shadowStyles)
}.done { context in
try self.templateRenderer.renderTemplate(
parameters.render.template,
to: parameters.render.destination,
context: context
)
try parameters.renderParameters.forEach { params in
try self.templateRenderer.renderTemplate(
params.template,
to: params.destination,
context: context
)
}
}
}

Expand Down
Loading

0 comments on commit efd4f50

Please sign in to comment.