Skip to content

Commit 38ab48a

Browse files
committed
dynamically add runtime dependency based on env var
1 parent b47d1b8 commit 38ab48a

File tree

4 files changed

+67
-48
lines changed

4 files changed

+67
-48
lines changed

Examples/APIGateway/Package.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ let package = Package(
1919
.executable(name: "APIGatewayLambda", targets: ["APIGatewayLambda"])
2020
],
2121
dependencies: [
22-
// dependency on swift-aws-lambda-runtime is added dynamically below
23-
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
24-
22+
// during CI, the dependency on local version of swift-aws-lambda-runtime is added dynamically below
23+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
2524
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main")
2625
],
2726
targets: [
@@ -36,20 +35,25 @@ let package = Package(
3635
]
3736
)
3837

39-
if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"],
38+
if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"],
4039
localDepsPath != "",
4140
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]),
42-
let _ = v.isDirectory
41+
v.isDirectory == true
4342
{
43+
// when we use the local runtime as deps, let's remove the dependency added above
44+
let indexToRemove = package.dependencies.firstIndex { dependency in
45+
if case .sourceControl(name: _, location: "https://github.com/swift-server/swift-aws-lambda-runtime.git", requirement: _) = dependency.kind {
46+
return true
47+
}
48+
return false
49+
}
50+
if let indexToRemove {
51+
package.dependencies.remove(at: indexToRemove)
52+
}
53+
54+
// then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..)
4455
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
4556
package.dependencies += [
4657
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
4758
]
48-
49-
} else {
50-
print("[INFO] LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code")
51-
print("[INFO] This project will compile against the main branch of the Lambda Runtime on GitHub")
52-
package.dependencies += [
53-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
54-
]
5559
}

Examples/HelloWorld/Package.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ let package = Package(
1919
.executable(name: "MyLambda", targets: ["MyLambda"])
2020
],
2121
dependencies: [
22-
// dependency on swift-aws-lambda-runtime is added dynamically below
23-
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
22+
// during CI, the dependency on local version of swift-aws-lambda-runtime is added dynamically below
23+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
2424
],
2525
targets: [
2626
.executableTarget(
@@ -33,20 +33,25 @@ let package = Package(
3333
]
3434
)
3535

36-
if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"],
36+
if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"],
3737
localDepsPath != "",
3838
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]),
39-
let _ = v.isDirectory
39+
v.isDirectory == true
4040
{
41+
// when we use the local runtime as deps, let's remove the dependency added above
42+
let indexToRemove = package.dependencies.firstIndex { dependency in
43+
if case .sourceControl(name: _, location: "https://github.com/swift-server/swift-aws-lambda-runtime.git", requirement: _) = dependency.kind {
44+
return true
45+
}
46+
return false
47+
}
48+
if let indexToRemove {
49+
package.dependencies.remove(at: indexToRemove)
50+
}
51+
52+
// then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..)
4153
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
4254
package.dependencies += [
4355
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
4456
]
45-
46-
} else {
47-
print("[INFO] LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code")
48-
print("[INFO] This project will compile against the main branch of the Lambda Runtime on GitHub")
49-
package.dependencies += [
50-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
51-
]
5257
}

Examples/S3_AWSSDK/Package.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ let package = Package(
1919
.executable(name: "AWSSDKExample", targets: ["AWSSDKExample"])
2020
],
2121
dependencies: [
22-
// dependency on swift-aws-lambda-runtime is added dynamically below
23-
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
22+
// during CI, the dependency on local version of swift-aws-lambda-runtime is added dynamically below
23+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
2424
.package(url: "https://github.com/swift-server/swift-aws-lambda-events", branch: "main"),
2525
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "1.0.0"),
2626
],
@@ -36,20 +36,25 @@ let package = Package(
3636
]
3737
)
3838

39-
if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"],
39+
if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"],
4040
localDepsPath != "",
4141
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]),
42-
let _ = v.isDirectory
42+
v.isDirectory == true
4343
{
44+
// when we use the local runtime as deps, let's remove the dependency added above
45+
let indexToRemove = package.dependencies.firstIndex { dependency in
46+
if case .sourceControl(name: _, location: "https://github.com/swift-server/swift-aws-lambda-runtime.git", requirement: _) = dependency.kind {
47+
return true
48+
}
49+
return false
50+
}
51+
if let indexToRemove {
52+
package.dependencies.remove(at: indexToRemove)
53+
}
54+
55+
// then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..)
4456
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
4557
package.dependencies += [
4658
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
4759
]
48-
49-
} else {
50-
print("[INFO] LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code")
51-
print("[INFO] This project will compile against the main branch of the Lambda Runtime on GitHub")
52-
package.dependencies += [
53-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
54-
]
55-
}
60+
}

Examples/S3_Soto/Package.swift

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ let platforms: [PackageDescription.SupportedPlatform]? = nil
1313
#endif
1414

1515
let package = Package(
16-
name: "SotoLambdaExample",
16+
name: "SotoExample",
1717
platforms: platforms,
1818
products: [
19-
.executable(name: "SotoLambdaExample", targets: ["SotoExample"])
19+
.executable(name: "SotoExample", targets: ["SotoExample"])
2020
],
2121
dependencies: [
2222
.package(url: "https://github.com/soto-project/soto.git", from: "7.0.0"),
2323

24-
// dependency on swift-aws-lambda-runtime is added dynamically below
25-
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
24+
// during CI, the dependency on local version of swift-aws-lambda-runtime is added dynamically below
25+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
2626
.package(url: "https://github.com/swift-server/swift-aws-lambda-events", branch: "main"),
2727
],
2828
targets: [
@@ -37,20 +37,25 @@ let package = Package(
3737
]
3838
)
3939

40-
if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"],
40+
if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"],
4141
localDepsPath != "",
4242
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]),
43-
let _ = v.isDirectory
43+
v.isDirectory == true
4444
{
45+
// when we use the local runtime as deps, let's remove the dependency added above
46+
let indexToRemove = package.dependencies.firstIndex { dependency in
47+
if case .sourceControl(name: _, location: "https://github.com/swift-server/swift-aws-lambda-runtime.git", requirement: _) = dependency.kind {
48+
return true
49+
}
50+
return false
51+
}
52+
if let indexToRemove {
53+
package.dependencies.remove(at: indexToRemove)
54+
}
55+
56+
// then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..)
4557
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
4658
package.dependencies += [
4759
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
4860
]
49-
50-
} else {
51-
print("[INFO] LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code")
52-
print("[INFO] This project will compile against the main branch of the Lambda Runtime on GitHub")
53-
package.dependencies += [
54-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
55-
]
5661
}

0 commit comments

Comments
 (0)