2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
- // @dart = 2.8
6
-
7
5
import '../android/gradle_utils.dart' as gradle;
8
6
import '../base/common.dart' ;
9
7
import '../base/context.dart' ;
@@ -32,8 +30,8 @@ const String kPlatformHelp =
32
30
33
31
class CreateCommand extends CreateBase {
34
32
CreateCommand ({
35
- bool verboseHelp = false ,
36
- }) : super (verboseHelp : verboseHelp) {
33
+ super . verboseHelp = false ,
34
+ }) {
37
35
addPlatformsOptions (customHelp: kPlatformHelp);
38
36
argParser.addOption (
39
37
'template' ,
@@ -57,7 +55,6 @@ class CreateCommand extends CreateBase {
57
55
flutterProjectTypeToString (FlutterProjectType .module): 'Generate a project to add a Flutter module to an '
58
56
'existing Android or iOS application.' ,
59
57
},
60
- defaultsTo: null ,
61
58
);
62
59
argParser.addOption (
63
60
'sample' ,
@@ -66,7 +63,6 @@ class CreateCommand extends CreateBase {
66
63
'"--template=app". The value should be the sample ID of the desired sample from the API '
67
64
'documentation website (http://docs.flutter.dev/). An example can be found at: '
68
65
'https://api.flutter.dev/flutter/widgets/SingleChildScrollView-class.html' ,
69
- defaultsTo: null ,
70
66
valueHelp: 'id' ,
71
67
);
72
68
argParser.addOption (
@@ -88,7 +84,7 @@ class CreateCommand extends CreateBase {
88
84
String get category => FlutterCommandCategory .project;
89
85
90
86
@override
91
- String get invocation => '${runner .executableName } $name <output directory>' ;
87
+ String get invocation => '${runner ? .executableName } $name <output directory>' ;
92
88
93
89
@override
94
90
Future <CustomDimensions > get usageValues async {
@@ -100,8 +96,7 @@ class CreateCommand extends CreateBase {
100
96
}
101
97
102
98
// Lazy-initialize the net utilities with values from the context.
103
- Net _cachedNet;
104
- Net get _net => _cachedNet ?? = Net (
99
+ late final Net _net = Net (
105
100
httpClientFactory: context.get <HttpClientFactory >(),
106
101
logger: globals.logger,
107
102
platform: globals.platform,
@@ -112,25 +107,25 @@ class CreateCommand extends CreateBase {
112
107
? 'api.flutter.dev'
113
108
: 'master-api.flutter.dev' ;
114
109
115
- Future <String > _fetchSampleFromServer (String sampleId) async {
110
+ Future <String ? > _fetchSampleFromServer (String sampleId) async {
116
111
// Sanity check the sampleId
117
112
if (sampleId.contains (RegExp (r'[^-\w\.]' ))) {
118
113
throwToolExit ('Sample ID "$sampleId " contains invalid characters. Check the ID in the '
119
114
'documentation and try again.' );
120
115
}
121
116
122
117
final Uri snippetsUri = Uri .https (_snippetsHost, 'snippets/$sampleId .dart' );
123
- final List <int > data = await _net.fetchUrl (snippetsUri);
118
+ final List <int >? data = await _net.fetchUrl (snippetsUri);
124
119
if (data == null || data.isEmpty) {
125
120
return null ;
126
121
}
127
122
return utf8.decode (data);
128
123
}
129
124
130
125
/// Fetches the samples index file from the Flutter docs website.
131
- Future <String > _fetchSamplesIndexFromServer () async {
126
+ Future <String ? > _fetchSamplesIndexFromServer () async {
132
127
final Uri snippetsUri = Uri .https (_snippetsHost, 'snippets/index.json' );
133
- final List <int > data = await _net.fetchUrl (snippetsUri, maxAttempts: 2 );
128
+ final List <int >? data = await _net.fetchUrl (snippetsUri, maxAttempts: 2 );
134
129
if (data == null || data.isEmpty) {
135
130
return null ;
136
131
}
@@ -145,7 +140,7 @@ class CreateCommand extends CreateBase {
145
140
if (outputFile.existsSync ()) {
146
141
throwToolExit ('File "$outputFilePath " already exists' , exitCode: 1 );
147
142
}
148
- final String samplesJson = await _fetchSamplesIndexFromServer ();
143
+ final String ? samplesJson = await _fetchSamplesIndexFromServer ();
149
144
if (samplesJson == null ) {
150
145
throwToolExit ('Unable to download samples' , exitCode: 2 );
151
146
} else {
@@ -158,11 +153,12 @@ class CreateCommand extends CreateBase {
158
153
}
159
154
160
155
FlutterProjectType _getProjectType (Directory projectDir) {
161
- FlutterProjectType template;
162
- FlutterProjectType detectedProjectType;
156
+ FlutterProjectType ? template;
157
+ FlutterProjectType ? detectedProjectType;
163
158
final bool metadataExists = projectDir.absolute.childFile ('.metadata' ).existsSync ();
164
- if (argResults['template' ] != null ) {
165
- template = stringToProjectType (stringArgDeprecated ('template' ));
159
+ final String ? templateArgument = stringArg ('template' );
160
+ if (templateArgument != null ) {
161
+ template = stringToProjectType (templateArgument);
166
162
}
167
163
// If the project directory exists and isn't empty, then try to determine the template
168
164
// type from the project directory.
@@ -188,23 +184,25 @@ class CreateCommand extends CreateBase {
188
184
189
185
@override
190
186
Future <FlutterCommandResult > runCommand () async {
191
- if (argResults['list-samples' ] != null ) {
187
+ final String ? listSamples = stringArg ('list-samples' );
188
+ if (listSamples != null ) {
192
189
// _writeSamplesJson can potentially be long-lived.
193
- await _writeSamplesJson (stringArgDeprecated ( 'list-samples' ) );
190
+ await _writeSamplesJson (listSamples );
194
191
return FlutterCommandResult .success ();
195
192
}
196
193
197
194
validateOutputDirectoryArg ();
198
195
199
- String sampleCode;
200
- if (argResults['sample' ] != null ) {
201
- if (argResults['template' ] != null &&
202
- stringToProjectType (stringArgDeprecated ('template' ) ?? 'app' ) != FlutterProjectType .app) {
196
+ String ? sampleCode;
197
+ final String ? sampleArgument = stringArg ('sample' );
198
+ if (sampleArgument != null ) {
199
+ final String ? templateArgument = stringArg ('template' );
200
+ if (templateArgument != null && stringToProjectType (templateArgument) != FlutterProjectType .app) {
203
201
throwToolExit ('Cannot specify --sample with a project type other than '
204
202
'"${flutterProjectTypeToString (FlutterProjectType .app )}"' );
205
203
}
206
204
// Fetch the sample from the server.
207
- sampleCode = await _fetchSampleFromServer (stringArgDeprecated ( 'sample' ) );
205
+ sampleCode = await _fetchSampleFromServer (sampleArgument );
208
206
}
209
207
210
208
final FlutterProjectType template = _getProjectType (projectDir);
@@ -215,7 +213,7 @@ class CreateCommand extends CreateBase {
215
213
216
214
final List <String > platforms = stringsArg ('platforms' );
217
215
// `--platforms` does not support module or package.
218
- if (argResults.wasParsed ('platforms' ) && (generateModule || generatePackage)) {
216
+ if (argResults! .wasParsed ('platforms' ) && (generateModule || generatePackage)) {
219
217
final String template = generateModule ? 'module' : 'package' ;
220
218
throwToolExit (
221
219
'The "--platforms" argument is not supported in $template template.' ,
@@ -224,18 +222,18 @@ class CreateCommand extends CreateBase {
224
222
} else if (platforms == null || platforms.isEmpty) {
225
223
throwToolExit ('Must specify at least one platform using --platforms' ,
226
224
exitCode: 2 );
227
- } else if (generateFfiPlugin && argResults.wasParsed ('platforms' ) && platforms.contains ('web' )) {
225
+ } else if (generateFfiPlugin && argResults! .wasParsed ('platforms' ) && platforms.contains ('web' )) {
228
226
throwToolExit (
229
227
'The web platform is not supported in plugin_ffi template.' ,
230
228
exitCode: 2 ,
231
229
);
232
- } else if (generateFfiPlugin && argResults.wasParsed ('ios-language' )) {
230
+ } else if (generateFfiPlugin && argResults! .wasParsed ('ios-language' )) {
233
231
throwToolExit (
234
232
'The "ios-language" option is not supported with the plugin_ffi '
235
233
'template: the language will always be C or C++.' ,
236
234
exitCode: 2 ,
237
235
);
238
- } else if (generateFfiPlugin && argResults.wasParsed ('android-language' )) {
236
+ } else if (generateFfiPlugin && argResults! .wasParsed ('android-language' )) {
239
237
throwToolExit (
240
238
'The "android-language" option is not supported with the plugin_ffi '
241
239
'template: the language will always be C or C++.' ,
@@ -258,7 +256,7 @@ class CreateCommand extends CreateBase {
258
256
259
257
final String dartSdk = globals.cache.dartSdkBuild;
260
258
final bool includeIos = featureFlags.isIOSEnabled && platforms.contains ('ios' );
261
- String developmentTeam;
259
+ String ? developmentTeam;
262
260
if (includeIos) {
263
261
developmentTeam = await getCodeSigningIdentityDevelopmentTeam (
264
262
processManager: globals.processManager,
@@ -272,7 +270,7 @@ class CreateCommand extends CreateBase {
272
270
// The dart project_name is in snake_case, this variable is the Title Case of the Project Name.
273
271
final String titleCaseProjectName = snakeCaseToTitleCase (projectName);
274
272
275
- final Map <String , Object > templateContext = createTemplateContext (
273
+ final Map <String , Object ? > templateContext = createTemplateContext (
276
274
organization: organization,
277
275
projectName: projectName,
278
276
titleCaseProjectName: titleCaseProjectName,
@@ -432,12 +430,12 @@ Your $application code is in $relativeAppMain.
432
430
433
431
Future <int > _generateModule (
434
432
Directory directory,
435
- Map <String , dynamic > templateContext, {
433
+ Map <String , Object ? > templateContext, {
436
434
bool overwrite = false ,
437
435
bool printStatusWhenWriting = true ,
438
436
}) async {
439
437
int generatedCount = 0 ;
440
- final String description = argResults.wasParsed ('description' )
438
+ final String ? description = argResults! .wasParsed ('description' )
441
439
? stringArgDeprecated ('description' )
442
440
: 'A new Flutter module project.' ;
443
441
templateContext['description' ] = description;
@@ -453,7 +451,6 @@ Your $application code is in $relativeAppMain.
453
451
context: PubContext .create,
454
452
directory: directory.path,
455
453
offline: boolArgDeprecated ('offline' ),
456
- generateSyntheticPackage: false ,
457
454
);
458
455
final FlutterProject project = FlutterProject .fromDirectory (directory);
459
456
await project.ensureReadyForPlatformSpecificTooling (
@@ -466,12 +463,12 @@ Your $application code is in $relativeAppMain.
466
463
467
464
Future <int > _generatePackage (
468
465
Directory directory,
469
- Map <String , dynamic > templateContext, {
466
+ Map <String , Object ? > templateContext, {
470
467
bool overwrite = false ,
471
468
bool printStatusWhenWriting = true ,
472
469
}) async {
473
470
int generatedCount = 0 ;
474
- final String description = argResults.wasParsed ('description' )
471
+ final String ? description = argResults! .wasParsed ('description' )
475
472
? stringArgDeprecated ('description' )
476
473
: 'A new Flutter package project.' ;
477
474
templateContext['description' ] = description;
@@ -487,21 +484,20 @@ Your $application code is in $relativeAppMain.
487
484
context: PubContext .createPackage,
488
485
directory: directory.path,
489
486
offline: boolArgDeprecated ('offline' ),
490
- generateSyntheticPackage: false ,
491
487
);
492
488
}
493
489
return generatedCount;
494
490
}
495
491
496
492
Future <int > _generateMethodChannelPlugin (
497
493
Directory directory,
498
- Map <String , dynamic > templateContext, {
494
+ Map <String , Object ? > templateContext, {
499
495
bool overwrite = false ,
500
496
bool printStatusWhenWriting = true ,
501
- FlutterProjectType projectType,
497
+ required FlutterProjectType projectType,
502
498
}) async {
503
499
// Plugins only add a platform if it was requested explicitly by the user.
504
- if (! argResults.wasParsed ('platforms' )) {
500
+ if (! argResults! .wasParsed ('platforms' )) {
505
501
for (final String platform in kAllCreatePlatforms) {
506
502
templateContext[platform] = false ;
507
503
}
@@ -517,7 +513,7 @@ Your $application code is in $relativeAppMain.
517
513
final bool willAddPlatforms = platformsToAdd.isNotEmpty;
518
514
templateContext['no_platforms' ] = ! willAddPlatforms;
519
515
int generatedCount = 0 ;
520
- final String description = argResults.wasParsed ('description' )
516
+ final String ? description = argResults! .wasParsed ('description' )
521
517
? stringArgDeprecated ('description' )
522
518
: 'A new Flutter plugin project.' ;
523
519
templateContext['description' ] = description;
@@ -534,7 +530,6 @@ Your $application code is in $relativeAppMain.
534
530
context: PubContext .createPlugin,
535
531
directory: directory.path,
536
532
offline: boolArgDeprecated ('offline' ),
537
- generateSyntheticPackage: false ,
538
533
);
539
534
}
540
535
@@ -545,9 +540,9 @@ Your $application code is in $relativeAppMain.
545
540
project: project, requireAndroidSdk: false );
546
541
}
547
542
548
- final String projectName = templateContext['projectName' ] as String ;
549
- final String organization = templateContext['organization' ] as String ;
550
- final String androidPluginIdentifier = templateContext['androidIdentifier' ] as String ;
543
+ final String ? projectName = templateContext['projectName' ] as String ? ;
544
+ final String organization = templateContext['organization' ]! as String ; // Required to make the context.
545
+ final String ? androidPluginIdentifier = templateContext['androidIdentifier' ] as String ? ;
551
546
final String exampleProjectName = '${projectName }_example' ;
552
547
templateContext['projectName' ] = exampleProjectName;
553
548
templateContext['androidIdentifier' ] = CreateBase .createAndroidIdentifier (organization, exampleProjectName);
@@ -572,13 +567,13 @@ Your $application code is in $relativeAppMain.
572
567
573
568
Future <int > _generateFfiPlugin (
574
569
Directory directory,
575
- Map <String , dynamic > templateContext, {
570
+ Map <String , Object ? > templateContext, {
576
571
bool overwrite = false ,
577
572
bool printStatusWhenWriting = true ,
578
- FlutterProjectType projectType,
573
+ required FlutterProjectType projectType,
579
574
}) async {
580
575
// Plugins only add a platform if it was requested explicitly by the user.
581
- if (! argResults.wasParsed ('platforms' )) {
576
+ if (! argResults! .wasParsed ('platforms' )) {
582
577
for (final String platform in kAllCreatePlatforms) {
583
578
templateContext[platform] = false ;
584
579
}
@@ -596,7 +591,7 @@ Your $application code is in $relativeAppMain.
596
591
final bool willAddPlatforms = platformsToAdd.isNotEmpty;
597
592
templateContext['no_platforms' ] = ! willAddPlatforms;
598
593
int generatedCount = 0 ;
599
- final String description = argResults.wasParsed ('description' )
594
+ final String ? description = argResults! .wasParsed ('description' )
600
595
? stringArgDeprecated ('description' )
601
596
: 'A new Flutter FFI plugin project.' ;
602
597
templateContext['description' ] = description;
@@ -613,7 +608,6 @@ Your $application code is in $relativeAppMain.
613
608
context: PubContext .createPlugin,
614
609
directory: directory.path,
615
610
offline: boolArgDeprecated ('offline' ),
616
- generateSyntheticPackage: false ,
617
611
);
618
612
}
619
613
@@ -623,9 +617,9 @@ Your $application code is in $relativeAppMain.
623
617
gradle.updateLocalProperties (project: project, requireAndroidSdk: false );
624
618
}
625
619
626
- final String projectName = templateContext['projectName' ] as String ;
627
- final String organization = templateContext['organization' ] as String ;
628
- final String androidPluginIdentifier = templateContext['androidIdentifier' ] as String ;
620
+ final String ? projectName = templateContext['projectName' ] as String ? ;
621
+ final String organization = templateContext['organization' ]! as String ; // Required to make the context.
622
+ final String ? androidPluginIdentifier = templateContext['androidIdentifier' ] as String ? ;
629
623
final String exampleProjectName = '${projectName }_example' ;
630
624
templateContext['projectName' ] = exampleProjectName;
631
625
templateContext['androidIdentifier' ] = CreateBase .createAndroidIdentifier (organization, exampleProjectName);
@@ -662,7 +656,7 @@ Your $application code is in $relativeAppMain.
662
656
return - files.length;
663
657
}
664
658
665
- List <String > _getSupportedPlatformsFromTemplateContext (Map <String , dynamic > templateContext) {
659
+ List <String > _getSupportedPlatformsFromTemplateContext (Map <String , Object ? > templateContext) {
666
660
return < String > [
667
661
for (String platform in kAllCreatePlatforms)
668
662
if (templateContext[platform] == true ) platform,
@@ -671,7 +665,7 @@ Your $application code is in $relativeAppMain.
671
665
672
666
// Returns a list of platforms that are explicitly requested by user via `--platforms`.
673
667
List <String > _getUserRequestedPlatforms () {
674
- if (! argResults.wasParsed ('platforms' )) {
668
+ if (! argResults! .wasParsed ('platforms' )) {
675
669
return < String > [];
676
670
}
677
671
return stringsArg ('platforms' );
@@ -682,10 +676,11 @@ Your $application code is in $relativeAppMain.
682
676
// Determine what platforms are supported based on generated files.
683
677
List <String > _getSupportedPlatformsInPlugin (Directory projectDir) {
684
678
final String pubspecPath = globals.fs.path.join (projectDir.absolute.path, 'pubspec.yaml' );
685
- final FlutterManifest manifest = FlutterManifest .createFromPath (pubspecPath, fileSystem: globals.fs, logger: globals.logger);
686
- final List <String > platforms = manifest.validSupportedPlatforms == null
679
+ final FlutterManifest ? manifest = FlutterManifest .createFromPath (pubspecPath, fileSystem: globals.fs, logger: globals.logger);
680
+ final Map <String , Object ?>? validSupportedPlatforms = manifest? .validSupportedPlatforms;
681
+ final List <String > platforms = validSupportedPlatforms == null
687
682
? < String > []
688
- : manifest. validSupportedPlatforms.keys.toList ();
683
+ : validSupportedPlatforms.keys.toList ();
689
684
return platforms;
690
685
}
691
686
0 commit comments