From f7ebf3abb63437d9ff7c70637c92e7d96d4896f6 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Sat, 29 Aug 2026 18:07:05 +0200 Subject: [PATCH 1/2] [pubspec_parse] Add hooks field to Pubspec --- pkgs/pubspec_parse/CHANGELOG.md | 4 ++ pkgs/pubspec_parse/lib/src/pubspec.dart | 8 ++++ pkgs/pubspec_parse/lib/src/pubspec.g.dart | 5 +++ pkgs/pubspec_parse/pubspec.yaml | 2 +- pkgs/pubspec_parse/test/parse_test.dart | 48 +++++++++++++++++++++++ pkgs/pubspec_parse/test/tojson_test.dart | 18 +++++++++ 6 files changed, 84 insertions(+), 1 deletion(-) diff --git a/pkgs/pubspec_parse/CHANGELOG.md b/pkgs/pubspec_parse/CHANGELOG.md index e0116ecca0..45ce1defb9 100644 --- a/pkgs/pubspec_parse/CHANGELOG.md +++ b/pkgs/pubspec_parse/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.7.0-wip + +- Added `hooks` field to `Pubspec`. + ## 1.6.0 - Added `toJson` method to `Pubspec` to serialize the object back to a `Map`. diff --git a/pkgs/pubspec_parse/lib/src/pubspec.dart b/pkgs/pubspec_parse/lib/src/pubspec.dart index dd51fe505a..57d17647f3 100644 --- a/pkgs/pubspec_parse/lib/src/pubspec.dart +++ b/pkgs/pubspec_parse/lib/src/pubspec.dart @@ -99,6 +99,13 @@ class Pubspec { /// Specifies how to resolve dependencies with the surrounding Pub Workspace. final String? resolution; + /// 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( @@ -121,6 +128,7 @@ class Pubspec { this.description, this.workspace, this.resolution, + 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 cfbab518ef..926328a45b 100644 --- a/pkgs/pubspec_parse/lib/src/pubspec.g.dart +++ b/pkgs/pubspec_parse/lib/src/pubspec.g.dart @@ -61,6 +61,10 @@ Pubspec _$PubspecFromJson(Map json) => $checkedCreate( (v) => (v as List?)?.map((e) => e as String).toList(), ), resolution: $checkedConvert('resolution', (v) => v as String?), + hooks: $checkedConvert( + 'hooks', + (v) => (v as Map?)?.map((k, e) => MapEntry(k as String, e)), + ), dependencies: $checkedConvert( 'dependencies', (v) => parseDeps(v as Map?), @@ -116,4 +120,5 @@ Map _$PubspecToJson(Pubspec instance) => { 'executables': _serializeExecutables(instance.executables), 'workspace': instance.workspace, 'resolution': instance.resolution, + 'hooks': instance.hooks, }; diff --git a/pkgs/pubspec_parse/pubspec.yaml b/pkgs/pubspec_parse/pubspec.yaml index cf7711d6ad..67b56a6633 100644 --- a/pkgs/pubspec_parse/pubspec.yaml +++ b/pkgs/pubspec_parse/pubspec.yaml @@ -1,5 +1,5 @@ name: pubspec_parse -version: 1.6.0 +version: 1.7.0-wip description: >- Simple package for parsing pubspec.yaml files with a type-safe API and rich error reporting. diff --git a/pkgs/pubspec_parse/test/parse_test.dart b/pkgs/pubspec_parse/test/parse_test.dart index f700b4efae..d141be0428 100644 --- a/pkgs/pubspec_parse/test/parse_test.dart +++ b/pkgs/pubspec_parse/test/parse_test.dart @@ -34,6 +34,7 @@ void main() { expect(value.workspace, isNull); expect(value.resolution, isNull); expect(value.executables, isEmpty); + expect(value.hooks, isNull); }); test('all fields set', () async { @@ -58,6 +59,11 @@ void main() { ], 'workspace': ['pkg1', 'pkg2'], 'resolution': 'workspace', + 'hooks': { + 'user_defines': { + 'my_package': {'enable_experimental': true}, + }, + }, 'executables': { 'my_script': 'bin/my_script.dart', 'my_script2': 'bin/my_script2.dart', @@ -101,6 +107,11 @@ void main() { expect(value.workspace!.first, 'pkg1'); expect(value.workspace!.last, 'pkg2'); expect(value.resolution, 'workspace'); + expect(value.hooks, { + 'user_defines': { + 'my_package': {'enable_experimental': true}, + }, + }); }); test('environment values can be null', () async { @@ -261,6 +272,7 @@ line 3, column 16: Unsupported value for "publish_to". Must be an http or https }, lenient: true); expect(value.name, 'sample'); expect(value.executables, isEmpty); + expect(value.hooks, isNull); }); }); @@ -398,6 +410,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 50c73aa978..350be5ad83 100644 --- a/pkgs/pubspec_parse/test/tojson_test.dart +++ b/pkgs/pubspec_parse/test/tojson_test.dart @@ -39,6 +39,7 @@ void main() { 'funding': null, 'topics': null, 'ignored_advisories': null, + 'hooks': null, }); }); @@ -65,6 +66,11 @@ void main() { ], 'workspace': ['pkg1', 'pkg2'], 'resolution': 'workspace', + 'hooks': { + 'user_defines': { + 'my_package': {'enable_experimental': true}, + }, + }, 'executables': { 'my_script': 'bin/my_script.dart', 'my_script2': 'bin/my_script2.dart', @@ -106,6 +112,11 @@ void main() { 'ignored_advisories': ['111', '222'], 'workspace': ['pkg1', 'pkg2'], 'resolution': 'workspace', + 'hooks': { + 'user_defines': { + 'my_package': {'enable_experimental': true}, + }, + }, 'executables': { 'my_script': 'bin/my_script.dart', 'my_script2': 'bin/my_script2.dart', @@ -139,6 +150,7 @@ void main() { expect(newValue.workspace, value.workspace); expect(newValue.resolution, value.resolution); expect(newValue.executables, value.executables); + expect(newValue.hooks, value.hooks); }); test('all fields set', () async { @@ -163,6 +175,11 @@ void main() { ], 'workspace': ['pkg1', 'pkg2'], 'resolution': 'workspace', + 'hooks': { + 'user_defines': { + 'my_package': {'enable_experimental': true}, + }, + }, 'executables': { 'my_script': 'bin/my_script.dart', 'my_script2': 'bin/my_script2.dart', @@ -200,6 +217,7 @@ void main() { expect(newValue.workspace, value.workspace); expect(newValue.resolution, value.resolution); expect(newValue.executables, value.executables); + expect(newValue.hooks, value.hooks); }); test('dependencies', () async { From 0ffca1e04ec7566d3d036e4febde509ce39c38a2 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Sat, 29 Aug 2026 18:15:09 +0200 Subject: [PATCH 2/2] Remove stray expectation from unrelated test --- pkgs/pubspec_parse/test/parse_test.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/pkgs/pubspec_parse/test/parse_test.dart b/pkgs/pubspec_parse/test/parse_test.dart index d141be0428..451b06c73e 100644 --- a/pkgs/pubspec_parse/test/parse_test.dart +++ b/pkgs/pubspec_parse/test/parse_test.dart @@ -272,7 +272,6 @@ line 3, column 16: Unsupported value for "publish_to". Must be an http or https }, lenient: true); expect(value.name, 'sample'); expect(value.executables, isEmpty); - expect(value.hooks, isNull); }); });