diff --git a/pkgs/native_assets_builder/test_data/transformer/hook/build.dart b/pkgs/native_assets_builder/test_data/transformer/hook/build.dart index 53fba9533..856d61fc9 100644 --- a/pkgs/native_assets_builder/test_data/transformer/hook/build.dart +++ b/pkgs/native_assets_builder/test_data/transformer/hook/build.dart @@ -2,8 +2,10 @@ // for details. 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:convert'; import 'dart:io'; +import 'package:crypto/crypto.dart'; import 'package:native_assets_cli/data_assets.dart'; import 'package:transformer/src/transform.dart'; @@ -17,6 +19,14 @@ void main(List arguments) async { var transformedFiles = 0; var cachedFiles = 0; + final hashesFile = + File.fromUri(config.outputDirectoryShared.resolve('hashes.json')); + final hashes = await hashesFile.exists() + ? (json.decoder.convert(await hashesFile.readAsString()) as Map) + .cast() + : {}; + final newHashes = {}; + await for (final sourceFile in dataDirectory.list()) { if (sourceFile is! File) { continue; @@ -24,14 +34,16 @@ void main(List arguments) async { final sourceName = sourceFile.uri.pathSegments.lastWhere((e) => e.isNotEmpty); + final sourceContents = await sourceFile.readAsString(); + final sourceHash = md5.convert(sourceContents.codeUnits).toString(); + newHashes[sourceName] = sourceHash; + final prevHash = hashes[sourceName]; + final name = sourceName.replaceFirst('data', 'data_transformed'); final targetFile = File.fromUri(config.outputDirectoryShared .resolve(sourceName.replaceFirst('data', 'data_transformed'))); - // TODO(dacoharkes): Timestamps are not enough for correct caching. - if (!await targetFile.exists() || - !(await sourceFile.lastModified()) - .isBefore(await targetFile.lastModified())) { + if (!await targetFile.exists() || sourceHash != prevHash) { await transformFile(sourceFile, targetFile); transformedFiles++; } else { @@ -50,6 +62,8 @@ void main(List arguments) async { ); } + await hashesFile.writeAsString(json.encoder.convert(newHashes)); + print('Transformed $transformedFiles files.'); print('Reused $cachedFiles cached files.'); }); diff --git a/pkgs/native_assets_builder/test_data/transformer/pubspec.yaml b/pkgs/native_assets_builder/test_data/transformer/pubspec.yaml index f2902b783..77b0e9360 100644 --- a/pkgs/native_assets_builder/test_data/transformer/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/transformer/pubspec.yaml @@ -8,6 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: + crypto: ^3.0.6 native_assets_cli: ^0.10.0 # native_assets_cli: # path: ../../../native_assets_cli/