Skip to content

Commit 243e25f

Browse files
authored
Move package:native_assets_builder (#69)
1 parent fa7b822 commit 243e25f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2119
-5
lines changed

.github/labeler.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
# This configures the .github/workflows/pull_request_label.yml workflow.
1+
# This configures the .github/workflows/pull_request_label.yml workflow.
22

33
'type-infra':
44
- '.github/**'
55

66
'package:c_compiler':
77
- pkgs/c_compiler/**/*
88

9+
'package:native_assets_builder':
10+
- pkgs/native_assets_builder/**/*
11+
912
'package:native_assets_cli':
1013
- pkgs/native_assets_cli/**/*

.github/workflows/dart.yaml

+20-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
matrix:
2222
os: [ubuntu, macos, windows]
2323
sdk: [stable, dev]
24-
package: [c_compiler, native_assets_cli]
24+
package: [c_compiler, native_assets_builder, native_assets_cli]
2525
exclude:
2626
# Only run analyze against dev on one host.
2727
- os: macos
@@ -49,8 +49,25 @@ jobs:
4949

5050
- run: dart pub get
5151

52-
- run: dart pub get
53-
working-directory: pkgs/native_assets_cli/example/native_add/
52+
- run: dart pub get -C test/data/dart_app/
53+
if: ${{ matrix.package == 'native_assets_builder' }}
54+
55+
- run: dart pub get -C test/data/native_add/
56+
if: ${{ matrix.package == 'native_assets_builder' }}
57+
58+
- run: dart pub get -C test/data/native_add_add_source/
59+
if: ${{ matrix.package == 'native_assets_builder' }}
60+
61+
- run: dart pub get -C test/data/native_subtract/
62+
if: ${{ matrix.package == 'native_assets_builder' }}
63+
64+
- run: dart pub get -C test/data/package_reading_metadata/
65+
if: ${{ matrix.package == 'native_assets_builder' }}
66+
67+
- run: dart pub get -C test/data/package_with_metadata/
68+
if: ${{ matrix.package == 'native_assets_builder' }}
69+
70+
- run: dart pub get -C example/native_add/
5471
if: ${{ matrix.package == 'native_assets_cli' }}
5572

5673
- run: dart analyze --fatal-infos

AUTHORS

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Below is a list of people and organizations that have contributed
2+
# to the Dart project. Names should be added to the list like so:
3+
#
4+
# Name/Organization <email address>
5+
6+
Google LLC

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ bundling.
1212
| -------------------------------------------- | ------------------------------------------------------------------------------------------- | ------- |
1313
| [c_compiler](pkgs/c_compiler/) | A library to invoke the native C compiler installed on the host machine. | |
1414
| [native_assets_cli](pkgs/native_assets_cli/) | A library that contains the argument and file formats for implementing a native assets CLI. | |
15+
| [native_assets_builder](pkgs/native_assets_builder/) | A library that contains the logic for building native assets. This should not be used by users, and is used as shared implementation between dartdev and flutter_tools. | |
1516

1617
<!-- ## Publishing automation
1718

pkgs/native_assets_builder/.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# https://dart.dev/guides/libraries/private-files
2+
# Created by `dart pub`
3+
.dart_tool/
4+
5+
# Avoid committing pubspec.lock for library packages; see
6+
# https://dart.dev/guides/libraries/private-files#pubspeclock.
7+
pubspec.lock
8+
9+
coverage/

pkgs/native_assets_builder/LICENSE

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2023, the Dart project authors.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following
11+
disclaimer in the documentation and/or other materials provided
12+
with the distribution.
13+
* Neither the name of Google LLC nor the names of its
14+
contributors may be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pkgs/native_assets_builder/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This package contains the logic for building native assets.
2+
3+
This package is the backend that invokes toplevel `build.dart` scripts.
4+
For more info on these scripts see https://github.com/dart-lang/native.
5+
6+
This is a separate package so that dartdev and flutter_tools can reuse
7+
the same logic with flutter_tools having to import dartdev.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
include: package:dart_flutter_team_lints/analysis_options.yaml
2+
3+
analyzer:
4+
language:
5+
strict-raw-types: true
6+
strict-inference: true
7+
8+
linter:
9+
rules:
10+
- always_declare_return_types
11+
- avoid_dynamic_calls
12+
- camel_case_types
13+
- depend_on_referenced_packages
14+
- directives_ordering
15+
- prefer_const_declarations
16+
- prefer_expression_function_bodies
17+
- prefer_final_in_for_each
18+
- prefer_final_locals
19+
- prefer_relative_imports
20+
- prefer_single_quotes
21+
- sort_pub_dependencies
22+
- unawaited_futures
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
paths:
2+
- test/build_runner/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
export 'package:native_assets_builder/src/build_runner/build_runner.dart';
6+
export 'package:native_assets_builder/src/package_layout/package_layout.dart';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:convert';
6+
import 'dart:io';
7+
8+
import 'package:graphs/graphs.dart' as graphs;
9+
import 'package:package_config/package_config.dart';
10+
11+
class NativeAssetsBuildPlanner {
12+
final PackageGraph packageGraph;
13+
final List<Package> packagesWithNativeAssets;
14+
final Uri dartExecutable;
15+
16+
NativeAssetsBuildPlanner({
17+
required this.packageGraph,
18+
required this.packagesWithNativeAssets,
19+
required this.dartExecutable,
20+
});
21+
22+
static Future<NativeAssetsBuildPlanner> fromRootPackageRoot({
23+
required Uri rootPackageRoot,
24+
required List<Package> packagesWithNativeAssets,
25+
required Uri dartExecutable,
26+
}) async {
27+
final result = await Process.run(
28+
dartExecutable.toFilePath(),
29+
[
30+
'pub',
31+
'deps',
32+
'--json',
33+
],
34+
workingDirectory: rootPackageRoot.toFilePath(),
35+
);
36+
final packageGraph =
37+
PackageGraph.fromPubDepsJsonString(result.stdout as String);
38+
return NativeAssetsBuildPlanner(
39+
packageGraph: packageGraph,
40+
packagesWithNativeAssets: packagesWithNativeAssets,
41+
dartExecutable: dartExecutable,
42+
);
43+
}
44+
45+
List<Package> plan() {
46+
final packageMap = {
47+
for (final package in packagesWithNativeAssets) package.name: package
48+
};
49+
final packagesToBuild = packageMap.keys.toSet();
50+
final stronglyConnectedComponents = packageGraph.computeStrongComponents();
51+
final result = <Package>[];
52+
for (final stronglyConnectedComponent in stronglyConnectedComponents) {
53+
final stronglyConnectedComponentWithNativeAssets = [
54+
for (final packageName in stronglyConnectedComponent)
55+
if (packagesToBuild.contains(packageName)) packageName
56+
];
57+
if (stronglyConnectedComponentWithNativeAssets.length > 1) {
58+
throw Exception(
59+
'Cyclic dependency for native asset builds in the following '
60+
'packages: $stronglyConnectedComponent.',
61+
);
62+
} else if (stronglyConnectedComponentWithNativeAssets.length == 1) {
63+
result.add(
64+
packageMap[stronglyConnectedComponentWithNativeAssets.single]!);
65+
}
66+
}
67+
return result;
68+
}
69+
}
70+
71+
class PackageGraph {
72+
final Map<String, List<String>> map;
73+
74+
PackageGraph(this.map);
75+
76+
/// Construct a graph from the JSON produced by `dart pub deps --json`.
77+
factory PackageGraph.fromPubDepsJsonString(String json) =>
78+
PackageGraph.fromPubDepsJson(jsonDecode(json) as Map<dynamic, dynamic>);
79+
80+
/// Construct a graph from the JSON produced by `dart pub deps --json`.
81+
factory PackageGraph.fromPubDepsJson(Map<dynamic, dynamic> map) {
82+
final result = <String, List<String>>{};
83+
final packages = map['packages'] as List<dynamic>;
84+
for (final package in packages) {
85+
final package_ = package as Map<dynamic, dynamic>;
86+
final name = package_['name'] as String;
87+
final dependencies = (package_['dependencies'] as List<dynamic>)
88+
.whereType<String>()
89+
.toList();
90+
result[name] = dependencies;
91+
}
92+
return PackageGraph(result);
93+
}
94+
95+
Iterable<String> neighborsOf(String vertex) => map[vertex] ?? [];
96+
97+
Iterable<String> get vertices => map.keys;
98+
99+
List<List<String>> computeStrongComponents() =>
100+
graphs.stronglyConnectedComponents(vertices, neighborsOf);
101+
}

0 commit comments

Comments
 (0)