From 473a2713344f68e449911708412d003be9b38490 Mon Sep 17 00:00:00 2001 From: Dustin Collins Date: Thu, 21 Nov 2024 07:15:40 -0500 Subject: [PATCH] Maintain relative resource paths in PDX (#103) * Rename Core dir to PlaydateKit With relative resource paths, this will make loading PlaydateKit resources require the PlaydateKit prefix which is less likely to collide with users own directory names. * Use relative resource paths in PDX root --- Plugins/PDCPlugin/PDCPlugin.swift | 52 ++++++++++++++---- Sources/PlaydateKit/Core/UI/UI.swift | 6 +- .../crank/crank-frames-1x-table-52-38.png | Bin .../crank/crank-frames-2x-table-26-19.png | Bin .../crank/crank-frames-4x-table-13-10.png | Bin .../crank/crank-frames-8x-table-6-5.png | Bin .../Images/crank/crank-notice-bubble-1x.png | Bin .../Images/crank/crank-notice-bubble-2x.png | Bin .../Images/crank/crank-notice-bubble-4x.png | Bin .../Images/crank/crank-notice-bubble-8x.png | Bin .../UI/Images/crank/crank-notice-bubble.png | Bin .../UI/Images/crank/crank-notice-text-1x.png | Bin .../UI/Images/crank/crank-notice-text-2x.png | Bin 13 files changed, 43 insertions(+), 15 deletions(-) rename Sources/PlaydateKit/Resources/{Core => PlaydateKit}/UI/Images/crank/crank-frames-1x-table-52-38.png (100%) rename Sources/PlaydateKit/Resources/{Core => PlaydateKit}/UI/Images/crank/crank-frames-2x-table-26-19.png (100%) rename Sources/PlaydateKit/Resources/{Core => PlaydateKit}/UI/Images/crank/crank-frames-4x-table-13-10.png (100%) rename Sources/PlaydateKit/Resources/{Core => PlaydateKit}/UI/Images/crank/crank-frames-8x-table-6-5.png (100%) rename Sources/PlaydateKit/Resources/{Core => PlaydateKit}/UI/Images/crank/crank-notice-bubble-1x.png (100%) rename Sources/PlaydateKit/Resources/{Core => PlaydateKit}/UI/Images/crank/crank-notice-bubble-2x.png (100%) rename Sources/PlaydateKit/Resources/{Core => PlaydateKit}/UI/Images/crank/crank-notice-bubble-4x.png (100%) rename Sources/PlaydateKit/Resources/{Core => PlaydateKit}/UI/Images/crank/crank-notice-bubble-8x.png (100%) rename Sources/PlaydateKit/Resources/{Core => PlaydateKit}/UI/Images/crank/crank-notice-bubble.png (100%) rename Sources/PlaydateKit/Resources/{Core => PlaydateKit}/UI/Images/crank/crank-notice-text-1x.png (100%) rename Sources/PlaydateKit/Resources/{Core => PlaydateKit}/UI/Images/crank/crank-notice-text-2x.png (100%) diff --git a/Plugins/PDCPlugin/PDCPlugin.swift b/Plugins/PDCPlugin/PDCPlugin.swift index a1b1f109..ac589c6b 100644 --- a/Plugins/PDCPlugin/PDCPlugin.swift +++ b/Plugins/PDCPlugin/PDCPlugin.swift @@ -80,13 +80,6 @@ struct ModuleBuildRequest { let productSource = context.package.sourceModules.first! let productSwiftFiles = productSource.sourceFiles(withSuffix: "swift").map(\.path.string) - let productResources = productSource.sourceFiles - .filter { $0.type == .unknown } - .map(\.path) - - let playdateKitResources = playdateKitSource.sourceFiles(withSuffix: "png") - .filter { $0.type == .unknown } - .map(\.path) let playdateKit = ModuleBuildRequest(name: "playdatekit", type: .playdateKit, relativePath: modulesPath, sourcefiles: playdateKitSwiftFiles) let product = ModuleBuildRequest(name: productName, type: .product, relativePath: context.pluginWorkDirectory, sourcefiles: productSwiftFiles) @@ -227,12 +220,47 @@ struct ModuleBuildRequest { atPath: sourcePath.string, withIntermediateDirectories: true ) - print("copying resources") - for resource in productResources + playdateKitResources { - print(resource.string) + + print("copying resources...") + // Create list or resources including a relative path + var resourcePaths: [(path: String, relativePath: String)] = [] + + // Scan package and dependencies for resources + for package in context.package.dependencies.map(\.package) + [context.package] { + for module in package.sourceModules { + let moduleResources = module.sourceFiles.filter { $0.type == .unknown }.map(\.path) + for resource in moduleResources { + let relativePrefix = module.directory.string + "/Resources/" + // Only copy resource from the Package's "Resources" directory + guard resource.string.hasPrefix(relativePrefix) else {continue} + let relativePath = resource.string.replacingOccurrences(of: relativePrefix, with: "") + resourcePaths.append((resource.string, relativePath)) + } + } + } + + // Copy resources + for resource in resourcePaths { + let dest = sourcePath.appending([resource.relativePath]) + let destDirectory = dest.removingLastComponent() + + var isDirectory: ObjCBool = false + if !FileManager.default.fileExists(atPath: destDirectory.string, isDirectory: &isDirectory) { + let relativeDestDirectory = Path(resource.relativePath).removingLastComponent() + print("creating directory \(relativeDestDirectory.string)/") + try FileManager.default.createDirectory(atPath: destDirectory.string, withIntermediateDirectories: true) + } + + // If the resource is pdxinfo, always place it in the pdx root + var destination = dest.string + if resource.path.hasSuffix("pdxinfo") { + destination = sourcePath.appending(["pdxinfo"]).string + } + + print("copying \(resource.relativePath)") try FileManager.default.copyItem( - atPath: resource.string, - toPath: sourcePath.appending([resource.lastComponent]).string + atPath: resource.path, + toPath: destination ) } diff --git a/Sources/PlaydateKit/Core/UI/UI.swift b/Sources/PlaydateKit/Core/UI/UI.swift index a93fa1ab..054c1723 100644 --- a/Sources/PlaydateKit/Core/UI/UI.swift +++ b/Sources/PlaydateKit/Core/UI/UI.swift @@ -145,7 +145,7 @@ public enum UI { crankIndicatorY = 210 / Int(scale.rawValue) - let imagePath = "crank-notice-bubble-\(scale.rawValue)x.png" + let imagePath = "PlaydateKit/UI/images/crank/crank-notice-bubble-\(scale.rawValue)x.png" bubbleImage = try Graphics.Bitmap(path: imagePath) if let bubbleImage { @@ -163,12 +163,12 @@ public enum UI { textOffset = 76 / Int(scale.rawValue) } - crankFrames = try Graphics.BitmapTable(path: "crank-frames-\(scale.rawValue)x.png") + crankFrames = try Graphics.BitmapTable(path: "PlaydateKit/UI/images/crank/crank-frames-\(scale.rawValue)x.png") frameCount = (crankFrames?.imageCount ?? 0) * 3 switch scale { case .oneTimes, .twoTimes: - textFrameImage = try Graphics.Bitmap(path: "crank-notice-text-\(scale.rawValue)x.png") + textFrameImage = try Graphics.Bitmap(path: "PlaydateKit/UI/images/crank/crank-notice-text-\(scale.rawValue)x.png") textFrameCount = 14 frameCount += textFrameCount default: diff --git a/Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-frames-1x-table-52-38.png b/Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-frames-1x-table-52-38.png similarity index 100% rename from Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-frames-1x-table-52-38.png rename to Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-frames-1x-table-52-38.png diff --git a/Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-frames-2x-table-26-19.png b/Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-frames-2x-table-26-19.png similarity index 100% rename from Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-frames-2x-table-26-19.png rename to Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-frames-2x-table-26-19.png diff --git a/Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-frames-4x-table-13-10.png b/Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-frames-4x-table-13-10.png similarity index 100% rename from Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-frames-4x-table-13-10.png rename to Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-frames-4x-table-13-10.png diff --git a/Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-frames-8x-table-6-5.png b/Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-frames-8x-table-6-5.png similarity index 100% rename from Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-frames-8x-table-6-5.png rename to Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-frames-8x-table-6-5.png diff --git a/Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-bubble-1x.png b/Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-bubble-1x.png similarity index 100% rename from Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-bubble-1x.png rename to Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-bubble-1x.png diff --git a/Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-bubble-2x.png b/Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-bubble-2x.png similarity index 100% rename from Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-bubble-2x.png rename to Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-bubble-2x.png diff --git a/Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-bubble-4x.png b/Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-bubble-4x.png similarity index 100% rename from Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-bubble-4x.png rename to Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-bubble-4x.png diff --git a/Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-bubble-8x.png b/Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-bubble-8x.png similarity index 100% rename from Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-bubble-8x.png rename to Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-bubble-8x.png diff --git a/Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-bubble.png b/Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-bubble.png similarity index 100% rename from Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-bubble.png rename to Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-bubble.png diff --git a/Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-text-1x.png b/Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-text-1x.png similarity index 100% rename from Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-text-1x.png rename to Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-text-1x.png diff --git a/Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-text-2x.png b/Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-text-2x.png similarity index 100% rename from Sources/PlaydateKit/Resources/Core/UI/Images/crank/crank-notice-text-2x.png rename to Sources/PlaydateKit/Resources/PlaydateKit/UI/Images/crank/crank-notice-text-2x.png