|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:process/process.dart'; |
| 6 | + |
| 7 | +import '../base/command.dart'; |
| 8 | +import '../base/file_system.dart'; |
| 9 | +import '../base/logger.dart'; |
| 10 | +import '../base/project.dart'; |
| 11 | +import '../base/terminal.dart'; |
| 12 | + |
| 13 | +import '../utils.dart'; |
| 14 | + |
| 15 | +/// Abandons the existing migration by deleting the migrate working directory. |
| 16 | +class MigrateAbandonCommand extends MigrateCommand { |
| 17 | + MigrateAbandonCommand({ |
| 18 | + required this.logger, |
| 19 | + required this.fileSystem, |
| 20 | + required this.terminal, |
| 21 | + required ProcessManager processManager, |
| 22 | + }) : migrateUtils = MigrateUtils( |
| 23 | + logger: logger, |
| 24 | + fileSystem: fileSystem, |
| 25 | + processManager: processManager, |
| 26 | + ) { |
| 27 | + argParser.addOption( |
| 28 | + 'staging-directory', |
| 29 | + help: 'Specifies the custom migration working directory used to stage ' |
| 30 | + 'and edit proposed changes. This path can be absolute or relative ' |
| 31 | + 'to the flutter project root. This defaults to ' |
| 32 | + '`$kDefaultMigrateStagingDirectoryName`', |
| 33 | + valueHelp: 'path', |
| 34 | + ); |
| 35 | + argParser.addOption( |
| 36 | + 'project-directory', |
| 37 | + help: 'The root directory of the flutter project. This defaults to the ' |
| 38 | + 'current working directory if omitted.', |
| 39 | + valueHelp: 'path', |
| 40 | + ); |
| 41 | + argParser.addFlag( |
| 42 | + 'force', |
| 43 | + abbr: 'f', |
| 44 | + help: 'Delete the migrate working directory without asking for confirmation.', |
| 45 | + ); |
| 46 | + argParser.addFlag( |
| 47 | + 'flutter-subcommand', |
| 48 | + help: 'Enable when using the flutter tool as a subcommand. This changes the ' |
| 49 | + 'wording of log messages to indicate the correct suggested commands to use.', |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + final Logger logger; |
| 54 | + |
| 55 | + final FileSystem fileSystem; |
| 56 | + |
| 57 | + final Terminal terminal; |
| 58 | + |
| 59 | + final MigrateUtils migrateUtils; |
| 60 | + |
| 61 | + @override |
| 62 | + final String name = 'abandon'; |
| 63 | + |
| 64 | + @override |
| 65 | + final String description = 'Deletes the current active migration working directory.'; |
| 66 | + |
| 67 | + @override |
| 68 | + Future<CommandResult> runCommand() async { |
| 69 | + final String? projectDirectory = stringArg('project-directory'); |
| 70 | + final FlutterProjectFactory flutterProjectFactory = FlutterProjectFactory(); |
| 71 | + final FlutterProject project = projectDirectory == null |
| 72 | + ? FlutterProject.current(fileSystem) |
| 73 | + : flutterProjectFactory.fromDirectory(fileSystem.directory(projectDirectory)); |
| 74 | + final bool isSubcommand = boolArg('flutter-subcommand') ?? false; |
| 75 | + Directory stagingDirectory = project.directory.childDirectory(kDefaultMigrateStagingDirectoryName); |
| 76 | + final String? customStagingDirectoryPath = stringArg('staging-directory'); |
| 77 | + if (customStagingDirectoryPath != null) { |
| 78 | + if (fileSystem.path.isAbsolute(customStagingDirectoryPath)) { |
| 79 | + stagingDirectory = fileSystem.directory(customStagingDirectoryPath); |
| 80 | + } else { |
| 81 | + stagingDirectory = project.directory.childDirectory(customStagingDirectoryPath); |
| 82 | + } |
| 83 | + if (!stagingDirectory.existsSync()) { |
| 84 | + logger.printError('Provided staging directory `$customStagingDirectoryPath` ' |
| 85 | + 'does not exist or is not valid.'); |
| 86 | + return const CommandResult(ExitStatus.fail); |
| 87 | + } |
| 88 | + } |
| 89 | + if (!stagingDirectory.existsSync()) { |
| 90 | + logger.printStatus('No migration in progress. Start a new migration with:'); |
| 91 | + printCommandText('start', logger, standalone: !isSubcommand); |
| 92 | + return const CommandResult(ExitStatus.fail); |
| 93 | + } |
| 94 | + |
| 95 | + logger.printStatus('\nAbandoning the existing migration will delete the ' |
| 96 | + 'migration staging directory at ${stagingDirectory.path}'); |
| 97 | + final bool force = boolArg('force') ?? false; |
| 98 | + if (!force) { |
| 99 | + String selection = 'y'; |
| 100 | + terminal.usesTerminalUi = true; |
| 101 | + try { |
| 102 | + selection = await terminal.promptForCharInput( |
| 103 | + <String>['y', 'n'], |
| 104 | + logger: logger, |
| 105 | + prompt: 'Are you sure you wish to continue with abandoning? (y)es, (N)o', |
| 106 | + defaultChoiceIndex: 1, |
| 107 | + ); |
| 108 | + } on StateError catch(e) { |
| 109 | + logger.printError( |
| 110 | + e.message, |
| 111 | + indent: 0, |
| 112 | + ); |
| 113 | + } |
| 114 | + if (selection != 'y') { |
| 115 | + return const CommandResult(ExitStatus.success); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + try { |
| 120 | + stagingDirectory.deleteSync(recursive: true); |
| 121 | + } on FileSystemException catch (e) { |
| 122 | + logger.printError('Deletion failed with: $e'); |
| 123 | + logger.printError('Please manually delete the staging directory at `${stagingDirectory.path}`'); |
| 124 | + } |
| 125 | + |
| 126 | + logger.printStatus('\nAbandon complete. Start a new migration with:'); |
| 127 | + printCommandText('start', logger, standalone: !isSubcommand); |
| 128 | + return const CommandResult(ExitStatus.success); |
| 129 | + } |
| 130 | +} |
0 commit comments