Unverified Commit 261ea1ba authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Suppress created file list for new "flutter create" projects (#93041)

parent 9976d751
...@@ -289,19 +289,46 @@ class CreateCommand extends CreateBase { ...@@ -289,19 +289,46 @@ class CreateCommand extends CreateBase {
int generatedFileCount = 0; int generatedFileCount = 0;
switch (template) { switch (template) {
case FlutterProjectType.app: case FlutterProjectType.app:
generatedFileCount += await generateApp('app', relativeDir, templateContext, overwrite: overwrite); generatedFileCount += await generateApp(
'app',
relativeDir,
templateContext,
overwrite: overwrite,
printStatusWhenWriting: !creatingNewProject,
);
break; break;
case FlutterProjectType.skeleton: case FlutterProjectType.skeleton:
generatedFileCount += await generateApp('skeleton', relativeDir, templateContext, overwrite: overwrite); generatedFileCount += await generateApp(
'skeleton',
relativeDir,
templateContext,
overwrite: overwrite,
printStatusWhenWriting: !creatingNewProject,
);
break; break;
case FlutterProjectType.module: case FlutterProjectType.module:
generatedFileCount += await _generateModule(relativeDir, templateContext, overwrite: overwrite); generatedFileCount += await _generateModule(
relativeDir,
templateContext,
overwrite: overwrite,
printStatusWhenWriting: !creatingNewProject,
);
break; break;
case FlutterProjectType.package: case FlutterProjectType.package:
generatedFileCount += await _generatePackage(relativeDir, templateContext, overwrite: overwrite); generatedFileCount += await _generatePackage(
relativeDir,
templateContext,
overwrite: overwrite,
printStatusWhenWriting: !creatingNewProject,
);
break; break;
case FlutterProjectType.plugin: case FlutterProjectType.plugin:
generatedFileCount += await _generatePlugin(relativeDir, templateContext, overwrite: overwrite); generatedFileCount += await _generatePlugin(
relativeDir,
templateContext,
overwrite: overwrite,
printStatusWhenWriting: !creatingNewProject,
);
break; break;
} }
if (sampleCode != null) { if (sampleCode != null) {
...@@ -367,13 +394,24 @@ Your $application code is in $relativeAppMain. ...@@ -367,13 +394,24 @@ Your $application code is in $relativeAppMain.
return FlutterCommandResult.success(); return FlutterCommandResult.success();
} }
Future<int> _generateModule(Directory directory, Map<String, dynamic> templateContext, { bool overwrite = false }) async { Future<int> _generateModule(
Directory directory,
Map<String, dynamic> templateContext, {
bool overwrite = false,
bool printStatusWhenWriting = true,
}) async {
int generatedCount = 0; int generatedCount = 0;
final String description = argResults.wasParsed('description') final String description = argResults.wasParsed('description')
? stringArg('description') ? stringArg('description')
: 'A new flutter module project.'; : 'A new flutter module project.';
templateContext['description'] = description; templateContext['description'] = description;
generatedCount += await renderTemplate(globals.fs.path.join('module', 'common'), directory, templateContext, overwrite: overwrite); generatedCount += await renderTemplate(
globals.fs.path.join('module', 'common'),
directory,
templateContext,
overwrite: overwrite,
printStatusWhenWriting: printStatusWhenWriting,
);
if (boolArg('pub')) { if (boolArg('pub')) {
await pub.get( await pub.get(
context: PubContext.create, context: PubContext.create,
...@@ -390,13 +428,24 @@ Your $application code is in $relativeAppMain. ...@@ -390,13 +428,24 @@ Your $application code is in $relativeAppMain.
return generatedCount; return generatedCount;
} }
Future<int> _generatePackage(Directory directory, Map<String, dynamic> templateContext, { bool overwrite = false }) async { Future<int> _generatePackage(
Directory directory,
Map<String, dynamic> templateContext, {
bool overwrite = false,
bool printStatusWhenWriting = true,
}) async {
int generatedCount = 0; int generatedCount = 0;
final String description = argResults.wasParsed('description') final String description = argResults.wasParsed('description')
? stringArg('description') ? stringArg('description')
: 'A new Flutter package project.'; : 'A new Flutter package project.';
templateContext['description'] = description; templateContext['description'] = description;
generatedCount += await renderTemplate('package', directory, templateContext, overwrite: overwrite); generatedCount += await renderTemplate(
'package',
directory,
templateContext,
overwrite: overwrite,
printStatusWhenWriting: printStatusWhenWriting,
);
if (boolArg('pub')) { if (boolArg('pub')) {
await pub.get( await pub.get(
context: PubContext.createPackage, context: PubContext.createPackage,
...@@ -408,7 +457,12 @@ Your $application code is in $relativeAppMain. ...@@ -408,7 +457,12 @@ Your $application code is in $relativeAppMain.
return generatedCount; return generatedCount;
} }
Future<int> _generatePlugin(Directory directory, Map<String, dynamic> templateContext, { bool overwrite = false }) async { Future<int> _generatePlugin(
Directory directory,
Map<String, dynamic> templateContext, {
bool overwrite = false,
bool printStatusWhenWriting = true,
}) async {
// Plugins only add a platform if it was requested explicitly by the user. // Plugins only add a platform if it was requested explicitly by the user.
if (!argResults.wasParsed('platforms')) { if (!argResults.wasParsed('platforms')) {
for (final String platform in kAllCreatePlatforms) { for (final String platform in kAllCreatePlatforms) {
...@@ -430,7 +484,13 @@ Your $application code is in $relativeAppMain. ...@@ -430,7 +484,13 @@ Your $application code is in $relativeAppMain.
? stringArg('description') ? stringArg('description')
: 'A new flutter plugin project.'; : 'A new flutter plugin project.';
templateContext['description'] = description; templateContext['description'] = description;
generatedCount += await renderTemplate('plugin', directory, templateContext, overwrite: overwrite); generatedCount += await renderTemplate(
'plugin',
directory,
templateContext,
overwrite: overwrite,
printStatusWhenWriting: printStatusWhenWriting,
);
if (boolArg('pub')) { if (boolArg('pub')) {
await pub.get( await pub.get(
...@@ -461,7 +521,14 @@ Your $application code is in $relativeAppMain. ...@@ -461,7 +521,14 @@ Your $application code is in $relativeAppMain.
templateContext['pluginProjectName'] = projectName; templateContext['pluginProjectName'] = projectName;
templateContext['androidPluginIdentifier'] = androidPluginIdentifier; templateContext['androidPluginIdentifier'] = androidPluginIdentifier;
generatedCount += await generateApp('app', project.example.directory, templateContext, overwrite: overwrite, pluginExampleApp: true); generatedCount += await generateApp(
'app',
project.example.directory,
templateContext,
overwrite: overwrite,
pluginExampleApp: true,
printStatusWhenWriting: printStatusWhenWriting,
);
return generatedCount; return generatedCount;
} }
......
...@@ -404,8 +404,12 @@ abstract class CreateBase extends FlutterCommand { ...@@ -404,8 +404,12 @@ abstract class CreateBase extends FlutterCommand {
/// If `overwrite` is true, overwrites existing files, `overwrite` defaults to `false`. /// If `overwrite` is true, overwrites existing files, `overwrite` defaults to `false`.
@protected @protected
Future<int> renderTemplate( Future<int> renderTemplate(
String templateName, Directory directory, Map<String, Object> context, String templateName,
{bool overwrite = false}) async { Directory directory,
Map<String, Object> context, {
bool overwrite = false,
bool printStatusWhenWriting = true,
}) async {
final Template template = await Template.fromName( final Template template = await Template.fromName(
templateName, templateName,
fileSystem: globals.fs, fileSystem: globals.fs,
...@@ -413,7 +417,12 @@ abstract class CreateBase extends FlutterCommand { ...@@ -413,7 +417,12 @@ abstract class CreateBase extends FlutterCommand {
templateRenderer: globals.templateRenderer, templateRenderer: globals.templateRenderer,
templateManifest: _templateManifest, templateManifest: _templateManifest,
); );
return template.render(directory, context, overwriteExisting: overwrite); return template.render(
directory,
context,
overwriteExisting: overwrite,
printStatusWhenWriting: printStatusWhenWriting,
);
} }
/// Merges named templates into a single template, output to `directory`. /// Merges named templates into a single template, output to `directory`.
...@@ -423,8 +432,12 @@ abstract class CreateBase extends FlutterCommand { ...@@ -423,8 +432,12 @@ abstract class CreateBase extends FlutterCommand {
/// If `overwrite` is true, overwrites existing files, `overwrite` defaults to `false`. /// If `overwrite` is true, overwrites existing files, `overwrite` defaults to `false`.
@protected @protected
Future<int> renderMerged( Future<int> renderMerged(
List<String> names, Directory directory, Map<String, Object> context, List<String> names,
{bool overwrite = false}) async { Directory directory,
Map<String, Object> context, {
bool overwrite = false,
bool printStatusWhenWriting = true,
}) async {
final Template template = await Template.merged( final Template template = await Template.merged(
names, names,
directory, directory,
...@@ -433,7 +446,12 @@ abstract class CreateBase extends FlutterCommand { ...@@ -433,7 +446,12 @@ abstract class CreateBase extends FlutterCommand {
templateRenderer: globals.templateRenderer, templateRenderer: globals.templateRenderer,
templateManifest: _templateManifest, templateManifest: _templateManifest,
); );
return template.render(directory, context, overwriteExisting: overwrite); return template.render(
directory,
context,
overwriteExisting: overwrite,
printStatusWhenWriting: printStatusWhenWriting,
);
} }
/// Generate application project in the `directory` using `templateContext`. /// Generate application project in the `directory` using `templateContext`.
...@@ -441,14 +459,20 @@ abstract class CreateBase extends FlutterCommand { ...@@ -441,14 +459,20 @@ abstract class CreateBase extends FlutterCommand {
/// If `overwrite` is true, overwrites existing files, `overwrite` defaults to `false`. /// If `overwrite` is true, overwrites existing files, `overwrite` defaults to `false`.
@protected @protected
Future<int> generateApp( Future<int> generateApp(
String templateName, Directory directory, Map<String, Object> templateContext, String templateName,
{bool overwrite = false, bool pluginExampleApp = false}) async { Directory directory,
Map<String, Object> templateContext, {
bool overwrite = false,
bool pluginExampleApp = false,
bool printStatusWhenWriting = true,
}) async {
int generatedCount = 0; int generatedCount = 0;
generatedCount += await renderMerged( generatedCount += await renderMerged(
<String>[templateName, 'app_shared'], <String>[templateName, 'app_shared'],
directory, directory,
templateContext, templateContext,
overwrite: overwrite, overwrite: overwrite,
printStatusWhenWriting: printStatusWhenWriting,
); );
final FlutterProject project = FlutterProject.fromDirectory(directory); final FlutterProject project = FlutterProject.fromDirectory(directory);
if (templateContext['android'] == true) { if (templateContext['android'] == true) {
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:typed_data';
import 'package:file/memory.dart'; import 'package:file/memory.dart';
import 'package:file_testing/file_testing.dart'; import 'package:file_testing/file_testing.dart';
import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/file_system.dart';
...@@ -41,29 +43,80 @@ void main() { ...@@ -41,29 +43,80 @@ void main() {
throwsToolExit()); throwsToolExit());
}); });
testWithoutContext('Template.render replaces .img.tmpl files with files from the image source', () { group('renders template', () {
late Directory destination;
const String imageName = 'some_image.png';
late File sourceImage;
late BufferLogger logger;
late Template template;
setUp(() {
final MemoryFileSystem fileSystem = MemoryFileSystem.test(); final MemoryFileSystem fileSystem = MemoryFileSystem.test();
final Directory templateDir = fileSystem.directory('templates'); final Directory templateDir = fileSystem.directory('templates');
final Directory imageSourceDir = fileSystem.directory('template_images'); final Directory imageSourceDir = fileSystem.directory('template_images');
final Directory destination = fileSystem.directory('target'); destination = fileSystem.directory('target');
const String imageName = 'some_image.png';
templateDir.childFile('$imageName.img.tmpl').createSync(recursive: true); templateDir.childFile('$imageName.img.tmpl').createSync(recursive: true);
final File sourceImage = imageSourceDir.childFile(imageName); sourceImage = imageSourceDir.childFile(imageName);
sourceImage.createSync(recursive: true); sourceImage.createSync(recursive: true);
sourceImage.writeAsStringSync("Ceci n'est pas une pipe"); sourceImage.writeAsStringSync("Ceci n'est pas une pipe");
final Template template = Template( logger = BufferLogger.test();
template = Template(
templateDir, templateDir,
imageSourceDir, imageSourceDir,
fileSystem: fileSystem, fileSystem: fileSystem,
logger: BufferLogger.test(), logger: logger,
templateRenderer: FakeTemplateRenderer(), templateRenderer: FakeTemplateRenderer(),
); );
template.render(destination, <String, Object>{}); });
testWithoutContext('overwrites .img.tmpl files with files from the image source', () {
expect(template.render(destination, <String, Object>{}), 1);
final File destinationImage = destination.childFile(imageName); final File destinationImage = destination.childFile(imageName);
final Uint8List sourceImageBytes = sourceImage.readAsBytesSync();
expect(destinationImage, exists); expect(destinationImage, exists);
expect(destinationImage.readAsBytesSync(), equals(sourceImage.readAsBytesSync())); expect(destinationImage.readAsBytesSync(), equals(sourceImageBytes));
expect(logger.errorText, isEmpty);
expect(logger.statusText, contains('${destinationImage.path} (created)'));
logger.clear();
// Run it again to overwrite (returns 1 file updated).
expect(template.render(destination, <String, Object>{}), 1);
expect(destinationImage.readAsBytesSync(), equals(sourceImageBytes));
expect(logger.errorText, isEmpty);
expect(logger.statusText, contains('${destinationImage.path} (overwritten)'));
});
testWithoutContext('does not overwrite .img.tmpl files with files from the image source', () {
expect(template.render(destination, <String, Object>{}), 1);
final File destinationImage = destination.childFile(imageName);
expect(destinationImage, exists);
expect(logger.errorText, isEmpty);
expect(logger.statusText, contains('${destinationImage.path} (created)'));
logger.clear();
// Run it again, do not overwrite (returns 0 files updated).
expect(template.render(destination, <String, Object>{}, overwriteExisting: false), 0);
expect(destinationImage, exists);
expect(logger.errorText, isEmpty);
expect(logger.statusText, isEmpty);
});
testWithoutContext('can suppress file printing', () {
template.render(destination, <String, Object>{}, printStatusWhenWriting: false);
final File destinationImage = destination.childFile(imageName);
expect(destinationImage, exists);
expect(logger.errorText, isEmpty);
expect(logger.statusText, isEmpty);
});
}); });
} }
......
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