Skip to content

Commit

Permalink
Load hostingbundle for class instead of identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mac-cain13 committed Dec 10, 2016
1 parent 8763760 commit 63b3a97
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 32 deletions.
6 changes: 6 additions & 0 deletions R.swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
D586D1C61BE20D8600F18FEC /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5646DE11BE2016E0034F4D7 /* Extensions.swift */; };
D586D1C71BE20D8600F18FEC /* PBXObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5646DE21BE2016E0034F4D7 /* PBXObject.swift */; };
D586D1C81BE20D8600F18FEC /* Serialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5646DE31BE2016E0034F4D7 /* Serialization.swift */; };
D58AFCE61DFC2A6400344CB7 /* Class.swift in Sources */ = {isa = PBXBuildFile; fileRef = D58AFCE51DFC2A6400344CB7 /* Class.swift */; };
D58AFCE71DFC2C6500344CB7 /* Class.swift in Sources */ = {isa = PBXBuildFile; fileRef = D58AFCE51DFC2A6400344CB7 /* Class.swift */; };
D59F72261C1963DA0089767C /* Type.swift in Sources */ = {isa = PBXBuildFile; fileRef = D59F72251C1963DA0089767C /* Type.swift */; };
D59F72271C1963DA0089767C /* Type.swift in Sources */ = {isa = PBXBuildFile; fileRef = D59F72251C1963DA0089767C /* Type.swift */; };
D59F72291C1963EA0089767C /* Struct.swift in Sources */ = {isa = PBXBuildFile; fileRef = D59F72281C1963EA0089767C /* Struct.swift */; };
Expand Down Expand Up @@ -133,6 +135,7 @@
D578CAD51CE064E900C50B7E /* version.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = version.swift; sourceTree = "<group>"; };
D579466A1B9347C20044D2FC /* XCProjectFile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCProjectFile.swift; sourceTree = "<group>"; };
D58672481C21FC9700A760EC /* TypeSequenceProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypeSequenceProvider.swift; sourceTree = "<group>"; };
D58AFCE51DFC2A6400344CB7 /* Class.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Class.swift; sourceTree = "<group>"; };
D59F72251C1963DA0089767C /* Type.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Type.swift; sourceTree = "<group>"; };
D59F72281C1963EA0089767C /* Struct.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Struct.swift; sourceTree = "<group>"; };
D59F722F1C19644F0089767C /* Function.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Function.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -345,6 +348,7 @@
D59F72251C1963DA0089767C /* Type.swift */,
D59F723B1C1964B20089767C /* Typealias.swift */,
D56F923C1C29421600177FF7 /* TypeVar.swift */,
D58AFCE51DFC2A6400344CB7 /* Class.swift */,
);
path = SwiftTypes;
sourceTree = "<group>";
Expand Down Expand Up @@ -463,6 +467,7 @@
D5B799771C199755009EA901 /* StoryboardGenerator.swift in Sources */,
D5B799811C1B0943009EA901 /* ErrorOutput.swift in Sources */,
D5F12D431BDACB87009A2C88 /* StructGenerator.swift in Sources */,
D58AFCE71DFC2C6500344CB7 /* Class.swift in Sources */,
E2762AC91CCCEE120009BCAA /* Locale.swift in Sources */,
D5F97E4A1C1819160066D7C0 /* Storyboard.swift in Sources */,
5D45C2191CA12913000B8DC9 /* StringsStructGenerator.swift in Sources */,
Expand Down Expand Up @@ -543,6 +548,7 @@
D59F723C1C1964B20089767C /* Typealias.swift in Sources */,
D5B799661C19939D009EA901 /* SegueGenerator.swift in Sources */,
D5DA249D1B9CB1BF00AAA43E /* StructGenerator.swift in Sources */,
D58AFCE61DFC2A6400344CB7 /* Class.swift in Sources */,
D5F97E3A1C1812AE0066D7C0 /* Font.swift in Sources */,
D5E890AB1DA630540059D5ED /* HeaderPrinter.swift in Sources */,
D5EA0DF81A3DF45600FFEBC4 /* main.swift in Sources */,
Expand Down
6 changes: 4 additions & 2 deletions R.swift/Generators/AggregatedStructGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class AggregatedStructGenerator: StructGenerator {
typealiasses: [],
properties: [],
functions: [],
structs: collectedResult.externalStructs
structs: collectedResult.externalStructs,
classes: []
)

