Skip to content

Commit

Permalink
[native_assets_cli] add packageName (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes authored Oct 3, 2023
1 parent 23d46c3 commit ec0a7c9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
5 changes: 5 additions & 0 deletions pkgs/native_assets_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.3.0

- **Breaking change** Add required `BuildConfig.packageName`
([#142](https://github.com/dart-lang/native/issues/142)).

## 0.2.0

- **Breaking change** Rename `Asset.name` to `Asset.id`
Expand Down
15 changes: 14 additions & 1 deletion pkgs/native_assets_cli/lib/src/model/build_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class BuildConfig {
Uri get outDir => _outDir;
late final Uri _outDir;

/// The name of the package the native assets are built for.
String get packageName => _packageName;
late final String _packageName;

/// The root of the package the native assets are built for.
///
/// Often a package's native assets are built because a package is a
Expand Down Expand Up @@ -124,6 +128,7 @@ class BuildConfig {

factory BuildConfig({
required Uri outDir,
required String packageName,
required Uri packageRoot,
required BuildMode buildMode,
required Architecture targetArchitecture,
Expand All @@ -136,6 +141,7 @@ class BuildConfig {
}) {
final nonValidated = BuildConfig._()
.._outDir = outDir
.._packageName = packageName
.._packageRoot = packageRoot
.._buildMode = buildMode
.._targetArchitecture = targetArchitecture
Expand All @@ -153,12 +159,14 @@ class BuildConfig {

factory BuildConfig.dryRun({
required Uri outDir,
required String packageName,
required Uri packageRoot,
required OS targetOs,
required LinkModePreference linkModePreference,
}) {
final nonValidated = BuildConfig._()
.._outDir = outDir
.._packageName = packageName
.._packageRoot = packageRoot
.._targetOs = targetOs
.._linkModePreference = linkModePreference
Expand All @@ -177,6 +185,7 @@ class BuildConfig {
/// In particular, it only takes the package name from [packageRoot],
/// so that the hash is equal across checkouts and ignores [outDir] itself.
static String checksum({
required String packageName,
required Uri packageRoot,
required Architecture targetArchitecture,
required OS targetOs,
Expand All @@ -187,7 +196,6 @@ class BuildConfig {
required LinkModePreference linkModePreference,
Map<String, Metadata>? dependencyMetadata,
}) {
final packageName = packageRoot.pathSegments.lastWhere((e) => e.isNotEmpty);
final input = [
packageName,
targetArchitecture.toString(),
Expand Down Expand Up @@ -273,6 +281,7 @@ class BuildConfig {
}

static const outDirConfigKey = 'out_dir';
static const packageNameConfigKey = 'package_name';
static const packageRootConfigKey = 'package_root';
static const dependencyMetadataConfigKey = 'dependency_metadata';
static const _versionKey = 'version';
Expand Down Expand Up @@ -303,6 +312,7 @@ class BuildConfig {
(config) => _config = config,
(config) => _dryRun = config.optionalBool(dryRunConfigKey),
(config) => _outDir = config.path(outDirConfigKey, mustExist: true),
(config) => _packageName = config.string(packageNameConfigKey),
(config) =>
_packageRoot = config.path(packageRootConfigKey, mustExist: true),
(config) {
Expand Down Expand Up @@ -469,6 +479,7 @@ class BuildConfig {

return {
outDirConfigKey: _outDir.toFilePath(),
packageNameConfigKey: _packageName,
packageRootConfigKey: _packageRoot.toFilePath(),
OS.configKey: _targetOs.toString(),
LinkModePreference.configKey: _linkModePreference.toString(),
Expand Down Expand Up @@ -498,6 +509,7 @@ class BuildConfig {
return false;
}
if (other.outDir != outDir) return false;
if (other.packageName != packageName) return false;
if (other.packageRoot != packageRoot) return false;
if (other.dryRun != dryRun) return false;
if (other.targetOs != targetOs) return false;
Expand All @@ -517,6 +529,7 @@ class BuildConfig {
@override
int get hashCode => Object.hashAll([
outDir,
packageName,
packageRoot,
targetOs,
linkModePreference,
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_cli/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: native_assets_cli
description: >-
A library that contains the argument and file formats for implementing a
native assets CLI.
version: 0.2.0
version: 0.3.0
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli

topics:
Expand Down
31 changes: 29 additions & 2 deletions pkgs/native_assets_cli/test/model/build_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void main() async {
late Uri tempUri;
late Uri outDirUri;
late Uri outDir2Uri;
late String packageName;
late Uri packageRootUri;
late Uri fakeClang;
late Uri fakeLd;
Expand All @@ -26,8 +27,9 @@ void main() async {
outDirUri = tempUri.resolve('out1/');
await Directory.fromUri(outDirUri).create();
outDir2Uri = tempUri.resolve('out2/');
packageName = 'my_package';
await Directory.fromUri(outDir2Uri).create();
packageRootUri = tempUri.resolve('my_package/');
packageRootUri = tempUri.resolve('$packageName/');
await Directory.fromUri(packageRootUri).create();
fakeClang = tempUri.resolve('fake_clang');
await File.fromUri(fakeClang).create();
Expand All @@ -48,6 +50,7 @@ void main() async {
test('BuildConfig ==', () {
final config1 = BuildConfig(
outDir: outDirUri,
packageName: packageName,
packageRoot: tempUri,
targetArchitecture: Architecture.arm64,
targetOs: OS.iOS,
Expand All @@ -63,6 +66,7 @@ void main() async {

final config2 = BuildConfig(
outDir: outDir2Uri,
packageName: packageName,
packageRoot: tempUri,
targetArchitecture: Architecture.arm64,
targetOs: OS.android,
Expand Down Expand Up @@ -92,6 +96,7 @@ void main() async {
test('BuildConfig fromConfig', () {
final buildConfig2 = BuildConfig(
outDir: outDirUri,
packageName: packageName,
packageRoot: packageRootUri,
targetArchitecture: Architecture.arm64,
targetOs: OS.android,
Expand All @@ -105,6 +110,7 @@ void main() async {
'dry_run': false,
'link_mode_preference': 'prefer-static',
'out_dir': outDirUri.toFilePath(),
'package_name': packageName,
'package_root': packageRootUri.toFilePath(),
'target_android_ndk_api': 30,
'target_architecture': 'arm64',
Expand All @@ -119,6 +125,7 @@ void main() async {
test('BuildConfig.dryRun', () {
final buildConfig2 = BuildConfig.dryRun(
outDir: outDirUri,
packageName: packageName,
packageRoot: packageRootUri,
targetOs: OS.android,
linkModePreference: LinkModePreference.preferStatic,
Expand All @@ -128,6 +135,7 @@ void main() async {
'dry_run': true,
'link_mode_preference': 'prefer-static',
'out_dir': outDirUri.toFilePath(),
'package_name': packageName,
'package_root': packageRootUri.toFilePath(),
'target_os': 'android',
'version': BuildOutput.version.toString(),
Expand All @@ -140,6 +148,7 @@ void main() async {
test('BuildConfig toYaml fromConfig', () {
final buildConfig1 = BuildConfig(
outDir: outDirUri,
packageName: packageName,
packageRoot: packageRootUri,
targetArchitecture: Architecture.arm64,
targetOs: OS.iOS,
Expand All @@ -161,6 +170,7 @@ void main() async {
test('BuildConfig == dependency metadata', () {
final buildConfig1 = BuildConfig(
outDir: outDirUri,
packageName: packageName,
packageRoot: tempUri,
targetArchitecture: Architecture.arm64,
targetOs: OS.android,
Expand All @@ -180,6 +190,7 @@ void main() async {

final buildConfig2 = BuildConfig(
outDir: outDirUri,
packageName: packageName,
packageRoot: tempUri,
targetArchitecture: Architecture.arm64,
targetOs: OS.android,
Expand All @@ -205,6 +216,7 @@ void main() async {
final outDir = outDirUri;
final buildConfig1 = BuildConfig(
outDir: outDir,
packageName: packageName,
packageRoot: tempUri,
targetArchitecture: Architecture.arm64,
targetOs: OS.iOS,
Expand Down Expand Up @@ -241,6 +253,7 @@ dependency_metadata:
- a
link_mode_preference: prefer-static
out_dir: ${outDir.toFilePath()}
package_name: $packageName
package_root: ${tempUri.toFilePath()}
target_architecture: arm64
target_ios_sdk: iphoneos
Expand Down Expand Up @@ -270,6 +283,7 @@ version: ${BuildConfig.version}''';
expect(
() => BuildConfig.fromConfig(Config(fileParsed: {
'version': BuildConfig.version.toString(),
'package_name': packageName,
'package_root': packageRootUri.toFilePath(),
'target_architecture': 'arm64',
'target_os': 'android',
Expand All @@ -288,6 +302,7 @@ version: ${BuildConfig.version}''';
() => BuildConfig.fromConfig(Config(fileParsed: {
'version': BuildConfig.version.toString(),
'out_dir': outDirUri.toFilePath(),
'package_name': packageName,
'package_root': packageRootUri.toFilePath(),
'target_architecture': 'arm64',
'target_os': 'android',
Expand All @@ -311,6 +326,7 @@ version: ${BuildConfig.version}''';
() => BuildConfig.fromConfig(Config(fileParsed: {
'out_dir': outDirUri.toFilePath(),
'version': BuildConfig.version.toString(),
'package_name': packageName,
'package_root': packageRootUri.toFilePath(),
'target_architecture': 'arm64',
'target_os': 'android',
Expand Down Expand Up @@ -342,6 +358,7 @@ version: ${BuildConfig.version}''';
test('BuildConfig toString', () {
final config = BuildConfig(
outDir: outDirUri,
packageName: packageName,
packageRoot: tempUri,
targetArchitecture: Architecture.arm64,
targetOs: OS.iOS,
Expand All @@ -359,6 +376,7 @@ version: ${BuildConfig.version}''';
test('BuildConfig fromArgs', () async {
final buildConfig = BuildConfig(
outDir: outDirUri,
packageName: packageName,
packageRoot: tempUri,
targetArchitecture: Architecture.arm64,
targetOs: OS.android,
Expand All @@ -380,6 +398,7 @@ version: ${BuildConfig.version}''';
test('dependency metadata via config accessor', () {
final buildConfig1 = BuildConfig(
outDir: outDirUri,
packageName: packageName,
packageRoot: tempUri,
targetArchitecture: Architecture.arm64,
targetOs: OS.android,
Expand Down Expand Up @@ -407,6 +426,7 @@ version: ${BuildConfig.version}''';
test('envScript', () {
final buildConfig1 = BuildConfig(
outDir: outDirUri,
packageName: packageName,
packageRoot: packageRootUri,
targetArchitecture: Architecture.x64,
targetOs: OS.windows,
Expand Down Expand Up @@ -455,6 +475,7 @@ version: ${BuildConfig.version}''';
await File.fromUri(fakeClangUri).create();

final name1 = BuildConfig.checksum(
packageName: packageName,
packageRoot: nativeAddUri,
targetArchitecture: Architecture.x64,
targetOs: OS.linux,
Expand All @@ -463,10 +484,11 @@ version: ${BuildConfig.version}''';
);

// Using the checksum for a build folder should be stable.
expect(name1, '1397e1b887565784da364fddc021491a');
expect(name1, '037109b9824b2559502fa7bd42e1b6f8');

// Build folder different due to metadata.
final name2 = BuildConfig.checksum(
packageName: packageName,
packageRoot: nativeAddUri,
targetArchitecture: Architecture.x64,
targetOs: OS.linux,
Expand All @@ -481,6 +503,7 @@ version: ${BuildConfig.version}''';

// Build folder different due to cc.
final name3 = BuildConfig.checksum(
packageName: packageName,
packageRoot: nativeAddUri,
targetArchitecture: Architecture.x64,
targetOs: OS.linux,
Expand All @@ -499,6 +522,7 @@ version: ${BuildConfig.version}''';
final config = Config(fileParsed: {
'link_mode_preference': 'prefer-static',
'out_dir': outDir.toFilePath(),
'package_name': packageName,
'package_root': tempUri.toFilePath(),
'target_os': 'windows',
'target_architecture': 'arm',
Expand All @@ -518,6 +542,7 @@ version: ${BuildConfig.version}''';
final config = Config(fileParsed: {
'link_mode_preference': 'prefer-static',
'out_dir': outDir.toFilePath(),
'package_name': packageName,
'package_root': tempUri.toFilePath(),
'target_os': 'windows',
'target_architecture': 'arm64',
Expand All @@ -539,6 +564,7 @@ version: ${BuildConfig.version}''';
final config = Config(fileParsed: {
'link_mode_preference': 'prefer-static',
'out_dir': outDir.toFilePath(),
'package_name': packageName,
'package_root': tempUri.toFilePath(),
'target_os': 'windows',
'dry_run': true,
Expand All @@ -555,6 +581,7 @@ version: ${BuildConfig.version}''';

test('BuildConfig dry_run access invalid args', () {
final buildConfig = BuildConfig.dryRun(
packageName: packageName,
outDir: outDirUri,
packageRoot: tempUri,
targetOs: OS.windows,
Expand Down

0 comments on commit ec0a7c9

Please sign in to comment.