From ebf1ecc5bd8f44f0c4f4c5da185206c1f26b40fb Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Tue, 1 Mar 2016 10:01:37 -0800 Subject: [PATCH] valdate project names in create --- .../lib/src/commands/create.dart | 49 ++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index 017b8cfebc..3449dd214e 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart @@ -76,7 +76,15 @@ class CreateCommand extends Command { projectDir = new Directory(argResults.rest.first); } - _renderTemplates(projectDir, flutterPackagesDirectory, + String dirPath = path.normalize(projectDir.absolute.path); + String projectName = _normalizeProjectName(path.basename(dirPath)); + + if (_validateProjectName(projectName) != null) { + printError(_validateProjectName(projectName)); + return 1; + } + + _renderTemplates(projectName, dirPath, flutterPackagesDirectory, renderDriverTest: argResults['with-driver-test']); if (argResults['pub']) { @@ -115,16 +123,14 @@ All done! In order to run your application, type: return 0; } - void _renderTemplates(Directory projectDir, String flutterPackagesDirectory, - { bool renderDriverTest: false }) { - String dirPath = path.normalize(projectDir.absolute.path); - String projectName = _normalizeProjectName(path.basename(dirPath)); + void _renderTemplates(String projectName, String dirPath, + String flutterPackagesDirectory, { bool renderDriverTest: false }) { String projectIdentifier = _createProjectIdentifier(path.basename(dirPath)); String relativeFlutterPackagesDirectory = path.relative(flutterPackagesDirectory, from: dirPath); printStatus('Creating project ${path.basename(projectName)}:'); - projectDir.createSync(recursive: true); + new Directory(dirPath).createSync(recursive: true); Map templateContext = { 'projectName': projectName, @@ -164,3 +170,34 @@ String _createProjectIdentifier(String name) { name = name.length == 0 ? 'untitled' : name; return 'com.yourcompany.$name'; } + +final Set _packageDependencies = new Set.from([ + 'args', + 'async', + 'collection', + 'convert', + 'flutter', + 'html', + 'intl', + 'logging', + 'matcher', + 'mime', + 'path', + 'plugin', + 'pool', + 'test', + 'utf', + 'watcher', + 'yaml' +]); + +/// Return `null` if the project name is legal. Return a validation message if +/// we should disallow the project name. +String _validateProjectName(String projectName) { + if (_packageDependencies.contains(projectName)) { + return "Invalid project name: '$projectName' - this will conflict with Flutter " + "package dependencies."; + } + + return null; +}