|
| 1 | +// Copyright 2013 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 | +import '../environment.dart'; |
| 13 | +import '../flutter_project_metadata.dart'; |
| 14 | + |
| 15 | +import '../manifest.dart'; |
| 16 | +import '../update_locks.dart'; |
| 17 | +import '../utils.dart'; |
| 18 | + |
| 19 | +/// Migrate subcommand that checks the migrate working directory for unresolved conflicts and |
| 20 | +/// applies the staged changes to the project. |
| 21 | +class MigrateApplyCommand extends MigrateCommand { |
| 22 | + MigrateApplyCommand({ |
| 23 | + bool verbose = false, |
| 24 | + required this.logger, |
| 25 | + required this.fileSystem, |
| 26 | + required this.terminal, |
| 27 | + required ProcessManager processManager, |
| 28 | + }) : _verbose = verbose, |
| 29 | + _processManager = processManager, |
| 30 | + migrateUtils = MigrateUtils( |
| 31 | + logger: logger, |
| 32 | + fileSystem: fileSystem, |
| 33 | + processManager: processManager, |
| 34 | + ) { |
| 35 | + argParser.addOption( |
| 36 | + 'staging-directory', |
| 37 | + help: 'Specifies the custom migration working directory used to stage ' |
| 38 | + 'and edit proposed changes. This path can be absolute or relative ' |
| 39 | + 'to the flutter project root. This defaults to ' |
| 40 | + '`$kDefaultMigrateStagingDirectoryName`', |
| 41 | + valueHelp: 'path', |
| 42 | + ); |
| 43 | + argParser.addOption( |
| 44 | + 'project-directory', |
| 45 | + help: 'The root directory of the flutter project. This defaults to the ' |
| 46 | + 'current working directory if omitted.', |
| 47 | + valueHelp: 'path', |
| 48 | + ); |
| 49 | + argParser.addFlag( |
| 50 | + 'force', |
| 51 | + abbr: 'f', |
| 52 | + help: 'Ignore unresolved merge conflicts and uncommitted changes and ' |
| 53 | + 'apply staged changes by force.', |
| 54 | + ); |
| 55 | + argParser.addFlag( |
| 56 | + 'keep-working-directory', |
| 57 | + help: 'Do not delete the working directory.', |
| 58 | + ); |
| 59 | + argParser.addFlag( |
| 60 | + 'flutter-subcommand', |
| 61 | + help: |
| 62 | + 'Enable when using the flutter tool as a subcommand. This changes the ' |
| 63 | + 'wording of log messages to indicate the correct suggested commands to use.', |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + final bool _verbose; |
| 68 | + |
| 69 | + final ProcessManager _processManager; |
| 70 | + |
| 71 | + final Logger logger; |
| 72 | + |
| 73 | + final FileSystem fileSystem; |
| 74 | + |
| 75 | + final Terminal terminal; |
| 76 | + |
| 77 | + final MigrateUtils migrateUtils; |
| 78 | + |
| 79 | + @override |
| 80 | + final String name = 'apply'; |
| 81 | + |
| 82 | + @override |
| 83 | + final String description = r'Accepts the changes produced by `$ flutter ' |
| 84 | + 'migrate start` and copies the changed files into ' |
| 85 | + 'your project files. All merge conflicts should ' |
| 86 | + 'be resolved before apply will complete ' |
| 87 | + 'successfully. If conflicts still exist, this ' |
| 88 | + 'command will print the remaining conflicted files.'; |
| 89 | + |
| 90 | + @override |
| 91 | + Future<CommandResult> runCommand() async { |
| 92 | + final String? projectDirectory = stringArg('project-directory'); |
| 93 | + final FlutterProjectFactory flutterProjectFactory = FlutterProjectFactory(); |
| 94 | + final FlutterProject project = projectDirectory == null |
| 95 | + ? FlutterProject.current(fileSystem) |
| 96 | + : flutterProjectFactory |
| 97 | + .fromDirectory(fileSystem.directory(projectDirectory)); |
| 98 | + final FlutterToolsEnvironment environment = |
| 99 | + await FlutterToolsEnvironment.initializeFlutterToolsEnvironment( |
| 100 | + _processManager, logger); |
| 101 | + final bool isSubcommand = boolArg('flutter-subcommand') ?? false; |
| 102 | + |
| 103 | + if (!await gitRepoExists(project.directory.path, logger, migrateUtils)) { |
| 104 | + logger.printStatus('No git repo found. Please run in a project with an ' |
| 105 | + 'initialized git repo or initialize one with:'); |
| 106 | + printCommandText('git init', logger, standalone: null); |
| 107 | + return const CommandResult(ExitStatus.fail); |
| 108 | + } |
| 109 | + |
| 110 | + final bool force = boolArg('force') ?? false; |
| 111 | + |
| 112 | + Directory stagingDirectory = |
| 113 | + project.directory.childDirectory(kDefaultMigrateStagingDirectoryName); |
| 114 | + final String? customStagingDirectoryPath = stringArg('staging-directory'); |
| 115 | + if (customStagingDirectoryPath != null) { |
| 116 | + if (fileSystem.path.isAbsolute(customStagingDirectoryPath)) { |
| 117 | + stagingDirectory = fileSystem.directory(customStagingDirectoryPath); |
| 118 | + } else { |
| 119 | + stagingDirectory = |
| 120 | + project.directory.childDirectory(customStagingDirectoryPath); |
| 121 | + } |
| 122 | + } |
| 123 | + if (!stagingDirectory.existsSync()) { |
| 124 | + logger.printStatus( |
| 125 | + 'No migration in progress at $stagingDirectory. Please run:'); |
| 126 | + printCommandText('start', logger, standalone: !isSubcommand); |
| 127 | + return const CommandResult(ExitStatus.fail); |
| 128 | + } |
| 129 | + |
| 130 | + final File manifestFile = |
| 131 | + MigrateManifest.getManifestFileFromDirectory(stagingDirectory); |
| 132 | + final MigrateManifest manifest = MigrateManifest.fromFile(manifestFile); |
| 133 | + if (!checkAndPrintMigrateStatus(manifest, stagingDirectory, |
| 134 | + warnConflict: true, logger: logger) && |
| 135 | + !force) { |
| 136 | + logger.printStatus( |
| 137 | + 'Conflicting files found. Resolve these conflicts and try again.'); |
| 138 | + logger.printStatus('Guided conflict resolution wizard:'); |
| 139 | + printCommandText('resolve-conflicts', logger, standalone: !isSubcommand); |
| 140 | + return const CommandResult(ExitStatus.fail); |
| 141 | + } |
| 142 | + |
| 143 | + if (await hasUncommittedChanges( |
| 144 | + project.directory.path, logger, migrateUtils) && |
| 145 | + !force) { |
| 146 | + return const CommandResult(ExitStatus.fail); |
| 147 | + } |
| 148 | + |
| 149 | + logger.printStatus('Applying migration.'); |
| 150 | + // Copy files from working directory to project root |
| 151 | + final List<String> allFilesToCopy = <String>[]; |
| 152 | + allFilesToCopy.addAll(manifest.mergedFiles); |
| 153 | + allFilesToCopy.addAll(manifest.conflictFiles); |
| 154 | + allFilesToCopy.addAll(manifest.addedFiles); |
| 155 | + if (allFilesToCopy.isNotEmpty && _verbose) { |
| 156 | + logger.printStatus('Modifying ${allFilesToCopy.length} files.', |
| 157 | + indent: 2); |
| 158 | + } |
| 159 | + for (final String localPath in allFilesToCopy) { |
| 160 | + if (_verbose) { |
| 161 | + logger.printStatus('Writing $localPath'); |
| 162 | + } |
| 163 | + final File workingFile = stagingDirectory.childFile(localPath); |
| 164 | + final File targetFile = project.directory.childFile(localPath); |
| 165 | + if (!workingFile.existsSync()) { |
| 166 | + continue; |
| 167 | + } |
| 168 | + |
| 169 | + if (!targetFile.existsSync()) { |
| 170 | + targetFile.createSync(recursive: true); |
| 171 | + } |
| 172 | + try { |
| 173 | + targetFile.writeAsStringSync(workingFile.readAsStringSync(), |
| 174 | + flush: true); |
| 175 | + } on FileSystemException { |
| 176 | + targetFile.writeAsBytesSync(workingFile.readAsBytesSync(), flush: true); |
| 177 | + } |
| 178 | + } |
| 179 | + // Delete files slated for deletion. |
| 180 | + if (manifest.deletedFiles.isNotEmpty) { |
| 181 | + logger.printStatus('Deleting ${manifest.deletedFiles.length} files.', |
| 182 | + indent: 2); |
| 183 | + } |
| 184 | + for (final String localPath in manifest.deletedFiles) { |
| 185 | + final File targetFile = project.directory.childFile(localPath); |
| 186 | + targetFile.deleteSync(); |
| 187 | + } |
| 188 | + |
| 189 | + // Update the migrate config files to reflect latest migration. |
| 190 | + if (_verbose) { |
| 191 | + logger.printStatus('Updating .migrate_configs'); |
| 192 | + } |
| 193 | + final FlutterProjectMetadata metadata = FlutterProjectMetadata( |
| 194 | + project.directory.childFile('.metadata'), logger); |
| 195 | + |
| 196 | + final String currentGitHash = |
| 197 | + environment.getString('FlutterVersion.frameworkRevision') ?? ''; |
| 198 | + metadata.migrateConfig.populate( |
| 199 | + projectDirectory: project.directory, |
| 200 | + currentRevision: currentGitHash, |
| 201 | + logger: logger, |
| 202 | + ); |
| 203 | + |
| 204 | + // Clean up the working directory |
| 205 | + final bool keepWorkingDirectory = |
| 206 | + boolArg('keep-working-directory') ?? false; |
| 207 | + if (!keepWorkingDirectory) { |
| 208 | + stagingDirectory.deleteSync(recursive: true); |
| 209 | + } |
| 210 | + |
| 211 | + // Detect pub dependency locking. Run flutter pub upgrade --major-versions |
| 212 | + await updatePubspecDependencies(project, migrateUtils, logger, terminal); |
| 213 | + |
| 214 | + // Detect gradle lockfiles in android directory. Delete lockfiles and regenerate with ./gradlew tasks (any gradle task that requires a build). |
| 215 | + await updateGradleDependencyLocking( |
| 216 | + project, migrateUtils, logger, terminal, _verbose, fileSystem); |
| 217 | + |
| 218 | + logger.printStatus('Migration complete. You may use commands like `git ' |
| 219 | + 'status`, `git diff` and `git restore <file>` to continue ' |
| 220 | + 'working with the migrated files.'); |
| 221 | + return const CommandResult(ExitStatus.success); |
| 222 | + } |
| 223 | +} |
0 commit comments