Add --empty to the flutter create command (#113873)
This commit is contained in:
@@ -65,6 +65,12 @@ class CreateCommand extends CreateBase {
|
||||
'https://api.flutter.dev/flutter/widgets/SingleChildScrollView-class.html',
|
||||
valueHelp: 'id',
|
||||
);
|
||||
argParser.addFlag(
|
||||
'empty',
|
||||
abbr: 'e',
|
||||
help: 'Specifies creating using an application template with a main.dart that is minimal, '
|
||||
'including no comments, as a starting point for a new application. Implies "--template=app".',
|
||||
);
|
||||
argParser.addOption(
|
||||
'list-samples',
|
||||
help: 'Specifies a JSON output file for a listing of Flutter code samples '
|
||||
@@ -191,9 +197,17 @@ class CreateCommand extends CreateBase {
|
||||
return FlutterCommandResult.success();
|
||||
}
|
||||
|
||||
if (argResults!.wasParsed('empty') && argResults!.wasParsed('sample')) {
|
||||
throwToolExit(
|
||||
'Only one of --empty or --sample may be specified, not both.',
|
||||
exitCode: 2,
|
||||
);
|
||||
}
|
||||
|
||||
validateOutputDirectoryArg();
|
||||
String? sampleCode;
|
||||
final String? sampleArgument = stringArg('sample');
|
||||
final bool emptyArgument = boolArg('empty') ?? false;
|
||||
if (sampleArgument != null) {
|
||||
final String? templateArgument = stringArg('template');
|
||||
if (templateArgument != null && stringToProjectType(templateArgument) != FlutterProjectType.app) {
|
||||
@@ -299,6 +313,7 @@ class CreateCommand extends CreateBase {
|
||||
flutterRoot: flutterRoot,
|
||||
withPlatformChannelPluginHook: generateMethodChannelsPlugin,
|
||||
withFfiPluginHook: generateFfiPlugin,
|
||||
withEmptyMain: emptyArgument,
|
||||
androidLanguage: stringArgDeprecated('android-language'),
|
||||
iosLanguage: stringArgDeprecated('ios-language'),
|
||||
iosDevelopmentTeam: developmentTeam,
|
||||
@@ -411,11 +426,15 @@ class CreateCommand extends CreateBase {
|
||||
);
|
||||
}
|
||||
if (sampleCode != null) {
|
||||
generatedFileCount += _applySample(relativeDir, sampleCode);
|
||||
_applySample(relativeDir, sampleCode);
|
||||
}
|
||||
if (sampleCode != null || emptyArgument) {
|
||||
generatedFileCount += _removeTestDir(relativeDir);
|
||||
}
|
||||
globals.printStatus('Wrote $generatedFileCount files.');
|
||||
globals.printStatus('\nAll done!');
|
||||
final String application = sampleCode != null ? 'sample application' : 'application';
|
||||
final String application =
|
||||
'${emptyArgument ? 'empty' : ''}${sampleCode != null ? 'sample' : ''} application';
|
||||
if (generatePackage) {
|
||||
final String relativeMainPath = globals.fs.path.normalize(globals.fs.path.join(
|
||||
relativeDirPath,
|
||||
@@ -657,10 +676,13 @@ Your $application code is in $relativeAppMain.
|
||||
// documentation website in sampleCode. Returns the difference in the number
|
||||
// of files after applying the sample, since it also deletes the application's
|
||||
// test directory (since the template's test doesn't apply to the sample).
|
||||
int _applySample(Directory directory, String sampleCode) {
|
||||
void _applySample(Directory directory, String sampleCode) {
|
||||
final File mainDartFile = directory.childDirectory('lib').childFile('main.dart');
|
||||
mainDartFile.createSync(recursive: true);
|
||||
mainDartFile.writeAsStringSync(sampleCode);
|
||||
}
|
||||
|
||||
int _removeTestDir(Directory directory) {
|
||||
final Directory testDir = directory.childDirectory('test');
|
||||
final List<FileSystemEntity> files = testDir.listSync(recursive: true);
|
||||
testDir.deleteSync(recursive: true);
|
||||
|
||||
@@ -354,6 +354,7 @@ abstract class CreateBase extends FlutterCommand {
|
||||
String? gradleVersion,
|
||||
bool withPlatformChannelPluginHook = false,
|
||||
bool withFfiPluginHook = false,
|
||||
bool withEmptyMain = false,
|
||||
bool ios = false,
|
||||
bool android = false,
|
||||
bool web = false,
|
||||
@@ -409,6 +410,7 @@ abstract class CreateBase extends FlutterCommand {
|
||||
'withFfiPluginHook': withFfiPluginHook,
|
||||
'withPlatformChannelPluginHook': withPlatformChannelPluginHook,
|
||||
'withPluginHook': withFfiPluginHook || withPlatformChannelPluginHook,
|
||||
'withEmptyMain': withEmptyMain,
|
||||
'androidLanguage': androidLanguage,
|
||||
'iosLanguage': iosLanguage,
|
||||
'hasIosDevelopmentTeam': iosDevelopmentTeam != null && iosDevelopmentTeam.isNotEmpty,
|
||||
|
||||
Reference in New Issue
Block a user