createWidgetProcess function
Implementation
Future<void> createWidgetProcess(
String widgetName, String destinationPath, bool isCommon) async {
String destinationFilePath = isCommon
? '$destinationPath/$widgetName.dart'
: '$destinationPath/widgets/$widgetName.dart';
if (doesFileExist(destinationFilePath)) {
print(
'File at $destinationFilePath already exists. Please choose a different widget name');
exit(1);
}
String currentFilePath = Platform.script.toFilePath();
List<String> pathSegments = currentFilePath.split(Platform.pathSeparator);
if (pathSegments.length > 2) {
pathSegments.removeRange(pathSegments.length - 5, pathSegments.length);
}
String projectDirectoryPath = pathSegments.join(Platform.pathSeparator);
String sourceFolderPath =
'$projectDirectoryPath/lib/create_widget/basic_widget';
grantPermissions(sourceFolderPath);
if (!Directory(sourceFolderPath).existsSync()) {
print('Error: $sourceFolderPath folder does not exist');
return;
}
try {
destinationPath =
isCommon ? '$destinationPath' : '$destinationPath/widgets';
await copyFolder(sourceFolderPath, destinationPath, widgetName);
processFiles(destinationPath, '/*', '', false);
processFiles(destinationPath, '*/', '', false);
renameFilesWithSubstring(destinationPath, 'basic_widget', widgetName);
processFiles(destinationPath, 'BasicWidget', widgetName, true);
String? name = getPubspecInfo('name');
if (name != null) {
processFiles(destinationPath, 'flutter_clean_architecture', name, false);
}
print('Widget $widgetName created at $destinationPath');
} catch (e) {
print('Error : $e');
}
}