let internalStruct = Struct(
Expand All @@ -40,7 +41,8 @@ class AggregatedStructGenerator: StructGenerator {
typealiasses: [],
properties: [],
functions: [],
structs: collectedResult.internalStructs
structs: collectedResult.internalStructs,
classes: []
)

return (externalStruct, internalStruct)
Expand Down
6 changes: 4 additions & 2 deletions R.swift/Generators/ColorStructGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ struct ColorStructGenerator: ExternalOnlyStructGenerator {
typealiasses: [],
properties: [],
functions: [],
structs: groupedPalettes.uniques.flatMap { colorStruct(from: $0, at: externalAccessLevel) }
structs: groupedPalettes.uniques.flatMap { colorStruct(from: $0, at: externalAccessLevel) },
classes: []
)
}

Expand All @@ -49,7 +50,8 @@ struct ColorStructGenerator: ExternalOnlyStructGenerator {
typealiasses: [],
properties: groupedColors.uniques.map { colorLet($0, color: $1, at: externalAccessLevel) },
functions: groupedColors.uniques.map { colorFunction($0, color: $1, at: externalAccessLevel) },
structs: []
structs: [],
classes: []
)
}

Expand Down
3 changes: 2 additions & 1 deletion R.swift/Generators/FontStructGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ struct FontStructGenerator: ExternalOnlyStructGenerator {
typealiasses: [],
properties: properties,
functions: functions,
structs: []
structs: [],
classes: []
)
}
}
3 changes: 2 additions & 1 deletion R.swift/Generators/ImageStructGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ struct ImageStructGenerator: ExternalOnlyStructGenerator {
typealiasses: [],
properties: imageLets,
functions: groupedFunctions.uniques.map { imageFunction(for: $0, at: externalAccessLevel) },
structs: []
structs: [],
classes: []
)
}

Expand Down
9 changes: 6 additions & 3 deletions R.swift/Generators/NibStructGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ struct NibStructGenerator: StructGenerator {
functions: [],
structs: groupedNibs
.uniques
.map { nibStruct(for: $0, at: externalAccessLevel) }
.map { nibStruct(for: $0, at: externalAccessLevel) },
classes: []
)

let nibProperties: [Let] = groupedNibs
Expand All @@ -71,7 +72,8 @@ struct NibStructGenerator: StructGenerator {
typealiasses: [],
properties: nibProperties,
functions: nibFunctions,
structs: []
structs: [],
classes: []
)

return (
Expand Down Expand Up @@ -205,7 +207,8 @@ struct NibStructGenerator: StructGenerator {
typealiasses: reuseTypealiasses,
properties: [bundleLet, nameVar] + reuseIdentifierProperties,
functions: viewFuncs + validateFunctions,
structs: []
structs: [],
classes: []
)
}
}
3 changes: 2 additions & 1 deletion R.swift/Generators/ResourceFileStructGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ struct ResourceFileStructGenerator: ExternalOnlyStructGenerator {
typealiasses: [],
properties: firstLocales.flatMap { propertiesFromResourceFiles(resourceFiles: $0.1, at: externalAccessLevel) },
functions: firstLocales.flatMap { functionsFromResourceFiles(resourceFiles: $0.1, at: externalAccessLevel) },
structs: []
structs: [],
classes: []
)
}

Expand Down
3 changes: 2 additions & 1 deletion R.swift/Generators/ReuseIdentifierGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ struct ReuseIdentifierStructGenerator: ExternalOnlyStructGenerator {
typealiasses: [],
properties: reuseIdentifierProperties,
functions: [],
structs: []
structs: [],
classes: []
)
}

Expand Down
6 changes: 4 additions & 2 deletions R.swift/Generators/SegueGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ struct SegueStructGenerator: ExternalOnlyStructGenerator {
typealiasses: [],
properties: [],
functions: [],
structs: structs
structs: structs,
classes: []
)
}

Expand Down Expand Up @@ -150,7 +151,8 @@ struct SegueStructGenerator: ExternalOnlyStructGenerator {
typealiasses: [],
properties: properties,
functions: functions,
structs: []
structs: [],
classes: []
)
}
}
Expand Down
9 changes: 6 additions & 3 deletions R.swift/Generators/StoryboardGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ struct StoryboardStructGenerator: StructGenerator {
typealiasses: [],
properties: storyboardTypes.map { $0.1 },
functions: storyboardTypes.map { $0.2 },
structs: []
structs: [],
classes: []
)

