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
diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index 4611e21..6a2269a 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart
@@ -21,7 +21,8 @@ final String name = 'create'; @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 final List<String> aliases = <String>['init']; @@ -57,13 +58,12 @@ if (argResults.rest.length > 1) { 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; } if (ArtifactStore.flutterRoot == null) { - printError('Neither the --flutter-root command line flag nor the FLUTTER_ROOT environment'); - printError('variable was specified. Unable to find package:flutter.'); + printError('Neither the --flutter-root command line flag nor the FLUTTER_ROOT environment\n' + 'variable was specified. Unable to find package:flutter.'); return 2; } @@ -230,15 +230,9 @@ /// if we should disallow the directory name. String _validateProjectDir(String projectName) { FileSystemEntityType type = FileSystemEntity.typeSync(projectName); + if (type != FileSystemEntityType.NOT_FOUND) { 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: // Do not overwrite files. return "Invalid project name: '$projectName' - file exists."; @@ -247,6 +241,7 @@ return "Invalid project name: '$projectName' - refers to a link."; } } + return null; }
diff --git a/packages/flutter_tools/test/create_test.dart b/packages/flutter_tools/test/create_test.dart index 8f89073..170bf5e 100644 --- a/packages/flutter_tools/test/create_test.dart +++ b/packages/flutter_tools/test/create_test.dart
@@ -32,7 +32,7 @@ CommandRunner runner = new CommandRunner('test_flutter', '') ..addCommand(command); await runner.run(['create', temp.path]) - .then((int code) => expect(code, equals(0))); + .then((int code) => expect(code, equals(0))); String mainPath = path.join(temp.path, 'lib', 'main.dart'); expect(new File(mainPath).existsSync(), true); @@ -49,6 +49,19 @@ // This test can take a while due to network requests. timeout: new Timeout(new Duration(minutes: 2))); + // Verify that we can regenerate over an existing project. + testUsingContext('can re-gen over existing project', () async { + ArtifactStore.flutterRoot = '../..'; + CreateCommand command = new CreateCommand(); + CommandRunner runner = new CommandRunner('test_flutter', '') + ..addCommand(command); + + await runner.run(['create', '--no-pub', temp.path]) + .then((int code) => expect(code, equals(0))); + 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. testUsingContext('fails when file exists', () async { ArtifactStore.flutterRoot = '../..'; @@ -58,21 +71,7 @@ File existingFile = new File("${temp.path.toString()}/bad"); if (!existingFile.existsSync()) existingFile.createSync(); await runner.run(['create', existingFile.path]) - .then((int code) => expect(code, equals(1))); - }); - - // Verify that we fail with an error code when the file exists. - testUsingContext('fails with non-empty directory', () async { - ArtifactStore.flutterRoot = '../..'; - CreateCommand command = new CreateCommand(); - CommandRunner runner = new CommandRunner('test_flutter', '') - ..addCommand(command); - File existingFile = new File("${temp.path.toString()}/bad"); - if (!existingFile.existsSync()) existingFile.createSync(); - RandomAccessFile raf = existingFile.openSync(); - raf.close(); - await runner.run(['create', temp.path]) - .then((int code) => expect(code, equals(1))); + .then((int code) => expect(code, equals(1))); }); }); }