From ed1b9db1a912ba489061180504b89f6e0f4095c7 Mon Sep 17 00:00:00 2001 From: Dhruv Maradiya Date: Fri, 27 Dec 2024 19:24:52 +0530 Subject: [PATCH 1/7] Add optional executables field to Pubspec --- pkgs/pubspec_parse/lib/src/pubspec.dart | 30 +++++++++++++++++++++++ pkgs/pubspec_parse/lib/src/pubspec.g.dart | 2 ++ 2 files changed, 32 insertions(+) diff --git a/pkgs/pubspec_parse/lib/src/pubspec.dart b/pkgs/pubspec_parse/lib/src/pubspec.dart index 1317a2309..a1ce99e89 100644 --- a/pkgs/pubspec_parse/lib/src/pubspec.dart +++ b/pkgs/pubspec_parse/lib/src/pubspec.dart @@ -93,6 +93,10 @@ class Pubspec { /// and other settings. final Map? flutter; + /// Optional field to specify executables + @JsonKey(fromJson: _executablesMap) + final Map executables; + /// If [author] and [authors] are both provided, their values are combined /// with duplicates eliminated. Pubspec( @@ -121,12 +125,14 @@ class Pubspec { Map? devDependencies, Map? dependencyOverrides, this.flutter, + Map? executables, }) : // ignore: deprecated_member_use_from_same_package authors = _normalizeAuthors(author, authors), environment = environment ?? const {}, dependencies = dependencies ?? const {}, devDependencies = devDependencies ?? const {}, + executables = executables ?? const {}, dependencyOverrides = dependencyOverrides ?? const {} { if (name.isEmpty) { throw ArgumentError.value(name, 'name', '"name" cannot be empty.'); @@ -224,3 +230,27 @@ Map _environmentMap(Map? source) => return MapEntry(key, constraint); }) ?? {}; + +Map _executablesMap(Map? source) => + source?.map((k, value) { + final key = k as String; + if (value == null) { + return MapEntry(key, null); + } else if (value is String) { + // Validate that the value is a valid file path. + try { + Uri.file(value); + } catch (e) { + throw CheckedFromJsonException(source, key, 'Pubspec', e.toString()); + } + return MapEntry(key, value); + } else { + throw CheckedFromJsonException( + source, + key, + 'String', + '`$value` is not a String.', + ); + } + }) ?? + {}; diff --git a/pkgs/pubspec_parse/lib/src/pubspec.g.dart b/pkgs/pubspec_parse/lib/src/pubspec.g.dart index fc285718d..45d4e2660 100644 --- a/pkgs/pubspec_parse/lib/src/pubspec.g.dart +++ b/pkgs/pubspec_parse/lib/src/pubspec.g.dart @@ -51,6 +51,8 @@ Pubspec _$PubspecFromJson(Map json) => $checkedCreate( (v) => (v as Map?)?.map( (k, e) => MapEntry(k as String, e), )), + executables: + $checkedConvert('executables', (v) => _executablesMap(v as Map?)), ); return val; }, From 0bdb357d04df83e750a2bd544db394af84727126 Mon Sep 17 00:00:00 2001 From: Dhruv Maradiya Date: Fri, 27 Dec 2024 19:25:06 +0530 Subject: [PATCH 2/7] Added tests for executables field in Pubspec parsing --- pkgs/pubspec_parse/test/parse_test.dart | 61 +++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/pkgs/pubspec_parse/test/parse_test.dart b/pkgs/pubspec_parse/test/parse_test.dart index 6251f41fb..6b5976c39 100644 --- a/pkgs/pubspec_parse/test/parse_test.dart +++ b/pkgs/pubspec_parse/test/parse_test.dart @@ -56,6 +56,10 @@ void main() { 'screenshots': [ {'description': 'my screenshot', 'path': 'path/to/screenshot'}, ], + 'executables': { + 'my_script': 'bin/my_script.dart', + 'my_script2': 'bin/my_script2.dart', + }, }); expect(value.name, 'sample'); expect(value.version, version); @@ -86,6 +90,11 @@ void main() { expect(value.screenshots, hasLength(1)); expect(value.screenshots!.first.description, 'my screenshot'); expect(value.screenshots!.first.path, 'path/to/screenshot'); + expect(value.executables, hasLength(2)); + expect(value.executables.keys, contains('my_script')); + expect(value.executables.keys, contains('my_script2')); + expect(value.executables['my_script'], 'bin/my_script.dart'); + expect(value.executables['my_script2'], 'bin/my_script2.dart'); }); test('environment values can be null', () async { @@ -208,6 +217,58 @@ line 3, column 16: Unsupported value for "publish_to". Must be an http or https }); }); + group('executables', () { + test('one executable', () async { + final value = await parse({ + ...defaultPubspec, + 'executables': {'my_script': 'bin/my_script.dart'}, + }); + expect(value.executables, hasLength(1)); + expect(value.executables.keys, contains('my_script')); + expect(value.executables['my_script'], 'bin/my_script.dart'); + }); + + test('many executables', () async { + final value = await parse({ + ...defaultPubspec, + 'executables': { + 'my_script': 'bin/my_script.dart', + 'my_script2': 'bin/my_script2.dart', + }, + }); + expect(value.executables, hasLength(2)); + expect(value.executables.keys, contains('my_script')); + expect(value.executables.keys, contains('my_script2')); + expect(value.executables['my_script'], 'bin/my_script.dart'); + expect(value.executables['my_script2'], 'bin/my_script2.dart'); + }); + + test('invalid value', () async { + expectParseThrowsContaining( + { + ...defaultPubspec, + 'executables': { + 'script': 32, + }, + }, + 'Unsupported value for "script". `32` is not a String.', + skipTryPub: true, + ); + }); + + test('invalid executable - lenient', () async { + final value = await parse( + { + ...defaultPubspec, + 'executables': 'Invalid value', + }, + lenient: true, + ); + expect(value.name, 'sample'); + expect(value.executables, isEmpty); + }); + }); + group('invalid', () { test('null', () { expectParseThrows( From f1514e6d6af6d5ce8bc29a178b495e848aaa1c1d Mon Sep 17 00:00:00 2001 From: Dhruv Maradiya Date: Fri, 27 Dec 2024 19:25:15 +0530 Subject: [PATCH 3/7] Update changelog to add support for executables field --- pkgs/pubspec_parse/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkgs/pubspec_parse/CHANGELOG.md b/pkgs/pubspec_parse/CHANGELOG.md index a5f0f1a30..00a212cc0 100644 --- a/pkgs/pubspec_parse/CHANGELOG.md +++ b/pkgs/pubspec_parse/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.4.1 + +- Added support for `executables` field. + ## 1.4.0 - Require Dart 3.2 From b0df50b45c33c59bf839d74d461f03de0492fd5e Mon Sep 17 00:00:00 2001 From: Dhruv Maradiya Date: Sat, 28 Dec 2024 21:06:17 +0530 Subject: [PATCH 4/7] Add test for executables field in Pubspec parsing --- pkgs/pubspec_parse/test/parse_test.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/pubspec_parse/test/parse_test.dart b/pkgs/pubspec_parse/test/parse_test.dart index 1936d81d9..e0698af16 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.screenshots, isEmpty); expect(value.workspace, isNull); expect(value.resolution, isNull); + expect(value.executables, isEmpty); }); test('all fields set', () async { From eede5f5ecce91372817e193ab9e45cbe11890af8 Mon Sep 17 00:00:00 2001 From: Dhruv Maradiya Date: Fri, 3 Jan 2025 12:24:27 +0530 Subject: [PATCH 5/7] Updated CHANGELOG for better readability Co-authored-by: Kevin Moore --- pkgs/pubspec_parse/CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkgs/pubspec_parse/CHANGELOG.md b/pkgs/pubspec_parse/CHANGELOG.md index 6261d696c..c169c0834 100644 --- a/pkgs/pubspec_parse/CHANGELOG.md +++ b/pkgs/pubspec_parse/CHANGELOG.md @@ -1,7 +1,6 @@ ## 1.5.0-wip -- Add `Pubspec.workspace` and `Pubspec.resolution` fields. -- Added support for `executables` field. +- Added fields to `Pubspec`: `executables`, `resolution`, `workspace`. ## 1.4.0 From 891b5d907fdb12973461c9388ed7dd230b4b92e4 Mon Sep 17 00:00:00 2001 From: Dhruv Maradiya Date: Tue, 7 Jan 2025 22:14:12 +0530 Subject: [PATCH 6/7] Update changelog for version 1.5.0 and remove file path validation from executables field --- pkgs/pubspec_parse/CHANGELOG.md | 2 +- pkgs/pubspec_parse/lib/src/pubspec.dart | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/pkgs/pubspec_parse/CHANGELOG.md b/pkgs/pubspec_parse/CHANGELOG.md index 6261d696c..8efb9785a 100644 --- a/pkgs/pubspec_parse/CHANGELOG.md +++ b/pkgs/pubspec_parse/CHANGELOG.md @@ -1,4 +1,4 @@ -## 1.5.0-wip +## 1.5.0 - Add `Pubspec.workspace` and `Pubspec.resolution` fields. - Added support for `executables` field. diff --git a/pkgs/pubspec_parse/lib/src/pubspec.dart b/pkgs/pubspec_parse/lib/src/pubspec.dart index 10afc9d17..eb779089a 100644 --- a/pkgs/pubspec_parse/lib/src/pubspec.dart +++ b/pkgs/pubspec_parse/lib/src/pubspec.dart @@ -245,12 +245,6 @@ Map _executablesMap(Map? source) => if (value == null) { return MapEntry(key, null); } else if (value is String) { - // Validate that the value is a valid file path. - try { - Uri.file(value); - } catch (e) { - throw CheckedFromJsonException(source, key, 'Pubspec', e.toString()); - } return MapEntry(key, value); } else { throw CheckedFromJsonException( From c50439550a56299409b37a168ae755ab71afc123 Mon Sep 17 00:00:00 2001 From: Dhruv Maradiya Date: Tue, 7 Jan 2025 22:31:39 +0530 Subject: [PATCH 7/7] Bump version to 1.5.0 in pubspec.yaml --- pkgs/pubspec_parse/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/pubspec_parse/pubspec.yaml b/pkgs/pubspec_parse/pubspec.yaml index 6bf96bb87..73a11171e 100644 --- a/pkgs/pubspec_parse/pubspec.yaml +++ b/pkgs/pubspec_parse/pubspec.yaml @@ -1,5 +1,5 @@ name: pubspec_parse -version: 1.5.0-wip +version: 1.5.0 description: >- Simple package for parsing pubspec.yaml files with a type-safe API and rich error reporting.