From 74a93cce19ca2b4441a1258d037f92d2979dde57 Mon Sep 17 00:00:00 2001 From: jrosen081 Date: Sun, 6 Oct 2024 09:16:12 -0400 Subject: [PATCH 1/3] Add Support For Copying All Resources Into Final Executable --- Examples/LocalDebugging/Shared/Package.swift | 4 +- .../Shared/Sources/Shared/Resources/test.json | 1 + Plugins/AWSLambdaPackager/Plugin.swift | 40 +++++++++++-------- 3 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 Examples/LocalDebugging/Shared/Sources/Shared/Resources/test.json diff --git a/Examples/LocalDebugging/Shared/Package.swift b/Examples/LocalDebugging/Shared/Package.swift index 2ac36e03..6c2bd90a 100644 --- a/Examples/LocalDebugging/Shared/Package.swift +++ b/Examples/LocalDebugging/Shared/Package.swift @@ -10,6 +10,8 @@ let package = Package( ], dependencies: [], targets: [ - .target(name: "Shared", dependencies: []) + .target(name: "Shared", dependencies: [], resources: [ + .process("Resources") + ]) ] ) diff --git a/Examples/LocalDebugging/Shared/Sources/Shared/Resources/test.json b/Examples/LocalDebugging/Shared/Sources/Shared/Resources/test.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/Examples/LocalDebugging/Shared/Sources/Shared/Resources/test.json @@ -0,0 +1 @@ +{} diff --git a/Plugins/AWSLambdaPackager/Plugin.swift b/Plugins/AWSLambdaPackager/Plugin.swift index 4359ab1d..f6cbf8f5 100644 --- a/Plugins/AWSLambdaPackager/Plugin.swift +++ b/Plugins/AWSLambdaPackager/Plugin.swift @@ -132,15 +132,13 @@ struct AWSLambdaPackager: CommandPlugin { // when developing locally, we must have the full swift-aws-lambda-runtime project in the container // because Examples' Package.swift have a dependency on ../.. // just like Package.swift's examples assume ../.., we assume we are two levels below the root project - let slice = packageDirectory.pathComponents.suffix(2) - let beforeLastComponent = packageDirectory.pathComponents[slice.startIndex] - let lastComponent = packageDirectory.pathComponents[slice.endIndex - 1] + let slice = packageDirectory.pathComponents.suffix(3) try Utils.execute( executable: dockerToolPath, arguments: [ "run", "--rm", "--env", "LAMBDA_USE_LOCAL_DEPS=true", "-v", - "\(packageDirectory.path())../..:/workspace", "-w", - "/workspace/\(beforeLastComponent)/\(lastComponent)", baseImage, "bash", "-cl", buildCommand, + "\(packageDirectory.path())../../..:/workspace", "-w", + "/workspace/\(slice.joined(separator: "/"))", baseImage, "bash", "-cl", buildCommand, ], logLevel: verboseLogging ? .debug : .output ) @@ -237,18 +235,28 @@ struct AWSLambdaPackager: CommandPlugin { // add resources var artifactPathComponents = artifactPath.pathComponents - _ = artifactPathComponents.removeLast() - let artifactDirectory = artifactPathComponents.joined(separator: "/") - let resourcesDirectoryName = "\(packageName)_\(product.name).resources" - let resourcesDirectory = artifactDirectory.appending(resourcesDirectoryName) - let relocatedResourcesDirectory = workingDirectory.appending(path: resourcesDirectoryName) - if FileManager.default.fileExists(atPath: resourcesDirectory) { - try FileManager.default.copyItem( - atPath: resourcesDirectory, - toPath: relocatedResourcesDirectory.path() - ) - arguments.append(resourcesDirectoryName) + _ = artifactPathComponents.removeFirst() // Get rid of beginning "/" + _ = artifactPathComponents.removeLast() // Get rid of the name of the package + let artifactDirectory = "/\(artifactPathComponents.joined(separator: "/"))" + for fileInArtifactDirectory in try FileManager.default.contentsOfDirectory(atPath: artifactDirectory) { + guard let artifactURL = URL(string: "\(artifactDirectory)/\(fileInArtifactDirectory)") else { + continue + } + + guard artifactURL.pathExtension == "resources" else { + continue // Not resources, so don't copy + } + let resourcesDirectoryName = artifactURL.lastPathComponent + let relocatedResourcesDirectory = workingDirectory.appending(path: resourcesDirectoryName) + if FileManager.default.fileExists(atPath: artifactURL.path()) { + try FileManager.default.copyItem( + atPath: artifactURL.path(), + toPath: relocatedResourcesDirectory.path() + ) + arguments.append(resourcesDirectoryName) + } } + // run the zip tool try Utils.execute( From a42741dced3b1cc36057b7939e6351ebf0cdea33 Mon Sep 17 00:00:00 2001 From: jrosen081 Date: Mon, 7 Oct 2024 18:00:48 -0400 Subject: [PATCH 2/3] Run formatter --- Examples/LocalDebugging/Shared/Package.swift | 10 +++++++--- Plugins/AWSLambdaPackager/Plugin.swift | 9 ++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Examples/LocalDebugging/Shared/Package.swift b/Examples/LocalDebugging/Shared/Package.swift index 6c2bd90a..12d5eb9f 100644 --- a/Examples/LocalDebugging/Shared/Package.swift +++ b/Examples/LocalDebugging/Shared/Package.swift @@ -10,8 +10,12 @@ let package = Package( ], dependencies: [], targets: [ - .target(name: "Shared", dependencies: [], resources: [ - .process("Resources") - ]) + .target( + name: "Shared", + dependencies: [], + resources: [ + .process("Resources") + ] + ) ] ) diff --git a/Plugins/AWSLambdaPackager/Plugin.swift b/Plugins/AWSLambdaPackager/Plugin.swift index f6cbf8f5..740293e7 100644 --- a/Plugins/AWSLambdaPackager/Plugin.swift +++ b/Plugins/AWSLambdaPackager/Plugin.swift @@ -235,16 +235,16 @@ struct AWSLambdaPackager: CommandPlugin { // add resources var artifactPathComponents = artifactPath.pathComponents - _ = artifactPathComponents.removeFirst() // Get rid of beginning "/" - _ = artifactPathComponents.removeLast() // Get rid of the name of the package + _ = artifactPathComponents.removeFirst() // Get rid of beginning "/" + _ = artifactPathComponents.removeLast() // Get rid of the name of the package let artifactDirectory = "/\(artifactPathComponents.joined(separator: "/"))" for fileInArtifactDirectory in try FileManager.default.contentsOfDirectory(atPath: artifactDirectory) { guard let artifactURL = URL(string: "\(artifactDirectory)/\(fileInArtifactDirectory)") else { continue } - + guard artifactURL.pathExtension == "resources" else { - continue // Not resources, so don't copy + continue // Not resources, so don't copy } let resourcesDirectoryName = artifactURL.lastPathComponent let relocatedResourcesDirectory = workingDirectory.appending(path: resourcesDirectoryName) @@ -256,7 +256,6 @@ struct AWSLambdaPackager: CommandPlugin { arguments.append(resourcesDirectoryName) } } - // run the zip tool try Utils.execute( From a4bc31dcac063973bb9fa98f983417d74cbab497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Tue, 8 Oct 2024 10:40:18 +0200 Subject: [PATCH 3/3] revert examples to two levels dir structure --- Plugins/AWSLambdaPackager/Plugin.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Plugins/AWSLambdaPackager/Plugin.swift b/Plugins/AWSLambdaPackager/Plugin.swift index 740293e7..f342c770 100644 --- a/Plugins/AWSLambdaPackager/Plugin.swift +++ b/Plugins/AWSLambdaPackager/Plugin.swift @@ -132,12 +132,12 @@ struct AWSLambdaPackager: CommandPlugin { // when developing locally, we must have the full swift-aws-lambda-runtime project in the container // because Examples' Package.swift have a dependency on ../.. // just like Package.swift's examples assume ../.., we assume we are two levels below the root project - let slice = packageDirectory.pathComponents.suffix(3) + let slice = packageDirectory.pathComponents.suffix(2) try Utils.execute( executable: dockerToolPath, arguments: [ "run", "--rm", "--env", "LAMBDA_USE_LOCAL_DEPS=true", "-v", - "\(packageDirectory.path())../../..:/workspace", "-w", + "\(packageDirectory.path())../..:/workspace", "-w", "/workspace/\(slice.joined(separator: "/"))", baseImage, "bash", "-cl", buildCommand, ], logLevel: verboseLogging ? .debug : .output