From 683d295a5a0e69a2cd33fc0aac63a1530001d4b6 Mon Sep 17 00:00:00 2001 From: Neer Pathak <84756607+Neer-Pathak@users.noreply.github.com> Date: Mon, 29 Apr 2024 17:56:48 +0530 Subject: [PATCH] feat: add delete bottom_sheet command (#44) Added delete bottom_sheet command --- .../delete/delete_bottomsheet_command.dart | 144 ++++++++++++++++++ lib/src/commands/delete/delete_command.dart | 2 + lib/src/services/posthog_service.dart | 14 ++ 3 files changed, 160 insertions(+) create mode 100644 lib/src/commands/delete/delete_bottomsheet_command.dart diff --git a/lib/src/commands/delete/delete_bottomsheet_command.dart b/lib/src/commands/delete/delete_bottomsheet_command.dart new file mode 100644 index 0000000..880576d --- /dev/null +++ b/lib/src/commands/delete/delete_bottomsheet_command.dart @@ -0,0 +1,144 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:args/command_runner.dart'; +import 'package:stacked_cli/src/constants/command_constants.dart'; +import 'package:stacked_cli/src/constants/message_constants.dart'; +import 'package:stacked_cli/src/locator.dart'; +import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart'; +import 'package:stacked_cli/src/services/colorized_log_service.dart'; +import 'package:stacked_cli/src/services/config_service.dart'; +import 'package:stacked_cli/src/services/file_service.dart'; +import 'package:stacked_cli/src/services/posthog_service.dart'; +import 'package:stacked_cli/src/services/process_service.dart'; +import 'package:stacked_cli/src/services/pubspec_service.dart'; +import 'package:stacked_cli/src/services/template_service.dart'; +import 'package:stacked_cli/src/templates/compiled_templates.dart'; +import 'package:stacked_cli/src/templates/template_constants.dart'; + +class DeleteBottomsheetCommand extends Command with ProjectStructureValidator { + final _configService = locator(); + final _fileService = locator(); + final _log = locator(); + final _processService = locator(); + final _pubspecService = locator(); + final _templateService = locator(); + final _analyticsService = locator(); + + @override + String get description => + 'Deletes a bottomsheet with all associated files and makes necessary code changes for deleting a bottomsheet.'; + + @override + String get name => kTemplateNameBottomSheet; + + DeleteBottomsheetCommand() { + argParser + ..addFlag( + ksExcludeRoute, + defaultsTo: false, + help: kCommandHelpExcludeRoute, + ) + ..addOption( + ksConfigPath, + abbr: 'c', + help: kCommandHelpConfigFilePath, + ) + ..addOption( + ksLineLength, + abbr: 'l', + help: kCommandHelpLineLength, + valueHelp: '80', + ); + } + + @override + Future run() async { + try { + final workingDirectory = + argResults!.rest.length > 1 ? argResults!.rest[1] : null; + final bottomsheetName = argResults!.rest.first; + await _configService.composeAndLoadConfigFile( + configFilePath: argResults![ksConfigPath], + projectPath: workingDirectory, + ); + _processService.formattingLineLength = argResults?[ksLineLength]; + await _pubspecService.initialise(workingDirectory: workingDirectory); + + await validateStructure(outputPath: workingDirectory); + await _deletebottomsheet( + outputPath: workingDirectory, bottomsheetName: bottomsheetName); + await _removebottomsheetFromDependency( + outputPath: workingDirectory, bottomsheetName: bottomsheetName); + await _processService.runBuildRunner(workingDirectory: workingDirectory); + await _analyticsService.deleteBottomsheetEvent( + name: argResults!.rest.first, + arguments: argResults!.arguments, + ); + } on PathNotFoundException catch (e) { + _log.error(message: e.toString()); + unawaited(_analyticsService.logExceptionEvent( + runtimeType: e.runtimeType.toString(), + message: e.toString(), + )); + } catch (e, s) { + _log.error(message: e.toString()); + unawaited(_analyticsService.logExceptionEvent( + runtimeType: e.runtimeType.toString(), + message: e.toString(), + stackTrace: s.toString(), + )); + } + } + + /// It deletes the bottomsheet files + /// + /// Args: + /// + /// `outputPath` (String): The path to the output folder. + /// + /// `bottomsheetName` (String): The name of the bottomsheet. + Future _deletebottomsheet( + {String? outputPath, required String bottomsheetName}) async { + /// Deleting the bottomsheet folder. + String directoryPath = _templateService.getTemplateOutputPath( + inputTemplatePath: 'lib/ui/bottom_sheets/generic', + name: bottomsheetName, + outputFolder: outputPath, + ); + await _fileService.deleteFolder(directoryPath: directoryPath); + + //Delete test file for bottomsheet + final filePath = _templateService.getTemplateOutputPath( + inputTemplatePath: kBottomSheetEmptyTemplateGenericSheetModelTestPath, + name: bottomsheetName, + outputFolder: outputPath, + ); + + final fileExists = await _fileService.fileExists(filePath: filePath); + if (fileExists) { + await _fileService.deleteFile(filePath: filePath); + } + } + + /// It removes the bottomsheet from [app.dart] + /// + /// Args: + /// + /// `outputPath` (String): The path to the output folder. + /// + /// `bottomsheetName` (String): The name of the bottomsheet. + Future _removebottomsheetFromDependency( + {String? outputPath, required String bottomsheetName}) async { + String filePath = _templateService.getTemplateOutputPath( + inputTemplatePath: kAppMobileTemplateAppPath, + name: bottomsheetName, + outputFolder: outputPath, + ); + await _fileService.removeSpecificFileLines( + filePath: filePath, + removedContent: bottomsheetName, + type: "sheet", + ); + } +} diff --git a/lib/src/commands/delete/delete_command.dart b/lib/src/commands/delete/delete_command.dart index 15c13cb..9f06b33 100644 --- a/lib/src/commands/delete/delete_command.dart +++ b/lib/src/commands/delete/delete_command.dart @@ -1,4 +1,5 @@ import 'package:args/command_runner.dart'; +import 'package:stacked_cli/src/commands/delete/delete_bottomsheet_command.dart'; import 'package:stacked_cli/src/commands/delete/delete_dialog_command.dart'; import 'package:stacked_cli/src/commands/delete/delete_service_command.dart'; @@ -18,5 +19,6 @@ class DeleteCommand extends Command { addSubcommand(DeleteServiceCommand()); addSubcommand(DeleteViewCommand()); addSubcommand(DeleteDialogCommand()); + addSubcommand(DeleteBottomsheetCommand()); } } diff --git a/lib/src/services/posthog_service.dart b/lib/src/services/posthog_service.dart index 47db7ef..77fc8b8 100644 --- a/lib/src/services/posthog_service.dart +++ b/lib/src/services/posthog_service.dart @@ -183,6 +183,20 @@ class PosthogService { ); } + // Below event added if needed + Future deleteBottomsheetEvent({ + required String name, + required List arguments, + }) async { + await _capture( + event: "Delete Bottom Sheet", + properties: { + "name": name, + "arguments": arguments, + }, + ); + } + Future deleteServiceEvent({ required String name, required List arguments,