@@ -24,6 +24,7 @@ import '../globals.dart';
2424import '../project.dart' ;
2525import '../runner/flutter_command.dart' ;
2626import '../template.dart' ;
27+ import '../usage.dart' ;
2728import '../version.dart' ;
2829
2930enum _ProjectType {
@@ -148,6 +149,15 @@ class CreateCommand extends FlutterCommand {
148149 @override
149150 String get invocation => '${runner .executableName } $name <output directory>' ;
150151
152+ @override
153+ Future <Map <String , String >> get usageValues async {
154+ return < String , String > {
155+ kCommandCreateProjectType: argResults['template' ],
156+ kCommandCreateAndroidLanguage: argResults['android-language' ],
157+ kCommandCreateIosLanguage: argResults['ios-language' ],
158+ };
159+ }
160+
151161 // If it has a .metadata file with the project_type in it, use that.
152162 // If it has an android dir and an android/app dir, it's a legacy app
153163 // If it has an ios dir and an ios/Flutter dir, it's a legacy app
@@ -228,6 +238,36 @@ class CreateCommand extends FlutterCommand {
228238 }
229239 }
230240
241+ _ProjectType _getProjectType (Directory projectDir) {
242+ _ProjectType template;
243+ _ProjectType detectedProjectType;
244+ final bool metadataExists = projectDir.absolute.childFile ('.metadata' ).existsSync ();
245+ if (argResults['template' ] != null ) {
246+ template = _stringToProjectType (argResults['template' ]);
247+ } else {
248+ // If the project directory exists and isn't empty, then try to determine the template
249+ // type from the project directory.
250+ if (projectDir.existsSync () && projectDir.listSync ().isNotEmpty) {
251+ detectedProjectType = _determineTemplateType (projectDir);
252+ if (detectedProjectType == null && metadataExists) {
253+ // We can only be definitive that this is the wrong type if the .metadata file
254+ // exists and contains a type that we don't understand, or doesn't contain a type.
255+ throwToolExit ('Sorry, unable to detect the type of project to recreate. '
256+ 'Try creating a fresh project and migrating your existing code to '
257+ 'the new project manually.' );
258+ }
259+ }
260+ }
261+ template ?? = detectedProjectType ?? _ProjectType .app;
262+ if (detectedProjectType != null && template != detectedProjectType && metadataExists) {
263+ // We can only be definitive that this is the wrong type if the .metadata file
264+ // exists and contains a type that doesn't match.
265+ throwToolExit ("The requested template type '${getEnumName (template )}' doesn't match the "
266+ "existing template type of '${getEnumName (detectedProjectType )}'." );
267+ }
268+ return template;
269+ }
270+
231271 @override
232272 Future <FlutterCommandResult > runCommand () async {
233273 if (argResults['list-samples' ] != null ) {
@@ -283,31 +323,7 @@ class CreateCommand extends FlutterCommand {
283323 sampleCode = await _fetchSampleFromServer (argResults['sample' ]);
284324 }
285325
286- _ProjectType template;
287- _ProjectType detectedProjectType;
288- final bool metadataExists = projectDir.absolute.childFile ('.metadata' ).existsSync ();
289- if (argResults['template' ] != null ) {
290- template = _stringToProjectType (argResults['template' ]);
291- } else {
292- if (projectDir.existsSync () && projectDir.listSync ().isNotEmpty) {
293- detectedProjectType = _determineTemplateType (projectDir);
294- if (detectedProjectType == null && metadataExists) {
295- // We can only be definitive that this is the wrong type if the .metadata file
296- // exists and contains a type that we don't understand, or doesn't contain a type.
297- throwToolExit ('Sorry, unable to detect the type of project to recreate. '
298- 'Try creating a fresh project and migrating your existing code to '
299- 'the new project manually.' );
300- }
301- }
302- }
303- template ?? = detectedProjectType ?? _ProjectType .app;
304- if (detectedProjectType != null && template != detectedProjectType && metadataExists) {
305- // We can only be definitive that this is the wrong type if the .metadata file
306- // exists and contains a type that doesn't match.
307- throwToolExit ("The requested template type '${getEnumName (template )}' doesn't match the "
308- "existing template type of '${getEnumName (detectedProjectType )}'." );
309- }
310-
326+ final _ProjectType template = _getProjectType (projectDir);
311327 final bool generateModule = template == _ProjectType .module;
312328 final bool generatePlugin = template == _ProjectType .plugin;
313329 final bool generatePackage = template == _ProjectType .package;
0 commit comments