Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkgs/pubspec_parse/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 1.5.1-wip
## 1.6.0-wip

- Added `toJson` method to `Pubspec` to serialize the object back to a `Map`.
- Require Dart 3.8

## 1.5.0
Expand Down
34 changes: 31 additions & 3 deletions pkgs/pubspec_parse/lib/src/dependency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ Map<String, Dependency> parseDeps(Map? source) =>

const _sourceKeys = ['sdk', 'git', 'path', 'hosted'];

/// Returns `null` if the data could not be parsed.
/// Converts [data] into a [Dependency] object.
///
/// If [data] is not a valid representation of a dependency,
/// returns null so that the parent logic can throw the proper error.
Dependency? _fromJson(Object? data, String name) {
if (data is String || data == null) {
return _$HostedDependencyFromJson({'version': data});
Expand Down Expand Up @@ -90,11 +93,13 @@ Dependency? _fromJson(Object? data, String name) {
}
}

// Not a String or a Map – return null so parent logic can throw proper error
return null;
}

sealed class Dependency {}
sealed class Dependency {
/// Creates a JSON representation of the data of this object.
Map<String, dynamic> toJson();
}

@JsonSerializable()
class SdkDependency extends Dependency {
Expand All @@ -114,6 +119,9 @@ class SdkDependency extends Dependency {

@override
String toString() => 'SdkDependency: $sdk';

@override
Map<String, dynamic> toJson() => {'sdk': sdk, 'version': version.toString()};
}

@JsonSerializable()
Expand Down Expand Up @@ -149,6 +157,11 @@ class GitDependency extends Dependency {

@override
String toString() => 'GitDependency: url@$url';

@override
Map<String, dynamic> toJson() => {
'git': {'url': url.toString(), 'ref': ?ref, 'path': ?path},
};
}

Uri? parseGitUriOrNull(String? value) =>
Expand Down Expand Up @@ -208,6 +221,9 @@ class PathDependency extends Dependency {

@override
String toString() => 'PathDependency: path@$path';

@override
Map<String, dynamic> toJson() => {'path': path};
}

@JsonSerializable(disallowUnrecognizedKeys: true)
Expand All @@ -232,6 +248,12 @@ class HostedDependency extends Dependency {

@override
String toString() => 'HostedDependency: $version';

@override
Map<String, dynamic> toJson() => {
'version': version.toString(),
if (hosted != null) 'hosted': hosted!.toJson(),
};
}

@JsonSerializable(disallowUnrecognizedKeys: true)
Expand Down Expand Up @@ -275,6 +297,12 @@ class HostedDetails {

@override
int get hashCode => Object.hash(name, url);

/// Creates a JSON representation of the data of this object.
Map<String, dynamic> toJson() => {
if (declaredName != null) 'name': declaredName,
'url': url.toString(),
};
}

VersionConstraint _constraintFromString(String? input) =>
Expand Down
33 changes: 25 additions & 8 deletions pkgs/pubspec_parse/lib/src/pubspec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import 'screenshot.dart';

part 'pubspec.g.dart';

@JsonSerializable()
@JsonSerializable(createToJson: true)
class Pubspec {
// TODO: executables

final String name;

@JsonKey(fromJson: _versionFromString)
@JsonKey(fromJson: _versionFromString, toJson: _versionToString)
final Version? version;

final String? description;
Expand Down Expand Up @@ -51,7 +51,7 @@ class Pubspec {
final List<String>? ignoredAdvisories;

/// Optional field for specifying included screenshot files.
@JsonKey(fromJson: parseScreenshots)
@JsonKey(fromJson: parseScreenshots, toJson: serializeScreenshots)
final List<Screenshot>? screenshots;

/// If there is exactly 1 value in [authors], returns it.
Expand All @@ -69,16 +69,16 @@ class Pubspec {
final List<String> authors;
final String? documentation;

@JsonKey(fromJson: _environmentMap)
@JsonKey(fromJson: _environmentMap, toJson: _serializeEnvironment)
final Map<String, VersionConstraint?> environment;

@JsonKey(fromJson: parseDeps)
@JsonKey(fromJson: parseDeps, toJson: _serializeDeps)
final Map<String, Dependency> dependencies;

@JsonKey(fromJson: parseDeps)
@JsonKey(fromJson: parseDeps, toJson: _serializeDeps)
final Map<String, Dependency> devDependencies;

@JsonKey(fromJson: parseDeps)
@JsonKey(fromJson: parseDeps, toJson: _serializeDeps)
final Map<String, Dependency> dependencyOverrides;

/// Optional configuration specific to [Flutter](https://flutter.io/)
Expand All @@ -90,7 +90,7 @@ class Pubspec {
final Map<String, dynamic>? flutter;

/// Optional field to specify executables
@JsonKey(fromJson: _executablesMap)
@JsonKey(fromJson: _executablesMap, toJson: _serializeExecutables)
final Map<String, String?> executables;

/// If this package is a Pub Workspace, this field lists the sub-packages.
Expand Down Expand Up @@ -171,6 +171,9 @@ class Pubspec {
return _$PubspecFromJson(json);
}

/// Creates a JSON-serializable map for this instance.
Map<String, dynamic> toJson() => _$PubspecToJson(this);

/// Parses source [yaml] into [Pubspec].
///
/// When [lenient] is set, top-level property-parsing or type cast errors are
Expand Down Expand Up @@ -247,3 +250,17 @@ Map<String, String?> _executablesMap(Map? source) =>
}
}) ??
{};

Map<String, String?> _serializeEnvironment(
Map<String, VersionConstraint?> map,
) => map.map((key, value) => MapEntry(key, value?.toString()));

String? _versionToString(Version? version) => version?.toString();

Map<String, String?> _serializeExecutables(Map<String, String?>? map) => {
...?map,
};

Map<String, dynamic> _serializeDeps(Map<String, Dependency> input) => {
for (final MapEntry(:key, :value) in input.entries) key: value.toJson(),
};
25 changes: 25 additions & 0 deletions pkgs/pubspec_parse/lib/src/pubspec.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkgs/pubspec_parse/lib/src/screenshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ List<Screenshot> parseScreenshots(List? input) {
}
return res;
}

/// Serializes a list of [Screenshot] objects into a JSON-serializable
/// list of maps.
List<Map<String, String>> serializeScreenshots(List<Screenshot>? input) => [
if (input != null)
for (var e in input) {'description': e.description, 'path': e.path},
];
2 changes: 1 addition & 1 deletion pkgs/pubspec_parse/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: pubspec_parse
version: 1.5.1-wip
version: 1.6.0-wip
description: >-
Simple package for parsing pubspec.yaml files with a type-safe API and rich
error reporting.
Expand Down
Loading