Skip to content

Commit 5b06b81

Browse files
authored
Use safe resource bundle lookup for Prism (#21)
1 parent b638d50 commit 5b06b81

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Foundation
2+
3+
private class Token {}
4+
5+
extension Bundle {
6+
// NB: Alternative to `Bundle.module` that does not crash when the bundle is not found
7+
static let textual: Bundle? = {
8+
let bundleName = "textual_Textual"
9+
10+
let overrides: [URL]
11+
#if DEBUG
12+
// The 'PACKAGE_RESOURCE_BUNDLE_PATH' name is preferred since the expected value is a path. The
13+
// check for 'PACKAGE_RESOURCE_BUNDLE_URL' will be removed when all clients have switched over.
14+
// This removal is tracked by rdar://107766372.
15+
if let override = ProcessInfo.processInfo.environment["PACKAGE_RESOURCE_BUNDLE_PATH"]
16+
?? ProcessInfo.processInfo.environment["PACKAGE_RESOURCE_BUNDLE_URL"]
17+
{
18+
overrides = [URL(fileURLWithPath: override)]
19+
} else {
20+
overrides = []
21+
}
22+
#else
23+
overrides = []
24+
#endif
25+
26+
let candidates =
27+
overrides + [
28+
// Bundle should be present here when the package is linked into an App.
29+
Bundle.main.resourceURL,
30+
31+
// Bundle should be present here when the package is linked into a framework.
32+
Bundle(for: Token.self).resourceURL,
33+
34+
// For command-line tools.
35+
Bundle.main.bundleURL,
36+
]
37+
38+
for candidate in candidates {
39+
let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
40+
if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
41+
return bundle
42+
}
43+
}
44+
45+
return nil
46+
}()
47+
}

Sources/Textual/Internal/Highlighter/CodeTokenizer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct CodeToken: Hashable, Sendable {
3333
}
3434

3535
guard
36-
let bundleURL = Bundle.module.url(
36+
let bundleURL = Bundle.textual?.url(
3737
forResource: "prism-bundle",
3838
withExtension: "js"
3939
),

0 commit comments

Comments
 (0)