Skip to content

Commit db9f17c

Browse files
jrosen081jsonfry
authored andcommitted
Add Support For Copying All Resources Into Final Executable
Cherrypick swift-server#386
1 parent 5ecc24f commit db9f17c

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

Examples/LocalDebugging/Shared/Package.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ let package = Package(
1111
dependencies: [
1212
],
1313
targets: [
14-
.target(name: "Shared", dependencies: []),
14+
.target(
15+
name: "Shared",
16+
dependencies: [],
17+
resources: [
18+
.process("Resources")
19+
]
20+
)
1521
]
1622
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

Plugins/AWSLambdaPackager/Plugin.swift

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,15 @@ struct AWSLambdaPackager: CommandPlugin {
128128
// when developing locally, we must have the full swift-aws-lambda-runtime project in the container
129129
// because Examples' Package.swift have a dependency on ../..
130130
// just like Package.swift's examples assume ../.., we assume we are two levels below the root project
131-
let lastComponent = packageDirectory.lastComponent
132-
let beforeLastComponent = packageDirectory.removingLastComponent().lastComponent
131+
let packageDirectoryURL = URL(string: packageDirectory.string)!
132+
let slice = packageDirectoryURL.pathComponents.suffix(2)
133133
try self.execute(
134134
executable: dockerToolPath,
135-
arguments: ["run", "--rm", "--env", "LAMBDA_USE_LOCAL_DEPS=true", "-v", "\(packageDirectory.string)/../..:/workspace", "-w", "/workspace/\(beforeLastComponent)/\(lastComponent)", baseImage, "bash", "-cl", buildCommand],
135+
arguments: [
136+
"run", "--rm", "--env", "LAMBDA_USE_LOCAL_DEPS=true", "-v",
137+
"\(packageDirectory)../..:/workspace", "-w",
138+
"/workspace/\(slice.joined(separator: "/"))", baseImage, "bash", "-cl", buildCommand,
139+
],
136140
logLevel: verboseLogging ? .debug : .output
137141
)
138142
} else {
@@ -226,13 +230,27 @@ struct AWSLambdaPackager: CommandPlugin {
226230
#endif
227231

228232
// add resources
229-
let artifactDirectory = artifactPath.removingLastComponent()
230-
let resourcesDirectoryName = "\(packageName)_\(product.name).resources"
231-
let resourcesDirectory = artifactDirectory.appending(resourcesDirectoryName)
232-
let relocatedResourcesDirectory = workingDirectory.appending(resourcesDirectoryName)
233-
if FileManager.default.fileExists(atPath: resourcesDirectory.string) {
234-
try FileManager.default.copyItem(atPath: resourcesDirectory.string, toPath: relocatedResourcesDirectory.string)
235-
arguments.append(resourcesDirectoryName)
233+
var artifactPathComponents = URL(string: artifactPath.string)!.pathComponents
234+
_ = artifactPathComponents.removeFirst() // Get rid of beginning "/"
235+
_ = artifactPathComponents.removeLast() // Get rid of the name of the package
236+
let artifactDirectory = "/\(artifactPathComponents.joined(separator: "/"))"
237+
for fileInArtifactDirectory in try FileManager.default.contentsOfDirectory(atPath: artifactDirectory) {
238+
guard let artifactURL = URL(string: "\(artifactDirectory)/\(fileInArtifactDirectory)") else {
239+
continue
240+
}
241+
242+
guard artifactURL.pathExtension == "resources" else {
243+
continue // Not resources, so don't copy
244+
}
245+
let resourcesDirectoryName = artifactURL.lastPathComponent
246+
let relocatedResourcesDirectory = workingDirectory.appending(resourcesDirectoryName)
247+
if FileManager.default.fileExists(atPath: artifactURL.path()) {
248+
try FileManager.default.copyItem(
249+
atPath: artifactURL.path(),
250+
toPath: relocatedResourcesDirectory.string
251+
)
252+
arguments.append(resourcesDirectoryName)
253+
}
236254
}
237255

238256
// run the zip tool

0 commit comments

Comments
 (0)