|
| 1 | +// Copyright (c) 2025, 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:convert'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:convert/convert.dart'; |
| 9 | +import 'package:crypto/crypto.dart'; |
| 10 | + |
| 11 | +class ExternalBuildStep { |
| 12 | + List<String> inputPaths; |
| 13 | + String outputPath; |
| 14 | + |
| 15 | + ExternalBuildStep({required this.inputPaths, required this.outputPath}); |
| 16 | + |
| 17 | + String get depsPath => '$outputPath.deps'; |
| 18 | + String get digestsPath => '$outputPath.digests'; |
| 19 | + |
| 20 | + bool needsToRun() { |
| 21 | + if (!File(depsPath).existsSync()) return true; |
| 22 | + if (!File(outputPath).existsSync()) return true; |
| 23 | + if (!File(digestsPath).existsSync()) return true; |
| 24 | + |
| 25 | + final digests = computeDigests(); |
| 26 | + final oldDigests = File(digestsPath).readAsStringSync(); |
| 27 | + return digests != oldDigests; |
| 28 | + } |
| 29 | + |
| 30 | + void stamp() { |
| 31 | + // TODO(davidmorgan): spaces. |
| 32 | + File(depsPath).writeAsStringSync('$outputPath: ${inputPaths.join(' ')}'); |
| 33 | + File(digestsPath).writeAsStringSync(computeDigests()); |
| 34 | + } |
| 35 | + |
| 36 | + String computeDigests() => ''' |
| 37 | +inputs digest: ${_computeDigest(inputPaths)} |
| 38 | +output digest: ${_computeDigest([outputPath])} |
| 39 | +'''; |
| 40 | + |
| 41 | + String _computeDigest(Iterable<String> deps) { |
| 42 | + final digestSink = AccumulatorSink<Digest>(); |
| 43 | + final result = md5.startChunkedConversion(digestSink); |
| 44 | + for (final dep in deps) { |
| 45 | + result.add(File(dep).readAsBytesSync()); |
| 46 | + } |
| 47 | + result.close(); |
| 48 | + return base64.encode(digestSink.events.first.bytes); |
| 49 | + } |
| 50 | +} |
0 commit comments