Skip to content

Instantly share code, notes, and snippets.

@ricobeck
Created July 26, 2019 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricobeck/de70a04a94e90bb3b602d0740797d08f to your computer and use it in GitHub Desktop.
Save ricobeck/de70a04a94e90bb3b602d0740797d08f to your computer and use it in GitHub Desktop.
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