Skip to content

Commit

Permalink
download assets in build hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes committed Jan 6, 2025
1 parent 8ec8291 commit d7b26fd
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,42 @@
// 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:io';

import 'package:download_asset/src/hook_helpers/c_build.dart';
import 'package:download_asset/src/hook_helpers/download.dart';
import 'package:native_assets_cli/code_assets_builder.dart';
import 'package:native_assets_cli/native_assets_cli.dart';

void main(List<String> args) async {
// TODO(https://github.com/dart-lang/native/issues/39): Use user-defines to
// control this instead.
const localBuild = false;

await build(args, (config, output) async {
// ignore: dead_code
if (localBuild) {
await runBuild(config, output);
} else {
// TODO(dcharkes): Download from GitHub.
final targetOS = config.codeConfig.targetOS;
final targetArchitecture = config.codeConfig.targetArchitecture;
final iOSSdk =
targetOS == OS.iOS ? config.codeConfig.iOSConfig.targetSdk : null;
final outputDirectory = Directory.fromUri(config.outputDirectory);
final file = await downloadAsset(
targetOS,
targetArchitecture,
iOSSdk,
outputDirectory,
);
output.codeAssets.add(CodeAsset(
package: config.packageName,
name: 'native_add.dart',
linkMode: DynamicLoadingBundled(),
os: targetOS,
architecture: targetArchitecture,
file: file.uri,
));
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import 'package:native_assets_cli/code_assets_builder.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';

Future<void> runBuild(BuildConfig config, BuildOutputBuilder output) async {
final target = createTargetName(
final name = createTargetName(
config.codeConfig.targetOS.name,
config.codeConfig.targetArchitecture.name,
config.codeConfig.targetOS == OS.iOS
? config.codeConfig.iOSConfig.targetSdk.type
: null,
);
final cbuilder = CBuilder.library(
name: 'native_add_$target',
name: name,
assetName: 'native_add.dart',
sources: [
'src/native_add.c',
Expand All @@ -31,7 +31,7 @@ Future<void> runBuild(BuildConfig config, BuildOutputBuilder output) async {
}

String createTargetName(String osString, String architecture, String? iOSSdk) {
var targetName = '${osString}_$architecture';
var targetName = 'native_add_${osString}_$architecture';
if (iOSSdk != null) {
targetName += '_$iOSSdk';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// 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:io';

import 'package:crypto/crypto.dart';
import 'package:native_assets_cli/code_assets_builder.dart';

import 'c_build.dart';
import 'version.dart';

Uri downloadUri(String target) => Uri.parse(
'https://github.com/dart-lang/native/releases/download/$version/$target');

Future<File> downloadAsset(
OS targetOS,
Architecture targetArchitecture,
IOSSdk? iOSSdk,
Directory outputDirectory,
) async {
final targetName = targetOS.dylibFileName(createTargetName(
targetOS.name,
targetArchitecture.name,
iOSSdk?.type,
));
final uri = downloadUri(targetName);
final request = await HttpClient().getUrl(uri);
final response = await request.close();
if (response.statusCode != 200) {
throw ArgumentError('The request to $uri failed.');
}
final library = File.fromUri(outputDirectory.uri.resolve(targetName));
await library.create();
await response.pipe(library.openWrite());
return library;
}

Future<String> hashAsset(File assetFile) async {
final fileHash = md5.convert(await assetFile.readAsBytes()).toString();
return fileHash;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@
//

const assetHashes = <String, String>{
'android_arm': '2c38f3edc805a399dad866d619f9157d',
'android_arm64': 'c4f0d8c4c50d1e83592e499e7434b967',
'android_ia32': 'e3277144d97bd2c54beee581ed7e6665',
'android_riscv64': '8c2576cbe75c9a23f2532ff895f94f76',
'android_x64': '9a7bec53e1591091669ecd2bd20911d1',
'ios_arm64_iphoneos': 'e9a4d8b4d47efc05da36c7b05991d174',
'ios_arm64_iphonesimulator': 'a745567274206e879a267c4f6ab998db',
'ios_x64_iphonesimulator': '6568ce774690893512c108bb4e2d1974',
'linux_arm': '1a5b9e4b459e13ee85c148582b9b2252',
'linux_arm64': '2b3d736e0c0ac1e1537bc43bd9b82cad',
'linux_ia32': 'ed6e130e53fa18eab5572ed106cdaab1',
'linux_riscv64': '7fa82325ba7803a0443ca27e3300e7f9',
'linux_x64': 'f7af8d1547cdfb150a73d513a7957999',
'macos_arm64': '075398c86f0a13f0ee166bc8aaf1e41a',
'macos_x64': 'ed56424be52561da5a57d3f5af837ce2',
'windows_arm64': 'c0a52c089afa52f6532717c4c91e904f',
'windows_ia32': '70b7c31e4546764618600f380a0db869',
'windows_x64': '73d2f8f4daf35111370a8a6fa380ac69',
'libnative_add_android_arm.so': '2c38f3edc805a399dad866d619f9157d',
'libnative_add_android_arm64.so': 'c4f0d8c4c50d1e83592e499e7434b967',
'libnative_add_android_ia32.so': 'e3277144d97bd2c54beee581ed7e6665',
'libnative_add_android_riscv64.so': '8c2576cbe75c9a23f2532ff895f94f76',
'libnative_add_android_x64.so': '9a7bec53e1591091669ecd2bd20911d1',
'libnative_add_ios_arm64_iphoneos.dylib': '1bf1473cacb7fd2778fc5bb28f0b61a2',
'libnative_add_ios_arm64_iphonesimulator.dylib':
'cdddffc0787e6a3a846affcb05fac3a8',
'libnative_add_ios_x64_iphonesimulator.dylib':
'4aea6d631350540d50452ac2bdd1a422',
'libnative_add_linux_arm.so': '1a5b9e4b459e13ee85c148582b9b2252',
'libnative_add_linux_arm64.so': '2b3d736e0c0ac1e1537bc43bd9b82cad',
'libnative_add_linux_ia32.so': 'ed6e130e53fa18eab5572ed106cdaab1',
'libnative_add_linux_riscv64.so': '7fa82325ba7803a0443ca27e3300e7f9',
'libnative_add_linux_x64.so': 'f7af8d1547cdfb150a73d513a7957999',
'libnative_add_macos_arm64.dylib': 'f0804ff4b55126996c180114f25ca5bd',
'libnative_add_macos_x64.dylib': 'b62263803dceb3c23508cb909b5a5583',
'native_add_windows_arm64.dll': '7aa6f5e0275ba1b94cd5b7356f89ef04',
'native_add_windows_ia32.dll': 'ab57b5504d92b5b5dc09732d990fd5e7',
'native_add_windows_x64.dll': 'be9ba2125800aa2e3481a759b1845a50',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c, null) 2025, the Dart project authors. Please see the AUTHORS file
// 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.

// THIS FILE IS AUTOGENERATED. TO UPDATE, RUN
//
// dart --enable-experiment=native-assets tool/generate_asset_hashes.dart
//

import 'package:native_assets_cli/code_assets_builder.dart';

const supportedTargets = [
(OS.android, Architecture.arm, null),
(OS.android, Architecture.arm64, null),
(OS.android, Architecture.ia32, null),
(OS.android, Architecture.riscv64, null),
(OS.android, Architecture.x64, null),
(OS.iOS, Architecture.arm64, IOSSdk.iPhoneOS),
(OS.iOS, Architecture.arm64, IOSSdk.iPhoneSimulator),
(OS.iOS, Architecture.x64, IOSSdk.iPhoneSimulator),
(OS.linux, Architecture.arm, null),
(OS.linux, Architecture.arm64, null),
(OS.linux, Architecture.ia32, null),
(OS.linux, Architecture.riscv64, null),
(OS.linux, Architecture.x64, null),
(OS.macOS, Architecture.arm64, null),
(OS.macOS, Architecture.x64, null),
(OS.windows, Architecture.arm64, null),
(OS.windows, Architecture.ia32, null),
(OS.windows, Architecture.x64, null),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// 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 'download.dart';
import 'hashes.dart';

/// The GitHub release to use for downloading assets.
///
/// Assets are downloaded from [downloadUri].
///
/// After changing [assetHashes] must be updated.
const version = 'download_asset-prebuild-assets-v0.1.0-try-3';
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ environment:
sdk: '>=3.3.0 <4.0.0'

dependencies:
crypto: ^3.0.3
logging: ^1.1.1
native_assets_cli: ^0.10.0
# native_assets_cli:
Expand All @@ -19,7 +20,6 @@ dependencies:

dev_dependencies:
args: ^2.6.0
crypto: ^3.0.3
ffigen: ^8.0.2
lints: ^3.0.0
test: ^1.21.0
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'dart:io';

import 'package:args/args.dart';
import 'package:download_asset/src/hook_helpers/c_build.dart';
import 'package:download_asset/src/hook_helpers/versions.dart';
import 'package:download_asset/src/hook_helpers/target_versions.dart';
import 'package:native_assets_cli/code_assets_builder.dart';

void main(List<String> args) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@

import 'dart:io';

import 'package:crypto/crypto.dart';
import 'package:download_asset/src/hook_helpers/download.dart';
import 'package:download_asset/src/hook_helpers/hashes.dart';
import 'package:download_asset/src/hook_helpers/targets.dart';

// TODO(dcharkes): Change this to download the files from GitHub releases,
// and run it manually _after_ a GitHub release before releasing a new build
// hook that uses the new assets.
/// Regenerates [assetHashes].
Future<void> main(List<String> args) async {
final assetsDir = Directory.fromUri(
Platform.script.resolve('../.dart_tool/download_asset/'));
await assetsDir.delete(recursive: true);
await assetsDir.create(recursive: true);
await Future.wait([
for (final (targetOS, targetArchitecture, iOSSdk) in supportedTargets)
downloadAsset(targetOS, targetArchitecture, iOSSdk, assetsDir),
]);
final assetFiles = assetsDir
.listSync(recursive: true)
.whereType<File>()
.toList()
..sort((f1, f2) => f1.path.compareTo(f2.path));
final assetHashes = <String, String>{};
for (final assetFile in assetFiles) {
final fileHash = md5.convert(await assetFile.readAsBytes()).toString();
final target =
assetFile.uri.resolve('.').pathSegments.lastWhere((e) => e.isNotEmpty);
final fileHash = await hashAsset(assetFile);
final target = assetFile.uri.pathSegments.lastWhere((e) => e.isNotEmpty);
assetHashes[target] = fileHash;
}

Expand Down

0 comments on commit d7b26fd

Please sign in to comment.