Skip to content

Commit 27876e0

Browse files
Revert "Devfs cleanup and testing (flutter#33374)" (flutter#33673)
This reverts commit 445505d.
1 parent b7bd576 commit 27876e0

14 files changed

+284
-169
lines changed

packages/flutter_tools/bin/fuchsia_asset_builder.dart

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ Future<void> main(List<String> args) {
3636
});
3737
}
3838

39-
void writeFile(libfs.File outputFile, DevFSContent content) {
39+
Future<void> writeFile(libfs.File outputFile, DevFSContent content) async {
4040
outputFile.createSync(recursive: true);
41-
content.copyToFile(outputFile);
41+
final List<int> data = await content.contentsAsBytes();
42+
outputFile.writeAsBytesSync(data);
4243
}
4344

4445
Future<void> run(List<String> args) async {
@@ -70,10 +71,12 @@ Future<void> run(List<String> args) async {
7071
exit(1);
7172
}
7273

74+
final List<Future<void>> calls = <Future<void>>[];
7375
assets.entries.forEach((String fileName, DevFSContent content) {
7476
final libfs.File outputFile = libfs.fs.file(libfs.fs.path.join(assetDir, fileName));
75-
writeFile(outputFile, content);
77+
calls.add(writeFile(outputFile, content));
7678
});
79+
await Future.wait<void>(calls);
7780

7881
final String outputMan = argResults[_kOptionAssetManifestOut];
7982
await writeFuchsiaManifest(assets, argResults[_kOptionAsset], outputMan, argResults[_kOptionComponentName]);

packages/flutter_tools/lib/src/bundle.dart

+12-11
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Future<void> build({
147147
if (assets == null)
148148
throwToolExit('Error building assets', exitCode: 1);
149149

150-
assemble(
150+
await assemble(
151151
buildMode: buildMode,
152152
assetBundle: assets,
153153
kernelContent: kernelContent,
@@ -182,14 +182,14 @@ Future<AssetBundle> buildAssets({
182182
return assetBundle;
183183
}
184184

185-
void assemble({
185+
Future<void> assemble({
186186
BuildMode buildMode,
187187
AssetBundle assetBundle,
188188
DevFSContent kernelContent,
189189
String privateKeyPath = defaultPrivateKeyPath,
190190
String assetDirPath,
191191
String compilationTraceFilePath,
192-
}) {
192+
}) async {
193193
assetDirPath ??= getAssetBuildDirectory();
194194
printTrace('Building bundle');
195195

@@ -214,21 +214,22 @@ void assemble({
214214
printTrace('Writing asset files to $assetDirPath');
215215
ensureDirectoryExists(assetDirPath);
216216

217-
writeBundle(fs.directory(assetDirPath), assetEntries);
217+
await writeBundle(fs.directory(assetDirPath), assetEntries);
218218
printTrace('Wrote $assetDirPath');
219219
}
220220

221-
void writeBundle(
221+
Future<void> writeBundle(
222222
Directory bundleDir,
223223
Map<String, DevFSContent> assetEntries,
224-
) {
224+
) async {
225225
if (bundleDir.existsSync())
226226
bundleDir.deleteSync(recursive: true);
227227
bundleDir.createSync(recursive: true);
228228

229-
for (MapEntry<String, DevFSContent> entry in assetEntries.entries) {
230-
final File file = fs.file(fs.path.join(bundleDir.path, entry.key));
231-
file.parent.createSync(recursive: true);
232-
entry.value.copyToFile(file);
233-
}
229+
await Future.wait<void>(
230+
assetEntries.entries.map<Future<void>>((MapEntry<String, DevFSContent> entry) async {
231+
final File file = fs.file(fs.path.join(bundleDir.path, entry.key));
232+
file.parent.createSync(recursive: true);
233+
await file.writeAsBytes(await entry.value.contentsAsBytes());
234+
}));
234235
}

packages/flutter_tools/lib/src/commands/test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class TestCommand extends FastFlutterCommand {
242242
throwToolExit('Error: Failed to build asset bundle');
243243
}
244244
if (_needRebuild(assetBundle.entries)) {
245-
writeBundle(fs.directory(fs.path.join('build', 'unit_test_assets')),
245+
await writeBundle(fs.directory(fs.path.join('build', 'unit_test_assets')),
246246
assetBundle.entries);
247247
}
248248
}

packages/flutter_tools/lib/src/devfs.dart

+85-48
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,21 @@ abstract class DevFSContent {
3838
/// or if the given time is null.
3939
bool isModifiedAfter(DateTime time);
4040

41-
/// The number of bytes in this file.
4241
int get size;
4342

44-
/// Returns the raw bytes of this file.
45-
List<int> contentsAsBytes();
43+
Future<List<int>> contentsAsBytes();
4644

47-
/// Returns a gzipped representation of the contents of this file.
48-
List<int> contentsAsCompressedBytes() {
49-
return gzip.encode(contentsAsBytes());
45+
Stream<List<int>> contentsAsStream();
46+
47+
Stream<List<int>> contentsAsCompressedStream() {
48+
return contentsAsStream().cast<List<int>>().transform<List<int>>(gzip.encoder);
5049
}
5150

52-
/// Copies the content into the provided file.
53-
///
54-
/// Requires that the `destination` directory already exists, but the target
55-
/// file need not.
56-
void copyToFile(File destination);
51+
/// Return the list of files this content depends on.
52+
List<String> get fileDependencies => <String>[];
5753
}
5854

59-
/// File content to be copied to the device.
55+
// File content to be copied to the device.
6056
class DevFSFileContent extends DevFSContent {
6157
DevFSFileContent(this.file);
6258

@@ -106,6 +102,9 @@ class DevFSFileContent extends DevFSContent {
106102
}
107103
}
108104

105+
@override
106+
List<String> get fileDependencies => <String>[_getFile().path];
107+
109108
@override
110109
bool get isModified {
111110
final FileStat _oldFileStat = _fileStat;
@@ -136,12 +135,10 @@ class DevFSFileContent extends DevFSContent {
136135
}
137136

138137
@override
139-
List<int> contentsAsBytes() => _getFile().readAsBytesSync().cast<int>();
138+
Future<List<int>> contentsAsBytes() => _getFile().readAsBytes();
140139

141140
@override
142-
void copyToFile(File destination) {
143-
_getFile().copySync(destination.path);
144-
}
141+
Stream<List<int>> contentsAsStream() => _getFile().openRead();
145142
}
146143

147144
/// Byte content to be copied to the device.
@@ -178,15 +175,14 @@ class DevFSByteContent extends DevFSContent {
178175
int get size => _bytes.length;
179176

180177
@override
181-
List<int> contentsAsBytes() => _bytes;
178+
Future<List<int>> contentsAsBytes() async => _bytes;
182179

183180
@override
184-
void copyToFile(File destination) {
185-
destination.writeAsBytesSync(contentsAsBytes());
186-
}
181+
Stream<List<int>> contentsAsStream() =>
182+
Stream<List<int>>.fromIterable(<List<int>>[_bytes]);
187183
}
188184

189-
/// String content to be copied to the device encoded as utf8.
185+
/// String content to be copied to the device.
190186
class DevFSStringContent extends DevFSByteContent {
191187
DevFSStringContent(String string)
192188
: _string = string,
@@ -207,30 +203,76 @@ class DevFSStringContent extends DevFSByteContent {
207203
}
208204
}
209205

210-
class DevFSOperations {
211-
DevFSOperations(this.vmService, this.fsName)
212-
: httpAddress = vmService.httpAddress;
213-
214-
final VMService vmService;
215-
final String fsName;
216-
final Uri httpAddress;
217-
final HttpClient _client = HttpClient();
206+
/// Abstract DevFS operations interface.
207+
abstract class DevFSOperations {
208+
Future<Uri> create(String fsName);
209+
Future<dynamic> destroy(String fsName);
210+
Future<dynamic> writeFile(String fsName, Uri deviceUri, DevFSContent content);
211+
}
218212

219-
static const int kMaxInFlight = 6;
213+
/// An implementation of [DevFSOperations] that speaks to the
214+
/// vm service.
215+
class ServiceProtocolDevFSOperations implements DevFSOperations {
216+
ServiceProtocolDevFSOperations(this.vmService);
220217

221-
int _inFlight = 0;
222-
Map<Uri, DevFSContent> _outstanding;
223-
Completer<void> _completer;
218+
final VMService vmService;
224219

220+
@override
225221
Future<Uri> create(String fsName) async {
226222
final Map<String, dynamic> response = await vmService.vm.createDevFS(fsName);
227223
return Uri.parse(response['uri']);
228224
}
229225

230-
Future<void> destroy(String fsName) async {
226+
@override
227+
Future<dynamic> destroy(String fsName) async {
231228
await vmService.vm.deleteDevFS(fsName);
232229
}
233230

231+
@override
232+
Future<dynamic> writeFile(String fsName, Uri deviceUri, DevFSContent content) async {
233+
List<int> bytes;
234+
try {
235+
bytes = await content.contentsAsBytes();
236+
} catch (e) {
237+
return e;
238+
}
239+
final String fileContents = base64.encode(bytes);
240+
try {
241+
return await vmService.vm.invokeRpcRaw(
242+
'_writeDevFSFile',
243+
params: <String, dynamic>{
244+
'fsName': fsName,
245+
'uri': deviceUri.toString(),
246+
'fileContents': fileContents,
247+
},
248+
);
249+
} catch (error) {
250+
printTrace('DevFS: Failed to write $deviceUri: $error');
251+
}
252+
}
253+
}
254+
255+
class DevFSException implements Exception {
256+
DevFSException(this.message, [this.error, this.stackTrace]);
257+
final String message;
258+
final dynamic error;
259+
final StackTrace stackTrace;
260+
}
261+
262+
class _DevFSHttpWriter {
263+
_DevFSHttpWriter(this.fsName, VMService serviceProtocol)
264+
: httpAddress = serviceProtocol.httpAddress;
265+
266+
final String fsName;
267+
final Uri httpAddress;
268+
269+
static const int kMaxInFlight = 6;
270+
271+
int _inFlight = 0;
272+
Map<Uri, DevFSContent> _outstanding;
273+
Completer<void> _completer;
274+
final HttpClient _client = HttpClient();
275+
234276
Future<void> write(Map<Uri, DevFSContent> entries) async {
235277
_client.maxConnectionsPerHost = kMaxInFlight;
236278
_completer = Completer<void>();
@@ -259,9 +301,9 @@ class DevFSOperations {
259301
final HttpClientRequest request = await _client.putUrl(httpAddress);
260302
request.headers.removeAll(HttpHeaders.acceptEncodingHeader);
261303
request.headers.add('dev_fs_name', fsName);
262-
request.headers.add('dev_fs_uri_b64',
263-
base64.encode(utf8.encode(deviceUri.toString())));
264-
request.add(content.contentsAsCompressedBytes());
304+
request.headers.add('dev_fs_uri_b64', base64.encode(utf8.encode('$deviceUri')));
305+
final Stream<List<int>> contents = content.contentsAsCompressedStream();
306+
await request.addStream(contents);
265307
final HttpClientResponse response = await request.close();
266308
await response.drain<void>();
267309
} catch (error, trace) {
@@ -275,14 +317,6 @@ class DevFSOperations {
275317
}
276318
}
277319

278-
class DevFSException implements Exception {
279-
DevFSException(this.message, [this.error, this.stackTrace]);
280-
281-
final String message;
282-
final dynamic error;
283-
final StackTrace stackTrace;
284-
}
285-
286320
// Basic statistics for DevFS update operation.
287321
class UpdateFSReport {
288322
UpdateFSReport({
@@ -319,17 +353,20 @@ class DevFS {
319353
this.fsName,
320354
this.rootDirectory, {
321355
String packagesFilePath,
322-
}) : _operations = DevFSOperations(serviceProtocol, fsName),
356+
}) : _operations = ServiceProtocolDevFSOperations(serviceProtocol),
357+
_httpWriter = _DevFSHttpWriter(fsName, serviceProtocol),
323358
_packagesFilePath = packagesFilePath ?? fs.path.join(rootDirectory.path, kPackagesFileName);
324359

325360
DevFS.operations(
326361
this._operations,
327362
this.fsName,
328363
this.rootDirectory, {
329364
String packagesFilePath,
330-
}) : _packagesFilePath = packagesFilePath ?? fs.path.join(rootDirectory.path, kPackagesFileName);
365+
}) : _httpWriter = null,
366+
_packagesFilePath = packagesFilePath ?? fs.path.join(rootDirectory.path, kPackagesFileName);
331367

332368
final DevFSOperations _operations;
369+
final _DevFSHttpWriter _httpWriter;
333370
final String fsName;
334371
final Directory rootDirectory;
335372
String _packagesFilePath;
@@ -452,7 +489,7 @@ class DevFS {
452489
printTrace('Updating files');
453490
if (dirtyEntries.isNotEmpty) {
454491
try {
455-
await _operations.write(dirtyEntries);
492+
await _httpWriter.write(dirtyEntries);
456493
} on SocketException catch (socketException, stackTrace) {
457494
printTrace('DevFS sync failed. Lost connection to device: $socketException');
458495
throw DevFSException('Lost connection to device.', socketException, stackTrace);

packages/flutter_tools/lib/src/fuchsia/fuchsia_build.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Future<void> _buildAssets(
5252

5353
final Map<String, DevFSContent> assetEntries =
5454
Map<String, DevFSContent>.from(assets.entries);
55-
writeBundle(fs.directory(assetDir), assetEntries);
55+
await writeBundle(fs.directory(assetDir), assetEntries);
5656

5757
final String appName = fuchsiaProject.project.manifest.appName;
5858
final String outDir = getFuchsiaBuildDirectory();

packages/flutter_tools/lib/src/resident_web_runner.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ class ResidentWebRunner extends ResidentRunner {
120120
if (build != 0) {
121121
throwToolExit('Error: Failed to build asset bundle');
122122
}
123-
writeBundle(fs.directory(getAssetBuildDirectory()), assetBundle.entries);
123+
await writeBundle(
124+
fs.directory(getAssetBuildDirectory()), assetBundle.entries);
124125

125126
// Step 2: Start an HTTP server
126127
_server = WebAssetServer(flutterProject, target, ipv6);

packages/flutter_tools/lib/src/web/web_device.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class WebDevice extends Device {
119119
if (build != 0) {
120120
throwToolExit('Error: Failed to build asset bundle');
121121
}
122-
writeBundle(fs.directory(getAssetBuildDirectory()), assetBundle.entries);
122+
await writeBundle(fs.directory(getAssetBuildDirectory()), assetBundle.entries);
123123

124124
_package = package;
125125
_server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);

packages/flutter_tools/test/asset_bundle_package_fonts_test.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,22 @@ $fontsSection
7474
final String entryKey = 'packages/$packageName/$packageFont';
7575
expect(bundle.entries.containsKey(entryKey), true);
7676
expect(
77-
utf8.decode(bundle.entries[entryKey].contentsAsBytes()),
77+
utf8.decode(await bundle.entries[entryKey].contentsAsBytes()),
7878
packageFont,
7979
);
8080
}
8181

8282
for (String localFont in localFonts) {
8383
expect(bundle.entries.containsKey(localFont), true);
8484
expect(
85-
utf8.decode(bundle.entries[localFont].contentsAsBytes()),
85+
utf8.decode(await bundle.entries[localFont].contentsAsBytes()),
8686
localFont,
8787
);
8888
}
8989
}
9090

9191
expect(
92-
json.decode(utf8.decode(bundle.entries['FontManifest.json'].contentsAsBytes())),
92+
json.decode(utf8.decode(await bundle.entries['FontManifest.json'].contentsAsBytes())),
9393
json.decode(expectedAssetManifest),
9494
);
9595
}

0 commit comments

Comments
 (0)