Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Add explicit bundle parameter to support frameworks #17

Merged
merged 10 commits into from
Feb 15, 2017
22 changes: 22 additions & 0 deletions Tests/Expected/Fonts/default-context-customname.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,32 @@ extension FontConvertible where Self: RawRepresentable, Self.RawValue == String
func font(size: CGFloat) -> Font! {
return Font(font: self, size: size)
}

func register() {
let extensions = ["otf", "ttf"]
let bundle = NSBundle(forClass: BundleToken.self)

guard let url = extensions.flatMap({ bundle.URLForResource(rawValue, withExtension: $0) }).first else { return }

var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .None, &errorRef)
}
}

extension Font {
convenience init!<FontType: FontConvertible
where FontType: RawRepresentable, FontType.RawValue == String>
(font: FontType, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNamesForFamilyName(font.rawValue).isEmpty {
font.register()
}
#elseif os(OSX)
if NSFontManager.sharedFontManager().availableMembersOfFontFamily(font.rawValue) == nil {
font.register()
}
#endif

self.init(name: font.rawValue, size: size)
}
}
Expand Down Expand Up @@ -62,3 +82,5 @@ enum CustomFamily {
case Internal = "private"
}
}

private final class BundleToken {}
22 changes: 22 additions & 0 deletions Tests/Expected/Fonts/default-context-defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,32 @@ extension FontConvertible where Self: RawRepresentable, Self.RawValue == String
func font(size: CGFloat) -> Font! {
return Font(font: self, size: size)
}

func register() {
let extensions = ["otf", "ttf"]
let bundle = NSBundle(forClass: BundleToken.self)

guard let url = extensions.flatMap({ bundle.URLForResource(rawValue, withExtension: $0) }).first else { return }

var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .None, &errorRef)
}
}

extension Font {
convenience init!<FontType: FontConvertible
where FontType: RawRepresentable, FontType.RawValue == String>
(font: FontType, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNamesForFamilyName(font.rawValue).isEmpty {
font.register()
}
#elseif os(OSX)
if NSFontManager.sharedFontManager().availableMembersOfFontFamily(font.rawValue) == nil {
font.register()
}
#endif

self.init(name: font.rawValue, size: size)
}
}
Expand Down Expand Up @@ -62,3 +82,5 @@ enum FontFamily {
case Internal = "private"
}
}

private final class BundleToken {}
22 changes: 22 additions & 0 deletions Tests/Expected/Fonts/swift3-context-customname.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,32 @@ extension FontConvertible where Self: RawRepresentable, Self.RawValue == String
func font(size: CGFloat) -> Font! {
return Font(font: self, size: size)
}

func register() {
let extensions = ["otf", "ttf"]
let bundle = Bundle(for: BundleToken.self)

guard let url = extensions.flatMap({ bundle.url(forResource: rawValue, withExtension: $0) }).first else { return }

var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .none, &errorRef)
}
}

extension Font {
convenience init!<FontType: FontConvertible>
(font: FontType, size: CGFloat)
where FontType: RawRepresentable, FontType.RawValue == String {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNames(forFamilyName: font.rawValue).isEmpty {
font.register()
}
#elseif os(OSX)
if NSFontManager.shared().availableMembers(ofFontFamily: font.rawValue) == nil {
font.register()
}
#endif

self.init(name: font.rawValue, size: size)
}
}
Expand Down Expand Up @@ -62,3 +82,5 @@ enum CustomFamily {
case `internal` = "private"
}
}

private final class BundleToken {}
22 changes: 22 additions & 0 deletions Tests/Expected/Fonts/swift3-context-defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,32 @@ extension FontConvertible where Self: RawRepresentable, Self.RawValue == String
func font(size: CGFloat) -> Font! {
return Font(font: self, size: size)
}

func register() {
let extensions = ["otf", "ttf"]
let bundle = Bundle(for: BundleToken.self)

guard let url = extensions.flatMap({ bundle.url(forResource: rawValue, withExtension: $0) }).first else { return }

var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .none, &errorRef)
}
}

extension Font {
convenience init!<FontType: FontConvertible>
(font: FontType, size: CGFloat)
where FontType: RawRepresentable, FontType.RawValue == String {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNames(forFamilyName: font.rawValue).isEmpty {
font.register()
}
#elseif os(OSX)
if NSFontManager.shared().availableMembers(ofFontFamily: font.rawValue) == nil {
font.register()
}
#endif

self.init(name: font.rawValue, size: size)
}
}
Expand Down Expand Up @@ -62,3 +82,5 @@ enum FontFamily {
case `internal` = "private"
}
}

