|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import PackagePlugin |
| 16 | + |
| 17 | +#if canImport(FoundationEssentials) |
| 18 | +import FoundationEssentials |
| 19 | +#else |
| 20 | +import Foundation |
| 21 | +#endif |
| 22 | + |
| 23 | +@main |
| 24 | +struct AWSLambdaPackager: CommandPlugin { |
| 25 | + |
| 26 | + func performCommand(context: PackagePlugin.PluginContext, arguments: [String]) async throws { |
| 27 | + |
| 28 | + // values to pass to the AWSLambdaPluginHelper |
| 29 | + let outputDirectory: URL |
| 30 | + let products: [Product] |
| 31 | + let buildConfiguration: PackageManager.BuildConfiguration |
| 32 | + let packageID: String = context.package.id |
| 33 | + let packageDisplayName = context.package.displayName |
| 34 | + let packageDirectory = context.package.directoryURL |
| 35 | + let dockerToolPath = try context.tool(named: "docker").url |
| 36 | + let zipToolPath = try context.tool(named: "zip").url |
| 37 | + |
| 38 | + // extract arguments that require PluginContext to fully resolve |
| 39 | + // resolve them here and pass them to the AWSLambdaPluginHelper as arguments |
| 40 | + var argumentExtractor = ArgumentExtractor(arguments) |
| 41 | + |
| 42 | + let outputPathArgument = argumentExtractor.extractOption(named: "output-path") |
| 43 | + let productsArgument = argumentExtractor.extractOption(named: "products") |
| 44 | + let configurationArgument = argumentExtractor.extractOption(named: "configuration") |
| 45 | + |
| 46 | + if let outputPath = outputPathArgument.first { |
| 47 | + #if os(Linux) |
| 48 | + var isDirectory: Bool = false |
| 49 | + #else |
| 50 | + var isDirectory: ObjCBool = false |
| 51 | + #endif |
| 52 | + guard FileManager.default.fileExists(atPath: outputPath, isDirectory: &isDirectory) |
| 53 | + else { |
| 54 | + throw BuilderErrors.invalidArgument("invalid output directory '\(outputPath)'") |
| 55 | + } |
| 56 | + outputDirectory = URL(string: outputPath)! |
| 57 | + } else { |
| 58 | + outputDirectory = context.pluginWorkDirectoryURL.appending(path: "\(AWSLambdaPackager.self)") |
| 59 | + } |
| 60 | + |
| 61 | + let explicitProducts = !productsArgument.isEmpty |
| 62 | + if explicitProducts { |
| 63 | + let _products = try context.package.products(named: productsArgument) |
| 64 | + for product in _products { |
| 65 | + guard product is ExecutableProduct else { |
| 66 | + throw BuilderErrors.invalidArgument("product named '\(product.name)' is not an executable product") |
| 67 | + } |
| 68 | + } |
| 69 | + products = _products |
| 70 | + |
| 71 | + } else { |
| 72 | + products = context.package.products.filter { $0 is ExecutableProduct } |
| 73 | + } |
| 74 | + |
| 75 | + if let _buildConfigurationName = configurationArgument.first { |
| 76 | + guard let _buildConfiguration = PackageManager.BuildConfiguration(rawValue: _buildConfigurationName) else { |
| 77 | + throw BuilderErrors.invalidArgument("invalid build configuration named '\(_buildConfigurationName)'") |
| 78 | + } |
| 79 | + buildConfiguration = _buildConfiguration |
| 80 | + } else { |
| 81 | + buildConfiguration = .release |
| 82 | + } |
| 83 | + |
| 84 | + // TODO: When running on Amazon Linux 2, we have to build directly from the plugin |
| 85 | + // launch the build, then call the helper just for the ZIP part |
| 86 | + |
| 87 | + let tool = try context.tool(named: "AWSLambdaPluginHelper") |
| 88 | + let args = [ |
| 89 | + "build", |
| 90 | + "--output-path", outputDirectory.path(), |
| 91 | + "--products", products.map { $0.name }.joined(separator: ","), |
| 92 | + "--configuration", buildConfiguration.rawValue, |
| 93 | + "--package-id", packageID, |
| 94 | + "--package-display-name", packageDisplayName, |
| 95 | + "--package-directory", packageDirectory.path(), |
| 96 | + "--docker-tool-path", dockerToolPath.path, |
| 97 | + "--zip-tool-path", zipToolPath.path |
| 98 | + ] + arguments |
| 99 | + |
| 100 | + // Invoke the plugin helper on the target directory, passing a configuration |
| 101 | + // file from the package directory. |
| 102 | + let process = try Process.run(tool.url, arguments: args) |
| 103 | + process.waitUntilExit() |
| 104 | + |
| 105 | + // Check whether the subprocess invocation was successful. |
| 106 | + if !(process.terminationReason == .exit && process.terminationStatus == 0) { |
| 107 | + let problem = "\(process.terminationReason):\(process.terminationStatus)" |
| 108 | + Diagnostics.error("AWSLambdaPluginHelper invocation failed: \(problem)") |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // TODO: When running on Amazon Linux 2, we have to build directly from the plugin |
| 113 | +// private func build( |
| 114 | +// packageIdentity: Package.ID, |
| 115 | +// products: [Product], |
| 116 | +// buildConfiguration: PackageManager.BuildConfiguration, |
| 117 | +// verboseLogging: Bool |
| 118 | +// ) throws -> [LambdaProduct: URL] { |
| 119 | +// print("-------------------------------------------------------------------------") |
| 120 | +// print("building \"\(packageIdentity)\"") |
| 121 | +// print("-------------------------------------------------------------------------") |
| 122 | +// |
| 123 | +// var results = [LambdaProduct: URL]() |
| 124 | +// for product in products { |
| 125 | +// print("building \"\(product.name)\"") |
| 126 | +// var parameters = PackageManager.BuildParameters() |
| 127 | +// parameters.configuration = buildConfiguration |
| 128 | +// parameters.otherSwiftcFlags = ["-static-stdlib"] |
| 129 | +// parameters.logging = verboseLogging ? .verbose : .concise |
| 130 | +// |
| 131 | +// let result = try packageManager.build( |
| 132 | +// .product(product.name), |
| 133 | +// parameters: parameters |
| 134 | +// ) |
| 135 | +// guard let artifact = result.executableArtifact(for: product) else { |
| 136 | +// throw Errors.productExecutableNotFound(product.name) |
| 137 | +// } |
| 138 | +// results[.init(product)] = artifact.url |
| 139 | +// } |
| 140 | +// return results |
| 141 | +// } |
| 142 | + |
| 143 | +// private func isAmazonLinux2() -> Bool { |
| 144 | +// if let data = FileManager.default.contents(atPath: "/etc/system-release"), |
| 145 | +// let release = String(data: data, encoding: .utf8) |
| 146 | +// { |
| 147 | +// return release.hasPrefix("Amazon Linux release 2") |
| 148 | +// } else { |
| 149 | +// return false |
| 150 | +// } |
| 151 | +// } |
| 152 | +} |
| 153 | + |
| 154 | +private enum BuilderErrors: Error, CustomStringConvertible { |
| 155 | + case invalidArgument(String) |
| 156 | + |
| 157 | + var description: String { |
| 158 | + switch self { |
| 159 | + case .invalidArgument(let description): |
| 160 | + return description |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +extension PackageManager.BuildResult { |
| 166 | + // find the executable produced by the build |
| 167 | + func executableArtifact(for product: Product) -> PackageManager.BuildResult.BuiltArtifact? { |
| 168 | + let executables = self.builtArtifacts.filter { |
| 169 | + $0.kind == .executable && $0.url.lastPathComponent == product.name |
| 170 | + } |
| 171 | + guard !executables.isEmpty else { |
| 172 | + return nil |
| 173 | + } |
| 174 | + guard executables.count == 1, let executable = executables.first else { |
| 175 | + return nil |
| 176 | + } |
| 177 | + return executable |
| 178 | + } |
| 179 | +} |
0 commit comments