Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use relative resource paths in PDX root #103

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 40 additions & 12 deletions Plugins/PDCPlugin/PDCPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/PlaydateKit/Core/UI/UI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand Down
Loading