private final class BundleToken {}
16 changes: 15 additions & 1 deletion Tests/Expected/Images/allvalues-context-customname.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,27 @@ enum XCTImages: String {
]

var image: Image {
return Image(asset: self)
let bundle = NSBundle(forClass: BundleToken.self)
#if os(iOS) || os(tvOS) || os(watchOS)
let image = Image(named: rawValue, inBundle: bundle, compatibleWithTraitCollection: nil)
#elseif os(OSX)
let image = bundle.imageForResource(rawValue)
#endif
guard let result = image else { fatalError("Unable to load image \(rawValue).") }
return result
}
}
// swiftlint:enable type_body_length

extension Image {
convenience init!(asset: XCTImages) {
#if os(iOS) || os(tvOS) || os(watchOS)
let bundle = NSBundle(forClass: BundleToken.self)
self.init(named: asset.rawValue, inBundle: bundle, compatibleWithTraitCollection: nil)
#elseif os(OSX)
self.init(named: asset.rawValue)
#endif
}
}

private final class BundleToken {}
16 changes: 15 additions & 1 deletion Tests/Expected/Images/allvalues-context-defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,27 @@ enum Asset: String {
]

var image: Image {
return Image(asset: self)
let bundle = NSBundle(forClass: BundleToken.self)
#if os(iOS) || os(tvOS) || os(watchOS)
let image = Image(named: rawValue, inBundle: bundle, compatibleWithTraitCollection: nil)
#elseif os(OSX)
let image = bundle.imageForResource(rawValue)
#endif
guard let result = image else { fatalError("Unable to load image \(rawValue).") }
return result
}
}
// swiftlint:enable type_body_length

extension Image {
convenience init!(asset: Asset) {
#if os(iOS) || os(tvOS) || os(watchOS)
let bundle = NSBundle(forClass: BundleToken.self)
self.init(named: asset.rawValue, inBundle: bundle, compatibleWithTraitCollection: nil)
#elseif os(OSX)
self.init(named: asset.rawValue)
#endif
}
}

private final class BundleToken {}
16 changes: 15 additions & 1 deletion Tests/Expected/Images/default-context-customname.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,27 @@ enum XCTImages: String {
case Round_Tomato = "Round/Tomato"

var image: Image {
return Image(asset: self)
let bundle = NSBundle(forClass: BundleToken.self)
#if os(iOS) || os(tvOS) || os(watchOS)
let image = Image(named: rawValue, inBundle: bundle, compatibleWithTraitCollection: nil)
#elseif os(OSX)
let image = bundle.imageForResource(rawValue)
#endif
guard let result = image else { fatalError("Unable to load image \(rawValue).") }
return result
}
}
// swiftlint:enable type_body_length

extension Image {
convenience init!(asset: XCTImages) {
#if os(iOS) || os(tvOS) || os(watchOS)
let bundle = NSBundle(forClass: BundleToken.self)
self.init(named: asset.rawValue, inBundle: bundle, compatibleWithTraitCollection: nil)
#elseif os(OSX)
self.init(named: asset.rawValue)
#endif
}
}

private final class BundleToken {}
16 changes: 15 additions & 1 deletion Tests/Expected/Images/default-context-defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,27 @@ enum Asset: String {
case Round_Tomato = "Round/Tomato"

var image: Image {
return Image(asset: self)
let bundle = NSBundle(forClass: BundleToken.self)
#if os(iOS) || os(tvOS) || os(watchOS)
let image = Image(named: rawValue, inBundle: bundle, compatibleWithTraitCollection: nil)
#elseif os(OSX)
let image = bundle.imageForResource(rawValue)
#endif
guard let result = image else { fatalError("Unable to load image \(rawValue).") }
return result
}
}
// swiftlint:enable type_body_length

extension Image {
convenience init!(asset: Asset) {
#if os(iOS) || os(tvOS) || os(watchOS)
let bundle = NSBundle(forClass: BundleToken.self)
self.init(named: asset.rawValue, inBundle: bundle, compatibleWithTraitCollection: nil)
#elseif os(OSX)
self.init(named: asset.rawValue)
#endif
}
}

