Skip to content

Commit

Permalink
Check hashes on change
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed Jul 18, 2024
1 parent 9944f52 commit 3ae1405
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/intl4x_artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
50 changes: 41 additions & 9 deletions pkgs/intl4x/tool/generate_hashes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ import 'package:native_assets_cli/native_assets_cli.dart';

import '../hook/build.dart';

final httpClient = HttpClient();

Future<void> main(List<String> 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;
Expand Down Expand Up @@ -56,6 +60,34 @@ ${fileHashes.map((key, value) => MapEntry(
''');
}

Future<bool> _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<bool> _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',
Expand Down

0 comments on commit 3ae1405

Please sign in to comment.