installWidgetsModule function
Implementation
Future<void> installWidgetsModule(String projectPath,
{bool force = false}) async {
print('📦 Installing common UI widgets and dependencies...');
final projectName = getProjectName(projectPath);
final Set<String> resolvedFiles = {};
final Set<String> pluginsToAdd = {};
void resolveAndWrite(String relativePath) {
if (resolvedFiles.contains(relativePath)) return;
final templateFunc = widgetsTemplatesMap[relativePath];
if (templateFunc == null) return;
resolvedFiles.add(relativePath);
final content = templateFunc(projectName);
final filePath = p.join(projectPath, 'lib', relativePath);
safeCreateDir(File(filePath).parent.path);
// Force write core theme/utils for consistent branding
if (relativePath.contains('theme/') ||
relativePath.contains('core/utils/responsive_size')) {
forceWriteFile(filePath, content);
} else {
safeWriteFile(filePath, content,
overwrite: force, projectPath: projectPath);
}
// 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)!;
// Handle internal dependencies (other templates)
if (importPath.startsWith('package:$projectName/')) {
final relativeImportPath =
importPath.replaceFirst('package:$projectName/', '');
if (widgetsTemplatesMap.containsKey(relativeImportPath)) {
resolveAndWrite(relativeImportPath);
}
} else if (!importPath.startsWith('package:') &&
!importPath.startsWith('dart:')) {
final dirPath = p.dirname(relativePath);
final joinedPath =
p.normalize(p.join(dirPath, importPath)).replaceAll(r'\', '/');
if (widgetsTemplatesMap.containsKey(joinedPath)) {
resolveAndWrite(joinedPath);
}
}
// Handle external plugin dependencies
else if (importPath.startsWith('package:') &&
!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);
}
}
}
}
// Start by installing all presentation widgets
final entryPoints = widgetsTemplatesMap.keys
.where((path) => path.startsWith('presentation/widgets/'))
.toList();
for (final path in entryPoints) {
resolveAndWrite(path);
}
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(
'✅ Required plugins ${pluginsToAdd.join(", ")} installed successfully.');
}
print(
'✅ Successfully added ${resolvedFiles.length} files and core dependencies.');
}