From fed8799f51f2957a89e551207e0828d420a48fb5 Mon Sep 17 00:00:00 2001 From: Jonathan Wight Date: Sun, 23 Nov 2025 21:15:00 +0100 Subject: [PATCH] Adds MetalCompilerPluginSupport with useful bundle extension to help make it easier to find shaders. --- Package.swift | 6 ++- .../MetalCompilerPluginSupport.swift | 37 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 Sources/MetalCompilerPluginSupport/MetalCompilerPluginSupport.swift diff --git a/Package.swift b/Package.swift index 4435011..c175c33 100644 --- a/Package.swift +++ b/Package.swift @@ -8,13 +8,17 @@ let package = Package( .plugin( name: "MetalCompilerPlugin", targets: ["MetalCompilerPlugin"] - ) + ), + .library(name: "MetalCompilerPluginSupport", targets: ["MetalCompilerPluginSupport"]), ], targets: [ .plugin( name: "MetalCompilerPlugin", capability: .buildTool() ), + .target( + name: "MetalCompilerPluginSupport" + ), // The following targets are for testing the plugin and are examples of its usage. .target( diff --git a/Sources/MetalCompilerPluginSupport/MetalCompilerPluginSupport.swift b/Sources/MetalCompilerPluginSupport/MetalCompilerPluginSupport.swift new file mode 100644 index 0000000..83d5baa --- /dev/null +++ b/Sources/MetalCompilerPluginSupport/MetalCompilerPluginSupport.swift @@ -0,0 +1,37 @@ +import Foundation + +public extension Bundle { + var parentBundle: Bundle? { + let components = bundlePath.split(separator: "/") + guard let index = components.dropLast().firstIndex(where: { $0.hasSuffix(".bundle") || $0.hasSuffix(".xctest") || $0.hasSuffix(".app") }) else { + return nil + } + let path = "/" + components[...index].joined(separator: "/") + return Bundle(path: path) + } + + var childBundles: [Bundle] { + guard let resourcePath else { + return [] + } + let fileManager = FileManager() + guard let paths = try? fileManager.contentsOfDirectory(atPath: resourcePath) else { + return [] + } + + return paths.filter { + $0.hasSuffix(".bundle") + } + .map { + Bundle(path: resourcePath.appending("/").appending($0))! + } + } + + /// Looks for a child bundle with a name of the form: `"*_.bundle"`. Use like `Bundle.module.parentBundle?.childBundle(withSuffix: "")` to help find a bundle with shaders + func childBundle(withSuffix suffix: String) -> Bundle? { + childBundles.first { + $0.bundleURL.lastPathComponent.hasSuffix(("_\(suffix).bundle")) + } + } +} +