|
| 1 | +// Copyright 2024, Nitric Technologies Pty Ltd. |
| 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 'dart:io'; |
| 16 | + |
| 17 | +/// The environment variable key that will be used to determine the current Nitric lifecycle/executing environment |
| 18 | +const String nitricEnvironment = 'NITRIC_ENVIRONMENT'; |
| 19 | + |
| 20 | +/// Possible nitric execution environments |
| 21 | +enum LifecycleStage { |
| 22 | + /// Local development run (using nitric run/start) |
| 23 | + local, |
| 24 | + |
| 25 | + /// Local development requirements building/collection (using nitric up) |
| 26 | + build, |
| 27 | + |
| 28 | + /// When the code is running in a deployed environment |
| 29 | + cloud, |
| 30 | +} |
| 31 | + |
| 32 | +/// Get the current lifecycle stage from environment variables |
| 33 | +LifecycleStage _getCurrentLifecycle() { |
| 34 | + final lifecycle = Platform.environment[nitricEnvironment]; |
| 35 | + |
| 36 | + if (lifecycle == null) { |
| 37 | + throw Exception( |
| 38 | + 'Unable to determine the current Nitric lifecycle, please ensure the $nitricEnvironment environment variable is set', |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + return LifecycleStage.values.firstWhere( |
| 44 | + (stage) => stage.name == lifecycle, |
| 45 | + orElse: () => throw Exception('Invalid lifecycle stage: $lifecycle'), |
| 46 | + ); |
| 47 | + } catch (e) { |
| 48 | + throw Exception( |
| 49 | + 'Unable to determine the current Nitric lifecycle, please ensure the $nitricEnvironment environment variable is set to a valid value', |
| 50 | + ); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/// Check if the current environment is one of the provided stages |
| 55 | +bool _isInLifecycle(List<LifecycleStage> stages) { |
| 56 | + final currentStage = _getCurrentLifecycle(); |
| 57 | + return stages.contains(currentStage); |
| 58 | +} |
| 59 | + |
| 60 | +/// If the current environment is one of the provided stages, execute the provided callback |
| 61 | +T? _whenInLifecycles<T>(List<LifecycleStage> stages, T Function() callback) { |
| 62 | + if (_isInLifecycle(stages)) { |
| 63 | + return callback(); |
| 64 | + } |
| 65 | + return null; |
| 66 | +} |
| 67 | + |
| 68 | +/// If the current environment is running (local or cloud), execute the provided callback |
| 69 | +T? _whenRunning<T>(T Function() callback) => _whenInLifecycles<T>( |
| 70 | + [LifecycleStage.local, LifecycleStage.cloud], callback); |
| 71 | + |
| 72 | +/// If the current environment is collecting requirements, execute the provided callback |
| 73 | +T? _whenCollecting<T>(T Function() callback) => |
| 74 | + _whenInLifecycles<T>([LifecycleStage.build], callback); |
| 75 | + |
| 76 | +/// Check if the current lifecycle is running (local or cloud) |
| 77 | +bool _isRunning() => |
| 78 | + _isInLifecycle([LifecycleStage.local, LifecycleStage.cloud]); |
| 79 | + |
| 80 | +/// Check if the current lifecycle is collecting application requirements |
| 81 | +bool _isCollecting() => _isInLifecycle([LifecycleStage.build]); |
| 82 | + |
| 83 | +/// Lifecycle utility class |
| 84 | +class Lifecycle { |
| 85 | + /// Check if the current environment is one of the provided stages |
| 86 | + static bool isInStage(List<LifecycleStage> stages) => _isInLifecycle(stages); |
| 87 | + |
| 88 | + /// Check if the current lifecycle is collecting application requirements |
| 89 | + static bool get isCollecting => _isCollecting(); |
| 90 | + |
| 91 | + /// Check if the current lifecycle is running the app |
| 92 | + static bool get isRunning => _isRunning(); |
| 93 | + |
| 94 | + /// If the current environment is one of the provided stages, execute the provided callback |
| 95 | + static T? when<T>(List<LifecycleStage> stages, T Function() callback) => |
| 96 | + _whenInLifecycles<T>(stages, callback); |
| 97 | + |
| 98 | + /// If the current environment is collecting application requirements |
| 99 | + static T? whenCollecting<T>(T Function() callback) => |
| 100 | + _whenCollecting<T>(callback); |
| 101 | + |
| 102 | + /// If the current environment is a cloud environment, execute the provided callback |
| 103 | + static T? whenRunning<T>(T Function() callback) => _whenRunning<T>(callback); |
| 104 | +} |
0 commit comments