diff --git a/frontend_server_common/CHANGELOG.md b/frontend_server_common/CHANGELOG.md index 51d1510cd..a2a8ac29e 100644 --- a/frontend_server_common/CHANGELOG.md +++ b/frontend_server_common/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.1.1 +- Remove dead code + ## 0.1.0 - Initial version diff --git a/frontend_server_common/lib/src/asset.dart b/frontend_server_common/lib/src/asset.dart deleted file mode 100644 index e08128db0..000000000 --- a/frontend_server_common/lib/src/asset.dart +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2020 The Dart Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// @dart = 2.9 - -// Note: this is a copy from flutter tools, updated to work with dwds tests, -// most functionality removed - -import 'dart:async'; - -import 'devfs_content.dart'; - -const AssetBundleFactory _manifestFactory = _MockManifestAssetBundleFactory(); - -const String defaultManifestPath = 'pubspec.yaml'; - -/// Injected factory class for spawning [AssetBundle] instances. -abstract class AssetBundleFactory { - static AssetBundleFactory get defaultInstance => _manifestFactory; - - /// Creates a new [AssetBundle]. - AssetBundle createBundle(); -} - -abstract class AssetBundle { - Map get entries; - - /// Returns 0 for success; non-zero for failure. - Future build({ - String manifestPath = defaultManifestPath, - String assetDirPath, - String packagesPath, - bool includeDefaultFonts = true, - bool reportLicensedPackages = false, - }); -} - -class _MockManifestAssetBundleFactory implements AssetBundleFactory { - const _MockManifestAssetBundleFactory(); - - @override - AssetBundle createBundle() => _MockManifestAssetBundle(); -} - -class _MockManifestAssetBundle implements AssetBundle { - /// Constructs an [_MockManifestAssetBundle] that gathers the set of assets from the - /// pubspec.yaml manifest. - _MockManifestAssetBundle(); - - @override - final Map entries = {}; - - @override - Future build({ - String manifestPath = defaultManifestPath, - String assetDirPath, - String packagesPath, - bool includeDefaultFonts = true, - bool reportLicensedPackages = false, - }) async { - return 0; - } -} diff --git a/frontend_server_common/lib/src/devfs.dart b/frontend_server_common/lib/src/devfs.dart index 2fa2027a8..323c92fc1 100644 --- a/frontend_server_common/lib/src/devfs.dart +++ b/frontend_server_common/lib/src/devfs.dart @@ -10,22 +10,17 @@ import 'dart:io'; import 'package:dwds/dwds.dart'; import 'package:file/file.dart'; -import 'package:logging/logging.dart'; import 'package:meta/meta.dart'; import 'package:package_config/package_config.dart'; import 'package:path/path.dart' as p; -import 'asset.dart'; import 'asset_server.dart'; import 'bootstrap.dart'; -import 'devfs_content.dart'; import 'frontend_server_client.dart'; import 'utilities.dart'; final String dartWebSdkPath = p.join(dartSdkPath, 'lib', 'dev_compiler'); -Logger _logger = Logger('WebDevFs'); - class WebDevFS { WebDevFS({ this.fileSystem, @@ -71,7 +66,6 @@ class WebDevFS { Future update({ String mainPath, - AssetBundle bundle, String dillOutputPath, @required ResidentCompiler generator, List invalidatedFiles, @@ -101,17 +95,8 @@ class WebDevFS { soundNullSafety ? dartSdkSourcemapSound : dartSdkSourcemap; assetServer.writeFile('/dart_sdk.js', sdk.readAsStringSync()); assetServer.writeFile('/dart_sdk.js.map', sdkSourceMap.readAsStringSync()); - // TODO(jonahwilliams): refactor the asset code in this and the regular devfs to - // be shared. - if (bundle != null) { - await writeBundle( - fileSystem.directory(p.joinAll(['build', 'assets'])), - bundle.entries, - ); - } generator.reset(); - var compilerOutput = await generator.recompile( Uri.parse('org-dartlang-app:///$mainPath'), invalidatedFiles, outputPath: p.join(dillOutputPath, 'app.dill'), @@ -191,6 +176,30 @@ class WebDevFS { )); } +class UpdateFSReport { + final bool _success; + final int _invalidatedSourcesCount; + final int _syncedBytes; + + UpdateFSReport({ + bool success = false, + int invalidatedSourcesCount = 0, + int syncedBytes = 0, + }) : _success = success, + _invalidatedSourcesCount = invalidatedSourcesCount, + _syncedBytes = syncedBytes; + + bool get success => _success; + int get invalidatedSourcesCount => _invalidatedSourcesCount; + int get syncedBytes => _syncedBytes; + + /// JavaScript modules produced by the incremental compiler in `dartdevc` + /// mode. + /// + /// Only used for JavaScript compilation. + List invalidatedModules; +} + String _filePathToUriFragment(String path) { if (Platform.isWindows) { var startWithSlash = path.startsWith('/'); @@ -203,26 +212,3 @@ String _filePathToUriFragment(String path) { } return path; } - -Future writeBundle( - Directory bundleDir, Map assetEntries) async { - if (bundleDir.existsSync()) { - try { - bundleDir.deleteSync(recursive: true); - } on FileSystemException catch (e, s) { - _logger.warning( - 'Failed to clean up asset directory ${bundleDir.path}.\n' - 'To clean build artifacts, use the command "flutter clean".', - e, - s); - } - } - bundleDir.createSync(recursive: true); - - await Future.wait(assetEntries.entries - .map>((MapEntry entry) async { - var file = fileSystem.file(fileSystem.path.join(bundleDir.path, entry.key)); - file.parent.createSync(recursive: true); - await file.writeAsBytes(await entry.value.contentsAsBytes()); - })); -} diff --git a/frontend_server_common/lib/src/devfs_content.dart b/frontend_server_common/lib/src/devfs_content.dart deleted file mode 100644 index 3b09c15c7..000000000 --- a/frontend_server_common/lib/src/devfs_content.dart +++ /dev/null @@ -1,230 +0,0 @@ -// Copyright 2020 The Dart Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// @dart = 2.9 - -// Note: this is a copy from flutter tools, updated to work with dwds tests - -import 'dart:async'; -import 'dart:convert'; - -import 'package:file/file.dart'; -import 'package:logging/logging.dart'; - -import 'utilities.dart'; - -Logger _logger = Logger('DevFsContent'); - -/// Common superclass for content copied to the device. -abstract class DevFSContent { - /// Return true if this is the first time this method is called - /// or if the entry has been modified since this method was last called. - bool get isModified; - - /// Return true if this is the first time this method is called - /// or if the entry has been modified after the given time - /// or if the given time is null. - bool isModifiedAfter(DateTime time); - - int get size; - - Future> contentsAsBytes(); - - Stream> contentsAsStream(); - - /// Return the list of files this content depends on. - List get fileDependencies => []; -} - -// File content to be copied to the device. -class DevFSFileContent extends DevFSContent { - DevFSFileContent(this.file); - - final FileSystemEntity file; - File _linkTarget; - FileStat _fileStat; - - File _getFile() { - if (_linkTarget != null) { - return _linkTarget; - } - if (file is Link) { - // The link target. - return fileSystem.file(file.resolveSymbolicLinksSync()); - } - return file as File; - } - - void _stat() { - if (_linkTarget != null) { - // Stat the cached symlink target. - var fileStat = _linkTarget.statSync(); - if (fileStat.type == FileSystemEntityType.notFound) { - _linkTarget = null; - } else { - _fileStat = fileStat; - return; - } - } - var fileStat = file.statSync(); - _fileStat = - fileStat.type == FileSystemEntityType.notFound ? null : fileStat; - if (_fileStat != null && _fileStat.type == FileSystemEntityType.link) { - // Resolve, stat the symlink target. - var resolved = file.resolveSymbolicLinksSync(); - var linkTarget = fileSystem.file(resolved); - // Stat the link target. - var fileStat = linkTarget.statSync(); - if (fileStat.type == FileSystemEntityType.notFound) { - _fileStat = null; - _linkTarget = null; - } - } - if (_fileStat == null) { - _logger.severe( - 'Unable to get status of file "${file.path}": file not found.'); - } - } - - @override - List get fileDependencies => [_getFile().path]; - - @override - bool get isModified { - var _oldFileStat = _fileStat; - _stat(); - if (_oldFileStat == null && _fileStat == null) { - return false; - } - return _oldFileStat == null || - _fileStat == null || - _fileStat.modified.isAfter(_oldFileStat.modified); - } - - @override - bool isModifiedAfter(DateTime time) { - var _oldFileStat = _fileStat; - _stat(); - if (_oldFileStat == null && _fileStat == null) { - return false; - } - return time == null || - _oldFileStat == null || - _fileStat == null || - _fileStat.modified.isAfter(time); - } - - @override - int get size { - if (_fileStat == null) { - _stat(); - } - // Can still be null if the file wasn't found. - return _fileStat?.size ?? 0; - } - - @override - Future> contentsAsBytes() => _getFile().readAsBytes(); - - @override - Stream> contentsAsStream() => _getFile().openRead(); -} - -/// Byte content to be copied to the device. -class DevFSByteContent extends DevFSContent { - DevFSByteContent(this._bytes); - - List _bytes; - - bool _isModified = true; - DateTime _modificationTime = DateTime.now(); - - List get bytes => _bytes; - - set bytes(List value) { - _bytes = value; - _isModified = true; - _modificationTime = DateTime.now(); - } - - /// Return true only once so that the content is written to the device only once. - @override - bool get isModified { - var modified = _isModified; - _isModified = false; - return modified; - } - - @override - bool isModifiedAfter(DateTime time) { - return time == null || _modificationTime.isAfter(time); - } - - @override - int get size => _bytes.length; - - @override - Future> contentsAsBytes() async => _bytes; - - @override - Stream> contentsAsStream() => - Stream>.fromIterable(>[_bytes]); -} - -/// String content to be copied to the device. -class DevFSStringContent extends DevFSByteContent { - DevFSStringContent(String string) - : _string = string, - super(utf8.encode(string)); - - String _string; - - String get string => _string; - - set string(String value) { - _string = value; - super.bytes = utf8.encode(_string); - } - - @override - set bytes(List value) { - string = utf8.decode(value); - } -} - -// Basic statistics for DevFS update operation. -class UpdateFSReport { - UpdateFSReport({ - bool success = false, - int invalidatedSourcesCount = 0, - int syncedBytes = 0, - }) { - _success = success; - _invalidatedSourcesCount = invalidatedSourcesCount; - _syncedBytes = syncedBytes; - } - - bool get success => _success; - int get invalidatedSourcesCount => _invalidatedSourcesCount; - int get syncedBytes => _syncedBytes; - - /// JavaScript modules produced by the incremental compiler in `dartdevc` - /// mode. - /// - /// Only used for JavaScript compilation. - List invalidatedModules; - - void incorporateResults(UpdateFSReport report) { - if (!report._success) { - _success = false; - } - _invalidatedSourcesCount += report._invalidatedSourcesCount; - _syncedBytes += report._syncedBytes; - invalidatedModules ??= report.invalidatedModules; - } - - bool _success; - int _invalidatedSourcesCount; - int _syncedBytes; -} diff --git a/frontend_server_common/lib/src/frontend_server_client.dart b/frontend_server_common/lib/src/frontend_server_client.dart index 68f9a236b..9d3f92050 100644 --- a/frontend_server_common/lib/src/frontend_server_client.dart +++ b/frontend_server_common/lib/src/frontend_server_client.dart @@ -164,9 +164,9 @@ abstract class _CompilationRequest { Completer completer; - Future _run(DefaultResidentCompiler compiler); + Future _run(ResidentCompiler compiler); - Future run(DefaultResidentCompiler compiler) async { + Future run(ResidentCompiler compiler) async { completer.complete(await _run(compiler)); } } @@ -186,7 +186,7 @@ class _RecompileRequest extends _CompilationRequest { PackageConfig packageConfig; @override - Future _run(DefaultResidentCompiler compiler) async => + Future _run(ResidentCompiler compiler) async => compiler._recompile(this); } @@ -209,7 +209,7 @@ class _CompileExpressionRequest extends _CompilationRequest { bool isStatic; @override - Future _run(DefaultResidentCompiler compiler) async => + Future _run(ResidentCompiler compiler) async => compiler._compileExpression(this); } @@ -234,7 +234,7 @@ class _CompileExpressionToJsRequest extends _CompilationRequest { String expression; @override - Future _run(DefaultResidentCompiler compiler) async => + Future _run(ResidentCompiler compiler) async => compiler._compileExpressionToJs(this); } @@ -242,7 +242,7 @@ class _RejectRequest extends _CompilationRequest { _RejectRequest(Completer completer) : super(completer); @override - Future _run(DefaultResidentCompiler compiler) async => + Future _run(ResidentCompiler compiler) async => compiler._reject(); } @@ -251,75 +251,8 @@ class _RejectRequest extends _CompilationRequest { /// /// The wrapper is intended to stay resident in memory as user changes, reloads, /// restarts the Flutter app. -abstract class ResidentCompiler { - factory ResidentCompiler( - String sdkRoot, { - String packageConfigPath, - List fileSystemRoots, - String fileSystemScheme, - String platformDill, - bool verbose, - CompilerMessageConsumer compilerMessageConsumer, - }) = DefaultResidentCompiler; - - // TODO(jonahwilliams): find a better way to configure additional file system - // roots from the runner. - // See: https://github.com/flutter/flutter/issues/50494 - void addFileSystemRoot(String root); - - /// If invoked for the first time, it compiles Dart script identified by - /// [mainUri], [invalidatedFiles] list is ignored. - /// On successive runs [invalidatedFiles] indicates which files need to be - /// recompiled. If [mainUri] is null, previously used [mainUri] entry - /// point that is used for recompilation. - /// Binary file name is returned if compilation was successful, otherwise - /// null is returned. - Future recompile(Uri mainUri, List invalidatedFiles, - {@required String outputPath, @required PackageConfig packageConfig}); - - Future compileExpression( - String expression, - List definitions, - List typeDefinitions, - String libraryUri, - String klass, - bool isStatic, - ); - - Future compileExpressionToJs( - String libraryUri, - int line, - int column, - Map jsModules, - Map jsFrameValues, - String moduleName, - String expression); - - /// Should be invoked when results of compilation are accepted by the client. - /// - /// Either [accept] or [reject] should be called after every [recompile] call. - void accept(); - - /// Should be invoked when results of compilation are rejected by the client. - /// - /// Either [accept] or [reject] should be called after every [recompile] call. - Future reject(); - - /// Should be invoked when frontend server compiler should forget what was - /// accepted previously so that next call to [recompile] produces complete - /// kernel file. - void reset(); - - /// stop the service normally - Future shutdown(); - - /// kill the service - Future kill(); -} - -@visibleForTesting -class DefaultResidentCompiler implements ResidentCompiler { - DefaultResidentCompiler( +class ResidentCompiler { + ResidentCompiler( String sdkRoot, { this.packageConfigPath, this.fileSystemRoots, @@ -338,11 +271,6 @@ class DefaultResidentCompiler implements ResidentCompiler { final String platformDill; final bool verbose; - @override - void addFileSystemRoot(String root) { - fileSystemRoots.add(root); - } - /// The path to the root of the Dart SDK used to compile. final String sdkRoot; @@ -353,7 +281,13 @@ class DefaultResidentCompiler implements ResidentCompiler { final StreamController<_CompilationRequest> _controller = StreamController<_CompilationRequest>(); - @override + /// If invoked for the first time, it compiles Dart script identified by + /// [mainUri], [invalidatedFiles] list is ignored. + /// On successive runs [invalidatedFiles] indicates which files need to be + /// recompiled. If [mainUri] is null, previously used [mainUri] entry + /// point that is used for recompilation. + /// Binary file name is returned if compilation was successful, otherwise + /// null is returned. Future recompile(Uri mainUri, List invalidatedFiles, {@required String outputPath, @required PackageConfig packageConfig}) async { @@ -485,7 +419,7 @@ class DefaultResidentCompiler implements ResidentCompiler { return _stdoutHandler.compilerOutput.future; } - @override + /// Compile dart expression to kernel. Future compileExpression( String expression, List definitions, @@ -528,7 +462,7 @@ class DefaultResidentCompiler implements ResidentCompiler { return _stdoutHandler.compilerOutput.future; } - @override + /// Compiles dart expression to JavaScript. Future compileExpressionToJs( String libraryUri, int line, @@ -577,7 +511,9 @@ class DefaultResidentCompiler implements ResidentCompiler { return _stdoutHandler.compilerOutput.future; } - @override + /// Should be invoked when results of compilation are accepted by the client. + /// + /// Either [accept] or [reject] should be called after every [recompile] call. void accept() { if (_compileRequestNeedsConfirmation) { _server.stdin.writeln('accept'); @@ -586,7 +522,9 @@ class DefaultResidentCompiler implements ResidentCompiler { _compileRequestNeedsConfirmation = false; } - @override + /// Should be invoked when results of compilation are rejected by the client. + /// + /// Either [accept] or [reject] should be called after every [recompile] call. Future reject() { if (!_controller.hasListener) { _controller.stream.listen(_handleCompilationRequest); @@ -608,7 +546,9 @@ class DefaultResidentCompiler implements ResidentCompiler { return _stdoutHandler.compilerOutput.future; } - @override + /// Should be invoked when frontend server compiler should forget what was + /// accepted previously so that next call to [recompile] produces complete + /// kernel file. void reset() { _server?.stdin?.writeln('reset'); _logger.info('<- reset'); @@ -620,7 +560,7 @@ class DefaultResidentCompiler implements ResidentCompiler { return _server.exitCode; } - @override + /// stop the service normally Future shutdown() async { // Server was never successfully created. if (_server == null) { @@ -629,7 +569,7 @@ class DefaultResidentCompiler implements ResidentCompiler { return quit(); } - @override + /// kill the service Future kill() async { if (_server == null) { return 0; diff --git a/frontend_server_common/lib/src/resident_runner.dart b/frontend_server_common/lib/src/resident_runner.dart index f5329ebe9..81c90c3cf 100644 --- a/frontend_server_common/lib/src/resident_runner.dart +++ b/frontend_server_common/lib/src/resident_runner.dart @@ -13,9 +13,7 @@ import 'package:dwds/dwds.dart'; import 'package:logging/logging.dart'; import 'package:path/path.dart' as p; -import 'asset.dart'; import 'devfs.dart'; -import 'devfs_content.dart'; import 'frontend_server_client.dart'; import 'utilities.dart'; @@ -56,7 +54,6 @@ class ResidentWebRunner { ResidentCompiler generator; ExpressionCompiler expressionCompiler; - AssetBundle assetBundle; WebDevFS devFS; Uri uri; Iterable modules; @@ -64,8 +61,6 @@ class ResidentWebRunner { Future run(String hostname, int port, String root) async { hostname ??= 'localhost'; - assetBundle = AssetBundleFactory.defaultInstance.createBundle(); - devFS = WebDevFS( fileSystem: fileSystem, hostname: hostname, @@ -90,14 +85,8 @@ class ResidentWebRunner { } Future _updateDevFS() async { - var result = await assetBundle.build(); - if (result != 0) { - return UpdateFSReport(success: false); - } - var report = await devFS.update( mainPath: mainPath, - bundle: assetBundle, dillOutputPath: outputPath, generator: generator, invalidatedFiles: []); diff --git a/frontend_server_common/lib/src/utilities.dart b/frontend_server_common/lib/src/utilities.dart index 9ca429eff..801cc600b 100644 --- a/frontend_server_common/lib/src/utilities.dart +++ b/frontend_server_common/lib/src/utilities.dart @@ -20,6 +20,5 @@ final String _sdkDir = (() { })(); final String dartSdkPath = _sdkDir; -final String dartPath = p.join(_sdkDir, 'bin', 'dart'); const fs.FileSystem fileSystem = LocalFileSystem();