Skip to content

Commit

Permalink
Merge pull request mac-cain13#113 from mac-cain13/feature/load-from-c…
Browse files Browse the repository at this point in the history
…orrect-bundle

Feature/load from correct bundle
  • Loading branch information
mac-cain13 committed Nov 22, 2015
2 parents 4207541 + ba957cf commit 4900558
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
10 changes: 5 additions & 5 deletions R.swift/func.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func readResourceFile(fileURL: NSURL) -> String? {
func imageStructFromAssetFolders(assetFolders: [AssetFolder], andImages images: [Image]) -> Struct {
let assetFolderImageVars = assetFolders
.flatMap { $0.imageAssets }
.map { Var(isStatic: true, name: $0, type: Type._UIImage.asOptional(), getter: "return UIImage(named: \"\($0)\")") }
.map { Var(isStatic: true, name: $0, type: Type._UIImage.asOptional(), getter: "return UIImage(named: \"\($0)\", inBundle: _R.hostingBundle, compatibleWithTraitCollection: nil)") }

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

let imageVars = uniqueImages
.map { Var(isStatic: true, name: $0.name, type: Type._UIImage.asOptional(), getter: "return UIImage(named: \"\($0.name)\")") }
.map { Var(isStatic: true, name: $0.name, type: Type._UIImage.asOptional(), getter: "return UIImage(named: \"\($0.name)\", inBundle: _R.hostingBundle, compatibleWithTraitCollection: nil)") }

let vars = (assetFolderImageVars + imageVars)
.groupUniquesAndDuplicates { $0.callName }
Expand Down Expand Up @@ -134,7 +134,7 @@ func storyboardStructAndFunctionFromStoryboards(storyboards: [Storyboard]) -> (S
}

func storyboardStructForStoryboard(storyboard: Storyboard) -> Struct {
let instanceVars = [Var(isStatic: true, name: "instance", type: Type._UIStoryboard, getter: "return UIStoryboard(name: \"\(storyboard.name)\", bundle: nil)")]
let instanceVars = [Var(isStatic: true, name: "instance", type: Type._UIStoryboard, getter: "return UIStoryboard(name: \"\(storyboard.name)\", bundle: _R.hostingBundle)")]

let initialViewControllerVar = [storyboard.initialViewController
.map { (vc) -> Var in
Expand Down Expand Up @@ -216,7 +216,7 @@ func nibStructForNib(nib: Nib) -> Struct {
isStatic: false,
name: "instance",
type: Type._UINib,
getter: "return UINib.init(nibName: \"\(nib.name)\", bundle: nil)"
getter: "return UINib.init(nibName: \"\(nib.name)\", bundle: _R.hostingBundle)"
)

let instantiateFunc = Function(
Expand Down Expand Up @@ -332,5 +332,5 @@ func resourceStructFromResourceFiles(resourceFiles: [ResourceFile]) -> Struct {

func varFromResourceFile(resourceFile: ResourceFile) -> Var {
let pathExtensionOrNilString = resourceFile.pathExtension ?? "nil"
return Var(isStatic: true, name: resourceFile.fullname, type: Type._NSURL.asOptional(), getter: "return NSBundle.mainBundle().URLForResource(\"\(resourceFile.filename)\", withExtension: \"\(pathExtensionOrNilString)\")")
return Var(isStatic: true, name: resourceFile.fullname, type: Type._NSURL.asOptional(), getter: "return _R.hostingBundle?.URLForResource(\"\(resourceFile.filename)\", withExtension: \"\(pathExtensionOrNilString)\")")
}
8 changes: 8 additions & 0 deletions R.swift/input.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ private let targetOption = Option(
numberOfParameters: 1,
helpDescription: "Target the R-file should be generated for, if non given R.swift will use the environment variable TARGET_NAME."
)
private let bundleIdentifierOption = Option(
trigger: .Long("bundleIdentifier"),
numberOfParameters: 1,
helpDescription: "Bundle identifier the R-file should be generated for, if non given R.swift will use the environment variable PRODUCT_BUNDLE_IDENTIFIER."
)
private let buildProductsDirOption = Option(
trigger: .Long("buildProductsDir"),
numberOfParameters: 1,
Expand Down Expand Up @@ -60,6 +65,7 @@ struct CallInformation {

let xcodeprojURL: NSURL
let targetName: String
let bundleIdentifier: String

private let buildProductsDirURL: NSURL
private let developerDirURL: NSURL
Expand Down Expand Up @@ -103,6 +109,8 @@ struct CallInformation {

targetName = try getFirstArgumentForOption(targetOption, defaultValue: environment["TARGET_NAME"])

bundleIdentifier = try getFirstArgumentForOption(bundleIdentifierOption, defaultValue: environment["PRODUCT_BUNDLE_IDENTIFIER"])

let buildProductsDirPath = try getFirstArgumentForOption(buildProductsDirOption, defaultValue: environment["BUILT_PRODUCTS_DIR"])
buildProductsDirURL = NSURL(fileURLWithPath: buildProductsDirPath)

Expand Down
2 changes: 1 addition & 1 deletion R.swift/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ do {

let resources = Resources(resourceURLs: resourceURLs, fileManager: NSFileManager.defaultManager())

let (internalStruct, externalStruct) = generateResourceStructsWithResources(resources)
let (internalStruct, externalStruct) = generateResourceStructsWithResources(resources, bundleIdentifier: callInformation.bundleIdentifier)

let fileContents = [
Header,
Expand Down
6 changes: 4 additions & 2 deletions R.swift/processing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct Resources {
}
}

func generateResourceStructsWithResources(resources: Resources) -> (Struct, Struct) {
func generateResourceStructsWithResources(resources: Resources, bundleIdentifier: String) -> (Struct, Struct) {
// Generate resource file contents
let storyboardStructAndFunction = storyboardStructAndFunctionFromStoryboards(resources.storyboards)

Expand Down Expand Up @@ -72,7 +72,9 @@ func generateResourceStructsWithResources(resources: Resources) -> (Struct, Stru
type: Type(name: "_R"),
implements: [],
lets: [],
vars: [],
vars: [
Var(isStatic: true, name: "hostingBundle", type: Type._NSBundle.asOptional(), getter: "return NSBundle(identifier: \"\(bundleIdentifier)\")")
],
functions: [],
structs: [
nibStructs.intern
Expand Down
1 change: 1 addition & 0 deletions R.swift/types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct Type: CustomStringConvertible, Equatable, Hashable {
static let _UINib = Type(name: "UINib")
static let _UIView = Type(name: "UIView")
static let _UIImage = Type(name: "UIImage")
static let _NSBundle = Type(name: "NSBundle")
static let _NSIndexPath = Type(name: "NSIndexPath")
static let _UITableView = Type(name: "UITableView")
static let _UITableViewCell = Type(name: "UITableViewCell")
Expand Down
2 changes: 1 addition & 1 deletion R.swift/values.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ let NibUIViewControllerExtension = Extension(
parameters: [
Function.Parameter(name: "nib", type: Type(name: "NibResource"))
],
body: "self.init(nibName: nib.name, bundle: nil)"
body: "self.init(nibName: nib.name, bundle: _R.hostingBundle)"
) as Func
]
)
Expand Down

0 comments on commit 4900558

Please sign in to comment.