Commit 80fabfd3 authored by Devon Carew's avatar Devon Carew

allow flutter create to re-gen over an existing project (#3419)

* allow flutter create to re-gen over an existing project

* add a regression test
parent ff9bdb2c
...@@ -21,7 +21,8 @@ class CreateCommand extends Command { ...@@ -21,7 +21,8 @@ class CreateCommand extends Command {
final String name = 'create'; final String name = 'create';
@override @override
final String description = 'Create a new Flutter project.\nIf run on a project that already exists, this will repair the project, recreating any files that are missing.'; final String description = 'Create a new Flutter project.\n\n'
'If run on a project that already exists, this will repair the project, recreating any files that are missing.';
@override @override
final List<String> aliases = <String>['init']; final List<String> aliases = <String>['init'];
...@@ -57,13 +58,12 @@ class CreateCommand extends Command { ...@@ -57,13 +58,12 @@ class CreateCommand extends Command {
if (argResults.rest.length > 1) { if (argResults.rest.length > 1) {
printStatus('Multiple output directories specified.'); printStatus('Multiple output directories specified.');
printStatus('To create (or repair) a flutter application directory, run "flutter create dirname" where "dirname" is the application directory.');
return 2; return 2;
} }
if (ArtifactStore.flutterRoot == null) { if (ArtifactStore.flutterRoot == null) {
printError('Neither the --flutter-root command line flag nor the FLUTTER_ROOT environment'); printError('Neither the --flutter-root command line flag nor the FLUTTER_ROOT environment\n'
printError('variable was specified. Unable to find package:flutter.'); 'variable was specified. Unable to find package:flutter.');
return 2; return 2;
} }
...@@ -230,15 +230,9 @@ String _validateProjectName(String projectName) { ...@@ -230,15 +230,9 @@ String _validateProjectName(String projectName) {
/// if we should disallow the directory name. /// if we should disallow the directory name.
String _validateProjectDir(String projectName) { String _validateProjectDir(String projectName) {
FileSystemEntityType type = FileSystemEntity.typeSync(projectName); FileSystemEntityType type = FileSystemEntity.typeSync(projectName);
if (type != FileSystemEntityType.NOT_FOUND) { if (type != FileSystemEntityType.NOT_FOUND) {
switch(type) { switch(type) {
case FileSystemEntityType.DIRECTORY:
// Do not re-use directory if it is not empty.
if (new Directory(projectName).listSync(followLinks: false).isNotEmpty) {
return "Invalid project name: '$projectName' - refers to a directory "
"that is not empty.";
};
break;
case FileSystemEntityType.FILE: case FileSystemEntityType.FILE:
// Do not overwrite files. // Do not overwrite files.
return "Invalid project name: '$projectName' - file exists."; return "Invalid project name: '$projectName' - file exists.";
...@@ -247,6 +241,7 @@ String _validateProjectDir(String projectName) { ...@@ -247,6 +241,7 @@ String _validateProjectDir(String projectName) {
return "Invalid project name: '$projectName' - refers to a link."; return "Invalid project name: '$projectName' - refers to a link.";
} }
} }
return null; return null;
} }
......
...@@ -49,29 +49,28 @@ void main() { ...@@ -49,29 +49,28 @@ void main() {
// This test can take a while due to network requests. // This test can take a while due to network requests.
timeout: new Timeout(new Duration(minutes: 2))); timeout: new Timeout(new Duration(minutes: 2)));
// Verify that we fail with an error code when the file exists. // Verify that we can regenerate over an existing project.
testUsingContext('fails when file exists', () async { testUsingContext('can re-gen over existing project', () async {
ArtifactStore.flutterRoot = '../..'; ArtifactStore.flutterRoot = '../..';
CreateCommand command = new CreateCommand(); CreateCommand command = new CreateCommand();
CommandRunner runner = new CommandRunner('test_flutter', '') CommandRunner runner = new CommandRunner('test_flutter', '')
..addCommand(command); ..addCommand(command);
File existingFile = new File("${temp.path.toString()}/bad");
if (!existingFile.existsSync()) existingFile.createSync(); await runner.run(['create', '--no-pub', temp.path])
await runner.run(['create', existingFile.path]) .then((int code) => expect(code, equals(0)));
.then((int code) => expect(code, equals(1))); await runner.run(['create', '--no-pub', temp.path])
.then((int code) => expect(code, equals(0)));
}); });
// Verify that we fail with an error code when the file exists. // Verify that we fail with an error code when the file exists.
testUsingContext('fails with non-empty directory', () async { testUsingContext('fails when file exists', () async {
ArtifactStore.flutterRoot = '../..'; ArtifactStore.flutterRoot = '../..';
CreateCommand command = new CreateCommand(); CreateCommand command = new CreateCommand();
CommandRunner runner = new CommandRunner('test_flutter', '') CommandRunner runner = new CommandRunner('test_flutter', '')
..addCommand(command); ..addCommand(command);
File existingFile = new File("${temp.path.toString()}/bad"); File existingFile = new File("${temp.path.toString()}/bad");
if (!existingFile.existsSync()) existingFile.createSync(); if (!existingFile.existsSync()) existingFile.createSync();
RandomAccessFile raf = existingFile.openSync(); await runner.run(['create', existingFile.path])
raf.close();
await runner.run(['create', temp.path])
.then((int code) => expect(code, equals(1))); .then((int code) => expect(code, equals(1)));
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment