Skip to content

Commit

Permalink
Migrate abandon (#102789)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryQian authored Jun 6, 2022
1 parent febc6a1 commit e9a3cbf
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 4 deletions.
13 changes: 10 additions & 3 deletions packages/flutter_tools/lib/src/commands/migrate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,33 @@ import '../base/platform.dart';
import '../base/terminal.dart';
import '../migrate/migrate_utils.dart';
import '../runner/flutter_command.dart';
import 'migrate_abandon.dart';
import 'migrate_status.dart';

/// Base command for the migration tool.
class MigrateCommand extends FlutterCommand {
MigrateCommand({
bool verbose = false,
required bool verbose,
required this.logger,
// TODO(garyq): Add parameter in as they are needed for subcommands.
required FileSystem fileSystem,
required Terminal terminal,
required Platform platform,
required ProcessManager processManager,
}) {
// TODO(garyq): Add each subcommand back in as they land.
addSubcommand(MigrateStatusCommand(
verbose: verbose,
logger: logger,
fileSystem: fileSystem,
platform: platform,
processManager: processManager
));
addSubcommand(MigrateAbandonCommand(
logger: logger,
fileSystem: fileSystem,
terminal: terminal,
platform: platform,
processManager: processManager
));
}

final Logger logger;
Expand Down
134 changes: 134 additions & 0 deletions packages/flutter_tools/lib/src/commands/migrate_abandon.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:process/process.dart';

import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/platform.dart';
import '../base/terminal.dart';
import '../migrate/migrate_utils.dart';
import '../project.dart';
import '../runner/flutter_command.dart';
import 'migrate.dart';

/// Abandons the existing migration by deleting the migrate working directory.
class MigrateAbandonCommand extends FlutterCommand {
MigrateAbandonCommand({
required this.logger,
required this.fileSystem,
required this.terminal,
required Platform platform,
required ProcessManager processManager,
}) : migrateUtils = MigrateUtils(
logger: logger,
fileSystem: fileSystem,
platform: platform,
processManager: processManager,
) {
requiresPubspecYaml();
argParser.addOption(
'staging-directory',
help: 'Specifies the custom migration working directory used to stage '
'and edit proposed changes. This path can be absolute or relative '
'to the flutter project root. This defaults to '
'`$kDefaultMigrateStagingDirectoryName`',
valueHelp: 'path',
);
argParser.addOption(
'project-directory',
help: 'The root directory of the flutter project. This defaults to the '
'current working directory if omitted.',
valueHelp: 'path',
);
argParser.addFlag(
'force',
abbr: 'f',
help: 'Delete the migrate working directory without asking for confirmation.',
);
}

final Logger logger;

final FileSystem fileSystem;

final Terminal terminal;

final MigrateUtils migrateUtils;

@override
final String name = 'abandon';

@override
final String description = 'Deletes the current active migration working directory.';

@override
String get category => FlutterCommandCategory.project;

@override
Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{};

@override
Future<FlutterCommandResult> runCommand() async {
final String? projectDirectory = stringArg('project-directory');
final FlutterProjectFactory flutterProjectFactory = FlutterProjectFactory(logger: logger, fileSystem: fileSystem);
final FlutterProject project = projectDirectory == null
? FlutterProject.current()
: flutterProjectFactory.fromDirectory(fileSystem.directory(projectDirectory));
Directory stagingDirectory = project.directory.childDirectory(kDefaultMigrateStagingDirectoryName);
final String? customStagingDirectoryPath = stringArg('staging-directory');
if (customStagingDirectoryPath != null) {
if (fileSystem.path.isAbsolute(customStagingDirectoryPath)) {
stagingDirectory = fileSystem.directory(customStagingDirectoryPath);
} else {
stagingDirectory = project.directory.childDirectory(customStagingDirectoryPath);
}
if (!stagingDirectory.existsSync()) {
logger.printError('Provided staging directory `$customStagingDirectoryPath` '
'does not exist or is not valid.');
return const FlutterCommandResult(ExitStatus.fail);
}
}
if (!stagingDirectory.existsSync()) {
logger.printStatus('No migration in progress. Start a new migration with:');
printCommandText('flutter migrate start', logger);
return const FlutterCommandResult(ExitStatus.fail);
}

logger.printStatus('\nAbandoning the existing migration will delete the '
'migration staging directory at ${stagingDirectory.path}');
final bool force = boolArg('force') ?? false;
if (!force) {
String selection = 'y';
terminal.usesTerminalUi = true;
try {
selection = await terminal.promptForCharInput(
<String>['y', 'n'],
logger: logger,
prompt: 'Are you sure you wish to continue with abandoning? (y)es, (N)o',
defaultChoiceIndex: 1,
);
} on StateError catch(e) {
logger.printError(
e.message,
indent: 0,
);
}
if (selection != 'y') {
return const FlutterCommandResult(ExitStatus.success);
}
}

try {
stagingDirectory.deleteSync(recursive: true);
} on FileSystemException catch (e) {
logger.printError('Deletion failed with: $e');
logger.printError('Please manually delete the staging directory at `${stagingDirectory.path}`');
}

logger.printStatus('\nAbandon complete. Start a new migration with:');
printCommandText('flutter migrate start', logger);
return const FlutterCommandResult(ExitStatus.success);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/migrate.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/migrate/migrate_utils.dart';

import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/test_flutter_command_runner.dart';

void main() {
FileSystem fileSystem;
BufferLogger logger;
Platform platform;
Terminal terminal;
ProcessManager processManager;
Directory appDir;

setUp(() {
fileSystem = globals.localFileSystem;
appDir = fileSystem.systemTempDirectory.createTempSync('apptestdir');
logger = BufferLogger.test();
platform = FakePlatform();
terminal = Terminal.test();
processManager = globals.processManager;
});

setUpAll(() {
Cache.disableLocking();
});

tearDown(() async {
tryToDelete(appDir);
});

testUsingContext('abandon deletes staging directory', () async {
final MigrateCommand command = MigrateCommand(
verbose: true,
logger: logger,
fileSystem: fileSystem,
terminal: terminal,
platform: platform,
processManager: processManager,
);
final Directory stagingDir = appDir.childDirectory(kDefaultMigrateStagingDirectoryName);
appDir.childFile('lib/main.dart').createSync(recursive: true);
final File pubspecOriginal = appDir.childFile('pubspec.yaml');
pubspecOriginal.createSync();
pubspecOriginal.writeAsStringSync('''
name: originalname
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: '>=2.18.0-58.0.dev <3.0.0'
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true''', flush: true);

expect(stagingDir.existsSync(), false);
await createTestCommandRunner(command).run(
<String>[
'migrate',
'abandon',
'--staging-directory=${stagingDir.path}',
'--project-directory=${appDir.path}',
]
);
expect(logger.errorText, contains('Provided staging directory'));
expect(logger.errorText, contains('migrate_staging_dir` does not exist or is not valid.'));

logger.clear();
await createTestCommandRunner(command).run(
<String>[
'migrate',
'abandon',
'--project-directory=${appDir.path}',
]
);
expect(logger.statusText, contains('No migration in progress. Start a new migration with:'));

final File pubspecModified = stagingDir.childFile('pubspec.yaml');
pubspecModified.createSync(recursive: true);
pubspecModified.writeAsStringSync('''
name: newname
description: new description of the test project
version: 1.0.0+1
environment:
sdk: '>=2.18.0-58.0.dev <3.0.0'
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: false
EXTRALINE''', flush: true);

final File addedFile = stagingDir.childFile('added.file');
addedFile.createSync(recursive: true);
addedFile.writeAsStringSync('new file contents');

final File manifestFile = stagingDir.childFile('.migrate_manifest');
manifestFile.createSync(recursive: true);
manifestFile.writeAsStringSync('''
merged_files:
- pubspec.yaml
conflict_files:
added_files:
- added.file
deleted_files:
''');

expect(appDir.childFile('lib/main.dart').existsSync(), true);

expect(stagingDir.existsSync(), true);
logger.clear();
await createTestCommandRunner(command).run(
<String>[
'migrate',
'abandon',
'--staging-directory=${stagingDir.path}',
'--project-directory=${appDir.path}',
'--force',
]
);
expect(logger.statusText, contains('Abandon complete. Start a new migration with:'));
expect(stagingDir.existsSync(), false);
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => processManager,
Platform: () => platform,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/migrate.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
Expand All @@ -20,7 +21,7 @@ void main() {
FileSystem fileSystem;
BufferLogger logger;
Platform platform;
// TODO(garyq): Add terminal back in when other subcommands land.
Terminal terminal;
ProcessManager processManager;
Directory appDir;

Expand All @@ -29,6 +30,7 @@ void main() {
appDir = fileSystem.systemTempDirectory.createTempSync('apptestdir');
logger = BufferLogger.test();
platform = FakePlatform();
terminal = Terminal.test();
processManager = globals.processManager;
});

Expand All @@ -45,6 +47,7 @@ void main() {
verbose: true,
logger: logger,
fileSystem: fileSystem,
terminal: terminal,
platform: platform,
processManager: processManager,
);
Expand Down

0 comments on commit e9a3cbf

Please sign in to comment.