Reland "[ Widget Previews ] Add support for detecting previews and generating code (#161911)"" (#162337)
Reverts flutter/flutter#162327
This commit is contained in:
@@ -11,6 +11,7 @@ import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/commands/widget_preview.dart';
|
||||
import 'package:flutter_tools/src/dart/pub.dart';
|
||||
import 'package:flutter_tools/src/globals.dart' as globals;
|
||||
import 'package:flutter_tools/src/widget_preview/preview_code_generator.dart';
|
||||
|
||||
import '../../src/common.dart';
|
||||
import '../../src/context.dart';
|
||||
@@ -33,8 +34,12 @@ void main() {
|
||||
tryToDelete(tempDir);
|
||||
});
|
||||
|
||||
Future<String> createRootProject() async {
|
||||
return createProject(tempDir, arguments: <String>['--pub']);
|
||||
Future<Directory> createRootProject() async {
|
||||
return globals.fs.directory(await createProject(tempDir, arguments: <String>['--pub']));
|
||||
}
|
||||
|
||||
Directory widgetPreviewScaffoldFromRootProject({required Directory rootProject}) {
|
||||
return rootProject.childDirectory('.dart_tool').childDirectory('widget_preview_scaffold');
|
||||
}
|
||||
|
||||
Future<void> runWidgetPreviewCommand(List<String> arguments) async {
|
||||
@@ -43,28 +48,29 @@ void main() {
|
||||
}
|
||||
|
||||
Future<void> startWidgetPreview({
|
||||
required String? rootProjectPath,
|
||||
required Directory? rootProject,
|
||||
List<String>? arguments,
|
||||
}) async {
|
||||
await runWidgetPreviewCommand(<String>[
|
||||
'start',
|
||||
...?arguments,
|
||||
if (rootProjectPath != null) rootProjectPath,
|
||||
if (rootProject != null) rootProject.path,
|
||||
]);
|
||||
final Directory widgetPreviewScaffoldDir = widgetPreviewScaffoldFromRootProject(
|
||||
rootProject: rootProject ?? globals.fs.currentDirectory,
|
||||
);
|
||||
expect(widgetPreviewScaffoldDir, exists);
|
||||
expect(
|
||||
globals.fs
|
||||
.directory(rootProjectPath ?? globals.fs.currentDirectory.path)
|
||||
.childDirectory('.dart_tool')
|
||||
.childDirectory('widget_preview_scaffold'),
|
||||
widgetPreviewScaffoldDir.childFile(PreviewCodeGenerator.generatedPreviewFilePath),
|
||||
exists,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> cleanWidgetPreview({required String rootProjectPath}) async {
|
||||
await runWidgetPreviewCommand(<String>['clean', rootProjectPath]);
|
||||
Future<void> cleanWidgetPreview({required Directory rootProject}) async {
|
||||
await runWidgetPreviewCommand(<String>['clean', rootProject.path]);
|
||||
expect(
|
||||
globals.fs
|
||||
.directory(rootProjectPath)
|
||||
.directory(rootProject)
|
||||
.childDirectory('.dart_tool')
|
||||
.childDirectory('widget_preview_scaffold'),
|
||||
isNot(exists),
|
||||
@@ -93,7 +99,7 @@ void main() {
|
||||
|
||||
testUsingContext('run outside of a Flutter project directory', () async {
|
||||
try {
|
||||
await startWidgetPreview(rootProjectPath: tempDir.path);
|
||||
await startWidgetPreview(rootProject: tempDir);
|
||||
fail('Successfully executed outside of a Flutter project directory');
|
||||
} on ToolExit catch (e) {
|
||||
expect(e.message, contains('${tempDir.path} is not a valid Flutter project.'));
|
||||
@@ -104,8 +110,8 @@ void main() {
|
||||
testUsingContext(
|
||||
'start creates .dart_tool/widget_preview_scaffold',
|
||||
() async {
|
||||
final String rootProjectPath = await createRootProject();
|
||||
await startWidgetPreview(rootProjectPath: rootProjectPath);
|
||||
final Directory rootProject = await createRootProject();
|
||||
await startWidgetPreview(rootProject: rootProject);
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
Pub:
|
||||
@@ -123,11 +129,87 @@ void main() {
|
||||
testUsingContext(
|
||||
'start creates .dart_tool/widget_preview_scaffold in the CWD',
|
||||
() async {
|
||||
final String rootProjectPath = await createRootProject();
|
||||
final Directory rootProject = await createRootProject();
|
||||
await io.IOOverrides.runZoned<Future<void>>(() async {
|
||||
// Try to execute using the CWD.
|
||||
await startWidgetPreview(rootProjectPath: null);
|
||||
}, getCurrentDirectory: () => globals.fs.directory(rootProjectPath));
|
||||
await startWidgetPreview(rootProject: null);
|
||||
}, getCurrentDirectory: () => rootProject);
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
Pub:
|
||||
() => Pub.test(
|
||||
fileSystem: globals.fs,
|
||||
logger: globals.logger,
|
||||
processManager: globals.processManager,
|
||||
botDetector: globals.botDetector,
|
||||
platform: globals.platform,
|
||||
stdio: mockStdio,
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
const String samplePreviewFile = '''
|
||||
// This doesn't need to be valid code for testing as long as it has the @Preview() annotation
|
||||
@Preview()
|
||||
WidgetPreview preview() => WidgetPreview();''';
|
||||
|
||||
const String expectedGeneratedFileContents = '''
|
||||
// ignore_for_file: no_leading_underscores_for_library_prefixes
|
||||
import 'package:flutter_project/foo.dart' as _i1;import 'package:widget_preview/widget_preview.dart';List<WidgetPreview> previews() => [_i1.preview()];''';
|
||||
|
||||
testUsingContext(
|
||||
'start finds existing previews and injects them into ${PreviewCodeGenerator.generatedPreviewFilePath}',
|
||||
() async {
|
||||
final Directory rootProject = await createRootProject();
|
||||
final Directory widgetPreviewScaffoldDir = widgetPreviewScaffoldFromRootProject(
|
||||
rootProject: rootProject,
|
||||
);
|
||||
rootProject
|
||||
.childDirectory('lib')
|
||||
.childFile('foo.dart')
|
||||
.writeAsStringSync(samplePreviewFile);
|
||||
|
||||
final File generatedFile = widgetPreviewScaffoldDir.childFile(
|
||||
PreviewCodeGenerator.generatedPreviewFilePath,
|
||||
);
|
||||
|
||||
await startWidgetPreview(rootProject: rootProject);
|
||||
expect(generatedFile.readAsStringSync(), expectedGeneratedFileContents);
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
Pub:
|
||||
() => Pub.test(
|
||||
fileSystem: globals.fs,
|
||||
logger: globals.logger,
|
||||
processManager: globals.processManager,
|
||||
botDetector: globals.botDetector,
|
||||
platform: globals.platform,
|
||||
stdio: mockStdio,
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
testUsingContext(
|
||||
'start finds existing previews in the CWD and injects them into ${PreviewCodeGenerator.generatedPreviewFilePath}',
|
||||
() async {
|
||||
final Directory rootProject = await createRootProject();
|
||||
final Directory widgetPreviewScaffoldDir = widgetPreviewScaffoldFromRootProject(
|
||||
rootProject: rootProject,
|
||||
);
|
||||
rootProject
|
||||
.childDirectory('lib')
|
||||
.childFile('foo.dart')
|
||||
.writeAsStringSync(samplePreviewFile);
|
||||
|
||||
final File generatedFile = widgetPreviewScaffoldDir.childFile(
|
||||
PreviewCodeGenerator.generatedPreviewFilePath,
|
||||
);
|
||||
|
||||
await io.IOOverrides.runZoned<Future<void>>(() async {
|
||||
// Try to execute using the CWD.
|
||||
await startWidgetPreview(rootProject: null);
|
||||
expect(generatedFile.readAsStringSync(), expectedGeneratedFileContents);
|
||||
}, getCurrentDirectory: () => globals.fs.directory(rootProject));
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
Pub:
|
||||
@@ -145,9 +227,9 @@ void main() {
|
||||
testUsingContext(
|
||||
'clean deletes .dart_tool/widget_preview_scaffold',
|
||||
() async {
|
||||
final String rootProjectPath = await createRootProject();
|
||||
await startWidgetPreview(rootProjectPath: rootProjectPath);
|
||||
await cleanWidgetPreview(rootProjectPath: rootProjectPath);
|
||||
final Directory rootProject = await createRootProject();
|
||||
await startWidgetPreview(rootProject: rootProject);
|
||||
await cleanWidgetPreview(rootProject: rootProject);
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
Pub:
|
||||
@@ -166,12 +248,12 @@ void main() {
|
||||
'invokes pub in online and offline modes',
|
||||
() async {
|
||||
// Run pub online first in order to populate the pub cache.
|
||||
final String rootProjectPath = await createRootProject();
|
||||
final Directory rootProject = await createRootProject();
|
||||
loggingProcessManager.clear();
|
||||
|
||||
final RegExp dartCommand = RegExp(r'dart-sdk[\\/]bin[\\/]dart');
|
||||
|
||||
await startWidgetPreview(rootProjectPath: rootProjectPath);
|
||||
await startWidgetPreview(rootProject: rootProject);
|
||||
expect(
|
||||
loggingProcessManager.commands,
|
||||
contains(
|
||||
@@ -182,12 +264,12 @@ void main() {
|
||||
),
|
||||
);
|
||||
|
||||
await cleanWidgetPreview(rootProjectPath: rootProjectPath);
|
||||
await cleanWidgetPreview(rootProject: rootProject);
|
||||
|
||||
// Run pub offline.
|
||||
loggingProcessManager.clear();
|
||||
await startWidgetPreview(
|
||||
rootProjectPath: rootProjectPath,
|
||||
rootProject: rootProject,
|
||||
arguments: <String>['--pub', '--offline'],
|
||||
);
|
||||
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:file_testing/file_testing.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/logger.dart';
|
||||
import 'package:flutter_tools/src/flutter_manifest.dart';
|
||||
import 'package:flutter_tools/src/project.dart';
|
||||
import 'package:flutter_tools/src/widget_preview/preview_code_generator.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../../src/context.dart';
|
||||
|
||||
void main() {
|
||||
group('$PreviewCodeGenerator', () {
|
||||
late PreviewCodeGenerator codeGenerator;
|
||||
late FlutterProject project;
|
||||
|
||||
setUp(() {
|
||||
final FileSystem fs = MemoryFileSystem.test();
|
||||
final FlutterManifest manifest = FlutterManifest.empty(logger: BufferLogger.test());
|
||||
final Directory projectDir =
|
||||
fs.currentDirectory.childDirectory('project')
|
||||
..createSync()
|
||||
..childDirectory('lib').createSync();
|
||||
project = FlutterProject(projectDir, manifest, manifest);
|
||||
codeGenerator = PreviewCodeGenerator(widgetPreviewScaffoldProject: project, fs: fs);
|
||||
});
|
||||
|
||||
testUsingContext(
|
||||
'correctly generates ${PreviewCodeGenerator.generatedPreviewFilePath}',
|
||||
() async {
|
||||
// Check that the generated preview file doesn't exist yet.
|
||||
final File generatedPreviewFile = project.directory.childFile(
|
||||
PreviewCodeGenerator.generatedPreviewFilePath,
|
||||
);
|
||||
expect(generatedPreviewFile, isNot(exists));
|
||||
|
||||
// Populate the generated preview file.
|
||||
codeGenerator.populatePreviewsInGeneratedPreviewScaffold(const <String, List<String>>{
|
||||
'foo.dart': <String>['preview'],
|
||||
'src/bar.dart': <String>['barPreview1', 'barPreview2'],
|
||||
});
|
||||
expect(generatedPreviewFile, exists);
|
||||
|
||||
// Check that the generated file contains:
|
||||
// - An import of the widget preview library
|
||||
// - Prefixed imports for both 'foo.dart' and 'src/bar.dart'
|
||||
// - A top-level function 'List<WidgetPreview> previews()'
|
||||
// - A returned list containing function calls to 'preview()' from 'foo.dart' and 'barPreview1()'
|
||||
// and 'barPreview2()' from 'src/bar.dart'
|
||||
//
|
||||
// The generated file is unfortunately unformatted.
|
||||
const String expectedGeneratedPreviewFileContents = '''
|
||||
// ignore_for_file: no_leading_underscores_for_library_prefixes
|
||||
import 'foo.dart' as _i1;import 'src/bar.dart' as _i2;import 'package:widget_preview/widget_preview.dart';List<WidgetPreview> previews() => [_i1.preview(), _i2.barPreview1(), _i2.barPreview2(), ];''';
|
||||
expect(generatedPreviewFile.readAsStringSync(), expectedGeneratedPreviewFileContents);
|
||||
|
||||
// Regenerate the generated file with no previews.
|
||||
codeGenerator.populatePreviewsInGeneratedPreviewScaffold(const <String, List<String>>{});
|
||||
expect(generatedPreviewFile, exists);
|
||||
|
||||
// The generated file should only contain:
|
||||
// - An import of the widget preview library
|
||||
// - A top-level function 'List<WidgetPreview> previews()' that returns an empty list.
|
||||
const String emptyGeneratedPreviewFileContents = '''
|
||||
import 'package:widget_preview/widget_preview.dart';List<WidgetPreview> previews() => [];''';
|
||||
expect(generatedPreviewFile.readAsStringSync(), emptyGeneratedPreviewFileContents);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/logger.dart';
|
||||
import 'package:flutter_tools/src/base/signals.dart';
|
||||
import 'package:flutter_tools/src/widget_preview/preview_detector.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../../src/common.dart';
|
||||
import '../../src/context.dart';
|
||||
|
||||
Directory createBasicProjectStructure(FileSystem fs) {
|
||||
return fs.systemTempDirectory.createTempSync('root');
|
||||
}
|
||||
|
||||
File addPreviewContainingFile(Directory projectRoot, String path) {
|
||||
return projectRoot.childDirectory('lib').childFile(path)
|
||||
..createSync(recursive: true)
|
||||
..writeAsStringSync(previewContainingFileContents);
|
||||
}
|
||||
|
||||
File addNonPreviewContainingFile(Directory projectRoot, String path) {
|
||||
return projectRoot.childDirectory('lib').childFile(path)
|
||||
..createSync(recursive: true)
|
||||
..writeAsStringSync(nonPreviewContainingFileContents);
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('$PreviewDetector', () {
|
||||
// Note: we don't use a MemoryFileSystem since we don't have a way to
|
||||
// provide it to package:analyzer APIs without writing a significant amount
|
||||
// of wrapper logic.
|
||||
late LocalFileSystem fs;
|
||||
late Logger logger;
|
||||
late PreviewDetector previewDetector;
|
||||
late Directory projectRoot;
|
||||
void Function(PreviewMapping)? onChangeDetected;
|
||||
|
||||
void onChangeDetectedRoot(PreviewMapping mapping) {
|
||||
onChangeDetected!(mapping);
|
||||
}
|
||||
|
||||
setUp(() {
|
||||
fs = LocalFileSystem.test(signals: Signals.test());
|
||||
projectRoot = createBasicProjectStructure(fs);
|
||||
logger = BufferLogger.test();
|
||||
previewDetector = PreviewDetector(logger: logger, onChangeDetected: onChangeDetectedRoot);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await previewDetector.dispose();
|
||||
projectRoot.deleteSync(recursive: true);
|
||||
onChangeDetected = null;
|
||||
});
|
||||
|
||||
testUsingContext('can detect previews in existing files', () async {
|
||||
final List<File> previewFiles = <File>[
|
||||
addPreviewContainingFile(projectRoot, 'foo.dart'),
|
||||
addPreviewContainingFile(projectRoot, 'src/bar.dart'),
|
||||
];
|
||||
addNonPreviewContainingFile(projectRoot, 'baz.dart');
|
||||
final PreviewMapping mapping = previewDetector.findPreviewFunctions(projectRoot);
|
||||
expect(mapping.keys.toSet(), previewFiles.map((File e) => e.uri.toString()).toSet());
|
||||
});
|
||||
|
||||
testUsingContext('can detect previews in updated files', () async {
|
||||
// Create two files with existing previews and one without.
|
||||
final PreviewMapping expectedInitialMapping = <String, List<String>>{
|
||||
addPreviewContainingFile(projectRoot, 'foo.dart').uri.toString(): <String>['previews'],
|
||||
addPreviewContainingFile(projectRoot, 'src/bar.dart').uri.toString(): <String>['previews'],
|
||||
};
|
||||
final File nonPreviewContainingFile = addNonPreviewContainingFile(projectRoot, 'baz.dart');
|
||||
|
||||
Completer<void> completer = Completer<void>();
|
||||
onChangeDetected = (PreviewMapping updated) {
|
||||
// The new preview in baz.dart should be included in the preview mapping.
|
||||
expect(updated, <String, List<String>>{
|
||||
...expectedInitialMapping,
|
||||
nonPreviewContainingFile.uri.toString(): <String>['previews'],
|
||||
});
|
||||
completer.complete();
|
||||
};
|
||||
// Initialize the file watcher.
|
||||
final PreviewMapping initialPreviews = await previewDetector.initialize(projectRoot);
|
||||
expect(initialPreviews, expectedInitialMapping);
|
||||
|
||||
// Update the file without an existing preview to include a preview and ensure it triggers
|
||||
// the preview detector.
|
||||
addPreviewContainingFile(projectRoot, 'baz.dart');
|
||||
await completer.future;
|
||||
|
||||
completer = Completer<void>();
|
||||
onChangeDetected = (PreviewMapping updated) {
|
||||
// The removed preview in baz.dart should not longer be included in the preview mapping.
|
||||
expect(updated, expectedInitialMapping);
|
||||
completer.complete();
|
||||
};
|
||||
|
||||
// Update the file with an existing preview to remove the preview and ensure it triggers
|
||||
// the preview detector.
|
||||
addNonPreviewContainingFile(projectRoot, 'baz.dart');
|
||||
await completer.future;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const String previewContainingFileContents = '''
|
||||
@Preview()
|
||||
// This isn't necessarily valid code. We're just looking for the annotation
|
||||
WidgetPreview previews() => WidgetPreview();
|
||||
''';
|
||||
|
||||
const String nonPreviewContainingFileContents = '''
|
||||
String foo() => 'bar';
|
||||
''';
|
||||
Reference in New Issue
Block a user