Skip to content

Commit 30ca0a3

Browse files
authored
Merge pull request swiftlang#2074 from millenomi/bundle-allbundles
Parity: Bundle.allBundles/.allFrameworks
2 parents 1662225 + 8bd4ffe commit 30ca0a3

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

Diff for: CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

+1
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ CF_EXPORT _Nullable CFErrorRef CFReadStreamCopyError(CFReadStreamRef _Null_unspe
391391

392392
CF_EXPORT _Nullable CFErrorRef CFWriteStreamCopyError(CFWriteStreamRef _Null_unspecified stream);
393393

394+
CF_CROSS_PLATFORM_EXPORT CFStringRef _Nullable _CFBundleCopyExecutablePath(CFBundleRef bundle);
394395
CF_CROSS_PLATFORM_EXPORT Boolean _CFBundleSupportsFHSBundles(void);
395396

396397
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

Diff for: CoreFoundation/PlugIn.subproj/CFBundle.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,11 @@ CF_PRIVATE CFURLRef _CFBundleCopyAppStoreReceiptURLInDirectory(CFURLRef bundleUR
951951
CFURLRef _CFBundleCopyAppStoreReceiptURL(CFBundleRef bundle) {
952952
return _CFBundleCopyAppStoreReceiptURLInDirectory(bundle->_url, bundle->_version);
953953
}
954-
954+
955+
CF_CROSS_PLATFORM_EXPORT CFStringRef _CFBundleCopyExecutablePath(CFBundleRef bundle) {
956+
return _CFBundleCopyExecutableName(bundle, NULL, NULL);
957+
}
958+
955959
CF_PRIVATE CFStringRef _CFBundleCopyExecutableName(CFBundleRef bundle, CFURLRef url, CFDictionaryRef infoDict) {
956960
CFStringRef executableName = NULL;
957961

Diff for: Foundation/Bundle.swift

+33-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,41 @@ open class Bundle: NSObject {
2222
}
2323
}
2424

25-
open class var allBundles: [Bundle] {
26-
NSUnimplemented()
25+
private class var allBundlesRegardlessOfType: [Bundle] {
26+
// FIXME: This doesn't return bundles that weren't loaded using CFBundle or class Bundle. https://bugs.swift.org/browse/SR-10433
27+
guard let bundles = CFBundleGetAllBundles()?._swiftObject as? [CFBundle] else { return [] }
28+
return bundles.map(Bundle.init(cfBundle:))
2729
}
2830

31+
private var isFramework: Bool {
32+
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
33+
return bundleURL.pathExtension == "framework"
34+
#else
35+
36+
#if os(Windows)
37+
if let name = _CFBundleCopyExecutablePath(_bundle)?._nsObject {
38+
return name.pathExtension.lowercased == "dll"
39+
}
40+
#else
41+
42+
// We're assuming this is an OS like Linux or BSD that uses FHS-style library names (lib….so or lib….so.2.3.4)
43+
if let name = _CFBundleCopyExecutablePath(_bundle)?._nsObject {
44+
return name.hasPrefix("lib") && (name.pathExtension == "so" || name.range(of: ".so.").location != NSNotFound)
45+
}
46+
47+
#endif
48+
49+
return false
50+
#endif
51+
}
52+
53+
open class var allBundles: [Bundle] {
54+
return allBundlesRegardlessOfType.filter { !$0.isFramework }
55+
}
56+
57+
open class var allFrameworks: [Bundle] {
58+
return allBundlesRegardlessOfType.filter { $0.isFramework }
59+
}
2960

3061
internal init(cfBundle: CFBundle) {
3162
super.init()

0 commit comments

Comments
 (0)