Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions R.swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@
D5B799771C199755009EA901 /* StoryboardGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B799671C1993B7009EA901 /* StoryboardGenerator.swift */; };
D5B799791C19C082009EA901 /* WhiteListedExtensionsResourceType.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B799781C19C082009EA901 /* WhiteListedExtensionsResourceType.swift */; };
D5B7997A1C19C1BD009EA901 /* WhiteListedExtensionsResourceType.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B799781C19C082009EA901 /* WhiteListedExtensionsResourceType.swift */; };
D5B7997D1C1B07C3009EA901 /* SanitizedSwiftName.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B7997C1C1B07C3009EA901 /* SanitizedSwiftName.swift */; };
D5B7997D1C1B07C3009EA901 /* SwiftIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B7997C1C1B07C3009EA901 /* SwiftIdentifier.swift */; };
D5B7997F1C1B07EB009EA901 /* ErrorOutput.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B7997E1C1B07EB009EA901 /* ErrorOutput.swift */; };
D5B799801C1B0943009EA901 /* SanitizedSwiftName.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B7997C1C1B07C3009EA901 /* SanitizedSwiftName.swift */; };
D5B799801C1B0943009EA901 /* SwiftIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B7997C1C1B07C3009EA901 /* SwiftIdentifier.swift */; };
D5B799811C1B0943009EA901 /* ErrorOutput.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B7997E1C1B07EB009EA901 /* ErrorOutput.swift */; };
D5B799831C1B8C78009EA901 /* Module.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B799821C1B8C78009EA901 /* Module.swift */; };
D5B7998A1C1B91A9009EA901 /* Module.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B799821C1B8C78009EA901 /* Module.swift */; };
Expand Down Expand Up @@ -139,7 +139,7 @@
D5B7996D1C19940F009EA901 /* ResourceFileGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResourceFileGenerator.swift; sourceTree = "<group>"; };
D5B7996F1C199420009EA901 /* FontGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FontGenerator.swift; sourceTree = "<group>"; };
D5B799781C19C082009EA901 /* WhiteListedExtensionsResourceType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WhiteListedExtensionsResourceType.swift; sourceTree = "<group>"; };
D5B7997C1C1B07C3009EA901 /* SanitizedSwiftName.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SanitizedSwiftName.swift; sourceTree = "<group>"; };
D5B7997C1C1B07C3009EA901 /* SwiftIdentifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftIdentifier.swift; sourceTree = "<group>"; };
D5B7997E1C1B07EB009EA901 /* ErrorOutput.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorOutput.swift; sourceTree = "<group>"; };
D5B799821C1B8C78009EA901 /* Module.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Module.swift; sourceTree = "<group>"; };
D5C4227D1B711FDF004EA9B9 /* rswiftTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = rswiftTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -227,7 +227,7 @@
isa = PBXGroup;
children = (
D5B7997E1C1B07EB009EA901 /* ErrorOutput.swift */,
D5B7997C1C1B07C3009EA901 /* SanitizedSwiftName.swift */,
D5B7997C1C1B07C3009EA901 /* SwiftIdentifier.swift */,
D56F923E1C2942B400177FF7 /* Struct+ChildValidation.swift */,
D58672481C21FC9700A760EC /* TypeSequenceProvider.swift */,
D5EA0DFE1A3DF4E300FFEBC4 /* UtilExtensions.swift */,
Expand Down Expand Up @@ -482,7 +482,7 @@
D5B799731C199755009EA901 /* NibGenerator.swift in Sources */,
D5B129B31C3BA75A00A1C5FC /* Let.swift in Sources */,
D5F97E491C1819160066D7C0 /* Font.swift in Sources */,
D5B799801C1B0943009EA901 /* SanitizedSwiftName.swift in Sources */,
D5B799801C1B0943009EA901 /* SwiftIdentifier.swift in Sources */,
D5B799711C199755009EA901 /* FontGenerator.swift in Sources */,
D586D1C61BE20D8600F18FEC /* Extensions.swift in Sources */,
D586D1C81BE20D8600F18FEC /* Serialization.swift in Sources */,
Expand Down Expand Up @@ -539,7 +539,7 @@
D5646DE51BE2016E0034F4D7 /* PBXObject.swift in Sources */,
D56DC76D1C41758800623437 /* AccessModifier.swift in Sources */,
D5B129AF1C3BA5F900A1C5FC /* Let.swift in Sources */,
D5B7997D1C1B07C3009EA901 /* SanitizedSwiftName.swift in Sources */,
D5B7997D1C1B07C3009EA901 /* SwiftIdentifier.swift in Sources */,
D5A0A82A1C47920A0089ED2C /* SwiftCodeConverible.swift in Sources */,
D5B799681C1993B7009EA901 /* StoryboardGenerator.swift in Sources */,
D5B799701C199420009EA901 /* FontGenerator.swift in Sources */,
Expand Down
25 changes: 9 additions & 16 deletions R.swift/Generators/ColorGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,27 @@ struct ColorGenerator: Generator {
let internalStruct: Struct? = nil

init(colorPalettes palettes: [ColorPalette]) {
let groupedPalettes = palettes.groupBySwiftIdentifiers { $0.filename }
groupedPalettes.printWarningsForDuplicatesAndEmpties(source: "color palette", result: "file")

externalStruct = Struct(
comments: ["This `R.color` struct is generated, and contains static references to \(palettes.count) color palettes."],
type: Type(module: .Host, name: "color"),
implements: [],
typealiasses: [],
properties: [],
functions: [],
structs: palettes.flatMap(ColorGenerator.colorStructFromPalette)
structs: groupedPalettes.uniques.flatMap(ColorGenerator.colorStructFromPalette)
)
}

private static func colorStructFromPalette(palette: ColorPalette) -> Struct? {
if palette.colors.isEmpty { return nil }

let name = sanitizedSwiftName(palette.filename)
let groupedColors = palette.colors.groupBySwiftNames { $0.0 }

for (sanitizedName, duplicates) in groupedColors.duplicates {
warn("Skipping \(duplicates.count) colors in palette '\(palette.filename)' because symbol '\(sanitizedName)' would be generated for all of these colors: \(duplicates.joinWithSeparator(", "))")
}
let name = SwiftIdentifier(name: palette.filename)
let groupedColors = palette.colors.groupBySwiftIdentifiers { $0.0 }

let empties = groupedColors.empties
if let empty = empties.first where empties.count == 1 {
warn("Skipping 1 color in palette '\(palette.filename)' because no swift identifier can be generated for image: \(empty)")
}
else if empties.count > 1 {
warn("Skipping \(empties.count) images in palette '\(palette.filename)' because no swift identifier can be generated for all of these images: \(empties.joinWithSeparator(", "))")
}
groupedColors.printWarningsForDuplicatesAndEmpties(source: "color", container: "in palette '\(palette.filename)'", result: "color")

return Struct(
comments: ["This `R.color.\(name)` struct is generated, and contains static references to \(groupedColors.uniques.count) colors."],
Expand All @@ -60,7 +53,7 @@ struct ColorGenerator: Generator {
"<span style='background-color: #\(color.hexString); color: #\(color.opposite.hexString); padding: 1px 3px;'>#\(color.hexString)</span> \(name)"
],
isStatic: true,
name: name,
name: SwiftIdentifier(name: name),
typeDefinition: .Inferred(Type.ColorResource),
value: "ColorResource(name: \"\(name)\", red: \(color.redComponent), green: \(color.greenComponent), blue: \(color.blueComponent), alpha: \(color.alphaComponent))"
)
Expand All @@ -74,7 +67,7 @@ struct ColorGenerator: Generator {
"UIColor(red: \(color.redComponent), green: \(color.greenComponent), blue: \(color.blueComponent), alpha: \(color.alphaComponent))"
],
isStatic: true,
name: name,
name: SwiftIdentifier(name: name),
generics: nil,
parameters: [
Function.Parameter(name: "_", type: Type._Void)
Expand Down
12 changes: 7 additions & 5 deletions R.swift/Generators/FontGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ struct FontGenerator: Generator {
let internalStruct: Struct? = nil

init(fonts: [Font]) {
let groupedFonts = fonts.groupBySwiftIdentifiers { $0.name }
groupedFonts.printWarningsForDuplicatesAndEmpties(source: "font resource", result: "file")

let fontProperties: [Property] = fonts.map {
let fontProperties: [Property] = groupedFonts.uniques.map {
Let(
comments: ["Font `\($0.name)`."],
isStatic: true,
name: $0.name,
name: SwiftIdentifier(name: $0.name),
typeDefinition: .Inferred(Type.FontResource),
value: "FontResource(fontName: \"\($0.name)\")"
)
Expand All @@ -30,7 +32,7 @@ struct FontGenerator: Generator {
implements: [],
typealiasses: [],
properties: fontProperties,
functions: fonts.map(FontGenerator.fontFunctionFromFont),
functions: groupedFonts.uniques.map(FontGenerator.fontFunctionFromFont),
structs: []
)
}
Expand All @@ -39,14 +41,14 @@ struct FontGenerator: Generator {
return Function(
comments: ["`UIFont(name: \"\(font.name)\", size: ...)`"],
isStatic: true,
name: font.name,
name: SwiftIdentifier(name: font.name),
generics: nil,
parameters: [
Function.Parameter(name: "size", localName: "size", type: Type._CGFloat)
],
doesThrow: false,
returnType: Type._UIFont.asOptional(),
body: "return UIFont(resource: \(sanitizedSwiftName(font.name)), size: size)"
body: "return UIFont(resource: \(SwiftIdentifier(name: font.name)), size: size)"
)
}
}
92 changes: 31 additions & 61 deletions R.swift/Generators/ImageGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,78 +13,28 @@ struct ImageGenerator: Generator {
let internalStruct: Struct? = nil

init(assetFolders: [AssetFolder], images: [Image]) {
let assetFolderImageFunctions = assetFolders
let assetFolderImageNames = assetFolders
.flatMap { $0.imageAssets }
.map {
Function(
comments: ["`UIImage(named: \"\($0)\", bundle: ..., traitCollection: ...)`"],
isStatic: true,
name: $0,
generics: nil,
parameters: [
Function.Parameter(
name: "compatibleWithTraitCollection",
localName: "traitCollection",
type: Type._UITraitCollection.asOptional(),
defaultValue: "nil"
)
],
doesThrow: false,
returnType: Type._UIImage.asOptional(),
body: "return UIImage(resource: R.image.\(sanitizedSwiftName($0)), compatibleWithTraitCollection: traitCollection)"
)
}

let uniqueImages = images
let imagesNames = images
.groupBy { $0.name }
.values
.flatMap { $0.first }

let imageFunctions = uniqueImages
.map {
Function(
comments: ["`UIImage(named: \"\($0.name)\", bundle: ..., traitCollection: ...)`"],
isStatic: true,
name: $0.name,
generics: nil,
parameters: [
Function.Parameter(
name: "compatibleWithTraitCollection",
localName: "traitCollection",
type: Type._UITraitCollection.asOptional(),
defaultValue: "nil"
)
],
doesThrow: false,
returnType: Type._UIImage.asOptional(),
body: "return \(Type._UIImage.name)(resource: R.image.\(sanitizedSwiftName($0.name)), compatibleWithTraitCollection: traitCollection)"
)
}

let allFunctions = assetFolderImageFunctions + imageFunctions
let groupedFunctions = allFunctions.groupBySwiftNames { $0.name }
.flatMap { $0.first?.name }

for (sanitizedName, duplicates) in groupedFunctions.duplicates {
warn("Skipping \(duplicates.count) images because symbol '\(sanitizedName)' would be generated for all of these images: \(duplicates.joinWithSeparator(", "))")
}
let allFunctions = assetFolderImageNames + imagesNames
let groupedFunctions = allFunctions.groupBySwiftIdentifiers { $0 }

let empties = groupedFunctions.empties
if let empty = empties.first where empties.count == 1 {
warn("Skipping 1 image because no swift identifier can be generated for image: \(empty)")
}
else if empties.count > 1 {
warn("Skipping \(empties.count) images because no swift identifier can be generated for all of these images: \(empties.joinWithSeparator(", "))")
}
groupedFunctions.printWarningsForDuplicatesAndEmpties(source: "image", result: "image")

let imageLets = groupedFunctions
.uniques
.map {
.map { name in
Let(
comments: ["Image `\($0.name)`."],
comments: ["Image `\(name)`."],
isStatic: true,
name: $0.name,
name: SwiftIdentifier(name: name),
typeDefinition: .Inferred(Type.ImageResource),
value: "\(Type.ImageResource.name)(bundle: _R.hostingBundle, name: \"\($0.name)\")"
value: "\(Type.ImageResource.name)(bundle: _R.hostingBundle, name: \"\(name)\")"
)
}

Expand All @@ -94,8 +44,28 @@ struct ImageGenerator: Generator {
implements: [],
typealiasses: [],
properties: imageLets.map(anyProperty),
functions: groupedFunctions.uniques,
functions: groupedFunctions.uniques.map(ImageGenerator.functionForImageName),
structs: []
)
}

static func functionForImageName(name: String) -> Function {
return Function(
comments: ["`UIImage(named: \"\(name)\", bundle: ..., traitCollection: ...)`"],
isStatic: true,
name: SwiftIdentifier(name: name),
generics: nil,
parameters: [
Function.Parameter(
name: "compatibleWithTraitCollection",
localName: "traitCollection",
type: Type._UITraitCollection.asOptional(),
defaultValue: "nil"
)
],
doesThrow: false,
returnType: Type._UIImage.asOptional(),
body: "return \(Type._UIImage.name)(resource: R.image.\(SwiftIdentifier(name: name)), compatibleWithTraitCollection: traitCollection)"
)
}
}
33 changes: 11 additions & 22 deletions R.swift/Generators/NibGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,8 @@ struct NibGenerator: Generator {
let internalStruct: Struct?

init(nibs: [Nib]) {
let groupedNibs = nibs.groupBySwiftNames { $0.name }

for (name, duplicates) in groupedNibs.duplicates {
warn("Skipping \(duplicates.count) xibs because symbol '\(name)' would be generated for all of these xibs: \(duplicates.joinWithSeparator(", "))")
}

let empties = groupedNibs.empties
if let empty = empties.first where empties.count == 1 {
warn("Skipping 1 xib because no swift identifier can be generated for xib: \(empty)")
}
else if empties.count > 1 {
warn("Skipping \(empties.count) xibs because no swift identifier can be generated for all of these xibs: \(empties.joinWithSeparator(", "))")
}
let groupedNibs = nibs.groupBySwiftIdentifiers { $0.name }
groupedNibs.printWarningsForDuplicatesAndEmpties(source: "xib", result: "file")

internalStruct = Struct(
type: Type(module: .Host, name: "nib"),
Expand Down Expand Up @@ -83,24 +72,24 @@ struct NibGenerator: Generator {
return Function(
comments: ["`UINib(name: \"\(nib.name)\", bundle: ...)`"],
isStatic: true,
name: nib.name,
name: SwiftIdentifier(name: nib.name),
generics: nil,
parameters: [
Function.Parameter(name: "_", type: Type._Void)
],
doesThrow: false,
returnType: Type._UINib,
body: "return UINib(resource: R.nib.\(sanitizedSwiftName(nib.name)))"
body: "return UINib(resource: R.nib.\(SwiftIdentifier(name: nib.name)))"
)
}

private static func nibVarForNib(nib: Nib) -> Let {
let nibStructName = sanitizedSwiftName("_\(nib.name)")
let structType = Type(module: .Host, name: "_R.nib.\(nibStructName)")
let nibStructName = SwiftIdentifier(name: "_\(nib.name)")
let structType = Type(module: .Host, name: SwiftIdentifier(rawValue: "_R.nib.\(nibStructName)"))
return Let(
comments: ["Nib `\(nib.name)`."],
isStatic: true,
name: nib.name,
name: SwiftIdentifier(name: nib.name),
typeDefinition: .Inferred(structType),
value: "\(structType)()"
)
Expand All @@ -110,7 +99,7 @@ struct NibGenerator: Generator {

let instantiateParameters = [
Function.Parameter(name: "owner", localName: "ownerOrNil", type: Type._AnyObject.asOptional()),
Function.Parameter(name: "options", localName: "optionsOrNil", type: Type(module: .StdLib, name: "[NSObject : AnyObject]", optional: true), defaultValue: "nil")
Function.Parameter(name: "options", localName: "optionsOrNil", type: Type(module: .StdLib, name: SwiftIdentifier(rawValue: "[NSObject : AnyObject]"), optional: true), defaultValue: "nil")
]

let bundleLet = Let(
Expand All @@ -134,7 +123,7 @@ struct NibGenerator: Generator {
let viewTypeString = viewInfo.view.description
return Function(
isStatic: false,
name: "\(viewInfo.ordinal.word)View",
name: SwiftIdentifier(name: "\(viewInfo.ordinal.word)View"),
generics: nil,
parameters: instantiateParameters,
doesThrow: false,
Expand All @@ -161,9 +150,9 @@ struct NibGenerator: Generator {
reuseProtocols = []
}

let sanitizedName = sanitizedSwiftName(nib.name, lowercaseFirstCharacter: false)
let sanitizedName = SwiftIdentifier(name: nib.name, lowercaseFirstCharacter: false)
return Struct(
type: Type(module: .Host, name: "_\(sanitizedName)"),
type: Type(module: .Host, name: SwiftIdentifier(name: "_\(sanitizedName)")),
implements: ([Type.NibResourceType] + reuseProtocols).map(TypePrinter.init),
typealiasses: reuseTypealiasses,
properties: [bundleLet, nameVar] + reuseIdentifierProperties,
Expand Down
21 changes: 5 additions & 16 deletions R.swift/Generators/ResourceFileGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,8 @@ struct ResourceFileGenerator: Generator {
let internalStruct: Struct? = nil

init(resourceFiles: [ResourceFile]) {
let groupedResourceFiles = resourceFiles.groupBySwiftNames { $0.fullname }

for (name, duplicates) in groupedResourceFiles.duplicates {
warn("Skipping \(duplicates.count) resource files because symbol '\(name)' would be generated for all of these files: \(duplicates.joinWithSeparator(", "))")
}

let empties = groupedResourceFiles.empties
if let empty = empties.first where empties.count == 1 {
warn("Skipping 1 resource file because no swift identifier can be generated for file: \(empty)")
}
else if empties.count > 1 {
warn("Skipping \(empties.count) resource files because no swift identifier can be generated for all of these files: \(empties.joinWithSeparator(", "))")
}
let groupedResourceFiles = resourceFiles.groupBySwiftIdentifiers { $0.fullname }
groupedResourceFiles.printWarningsForDuplicatesAndEmpties(source: "resource file", result: "file")

let resourceFileProperties: [Property] = groupedResourceFiles
.uniques
Expand All @@ -34,7 +23,7 @@ struct ResourceFileGenerator: Generator {
return Let(
comments: ["Resource file `\($0.fullname)`."],
isStatic: true,
name: $0.fullname,
name: SwiftIdentifier(name: $0.fullname),
typeDefinition: .Inferred(Type.FileResource),
value: "FileResource(bundle: _R.hostingBundle, name: \"\($0.filename)\", pathExtension: \(pathExtensionOrNilString))"
)
Expand All @@ -50,14 +39,14 @@ struct ResourceFileGenerator: Generator {
Function(
comments: ["`bundle.URLForResource(\"\(filename)\", withExtension: \(pathExtension))`"],
isStatic: true,
name: fullname,
name: SwiftIdentifier(name: fullname),
generics: nil,
parameters: [
Function.Parameter(name: "_", type: Type._Void)
],
doesThrow: false,
returnType: Type._NSURL.asOptional(),
body: "let fileResource = R.file.\(sanitizedSwiftName(fullname))\nreturn fileResource.bundle.URLForResource(fileResource)"
body: "let fileResource = R.file.\(SwiftIdentifier(name: fullname))\nreturn fileResource.bundle.URLForResource(fileResource)"
)
]
}
Expand Down
Loading