From 82d6c4e04a1d1d303a1f62c6e81c42e0ea84cd37 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 2 Oct 2023 10:40:39 +0200 Subject: [PATCH] [native_assets_cli] add packageName --- pkgs/native_assets_cli/CHANGELOG.md | 5 +++ .../lib/src/model/build_config.dart | 15 ++++++++- pkgs/native_assets_cli/pubspec.yaml | 2 +- .../test/model/build_config_test.dart | 31 +++++++++++++++++-- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/pkgs/native_assets_cli/CHANGELOG.md b/pkgs/native_assets_cli/CHANGELOG.md index 176d17f8c..cb76192c3 100644 --- a/pkgs/native_assets_cli/CHANGELOG.md +++ b/pkgs/native_assets_cli/CHANGELOG.md @@ -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` diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index f97447f74..786aefe4d 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -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 @@ -124,6 +128,7 @@ class BuildConfig { factory BuildConfig({ required Uri outDir, + required String packageName, required Uri packageRoot, required BuildMode buildMode, required Architecture targetArchitecture, @@ -136,6 +141,7 @@ class BuildConfig { }) { final nonValidated = BuildConfig._() .._outDir = outDir + .._packageName = packageName .._packageRoot = packageRoot .._buildMode = buildMode .._targetArchitecture = targetArchitecture @@ -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 @@ -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, @@ -187,7 +196,6 @@ class BuildConfig { required LinkModePreference linkModePreference, Map? dependencyMetadata, }) { - final packageName = packageRoot.pathSegments.lastWhere((e) => e.isNotEmpty); final input = [ packageName, targetArchitecture.toString(), @@ -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'; @@ -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) { @@ -469,6 +479,7 @@ class BuildConfig { return { outDirConfigKey: _outDir.toFilePath(), + packageNameConfigKey: _packageName, packageRootConfigKey: _packageRoot.toFilePath(), OS.configKey: _targetOs.toString(), LinkModePreference.configKey: _linkModePreference.toString(), @@ -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; @@ -517,6 +529,7 @@ class BuildConfig { @override int get hashCode => Object.hashAll([ outDir, + packageName, packageRoot, targetOs, linkModePreference, diff --git a/pkgs/native_assets_cli/pubspec.yaml b/pkgs/native_assets_cli/pubspec.yaml index 21fd0e17d..62321836f 100644 --- a/pkgs/native_assets_cli/pubspec.yaml +++ b/pkgs/native_assets_cli/pubspec.yaml @@ -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: diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index 5401f072d..e19f94f5a 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -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; @@ -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(); @@ -48,6 +50,7 @@ void main() async { test('BuildConfig ==', () { final config1 = BuildConfig( outDir: outDirUri, + packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, targetOs: OS.iOS, @@ -63,6 +66,7 @@ void main() async { final config2 = BuildConfig( outDir: outDir2Uri, + packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, targetOs: OS.android, @@ -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, @@ -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', @@ -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, @@ -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(), @@ -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, @@ -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, @@ -180,6 +190,7 @@ void main() async { final buildConfig2 = BuildConfig( outDir: outDirUri, + packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, targetOs: OS.android, @@ -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, @@ -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 @@ -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', @@ -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', @@ -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', @@ -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, @@ -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, @@ -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, @@ -407,6 +426,7 @@ version: ${BuildConfig.version}'''; test('envScript', () { final buildConfig1 = BuildConfig( outDir: outDirUri, + packageName: packageName, packageRoot: packageRootUri, targetArchitecture: Architecture.x64, targetOs: OS.windows, @@ -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, @@ -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, @@ -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, @@ -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', @@ -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', @@ -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, @@ -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,