Commit 030f623a authored by Adam Barth's avatar Adam Barth Committed by GitHub

Prevent `flutter create` from running inside SDK (#6587)

Fixes #6480
parent 02883908
...@@ -91,13 +91,15 @@ class CreateCommand extends FlutterCommand { ...@@ -91,13 +91,15 @@ class CreateCommand extends FlutterCommand {
String relativePath = path.relative(dirPath); String relativePath = path.relative(dirPath);
String projectName = _normalizeProjectName(path.basename(dirPath)); String projectName = _normalizeProjectName(path.basename(dirPath));
if (_validateProjectDir(dirPath) != null) { String error =_validateProjectDir(dirPath, flutterRoot: flutterRoot);
printError(_validateProjectDir(dirPath)); if (error != null) {
printError(error);
return 1; return 1;
} }
if (_validateProjectName(projectName) != null) { error = _validateProjectName(projectName);
printError(_validateProjectName(projectName)); if (error != null) {
printError(error);
return 1; return 1;
} }
...@@ -242,17 +244,22 @@ String _validateProjectName(String projectName) { ...@@ -242,17 +244,22 @@ String _validateProjectName(String projectName) {
/// Return `null` if the project directory is legal. Return a validation message /// Return `null` if the project directory is legal. Return a validation message
/// if we should disallow the directory name. /// if we should disallow the directory name.
String _validateProjectDir(String projectName) { String _validateProjectDir(String dirPath, { String flutterRoot }) {
FileSystemEntityType type = FileSystemEntity.typeSync(projectName); if (path.isWithin(flutterRoot, dirPath)) {
return "Cannot create a project within the Flutter SDK.\n"
"Target directory '$dirPath' is within the Flutter SDK at '$flutterRoot'.";
}
FileSystemEntityType type = FileSystemEntity.typeSync(dirPath);
if (type != FileSystemEntityType.NOT_FOUND) { if (type != FileSystemEntityType.NOT_FOUND) {
switch(type) { switch(type) {
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: '$dirPath' - file exists.";
case FileSystemEntityType.LINK: case FileSystemEntityType.LINK:
// Do not overwrite links. // Do not overwrite links.
return "Invalid project name: '$projectName' - refers to a link."; return "Invalid project name: '$dirPath' - refers to a link.";
} }
} }
......
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