Skip to content

Commit

Permalink
Refactor ImageStructGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlokhorst committed Jun 6, 2017
1 parent 4e8569b commit 8f38351
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 38 deletions.
37 changes: 37 additions & 0 deletions Sources/RswiftCore/Generators/AssetSubfolders.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// AssetSubfolders.swift
// Spectre
//
// Created by Tom Lokhorst on 2017-06-06.
//

import Foundation

struct AssetSubfolders {
let folders: [NamespacedAssetSubfolder]
let duplicates: [NamespacedAssetSubfolder]

init(all subfolders: [NamespacedAssetSubfolder], assetIdentifiers: [SwiftIdentifier]) {
var dict: [SwiftIdentifier: NamespacedAssetSubfolder] = [:]

for subfolder in subfolders {
let name = SwiftIdentifier(name: subfolder.name)
if let duplicate = dict[name] {
duplicate.subfolders += subfolder.subfolders
duplicate.imageAssets += subfolder.imageAssets
} else {
dict[name] = subfolder
}
}

self.folders = dict.values.filter { !assetIdentifiers.contains(SwiftIdentifier(name: $0.name)) }
self.duplicates = dict.values.filter { assetIdentifiers.contains(SwiftIdentifier(name: $0.name)) }
}

func printWarningsForDuplicates() {
for subfolder in duplicates {
warn("Skipping asset subfolder because symbol '\(subfolder.name)' would conflict with image: \(subfolder.name)")
}
}
}

54 changes: 16 additions & 38 deletions Sources/RswiftCore/Generators/ImageStructGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,27 @@ struct ImageStructGenerator: ExternalOnlyStructGenerator {
}

return Struct(
comments: ["This `R.image` struct is generated, and contains static references to \(imageLets.count) images."],
comments: ["This `\(qualifiedName)` struct is generated, and contains static references to \(imageLets.count) images."],
accessModifier: externalAccessLevel,
type: Type(module: .host, name: "image"),
type: Type(module: .host, name: structName),
implements: [],
typealiasses: [],
properties: imageLets,
functions: groupedFunctions.uniques.map { imageFunction(for: $0, at: externalAccessLevel) },
functions: groupedFunctions.uniques.map { imageFunction(for: $0, at: externalAccessLevel, prefix: qualifiedName) },
structs: structs,
classes: []
)
}

private func imageFunction(for name: String, at externalAccessLevel: AccessLevel) -> Function {
private func imageFunction(for name: String, at externalAccessLevel: AccessLevel, prefix: SwiftIdentifier) -> Function {
let structName = SwiftIdentifier(name: name)
let qualifiedName = prefix + structName

return Function(
comments: ["`UIImage(named: \"\(name)\", bundle: ..., traitCollection: ...)`"],
accessModifier: externalAccessLevel,
isStatic: true,
name: SwiftIdentifier(name: name),
name: structName,
generics: nil,
parameters: [
Function.Parameter(
Expand All @@ -87,7 +90,7 @@ struct ImageStructGenerator: ExternalOnlyStructGenerator {
],
doesThrow: false,
returnType: Type._UIImage.asOptional(),
body: "return UIKit.UIImage(resource: R.image.\(SwiftIdentifier(name: name)), compatibleWith: traitCollection)"
body: "return UIKit.UIImage(resource: \(qualifiedName), compatibleWith: traitCollection)"
)
}
}
Expand Down Expand Up @@ -132,18 +135,21 @@ extension NamespacedAssetSubfolder: ExternalOnlyStructGenerator {
implements: [],
typealiasses: [],
properties: imageLets,
functions: groupedFunctions.uniques.map { imageFunction(for: $0, at: externalAccessLevel) },
functions: groupedFunctions.uniques.map { imageFunction(for: $0, at: externalAccessLevel, prefix: qualifiedName) },
structs: structs,
classes: []
)
}

private func imageFunction(for name: String, at externalAccessLevel: AccessLevel) -> Function {
private func imageFunction(for name: String, at externalAccessLevel: AccessLevel, prefix: SwiftIdentifier) -> Function {
let structName = SwiftIdentifier(name: name)
let qualifiedName = prefix + structName

return Function(
comments: ["`UIImage(named: \"\(name)\", bundle: ..., traitCollection: ...)`"],
accessModifier: externalAccessLevel,
isStatic: true,
name: SwiftIdentifier(name: name),
name: structName,
generics: nil,
parameters: [
Function.Parameter(
Expand All @@ -155,35 +161,7 @@ extension NamespacedAssetSubfolder: ExternalOnlyStructGenerator {
],
doesThrow: false,
returnType: Type._UIImage.asOptional(),
body: "return UIKit.UIImage(resource: R.image.\(path).\(SwiftIdentifier(name: name)), compatibleWith: traitCollection)"
body: "return UIKit.UIImage(resource: \(qualifiedName), compatibleWith: traitCollection)"
)
}
}

struct AssetSubfolders {
let folders: [NamespacedAssetSubfolder]
let duplicates: [NamespacedAssetSubfolder]

init(all subfolders: [NamespacedAssetSubfolder], assetIdentifiers: [SwiftIdentifier]) {
var dict: [SwiftIdentifier: NamespacedAssetSubfolder] = [:]

for subfolder in subfolders {
let name = SwiftIdentifier(name: subfolder.name)
if let duplicate = dict[name] {
duplicate.subfolders += subfolder.subfolders
duplicate.imageAssets += subfolder.imageAssets
} else {
dict[name] = subfolder
}
}

self.folders = dict.values.filter { !assetIdentifiers.contains(SwiftIdentifier(name: $0.name)) }
self.duplicates = dict.values.filter { assetIdentifiers.contains(SwiftIdentifier(name: $0.name)) }
}

func printWarningsForDuplicates() {
for subfolder in duplicates {
warn("Skipping asset subfolder because symbol '\(subfolder.name)' would conflict with image: \(subfolder.name)")
}
}
}

0 comments on commit 8f38351

Please sign in to comment.