|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | + |
| 8 | +import 'package:build/build.dart'; |
| 9 | +import 'package:glob/glob.dart'; |
| 10 | +import 'package:meta/meta.dart'; |
| 11 | + |
| 12 | +import '../environment/io_environment.dart'; |
| 13 | +import 'reader.dart'; |
| 14 | +import 'writer.dart'; |
| 15 | + |
| 16 | +/// A batch of file system writes that should be committed at once instead of |
| 17 | +/// when [AssetWriter.writeAsBytes] or [AssetWriter.writeAsString] is called. |
| 18 | +/// |
| 19 | +/// During a typical build run emitting generated files one-by-one, it's |
| 20 | +/// possible that other running tools such as an analysis server will have to |
| 21 | +/// re-analyze incomplete states multiple times. |
| 22 | +/// By storing pending outputs in memory first and then committing them at the |
| 23 | +/// end of the build, we have a better view over that needs to happen. |
| 24 | +/// |
| 25 | +/// The default [IOEnvironment] uses readers and writes that are batch-aware |
| 26 | +/// outside of low-memory mode. |
| 27 | +final class _FileSystemWriteBatch { |
| 28 | + final Map<AssetId, _PendingFileState> _pendingWrites = {}; |
| 29 | + |
| 30 | + _FileSystemWriteBatch._(); |
| 31 | + |
| 32 | + Future<void> completeWrites(RunnerAssetWriter writer) async { |
| 33 | + await Future.wait(_pendingWrites.keys.map((id) async { |
| 34 | + final pending = _pendingWrites[id]!; |
| 35 | + |
| 36 | + if (pending.content case final content?) { |
| 37 | + await writer.writeAsBytes(id, content); |
| 38 | + } else { |
| 39 | + await writer.delete(id); |
| 40 | + } |
| 41 | + })); |
| 42 | + |
| 43 | + _pendingWrites.clear(); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Wraps a pair of a [RunnerAssetReader] with path-prividing capabilities and |
| 48 | +/// a [RunnerAssetWriter] into a pair of readers and writers that will |
| 49 | +/// internally buffer writes and only flush them in |
| 50 | +/// [RunnerAssetWriter.completeBuild]. |
| 51 | +/// |
| 52 | +/// The returned reader will see pending writes by the returned writer before |
| 53 | +/// they are flushed to the file system. |
| 54 | +(RunnerAssetReader, RunnerAssetWriter) wrapInBatch({ |
| 55 | + required RunnerAssetReader reader, |
| 56 | + required PathProvidingAssetReader pathProvidingReader, |
| 57 | + required RunnerAssetWriter writer, |
| 58 | +}) { |
| 59 | + final batch = _FileSystemWriteBatch._(); |
| 60 | + |
| 61 | + return ( |
| 62 | + BatchReader(reader, pathProvidingReader, batch), |
| 63 | + BatchWriter(writer, batch), |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +final class _PendingFileState { |
| 68 | + final List<int>? content; |
| 69 | + |
| 70 | + const _PendingFileState(this.content); |
| 71 | + |
| 72 | + bool get isDeleted => content == null; |
| 73 | +} |
| 74 | + |
| 75 | +@internal |
| 76 | +final class BatchReader extends AssetReader |
| 77 | + implements RunnerAssetReader, PathProvidingAssetReader { |
| 78 | + final RunnerAssetReader _inner; |
| 79 | + final PathProvidingAssetReader _innerPathProviding; |
| 80 | + final _FileSystemWriteBatch _batch; |
| 81 | + |
| 82 | + BatchReader(this._inner, this._innerPathProviding, this._batch); |
| 83 | + |
| 84 | + _PendingFileState? _stateFor(AssetId id) { |
| 85 | + return _batch._pendingWrites[id]; |
| 86 | + } |
| 87 | + |
| 88 | + @override |
| 89 | + Future<bool> canRead(AssetId id) async { |
| 90 | + if (_stateFor(id) case final state?) { |
| 91 | + return !state.isDeleted; |
| 92 | + } else { |
| 93 | + return await _inner.canRead(id); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @override |
| 98 | + Stream<AssetId> findAssets(Glob glob, {String? package}) { |
| 99 | + return _inner |
| 100 | + .findAssets(glob, package: package) |
| 101 | + .where((asset) => _stateFor(asset)?.isDeleted != true); |
| 102 | + } |
| 103 | + |
| 104 | + @override |
| 105 | + String pathTo(AssetId id) { |
| 106 | + return _innerPathProviding.pathTo(id); |
| 107 | + } |
| 108 | + |
| 109 | + @override |
| 110 | + Future<List<int>> readAsBytes(AssetId id) async { |
| 111 | + if (_stateFor(id) case final state?) { |
| 112 | + if (state.isDeleted) { |
| 113 | + throw AssetNotFoundException(id); |
| 114 | + } else { |
| 115 | + return state.content!; |
| 116 | + } |
| 117 | + } else { |
| 118 | + return await _inner.readAsBytes(id); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @override |
| 123 | + Future<String> readAsString(AssetId id, {Encoding encoding = utf8}) async { |
| 124 | + if (_stateFor(id) case final state?) { |
| 125 | + if (state.isDeleted) { |
| 126 | + throw AssetNotFoundException(id); |
| 127 | + } else { |
| 128 | + return encoding.decode(state.content!); |
| 129 | + } |
| 130 | + } else { |
| 131 | + return await _inner.readAsString(id, encoding: encoding); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +@internal |
| 137 | +final class BatchWriter extends RunnerAssetWriter { |
| 138 | + final RunnerAssetWriter _inner; |
| 139 | + final _FileSystemWriteBatch _batch; |
| 140 | + |
| 141 | + BatchWriter(this._inner, this._batch); |
| 142 | + |
| 143 | + @override |
| 144 | + Future delete(AssetId id) async { |
| 145 | + _batch._pendingWrites[id] = const _PendingFileState(null); |
| 146 | + } |
| 147 | + |
| 148 | + @override |
| 149 | + Future<void> writeAsBytes(AssetId id, List<int> bytes) async { |
| 150 | + _batch._pendingWrites[id] = _PendingFileState(bytes); |
| 151 | + } |
| 152 | + |
| 153 | + @override |
| 154 | + Future<void> writeAsString(AssetId id, String contents, |
| 155 | + {Encoding encoding = utf8}) async { |
| 156 | + _batch._pendingWrites[id] = _PendingFileState(encoding.encode(contents)); |
| 157 | + } |
| 158 | + |
| 159 | + @override |
| 160 | + Future<void> completeBuild() async { |
| 161 | + await _batch.completeWrites(_inner); |
| 162 | + } |
| 163 | +} |
0 commit comments