private final class BundleToken {}
18 changes: 16 additions & 2 deletions Tests/Expected/Images/dot-syntax-context-customname.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@
struct XCTImagesType: StringLiteralConvertible {
private var value: String

var image: UIImage {
return UIImage(asset: self)
var image: Image {
let bundle = NSBundle(forClass: BundleToken.self)
#if os(iOS) || os(tvOS) || os(watchOS)
let image = Image(named: value, inBundle: bundle, compatibleWithTraitCollection: nil)
#elseif os(OSX)
let image = bundle.imageForResource(value)
#endif
guard let result = image else { fatalError("Unable to load image \(value).") }
return result
}

init(stringLiteral value: String) {
Expand Down Expand Up @@ -55,6 +62,13 @@ enum XCTImages {

extension Image {
convenience init!(asset: XCTImagesType) {
#if os(iOS) || os(tvOS) || os(watchOS)
let bundle = NSBundle(forClass: BundleToken.self)
self.init(named: asset.value, inBundle: bundle, compatibleWithTraitCollection: nil)
#elseif os(OSX)
self.init(named: asset.value)
#endif
}
}

private final class BundleToken {}
18 changes: 16 additions & 2 deletions Tests/Expected/Images/dot-syntax-context-defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@
struct AssetType: StringLiteralConvertible {
private var value: String

var image: UIImage {
return UIImage(asset: self)
var image: Image {
let bundle = NSBundle(forClass: BundleToken.self)
#if os(iOS) || os(tvOS) || os(watchOS)
let image = Image(named: value, inBundle: bundle, compatibleWithTraitCollection: nil)
#elseif os(OSX)
let image = bundle.imageForResource(value)
#endif
guard let result = image else { fatalError("Unable to load image \(value).") }
return result
}

init(stringLiteral value: String) {
Expand Down Expand Up @@ -55,6 +62,13 @@ enum Asset {

extension Image {
convenience init!(asset: AssetType) {
#if os(iOS) || os(tvOS) || os(watchOS)
let bundle = NSBundle(forClass: BundleToken.self)
self.init(named: asset.value, inBundle: bundle, compatibleWithTraitCollection: nil)
#elseif os(OSX)
self.init(named: asset.value)
#endif
}
}

private final class BundleToken {}
18 changes: 16 additions & 2 deletions Tests/Expected/Images/dot-syntax-swift3-context-customname.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@
struct XCTImagesType: ExpressibleByStringLiteral {
fileprivate var value: String

var image: UIImage {
return UIImage(asset: self)
var image: Image {
let bundle = Bundle(for: BundleToken.self)
#if os(iOS) || os(tvOS) || os(watchOS)
let image = Image(named: value, in: bundle, compatibleWith: nil)
#elseif os(OSX)
let image = bundle.image(forResource: value)
#endif
guard let result = image else { fatalError("Unable to load image \(value).") }
return result
}

init(stringLiteral value: String) {
Expand Down Expand Up @@ -55,6 +62,13 @@ enum XCTImages {

extension Image {
convenience init!(asset: XCTImagesType) {
#if os(iOS) || os(tvOS) || os(watchOS)
let bundle = Bundle(for: BundleToken.self)
self.init(named: asset.value, in: bundle, compatibleWith: nil)
#elseif os(OSX)
self.init(named: asset.value)
#endif
}
}

private final class BundleToken {}
18 changes: 16 additions & 2 deletions Tests/Expected/Images/dot-syntax-swift3-context-defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@
struct AssetType: ExpressibleByStringLiteral {
fileprivate var value: String

var image: UIImage {
return UIImage(asset: self)
var image: Image {
let bundle = Bundle(for: BundleToken.self)
#if os(iOS) || os(tvOS) || os(watchOS)
let image = Image(named: value, in: bundle, compatibleWith: nil)
#elseif os(OSX)
let image = bundle.image(forResource: value)
#endif
guard let result = image else { fatalError("Unable to load image \(value).") }
return result
}

init(stringLiteral value: String) {
Expand Down Expand Up @@ -55,6 +62,13 @@ enum Asset {

extension Image {
convenience init!(asset: AssetType) {
#if os(iOS) || os(tvOS) || os(watchOS)
let bundle = Bundle(for: BundleToken.self)
self.init(named: asset.value, in: bundle, compatibleWith: nil)
#elseif os(OSX)
self.init(named: asset.value)
#endif
}
}

private final class BundleToken {}
Loading