Created
July 26, 2019 15:59
-
-
Save ricobeck/de70a04a94e90bb3b602d0740797d08f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// # Structure | |
// - SwiftPackage | |
// - Sources | |
// - Tests | |
// - Resources | |
// - test.txt | |
// - SwiftPackageTests | |
// - SwiftPackageTests.swift | |
// | |
// # Usage in SwiftPackageTests.swift | |
// let file = Resource(name: "test", type: "txt") | |
// let content = String(bytes: file.content!, encoding: .utf8) | |
// XCTAssertEqual("Hello World!\n", content) | |
struct Resource { | |
let name: String | |
let type: String | |
var path: String { | |
let components = (#file as NSString).pathComponents | |
let resourcePath = ["Resources", filename] | |
let finalComponents = components.prefix(components.count - 2) + resourcePath | |
return finalComponents.joined(separator: "/") | |
} | |
var url: URL { URL(fileURLWithPath: path) } | |
var filename: String { "\(name).\(type)" } | |
var content: Data? { try? Data(contentsOf: url) } | |
init(name: String, type: String) { | |
self.name = name | |
self.type = type | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment