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