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 packageName = 'jt_flutter_cli';
String version = getPackageVersion(packageName);
String dependencyName = '$packageName-$version';
String pubCachePath = Platform.environment['PUB_CACHE'] ??
'${Platform.environment['HOME']}/.pub-cache';
String dependencyPath =
path.join(pubCachePath, 'hosted', 'pub.dev', dependencyName);
if (!Directory(dependencyPath).existsSync()) {
print('Dependency $dependencyName not found in .pub-cache directory');
return;
}
String dependencyLibPath = path.join(dependencyPath, 'lib');
if (!Directory(dependencyLibPath).existsSync()) {
print('Dependency $dependencyName does not contain a lib folder');
return;
}
String sourceFolderPath = '$dependencyLibPath/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');
}
}