From 3ae1405987d500f9fa29fa183e464c56a2cfd5e1 Mon Sep 17 00:00:00 2001 From: Moritz Date: Thu, 18 Jul 2024 16:09:04 +0200 Subject: [PATCH] Check hashes on change --- .github/workflows/intl4x_artifacts.yml | 16 +++++++++ pkgs/intl4x/tool/generate_hashes.dart | 50 +++++++++++++++++++++----- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/.github/workflows/intl4x_artifacts.yml b/.github/workflows/intl4x_artifacts.yml index a8dd5732..e52a180d 100644 --- a/.github/workflows/intl4x_artifacts.yml +++ b/.github/workflows/intl4x_artifacts.yml @@ -4,9 +4,15 @@ permissions: contents: write on: + pull_request: + branches: [ main ] + paths: + - pkgs/intl4x/hook/hashes.dart push: tags: - 'intl4x-icu*' + schedule: + - cron: '0 0 * * 0' # weekly workflow_dispatch: jobs: @@ -138,6 +144,16 @@ jobs: with: name: dart-${{matrix.os}}-libs path: submodules/icu4x/bin + + check_hashes: + needs: dart-libs + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + steps: + - run: | + cd pkgs/intl4x + dart --enable-experiment=native-assets run tool/generate_hashes.dart local ../../submodules/icu4x/bin/ + git diff --name-only --exit-code release: needs: dart-libs diff --git a/pkgs/intl4x/tool/generate_hashes.dart b/pkgs/intl4x/tool/generate_hashes.dart index d667505a..5b53489b 100644 --- a/pkgs/intl4x/tool/generate_hashes.dart +++ b/pkgs/intl4x/tool/generate_hashes.dart @@ -5,25 +5,29 @@ import 'package:native_assets_cli/native_assets_cli.dart'; import '../hook/build.dart'; +final httpClient = HttpClient(); + Future main(List args) async { + final mode = args.firstOrNull; + String folder() => args[1]; + print('Checking hashes for $version'); final fileHashes = <(OS, Architecture), String>{}; final dynamicLibrary = File.fromUri(Directory.systemTemp.uri.resolve('lib')); await dynamicLibrary.create(); - final httpClient = HttpClient(); for (final os in OS.values) { for (final architecture in Architecture.values) { final target = '${os}_$architecture'; print('Checking hash for $target'); - final uri = Uri.parse( - 'https://github.com/dart-lang/i18n/releases/download/$version/$target'); - final request = await httpClient.getUrl(uri); - final response = await request.close(); - if (response.statusCode != 200) { - print('File not found at $uri'); + final bool success; + if (mode == 'fetch' || mode == null) { + success = await _fetchLibrary(target, httpClient, dynamicLibrary); + } else if (mode == 'local') { + success = await _copyLibrary(target, folder(), dynamicLibrary); } else { - await response.pipe(dynamicLibrary.openWrite()); - + throw UnsupportedError('Mode must be fetch or local'); + } + if (success) { final bytes = await dynamicLibrary.readAsBytes(); final fileHash = sha256.convert(bytes).toString(); fileHashes[(os, architecture)] = fileHash; @@ -56,6 +60,34 @@ ${fileHashes.map((key, value) => MapEntry( '''); } +Future _copyLibrary( + String target, + String folder, + File dynamicLibrary, +) async { + final fileUri = Directory.current.uri.resolve(folder).resolve(target); + final file = File.fromUri(fileUri); + if (await file.exists()) { + await file.copy(dynamicLibrary.path); + return true; + } + return false; +} + +Future _fetchLibrary( + String target, HttpClient httpClient, File dynamicLibrary) async { + final uri = Uri.parse( + 'https://github.com/dart-lang/i18n/releases/download/$version/$target'); + final request = await httpClient.getUrl(uri); + final response = await request.close(); + if (response.statusCode != 200) { + print('File not found at $uri'); + return false; + } + await response.pipe(dynamicLibrary.openWrite()); + return true; +} + extension OSExt on OS { String get varName => switch (this) { OS.linux => 'linux',