Skip to content

Commit 426e658

Browse files
jrosen081sebsto
andauthored
Add Support For Copying All Resources Into Final Executable (#386)
* Add Support For Copying All Resources Into Final Executable * Run formatter --------- Co-authored-by: Sébastien Stormacq <[email protected]>
1 parent 8131a83 commit 426e658

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

Diff for: Examples/LocalDebugging/Shared/Package.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ let package = Package(
1010
],
1111
dependencies: [],
1212
targets: [
13-
.target(name: "Shared", dependencies: [])
13+
.target(
14+
name: "Shared",
15+
dependencies: [],
16+
resources: [
17+
.process("Resources")
18+
]
19+
)
1420
]
1521
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

Diff for: Plugins/AWSLambdaPackager/Plugin.swift

+21-14
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,12 @@ struct AWSLambdaPackager: CommandPlugin {
133133
// because Examples' Package.swift have a dependency on ../..
134134
// just like Package.swift's examples assume ../.., we assume we are two levels below the root project
135135
let slice = packageDirectory.pathComponents.suffix(2)
136-
let beforeLastComponent = packageDirectory.pathComponents[slice.startIndex]
137-
let lastComponent = packageDirectory.pathComponents[slice.endIndex - 1]
138136
try Utils.execute(
139137
executable: dockerToolPath,
140138
arguments: [
141139
"run", "--rm", "--env", "LAMBDA_USE_LOCAL_DEPS=true", "-v",
142140
"\(packageDirectory.path())../..:/workspace", "-w",
143-
"/workspace/\(beforeLastComponent)/\(lastComponent)", baseImage, "bash", "-cl", buildCommand,
141+
"/workspace/\(slice.joined(separator: "/"))", baseImage, "bash", "-cl", buildCommand,
144142
],
145143
logLevel: verboseLogging ? .debug : .output
146144
)
@@ -237,17 +235,26 @@ struct AWSLambdaPackager: CommandPlugin {
237235

238236
// add resources
239237
var artifactPathComponents = artifactPath.pathComponents
240-
_ = artifactPathComponents.removeLast()
241-
let artifactDirectory = artifactPathComponents.joined(separator: "/")
242-
let resourcesDirectoryName = "\(packageName)_\(product.name).resources"
243-
let resourcesDirectory = artifactDirectory.appending(resourcesDirectoryName)
244-
let relocatedResourcesDirectory = workingDirectory.appending(path: resourcesDirectoryName)
245-
if FileManager.default.fileExists(atPath: resourcesDirectory) {
246-
try FileManager.default.copyItem(
247-
atPath: resourcesDirectory,
248-
toPath: relocatedResourcesDirectory.path()
249-
)
250-
arguments.append(resourcesDirectoryName)
238+
_ = artifactPathComponents.removeFirst() // Get rid of beginning "/"
239+
_ = artifactPathComponents.removeLast() // Get rid of the name of the package
240+
let artifactDirectory = "/\(artifactPathComponents.joined(separator: "/"))"
241+
for fileInArtifactDirectory in try FileManager.default.contentsOfDirectory(atPath: artifactDirectory) {
242+
guard let artifactURL = URL(string: "\(artifactDirectory)/\(fileInArtifactDirectory)") else {
243+
continue
244+
}
245+
246+
guard artifactURL.pathExtension == "resources" else {
247+
continue // Not resources, so don't copy
248+
}
249+
let resourcesDirectoryName = artifactURL.lastPathComponent
250+
let relocatedResourcesDirectory = workingDirectory.appending(path: resourcesDirectoryName)
251+
if FileManager.default.fileExists(atPath: artifactURL.path()) {
252+
try FileManager.default.copyItem(
253+
atPath: artifactURL.path(),
254+
toPath: relocatedResourcesDirectory.path()
255+
)
256+
arguments.append(resourcesDirectoryName)
257+
}
251258
}
252259

253260
// run the zip tool

0 commit comments

Comments
 (0)