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);
if (hasGet && !hasRiverpod && !hasBloc) return StateManagement.getx;
if (hasRiverpod && !hasGet && !hasBloc) return StateManagement.riverpod;
if (hasBloc && !hasGet && !hasRiverpod) return StateManagement.bloc;
// Kalau keduanya ada (possible), pakai rule tambahan
if (hasGet || hasRiverpod || hasBloc) {
// Prefer routing detection
final hasGetMaterialApp = await _hasTextInLib(
root.path,
'GetMaterialApp',
);
if (hasGetMaterialApp) return StateManagement.getx;
final hasBlocProvider = await _hasTextInLib(root.path, 'BlocProvider');
if (hasBlocProvider) return StateManagement.bloc;
final hasGoRouter = await _hasTextInLib(root.path, 'GoRouter');
if (hasGoRouter) return StateManagement.riverpod;
}
throw Exception(
'Cannot detect state management. '
'No get/flutter_riverpod/flutter_bloc dependency found.',
);
}