Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SwiftPM resources via Swift 5.3 #138

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ Pods

Packages
.build
.swiftpm
25 changes: 25 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"object": {
"pins": [
{
"package": "Nimble",
"repositoryURL": "https://github.com/Quick/Nimble.git",
"state": {
"branch": null,
"revision": "2b1809051b4a65c1d7f5233331daa24572cd7fca",
"version": "8.1.1"
}
},
{
"package": "Quick",
"repositoryURL": "https://github.com/Quick/Quick.git",
"state": {
"branch": null,
"revision": "09b3becb37cb2163919a3842a4c5fa6ec7130792",
"version": "2.2.1"
}
}
]
},
"version": 1
}
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.0
// swift-tools-version:5.3

import PackageDescription

Expand All @@ -14,7 +14,7 @@ let package = Package(

],
targets: [
.target(name: "Fakery"),
.target(name: "Fakery", resources: [.copy("Resources/Locales")]),
.testTarget(name: "FakeryTests", dependencies: ["Fakery","Quick", "Nimble"], path: "Tests/Fakery")
]
)
4 changes: 1 addition & 3 deletions Sources/Fakery/Config.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
public struct Config {
public static let defaultLocale: String = "en"
public static let pathExtension: String = "json"
public static var dirPath: String = "Resources/Locales"
public static var dirFrameworkPath: String = ""
public static var dirResourcePath: String = ""
public static var localesDirectory = "Locales"
}
29 changes: 5 additions & 24 deletions Sources/Fakery/Data/Provider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,11 @@ public final class Provider {
if let translationData = translations[locale] {
translation = translationData
} else {
let bundle = Bundle(for: Provider.self)

var path = bundle.path(forResource: locale,
ofType: Config.pathExtension,
inDirectory: Config.dirPath) ??
bundle.path(forResource: locale,
ofType: Config.pathExtension,
inDirectory: Config.dirFrameworkPath)

if !Config.dirResourcePath.isEmpty {
path = "\(Config.dirResourcePath)/\(locale).\(Config.pathExtension)"
}

if let resourcePath = Bundle(for: Provider.self).resourcePath {
let bundlePath = resourcePath + "/Faker.bundle"

if let bundle = Bundle(path: bundlePath) {
path = bundle.path(forResource: locale, ofType: Config.pathExtension)
}
}

if let path = path {
let fileURL = URL(fileURLWithPath: path)

if
let fileURL = Bundle.module.resourceURL?.appendingPathComponent(Config.localesDirectory)
.appendingPathComponent(locale)
.appendingPathExtension(Config.pathExtension)
{
if let data = try? Data(contentsOf: fileURL) {
translation = data
translations[locale] = data
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion Tests/Fakery/ConfigSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ final class ConfigSpec: QuickSpec {
describe("Config") {
it("has default values") {
expect(Config.defaultLocale).to(equal("en"))
expect(Config.dirPath).to(equal("Resources/Locales"))
expect(Config.pathExtension).to(equal("json"))
expect(Config.localesDirectory).to(equal("Locales"))
}
}
}
Expand Down