diff --git a/pkgs/pubspec_parse/CHANGELOG.md b/pkgs/pubspec_parse/CHANGELOG.md index af153fbcaf..ef08dc6ff9 100644 --- a/pkgs/pubspec_parse/CHANGELOG.md +++ b/pkgs/pubspec_parse/CHANGELOG.md @@ -8,7 +8,7 @@ - Preserve nested `Map` (`YamlMap`) keys and spans when `git` or `hosted` dependency shorthands fail to parse or when `Pubspec.fromJson` is called with a non-`YamlMap` `Map`. -- Added fields to `Pubspec`: `falseSecrets`, `platforms`. +- Added fields to `Pubspec`: `falseSecrets`, `hooks`, `platforms`. ## 1.6.0 diff --git a/pkgs/pubspec_parse/lib/src/pubspec.dart b/pkgs/pubspec_parse/lib/src/pubspec.dart index 1010af7ce9..a2a8ac481e 100644 --- a/pkgs/pubspec_parse/lib/src/pubspec.dart +++ b/pkgs/pubspec_parse/lib/src/pubspec.dart @@ -113,6 +113,13 @@ class Pubspec { /// See https://dart.dev/tools/pub/pubspec#false_secrets. final List? falseSecrets; + /// Configuration for hooks, such as native build or link hooks. + /// + /// May include `user_defines` and other settings. + /// + /// See https://dart.dev/tools/pub/pubspec#hooks. + final Map? hooks; + /// If [author] and [authors] are both provided, their values are combined /// with duplicates eliminated. Pubspec( @@ -137,6 +144,7 @@ class Pubspec { this.resolution, this.platforms, this.falseSecrets, + this.hooks, Map? dependencies, Map? devDependencies, Map? dependencyOverrides, diff --git a/pkgs/pubspec_parse/lib/src/pubspec.g.dart b/pkgs/pubspec_parse/lib/src/pubspec.g.dart index 3ea3850bc4..0c0d738681 100644 --- a/pkgs/pubspec_parse/lib/src/pubspec.g.dart +++ b/pkgs/pubspec_parse/lib/src/pubspec.g.dart @@ -69,6 +69,10 @@ Pubspec _$PubspecFromJson(Map json) => $checkedCreate( 'false_secrets', (v) => (v as List?)?.map((e) => e as String).toList(), ), + hooks: $checkedConvert( + 'hooks', + (v) => (v as Map?)?.map((k, e) => MapEntry(k as String, e)), + ), dependencies: $checkedConvert( 'dependencies', (v) => parseDeps(v as Map?), @@ -127,4 +131,5 @@ Map _$PubspecToJson(Pubspec instance) => { 'resolution': instance.resolution, 'platforms': instance.platforms, 'false_secrets': instance.falseSecrets, + 'hooks': instance.hooks, }; diff --git a/pkgs/pubspec_parse/test/parse_test.dart b/pkgs/pubspec_parse/test/parse_test.dart index 810a46f8af..64573b8274 100644 --- a/pkgs/pubspec_parse/test/parse_test.dart +++ b/pkgs/pubspec_parse/test/parse_test.dart @@ -36,6 +36,7 @@ void main() { expect(value.executables, isEmpty); expect(value.platforms, isNull); expect(value.falseSecrets, isNull); + expect(value.hooks, isNull); }); test('all fields set', () async { @@ -62,6 +63,11 @@ void main() { 'resolution': 'workspace', 'platforms': {'android': null, 'web': null}, 'false_secrets': ['/lib/src/hardcoded_api_key.dart', '/test/*.pem'], + 'hooks': { + 'user_defines': { + 'my_package': {'enable_experimental': true}, + }, + }, 'executables': { 'my_script': 'bin/my_script.dart', 'my_script2': 'bin/my_script2.dart', @@ -110,6 +116,11 @@ void main() { '/lib/src/hardcoded_api_key.dart', '/test/*.pem', ]); + expect(value.hooks, { + 'user_defines': { + 'my_package': {'enable_experimental': true}, + }, + }); }); test('environment values can be null', () async { @@ -483,6 +494,42 @@ line 4, column 10: Unsupported value for "sdk". Could not parse version "silly". }); }); + group('hooks', () { + test('map', () async { + final value = await parse({ + ...defaultPubspec, + 'hooks': { + 'user_defines': { + 'my_package': {'enable_experimental': true}, + }, + }, + }); + expect(value.hooks, { + 'user_defines': { + 'my_package': {'enable_experimental': true}, + }, + }); + }); + + test('not a map', () { + expectParseThrowsContaining( + {...defaultPubspec, 'hooks': 1}, + "Unsupported value for \"hooks\". type 'int' is not a subtype of type 'Map?'", + skipTryPub: true, + ); + }); + + test('invalid data - lenient', () async { + final value = await parse( + {...defaultPubspec, 'hooks': 1}, + skipTryPub: true, + lenient: true, + ); + expect(value.name, 'sample'); + expect(value.hooks, isNull); + }); + }); + group('funding', () { test('not a list', () { expectParseThrowsContaining( diff --git a/pkgs/pubspec_parse/test/tojson_test.dart b/pkgs/pubspec_parse/test/tojson_test.dart index 2460ef0f75..24d7b3c583 100644 --- a/pkgs/pubspec_parse/test/tojson_test.dart +++ b/pkgs/pubspec_parse/test/tojson_test.dart @@ -41,6 +41,7 @@ void main() { 'ignored_advisories': null, 'platforms': null, 'false_secrets': null, + 'hooks': null, }); }); @@ -69,6 +70,11 @@ void main() { 'resolution': 'workspace', 'platforms': {'android': null, 'web': null}, 'false_secrets': ['/lib/src/hardcoded_api_key.dart', '/test/*.pem'], + 'hooks': { + 'user_defines': { + 'my_package': {'enable_experimental': true}, + }, + }, 'executables': { 'my_script': 'bin/my_script.dart', 'my_script2': 'bin/my_script2.dart', @@ -112,6 +118,11 @@ void main() { 'resolution': 'workspace', 'platforms': {'android': null, 'web': null}, 'false_secrets': ['/lib/src/hardcoded_api_key.dart', '/test/*.pem'], + 'hooks': { + 'user_defines': { + 'my_package': {'enable_experimental': true}, + }, + }, 'executables': { 'my_script': 'bin/my_script.dart', 'my_script2': 'bin/my_script2.dart', @@ -147,6 +158,7 @@ void main() { expect(newValue.executables, value.executables); expect(newValue.platforms, value.platforms); expect(newValue.falseSecrets, value.falseSecrets); + expect(newValue.hooks, value.hooks); }); test('all fields set', () async { @@ -173,6 +185,11 @@ void main() { 'resolution': 'workspace', 'platforms': {'android': null, 'web': null}, 'false_secrets': ['/lib/src/hardcoded_api_key.dart', '/test/*.pem'], + 'hooks': { + 'user_defines': { + 'my_package': {'enable_experimental': true}, + }, + }, 'executables': { 'my_script': 'bin/my_script.dart', 'my_script2': 'bin/my_script2.dart', @@ -212,6 +229,7 @@ void main() { expect(newValue.executables, value.executables); expect(newValue.platforms, value.platforms); expect(newValue.falseSecrets, value.falseSecrets); + expect(newValue.hooks, value.hooks); }); test('dependencies', () async {