installUtilsModule function

Future<void> installUtilsModule(
  1. String projectPath, {
  2. bool force = false,
})

Implementation

Future<void> installUtilsModule(String projectPath,
    {bool force = false}) async {
  print('📦 Installing core utilities...');

  final projectName = getProjectName(projectPath);
  final Set<String> pluginsToAdd = {};

  var count = 0;
  final utilsPaths = widgetsTemplatesMap.keys
      .where((path) => path.startsWith('core/utils/'))
      .toList();

  for (final relativePath in utilsPaths) {
    final templateFunc = widgetsTemplatesMap[relativePath]!;
    final content = templateFunc(projectName);
    final filePath = p.join(projectPath, 'lib', relativePath);

    safeCreateDir(File(filePath).parent.path);
    safeWriteFile(filePath, content,
        overwrite: force, projectPath: projectPath);
    count++;

    // Collect dependencies from imports
    final importRegex = RegExp(r"import\s+['" "]([^'" "]+)['" "]");
    final matches = importRegex.allMatches(content);
    for (final match in matches) {
      final importPath = match.group(1)!;

      if (importPath.startsWith('package:') &&
          !importPath.startsWith('package:$projectName/') &&
          !importPath.startsWith('package:flutter/') &&
          !importPath.startsWith('package:flutter_enterprise_cli/')) {
        final pluginName =
            importPath.split('/')[0].replaceFirst('package:', '');
        if (pluginName != 'flutter' && pluginName != 'dart') {
          pluginsToAdd.add(pluginName);
        }
      }
    }
  }

  if (pluginsToAdd.isNotEmpty) {
    print('📦 Installing required external plugins...');
    final flutterCmd = Platform.isWindows ? 'flutter.bat' : 'flutter';
    final args = ['pub', 'add'];
    args.addAll(pluginsToAdd);
    await Process.run(flutterCmd, args,
        workingDirectory: projectPath, runInShell: true);
  }

  print('✅ Successfully added $count utilities and dependencies.');
}