let internalStruct = Struct(
Expand All @@ -70,7 +71,8 @@ struct StoryboardStructGenerator: StructGenerator {
typealiasses: [],
properties: [],
functions: [],
structs: storyboardTypes.map { $0.0 }
structs: storyboardTypes.map { $0.0 },
classes: []
)

return (
Expand Down Expand Up @@ -181,7 +183,8 @@ struct StoryboardStructGenerator: StructGenerator {
typealiasses: typealiasses,
properties: properties,
functions: functions,
structs: []
structs: [],
classes: []
)
}
}
6 changes: 4 additions & 2 deletions R.swift/Generators/StringsStructGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ struct StringsStructGenerator: ExternalOnlyStructGenerator {
typealiasses: [],
properties: [],
functions: [],
structs: groupedLocalized.uniques.flatMap { stringStructFromLocalizableStrings(filename: $0.0, strings: $0.1, at: externalAccessLevel) }
structs: groupedLocalized.uniques.flatMap { stringStructFromLocalizableStrings(filename: $0.0, strings: $0.1, at: externalAccessLevel) },
classes: []
)
}

Expand All @@ -47,7 +48,8 @@ struct StringsStructGenerator: ExternalOnlyStructGenerator {
typealiasses: [],
properties: params.map { stringLet(values: $0, at: externalAccessLevel) },
functions: params.map { stringFunction(values: $0, at: externalAccessLevel) },
structs: []
structs: [],
classes: []
)
}

Expand Down
3 changes: 2 additions & 1 deletion R.swift/Generators/ValidatedStructGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class ValidatedStructGenerator: StructGenerator {
body: validationFunctionBody
)
],
structs: []
structs: [],
classes: []
)

var externalStruct = validationSubject.externalStruct
Expand Down
20 changes: 20 additions & 0 deletions R.swift/SwiftTypes/Class.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Class.swift
// R.swift
//
// Created by Mathijs Kadijk on 10-12-16.
// Copyright © 2016 Mathijs Kadijk. All rights reserved.
//

import Foundation

struct Class: SwiftCodeConverible {
let accessModifier: AccessLevel
let type: Type

var swiftCode: String {
let accessModifierString = (accessModifier == .Internal) ? "" : accessModifier.rawValue + " "

return "\(accessModifierString)class \(type) {}"
}
}
18 changes: 6 additions & 12 deletions R.swift/SwiftTypes/Struct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct Struct: UsedTypesProvider, SwiftCodeConverible {
var properties: [Let]
var functions: [Function]
var structs: [Struct]
var classes: [Class]

var usedTypes: [UsedType] {
return [
Expand All @@ -30,17 +31,6 @@ struct Struct: UsedTypesProvider, SwiftCodeConverible {
].flatten()
}

init(comments: [String], accessModifier: AccessLevel, type: Type, implements: [TypePrinter], typealiasses: [Typealias], properties: [Let], functions: [Function], structs: [Struct]) {
self.comments = comments
self.accessModifier = accessModifier
self.type = type
self.implements = implements
self.typealiasses = typealiasses
self.properties = properties
self.functions = functions
self.structs = structs
}

var swiftCode: String {
let commentsString = comments.map { "/// \($0)\n" }.joined(separator: "")
let accessModifierString = (accessModifier == .Internal) ? "" : accessModifier.rawValue + " "
Expand All @@ -65,11 +55,15 @@ struct Struct: UsedTypesProvider, SwiftCodeConverible {
.sorted()
.joined(separator: "\n\n")

let classesString = classes
.map { $0.swiftCode }
.sorted()
.joined(separator: "\n\n")

// File private `init`, so that struct can't be initialized from the outside world
let fileprivateInit = "fileprivate init() {}"

let bodyComponents = [typealiasString, varsString, functionsString, structsString, fileprivateInit].filter { $0 != "" }
let bodyComponents = [typealiasString, varsString, functionsString, structsString, classesString, fileprivateInit].filter { $0 != "" }
let bodyString = bodyComponents.joined(separator: "\n\n").indentWithString(IndentationString)

return "\(commentsString)\(accessModifierString)struct \(type)\(implementsString) {\n\(bodyString)\n}"
Expand Down
7 changes: 6 additions & 1 deletion R.swift/Util/Struct+InternalProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension Struct {
isStatic: true,
name: "hostingBundle",
typeDefinition: .inferred(Type._Bundle),
value: "Bundle(identifier: \"\(bundleIdentifier)\") ?? Bundle.main"),
value: "Bundle(for: R.Class.self)"),
Let(
comments: [],
accessModifier: .FilePrivate,
Expand All @@ -28,8 +28,13 @@ extension Struct {
value: "hostingBundle.preferredLocalizations.first.flatMap(Locale.init) ?? Locale.current")
]

let internalClasses = [
Class(accessModifier: .FilePrivate, type: Type(module: .host, name: "Class"))
]

var externalStruct = self
externalStruct.properties.append(contentsOf: internalProperties)
externalStruct.classes.append(contentsOf: internalClasses)

return externalStruct
}
Expand Down

0 comments on commit 63b3a97

Please sign in to comment.