[go: nahoru, domu]

Skip to content

Commit

Permalink
Add --empty to the flutter create command (flutter#113873)
Browse files Browse the repository at this point in the history
  • Loading branch information
gspencergoog committed Oct 24, 2022
1 parent 3c2f500 commit 5259e1b
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 3 deletions.
28 changes: 25 additions & 3 deletions packages/flutter_tools/lib/src/commands/create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions packages/flutter_tools/lib/src/commands/create_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions packages/flutter_tools/templates/app/README.md.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# {{projectName}}

{{description}}
{{^withEmptyMain}}

## Getting Started

Expand All @@ -14,3 +15,4 @@ A few resources to get you started if this is your first Flutter project:
For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
{{/withEmptyMain}}
23 changes: 23 additions & 0 deletions packages/flutter_tools/templates/app/lib/main.dart.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
import 'package:flutter/material.dart';
{{#withEmptyMain}}

void main() {
runApp(const MainApp());
}

class MainApp extends StatelessWidget {
const MainApp({super.key});

@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(
body: Center(
child: Text('Hello World!'),
),
),
);
}
}
{{/withEmptyMain}}
{{^withEmptyMain}}
{{#withPlatformChannelPluginHook}}
import 'dart:async';

Expand Down Expand Up @@ -248,3 +270,4 @@ class _MyAppState extends State<MyApp> {
}
}
{{/withFfiPluginHook}}
{{/withEmptyMain}}
20 changes: 20 additions & 0 deletions packages/flutter_tools/templates/app/pubspec.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
name: {{projectName}}
description: {{description}}
{{#withEmptyMain}}
publish_to: 'none'
version: 0.1.0

environment:
sdk: {{dartSdkVersionBounds}}

dependencies:
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0

flutter:
uses-material-design: true
{{/withEmptyMain}}
{{^withEmptyMain}}
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
Expand Down Expand Up @@ -100,3 +119,4 @@ flutter:
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
{{/withEmptyMain}}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{^withEmptyMain}}
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
Expand All @@ -7,7 +8,9 @@

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
{{/withEmptyMain}}
include: package:flutter_lints/flutter.yaml
{{^withEmptyMain}}

linter:
# The lint rules applied to this project can be customized in the
Expand All @@ -27,3 +30,4 @@ linter:

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
{{/withEmptyMain}}
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,28 @@ void main() {
},
);

testUsingContext('can create an empty application project', () async {
await _createAndAnalyzeProject(
projectDir,
<String>['--no-pub', '--empty'],
<String>[
'lib/main.dart',
'flutter_project.iml',
'android/app/src/main/AndroidManifest.xml',
'ios/Flutter/AppFrameworkInfo.plist',
],
unexpectedPaths: <String>['test'],
);
expect(projectDir.childDirectory('lib').childFile('main.dart').readAsStringSync(),
contains("Text('Hello World!')"));
expect(projectDir.childDirectory('lib').childFile('main.dart').readAsStringSync(),
isNot(contains('int _counter')));
expect(projectDir.childFile('analysis_options.yaml').readAsStringSync(),
isNot(contains('#')));
expect(projectDir.childFile('README.md').readAsStringSync(),
isNot(contains('Getting Started')));
});

testUsingContext('can create a sample-based project', () async {
await _createAndAnalyzeProject(
projectDir,
Expand Down

0 comments on commit 5259e1b

Please sign in to comment.