|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:args/command_runner.dart'; |
| 5 | +import 'package:stacked_cli/src/constants/command_constants.dart'; |
| 6 | +import 'package:stacked_cli/src/constants/message_constants.dart'; |
| 7 | +import 'package:stacked_cli/src/locator.dart'; |
| 8 | +import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart'; |
| 9 | +import 'package:stacked_cli/src/services/colorized_log_service.dart'; |
| 10 | +import 'package:stacked_cli/src/services/config_service.dart'; |
| 11 | +import 'package:stacked_cli/src/services/file_service.dart'; |
| 12 | +import 'package:stacked_cli/src/services/posthog_service.dart'; |
| 13 | +import 'package:stacked_cli/src/services/process_service.dart'; |
| 14 | +import 'package:stacked_cli/src/services/pubspec_service.dart'; |
| 15 | +import 'package:stacked_cli/src/services/template_service.dart'; |
| 16 | +import 'package:stacked_cli/src/templates/compiled_templates.dart'; |
| 17 | +import 'package:stacked_cli/src/templates/template_constants.dart'; |
| 18 | + |
| 19 | +class DeleteBottomsheetCommand extends Command with ProjectStructureValidator { |
| 20 | + final _configService = locator<ConfigService>(); |
| 21 | + final _fileService = locator<FileService>(); |
| 22 | + final _log = locator<ColorizedLogService>(); |
| 23 | + final _processService = locator<ProcessService>(); |
| 24 | + final _pubspecService = locator<PubspecService>(); |
| 25 | + final _templateService = locator<TemplateService>(); |
| 26 | + final _analyticsService = locator<PosthogService>(); |
| 27 | + |
| 28 | + @override |
| 29 | + String get description => |
| 30 | + 'Deletes a bottomsheet with all associated files and makes necessary code changes for deleting a bottomsheet.'; |
| 31 | + |
| 32 | + @override |
| 33 | + String get name => kTemplateNameBottomSheet; |
| 34 | + |
| 35 | + DeleteBottomsheetCommand() { |
| 36 | + argParser |
| 37 | + ..addFlag( |
| 38 | + ksExcludeRoute, |
| 39 | + defaultsTo: false, |
| 40 | + help: kCommandHelpExcludeRoute, |
| 41 | + ) |
| 42 | + ..addOption( |
| 43 | + ksConfigPath, |
| 44 | + abbr: 'c', |
| 45 | + help: kCommandHelpConfigFilePath, |
| 46 | + ) |
| 47 | + ..addOption( |
| 48 | + ksLineLength, |
| 49 | + abbr: 'l', |
| 50 | + help: kCommandHelpLineLength, |
| 51 | + valueHelp: '80', |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + @override |
| 56 | + Future<void> run() async { |
| 57 | + try { |
| 58 | + final workingDirectory = |
| 59 | + argResults!.rest.length > 1 ? argResults!.rest[1] : null; |
| 60 | + final bottomsheetName = argResults!.rest.first; |
| 61 | + await _configService.composeAndLoadConfigFile( |
| 62 | + configFilePath: argResults![ksConfigPath], |
| 63 | + projectPath: workingDirectory, |
| 64 | + ); |
| 65 | + _processService.formattingLineLength = argResults?[ksLineLength]; |
| 66 | + await _pubspecService.initialise(workingDirectory: workingDirectory); |
| 67 | + |
| 68 | + await validateStructure(outputPath: workingDirectory); |
| 69 | + await _deletebottomsheet( |
| 70 | + outputPath: workingDirectory, bottomsheetName: bottomsheetName); |
| 71 | + await _removebottomsheetFromDependency( |
| 72 | + outputPath: workingDirectory, bottomsheetName: bottomsheetName); |
| 73 | + await _processService.runBuildRunner(workingDirectory: workingDirectory); |
| 74 | + await _analyticsService.deleteBottomsheetEvent( |
| 75 | + name: argResults!.rest.first, |
| 76 | + arguments: argResults!.arguments, |
| 77 | + ); |
| 78 | + } on PathNotFoundException catch (e) { |
| 79 | + _log.error(message: e.toString()); |
| 80 | + unawaited(_analyticsService.logExceptionEvent( |
| 81 | + runtimeType: e.runtimeType.toString(), |
| 82 | + message: e.toString(), |
| 83 | + )); |
| 84 | + } catch (e, s) { |
| 85 | + _log.error(message: e.toString()); |
| 86 | + unawaited(_analyticsService.logExceptionEvent( |
| 87 | + runtimeType: e.runtimeType.toString(), |
| 88 | + message: e.toString(), |
| 89 | + stackTrace: s.toString(), |
| 90 | + )); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// It deletes the bottomsheet files |
| 95 | + /// |
| 96 | + /// Args: |
| 97 | + /// |
| 98 | + /// `outputPath` (String): The path to the output folder. |
| 99 | + /// |
| 100 | + /// `bottomsheetName` (String): The name of the bottomsheet. |
| 101 | + Future<void> _deletebottomsheet( |
| 102 | + {String? outputPath, required String bottomsheetName}) async { |
| 103 | + /// Deleting the bottomsheet folder. |
| 104 | + String directoryPath = _templateService.getTemplateOutputPath( |
| 105 | + inputTemplatePath: 'lib/ui/bottom_sheets/generic', |
| 106 | + name: bottomsheetName, |
| 107 | + outputFolder: outputPath, |
| 108 | + ); |
| 109 | + await _fileService.deleteFolder(directoryPath: directoryPath); |
| 110 | + |
| 111 | + //Delete test file for bottomsheet |
| 112 | + final filePath = _templateService.getTemplateOutputPath( |
| 113 | + inputTemplatePath: kBottomSheetEmptyTemplateGenericSheetModelTestPath, |
| 114 | + name: bottomsheetName, |
| 115 | + outputFolder: outputPath, |
| 116 | + ); |
| 117 | + |
| 118 | + final fileExists = await _fileService.fileExists(filePath: filePath); |
| 119 | + if (fileExists) { |
| 120 | + await _fileService.deleteFile(filePath: filePath); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /// It removes the bottomsheet from [app.dart] |
| 125 | + /// |
| 126 | + /// Args: |
| 127 | + /// |
| 128 | + /// `outputPath` (String): The path to the output folder. |
| 129 | + /// |
| 130 | + /// `bottomsheetName` (String): The name of the bottomsheet. |
| 131 | + Future<void> _removebottomsheetFromDependency( |
| 132 | + {String? outputPath, required String bottomsheetName}) async { |
| 133 | + String filePath = _templateService.getTemplateOutputPath( |
| 134 | + inputTemplatePath: kAppMobileTemplateAppPath, |
| 135 | + name: bottomsheetName, |
| 136 | + outputFolder: outputPath, |
| 137 | + ); |
| 138 | + await _fileService.removeSpecificFileLines( |
| 139 | + filePath: filePath, |
| 140 | + removedContent: bottomsheetName, |
| 141 | + type: "sheet", |
| 142 | + ); |
| 143 | + } |
| 144 | +} |
0 commit comments