Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native_assets_cli] Move non-build.dart code to package:native_assets_builder #881

Merged
merged 6 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/health.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
uses: dart-lang/ecosystem/.github/workflows/health.yaml@main
with:
coverage_web: false
checks: "version,changelog,license,do-not-submit,breaking"
checks: "version,changelog,license,do-not-submit,breaking,coverage"
use-flutter: true
sdk: master
permissions:
Expand Down
1 change: 1 addition & 0 deletions pkgs/native_assets_builder/dart_test.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
paths:
- test/build_runner/
- test/model/
1 change: 1 addition & 0 deletions pkgs/native_assets_builder/lib/native_assets_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
// BSD-style license that can be found in the LICENSE file.

export 'package:native_assets_builder/src/build_runner/build_runner.dart';
export 'package:native_assets_builder/src/model/asset.dart';
export 'package:native_assets_builder/src/package_layout/package_layout.dart';
62 changes: 62 additions & 0 deletions pkgs/native_assets_builder/lib/src/model/asset.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2024, 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 'package:native_assets_cli/native_assets_cli.dart';

import '../utils/yaml.dart';

extension on AssetPath {}

extension AssetIterable on Iterable<Asset> {
Iterable<Asset> whereLinkMode(LinkMode linkMode) =>
where((e) => e.linkMode == linkMode);

Map<Target, List<Asset>> get assetsPerTarget {
final result = <Target, List<Asset>>{};
for (final asset in this) {
final assets = result[asset.target] ?? [];
assets.add(asset);
result[asset.target] = assets;
}
return result;
}

Map<String, Map<String, List<String>>> toDartConst() => {
for (final entry in assetsPerTarget.entries)
entry.key.toString(): _combineMaps(entry.value
.map((e) => {
e.id: _toDartConst(e.path),
})
.toList())
};

Map<Object, Object> toNativeAssetsFileEncoding() => {
'format-version': [1, 0, 0],
'native-assets': toDartConst(),
};

String toNativeAssetsFile() => yamlEncode(toNativeAssetsFileEncoding());
}

List<String> _toDartConst(AssetPath path) {
switch (path) {
case AssetAbsolutePath _:
return ['absolute', path.uri.toFilePath()];
case AssetSystemPath _:
return ['system', path.uri.toFilePath()];
case AssetInProcess _:
return ['process'];
default:
assert(path is AssetInExecutable);
return ['executable'];
}
}

Map<X, Y> _combineMaps<X, Y>(Iterable<Map<X, Y>> maps) {
final result = <X, Y>{};
for (final map in maps) {
result.addAll(map);
}
return result;
}
18 changes: 18 additions & 0 deletions pkgs/native_assets_builder/lib/src/utils/yaml.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2024, 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 'package:yaml/yaml.dart';
import 'package:yaml_edit/yaml_edit.dart';

String yamlEncode(Object yamlEncoding) {
final editor = YamlEditor('');
editor.update(
[],
wrapAsYamlNode(
yamlEncoding,
collectionStyle: CollectionStyle.BLOCK,
),
);
return editor.toString();
}
3 changes: 2 additions & 1 deletion pkgs/native_assets_builder/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ dependencies:
logging: ^1.2.0
native_assets_cli: ^0.3.2
package_config: ^2.1.0
yaml: ^3.1.2
yaml_edit: ^2.1.0

dev_dependencies:
dart_flutter_team_lints: ^2.1.1
file_testing: ^3.0.0
test: ^1.24.3
yaml: ^3.1.2
90 changes: 90 additions & 0 deletions pkgs/native_assets_builder/test/model/asset_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2024, 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.

// ignore_for_file: undefined_hidden_name

import 'package:native_assets_builder/src/model/asset.dart';
import 'package:native_assets_cli/native_assets_cli.dart'
hide AssetIterable, AssetRelativePath;
import 'package:test/test.dart';

void main() {
final fooUri = Uri.file('path/to/libfoo.so');
final foo3Uri = Uri(path: 'libfoo3.so');
final barUri = Uri(path: 'path/to/libbar.a');
final blaUri = Uri(path: 'path/with spaces/bla.dll');
final assets = [
Asset(
id: 'foo',
path: AssetAbsolutePath(fooUri),
target: Target.androidX64,
linkMode: LinkMode.dynamic,
),
Asset(
id: 'foo3',
path: AssetSystemPath(foo3Uri),
target: Target.androidX64,
linkMode: LinkMode.dynamic,
),
Asset(
id: 'foo4',
path: AssetInExecutable(),
target: Target.androidX64,
linkMode: LinkMode.dynamic,
),
Asset(
id: 'foo5',
path: AssetInProcess(),
target: Target.androidX64,
linkMode: LinkMode.dynamic,
),
Asset(
id: 'bar',
path: AssetAbsolutePath(barUri),
target: Target.linuxArm64,
linkMode: LinkMode.static,
),
Asset(
id: 'bla',
path: AssetAbsolutePath(blaUri),
target: Target.windowsX64,
linkMode: LinkMode.dynamic,
),
];

final assetsDartEncoding = '''format-version:
- 1
- 0
- 0
native-assets:
android_x64:
foo:
- absolute
- ${fooUri.toFilePath()}
foo3:
- system
- ${foo3Uri.toFilePath()}
foo4:
- executable
foo5:
- process
linux_arm64:
bar:
- absolute
- ${barUri.toFilePath()}
windows_x64:
bla:
- absolute
- ${blaUri.toFilePath()}''';

test('asset yaml', () async {
final fileContents = assets.toNativeAssetsFile();
expect(fileContents, assetsDartEncoding);
});

test('List<Asset> whereLinkMode', () async {
final assets2 = assets.whereLinkMode(LinkMode.dynamic);
expect(assets2.length, 5);
});
}
5 changes: 4 additions & 1 deletion pkgs/native_assets_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## 0.3.3-wip
## 0.4.0-wip

- Added [example/use_dart_api/](example/use_dart_api/) detailing how to use
`dart_api_dl.h` from the Dart SDK in native code.
- **Breaking change** Moved code not used in `build.dart` to
`package:native_assets_builder`.


## 0.3.2

Expand Down
Loading
Loading