Skip to content

Commit 94014cb

Browse files
authored
[flutter_migrate] Reland apply, abandon, and status commands and add environment.dart (#2723)
* Checkout commands files * fix licences and tests * Skip apply test on old flutter version * Formatting * Fix version booleans * Move flutter version test tcheck first * Working tests * Formatting * Env test * Docs and fix windows test * Null safe, extra test * Improve cleanliness checking * Regex and fake process manager * Formatting * Change env test contents * format * Nits
1 parent f3ac125 commit 94014cb

File tree

10 files changed

+1320
-1
lines changed

10 files changed

+1320
-1
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
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:
45+
'Delete the migrate working directory without asking for confirmation.',
46+
);
47+
argParser.addFlag(
48+
'flutter-subcommand',
49+
help:
50+
'Enable when using the flutter tool as a subcommand. This changes the '
51+
'wording of log messages to indicate the correct suggested commands to use.',
52+
);
53+
}
54+
55+
final Logger logger;
56+
57+
final FileSystem fileSystem;
58+
59+
final Terminal terminal;
60+
61+
final MigrateUtils migrateUtils;
62+
63+
@override
64+
final String name = 'abandon';
65+
66+
@override
67+
final String description =
68+
'Deletes the current active migration working directory.';
69+
70+
@override
71+
Future<CommandResult> runCommand() async {
72+
final String? projectDirectory = stringArg('project-directory');
73+
final FlutterProjectFactory flutterProjectFactory = FlutterProjectFactory();
74+
final FlutterProject project = projectDirectory == null
75+
? FlutterProject.current(fileSystem)
76+
: flutterProjectFactory
77+
.fromDirectory(fileSystem.directory(projectDirectory));
78+
final bool isSubcommand = boolArg('flutter-subcommand') ?? false;
79+
Directory stagingDirectory =
80+
project.directory.childDirectory(kDefaultMigrateStagingDirectoryName);
81+
final String? customStagingDirectoryPath = stringArg('staging-directory');
82+
if (customStagingDirectoryPath != null) {
83+
if (fileSystem.path.isAbsolute(customStagingDirectoryPath)) {
84+
stagingDirectory = fileSystem.directory(customStagingDirectoryPath);
85+
} else {
86+
stagingDirectory =
87+
project.directory.childDirectory(customStagingDirectoryPath);
88+
}
89+
if (!stagingDirectory.existsSync()) {
90+
logger.printError(
91+
'Provided staging directory `$customStagingDirectoryPath` '
92+
'does not exist or is not valid.');
93+
return const CommandResult(ExitStatus.fail);
94+
}
95+
}
96+
if (!stagingDirectory.existsSync()) {
97+
logger
98+
.printStatus('No migration in progress. Start a new migration with:');
99+
printCommandText('start', logger, standalone: !isSubcommand);
100+
return const CommandResult(ExitStatus.fail);
101+
}
102+
103+
logger.printStatus('\nAbandoning the existing migration will delete the '
104+
'migration staging directory at ${stagingDirectory.path}');
105+
final bool force = boolArg('force') ?? false;
106+
if (!force) {
107+
String selection = 'y';
108+
terminal.usesTerminalUi = true;
109+
try {
110+
selection = await terminal.promptForCharInput(
111+
<String>['y', 'n'],
112+
logger: logger,
113+
prompt:
114+
'Are you sure you wish to continue with abandoning? (y)es, (N)o',
115+
defaultChoiceIndex: 1,
116+
);
117+
} on StateError catch (e) {
118+
logger.printError(
119+
e.message,
120+
indent: 0,
121+
);
122+
}
123+
if (selection != 'y') {
124+
return const CommandResult(ExitStatus.success);
125+
}
126+
}
127+
128+
try {
129+
stagingDirectory.deleteSync(recursive: true);
130+
} on FileSystemException catch (e) {
131+
logger.printError('Deletion failed with: $e');
132+
logger.printError(
133+
'Please manually delete the staging directory at `${stagingDirectory.path}`');
134+
}
135+
136+
logger.printStatus('\nAbandon complete. Start a new migration with:');
137+
printCommandText('start', logger, standalone: !isSubcommand);
138+
return const CommandResult(ExitStatus.success);
139+
}
140+
}
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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

Comments
 (0)