detectStateManagementFromProject static method
Detect the state management (GetX or Riverpod) used in the project at root.
Implementation
static Future<StateManagement> detectStateManagementFromProject(
Directory root,
) async {
final pubspecFile = File(path.join(root.path, 'pubspec.yaml'));
if (!pubspecFile.existsSync()) {
throw Exception('pubspec.yaml not found');
}
final content = await pubspecFile.readAsString();
final hasGet = RegExp(r'^\s*get:\s*', multiLine: true).hasMatch(content);
final hasRiverpod = RegExp(
r'^\s*flutter_riverpod:\s*',
multiLine: true,
).hasMatch(content);
final hasBloc = RegExp(
r'^\s*flutter_bloc:\s*',
multiLine: true,
).hasMatch(content);
final hasProviderPackage = RegExp(
r'^\s*provider:\s*',
multiLine: true,
).hasMatch(content);
if (hasGet &&
!hasRiverpod &&
!hasBloc &&
!hasProviderPackage) {
return StateManagement.getx;
}
if (hasRiverpod &&
!hasGet &&
!hasBloc &&
!hasProviderPackage) {
return StateManagement.riverpod;
}
if (hasBloc && !hasGet && !hasRiverpod && !hasProviderPackage) {
return StateManagement.bloc;
}
if (hasProviderPackage && !hasGet && !hasRiverpod && !hasBloc) {
return StateManagement.provider;
}
// Kalau keduanya ada (possible), pakai rule tambahan
if (hasGet || hasRiverpod || hasBloc || hasProviderPackage) {
// Prefer routing detection
final hasGetMaterialApp = await _hasTextInLib(
root.path,
'GetMaterialApp',
);
if (hasGetMaterialApp) return StateManagement.getx;
final hasProviderScope = await _hasTextInLib(root.path, 'ProviderScope');
if (hasProviderScope) return StateManagement.riverpod;
final hasBlocProvider = await _hasTextInLib(root.path, 'BlocProvider');
if (hasBlocProvider) return StateManagement.bloc;
final hasChangeNotifierProvider = await _hasTextInLib(
root.path,
'ChangeNotifierProvider',
);
if (hasChangeNotifierProvider) return StateManagement.provider;
}
throw Exception(
'Cannot detect state management. '
'No get/flutter_riverpod/flutter_bloc/provider dependency found.',
);
}