|
| 1 | +// Copyright Dave Verwer, Sven A. Schmidt, and other contributors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Dependencies |
| 16 | +import DependenciesMacros |
| 17 | +import Vapor |
| 18 | + |
| 19 | + |
| 20 | +@DependencyClient |
| 21 | +struct EnvironmentClient { |
| 22 | + // See https://swiftpackageindex.com/pointfreeco/swift-dependencies/main/documentation/dependenciesmacros/dependencyclient()#Restrictions |
| 23 | + // regarding the use of XCTFail here. |
| 24 | + var allowBuildTriggers: @Sendable () -> Bool = { XCTFail(#function); return false } |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +extension EnvironmentClient: DependencyKey { |
| 29 | + static var liveValue: EnvironmentClient { |
| 30 | + .init( |
| 31 | + allowBuildTriggers: { |
| 32 | + Environment.get("ALLOW_BUILD_TRIGGERS").flatMap(\.asBool) ?? Constants.defaultAllowBuildTriggering |
| 33 | + } |
| 34 | + ) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +extension EnvironmentClient: TestDependencyKey { |
| 40 | + static var testValue: Self { Self() } |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +extension DependencyValues { |
| 45 | + var environment: EnvironmentClient { |
| 46 | + get { self[EnvironmentClient.self] } |
| 47 | + set { self[EnvironmentClient.self] = newValue } |
| 48 | + } |
| 49 | +} |
0 commit comments