detectEnvironment method
Iterates through the detection strategies in order.
Returns the first non-null environment detected. Throws a detailed ReleaseManagerException if all strategies fail.
Implementation
Future<EnvironmentModel> detectEnvironment(ArgResults? argResults) async {
for (final strategy in _strategies) {
final result = await strategy.detect(argResults);
if (result != null && result.isNotEmpty) {
_logger.detail('Environment detected via ${strategy.name}: $result');
return EnvironmentModel(result);
}
}
// If we reach here, all strategies failed.
throw const ReleaseManagerException(
'Could not automatically detect the build environment.',
details: 'No environment could be resolved from flags or configurations.\n\n'
'To fix this, please provide the environment manually using one of the following methods:\n'
' 1. Pass the --env flag: flutter_build_manager build --env=STAGING\n'
' 2. Provide a flavor: --flavor=dev\n'
' 3. Use dart defines: --dart-define=ENV=PROD\n'
' 4. Specify a fallback in flutter_build_manager.yaml under release_manager > environment > source.',
);
}