Skip to content

Commit

Permalink
Better yaml formatting with json2yaml 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alexei-sintotski committed Nov 8, 2021
1 parent ba42eda commit f56c4f7
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 73 deletions.
2 changes: 1 addition & 1 deletion dev/format_dart_code.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash -ex

dartfmt --overwrite --fix "$@" .
dart format --fix "$@" .
94 changes: 43 additions & 51 deletions lib/src/pubspec_yaml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import 'package:meta/meta.dart';
import 'package:plain_optional/plain_optional.dart';
import 'package:yaml/yaml.dart';

import '../pubspec_yaml.dart';
import 'package_dependency_spec/package_dependency_spec.dart';
import 'package_dependency_spec/serializers.dart';

Expand Down Expand Up @@ -157,33 +158,30 @@ extension PubspecYamlFromYamlString on String {
// Implementation details
// =============================================================================

String _formatToYaml(PubspecYaml pubspecYaml) => '${[
_packageMetadataToYaml(pubspecYaml),
if (pubspecYaml.dependencies.isNotEmpty)
_dependenciesToYaml(pubspecYaml.dependencies, _Tokens.dependencies),
if (pubspecYaml.devDependencies.isNotEmpty)
_dependenciesToYaml(
pubspecYaml.devDependencies,
_Tokens.devDependencies,
),
if (pubspecYaml.dependencyOverrides.isNotEmpty)
_dependenciesToYaml(
pubspecYaml.dependencyOverrides,
_Tokens.dependencyOverrides,
),
if (pubspecYaml.environment.isNotEmpty)
_environmentToYaml(pubspecYaml.environment),
if (pubspecYaml.executables.isNotEmpty)
_executablesToYaml(pubspecYaml.executables),
for (final customField in pubspecYaml.customFields.entries)
json2yaml(
Map<String, dynamic>.fromEntries({customField}),
yamlStyle: YamlStyle.pubspecYaml,
),
].join("\n\n")}\n';

String _packageMetadataToYaml(PubspecYaml pubspecYaml) =>
json2yaml(<String, dynamic>{
String _formatToYaml(PubspecYaml pubspecYaml) => json2yaml(
_pubspecYamlToJson(pubspecYaml),
yamlStyle: YamlStyle.pubspecYaml,
);

Map<String, dynamic> _pubspecYamlToJson(PubspecYaml pubspecYaml) =>
<String, dynamic>{
..._packageMetadataToJson(pubspecYaml),
..._executablesToJson(pubspecYaml.executables),
..._environmentToJson(pubspecYaml.environment),
..._dependenciesToJson(pubspecYaml.dependencies, _Tokens.dependencies),
..._dependenciesToJson(
pubspecYaml.devDependencies,
_Tokens.devDependencies,
),
..._dependenciesToJson(
pubspecYaml.dependencyOverrides,
_Tokens.dependencyOverrides,
),
...pubspecYaml.customFields,
};

Map<String, dynamic> _packageMetadataToJson(PubspecYaml pubspecYaml) =>
<String, dynamic>{
_Tokens.name: pubspecYaml.name,
if (pubspecYaml.version.hasValue)
_Tokens.version: pubspecYaml.version.valueOr(() => ''),
Expand All @@ -202,43 +200,37 @@ String _packageMetadataToYaml(PubspecYaml pubspecYaml) =>
_Tokens.documentation: pubspecYaml.documentation.valueOr(() => ''),
if (pubspecYaml.publishTo.hasValue)
_Tokens.publishTo: pubspecYaml.publishTo.valueOr(() => ''),
}, yamlStyle: YamlStyle.pubspecYaml);

String _dependenciesToYaml(
Iterable<PackageDependencySpec> dependencies,
String key,
) =>
json2yaml(_dependenciesToJson(dependencies, key),
yamlStyle: YamlStyle.pubspecYaml);
};

Map<String, dynamic> _dependenciesToJson(
Iterable<PackageDependencySpec> dependencies,
String key,
) =>
<String, dynamic>{
key: <String, dynamic>{
for (final dep
in dependencies.toList()
..sort((a, b) => a.package().compareTo(b.package())))
...dep.toJson()
}
if (dependencies.isNotEmpty)
key: <String, dynamic>{
for (final dep
in dependencies.toList()
..sort((a, b) => a.package().compareTo(b.package())))
...dep.toJson()
}
};

String _environmentToYaml(Map<String, String> environment) => json2yaml(
<String, dynamic>{_Tokens.environment: environment},
yamlStyle: YamlStyle.pubspecYaml,
);
Map<String, dynamic> _environmentToJson(Map<String, String> environment) =>
<String, dynamic>{
if (environment.isNotEmpty) _Tokens.environment: environment
};

String _executablesToYaml(Map<String, Optional<String>> executables) =>
json2yaml(
<String, dynamic>{
Map<String, dynamic> _executablesToJson(
Map<String, Optional<String>> executables,
) =>
<String, dynamic>{
if (executables.isNotEmpty)
_Tokens.executables: <String, dynamic>{
for (final entry in executables.entries)
entry.key: _nullIfEmpty(entry.value.valueOr(() => '')),
}
},
yamlStyle: YamlStyle.pubspecYaml,
);
};

PubspecYaml _loadFromYaml(String content) {
final jsonMap =
Expand Down
8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ environment:
dependencies:
collection: ^1.15.0
functional_data: ^1.0.0
json2yaml: ^2.0.0
meta: ^1.1.0
json2yaml: ^3.0.0
meta: ^1.7.0
plain_optional: ^1.0.0
sum_types: ^0.3.0
yaml: ^3.1.0

dev_dependencies:
build_runner: ^2.1.0
build_runner: ^2.1.4
dependency_validator: ^3.1.0
functional_data_generator: ^1.1.0
sum_types_generator: ^0.3.0
test: ^1.17.0
test: ^1.18.0
28 changes: 26 additions & 2 deletions test/custom_fields_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,19 @@ void main() {
group('$PubspecYaml.toYamlString', () {
test('preserves custom fields data', () {
expect(
PubspecYaml.loadFromYamlString(pubspec.toYamlString()).customFields,
pubspec.customFields);
PubspecYaml.loadFromYamlString(pubspec.toYamlString()).customFields,
pubspec.customFields,
);
});
});
});

// https: //github.com/alexei-sintotski/pubspec_yaml/issues/31
group('given pubspec.yaml with nested array', () {
final pubspec = PubspecYaml.loadFromYamlString(pubspecWithNestedArray);
group('$PubspecYaml.toYamlString', () {
test('preserves custom fields formatting', () {
expect(pubspec.toYamlString(), pubspecWithNestedArray);
});
});
});
Expand All @@ -58,3 +69,16 @@ $customFieldName:
- assets/background.png
another_custom_field: value
''';

const pubspecWithNestedArray = '''
name: pubspecWithNestedArray
fonts:
- family: larsseit
fonts:
- asset: assets/fonts/larsseit/Larsseit-Bold.ttf
- asset: assets/fonts/larsseit/Larsseit-Medium.ttf
- family: roger_icons
fonts:
- asset: assets/fonts/roger_icons/RogerIcons.ttf
''';
2 changes: 1 addition & 1 deletion test/dependency_specs/dev_dependencies_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ const pubspecWithDevDependency = '''
name: pubspec_yaml
dev_dependencies:
$dependency: ">=0.5.0 <0.12.0"
$dependency: '>=0.5.0 <0.12.0'
''';
8 changes: 4 additions & 4 deletions test/dependency_specs/git_package_dependency_spec_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ name: pubspec_yaml
dependencies:
$dependency:
git: "$url"
git: $url
''';

const ref = 'some-branch';
Expand All @@ -166,8 +166,8 @@ name: pubspec_yaml
dependencies:
$dependency:
git:
url: "$url"
ref: "$ref"
url: $url
ref: $ref
''';

const path = 'path/to/kittens';
Expand All @@ -177,6 +177,6 @@ name: pubspec_yaml
dependencies:
$dependency:
git:
url: "$url"
url: $url
path: $path
''';
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ dependencies:
$dependency:
hosted:
name: $name
url: "$url"
url: $url
''';

const pubspecWithPackageHostedElsewhereWithVersionSpec = '''
Expand All @@ -221,6 +221,6 @@ dependencies:
$dependency:
hosted:
name: $name
url: "$url"
url: $url
version: $version
''';
2 changes: 1 addition & 1 deletion test/environment_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ const pubspecWithSdkConstraints = '''
name: pubspec_yaml
environment:
$dartSdk: "$dartSdkConstraint"
$dartSdk: '$dartSdkConstraint'
$flutterSdk: $flutterSdkConstraint
''';
1 change: 0 additions & 1 deletion test/executables_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const customScriptName = 'new_element';
const executableWithoutCustomScriptName = 'useful-script';
const pubspecYamlWithExecutables = '''
name: pubspec_yaml
executables:
$executableWithCustomScriptName: $customScriptName
$executableWithoutCustomScriptName:
Expand Down
4 changes: 2 additions & 2 deletions test/metadata_fields/package_authors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void main() {
const author = 'Natalie Weizenbaum <nweiz@google.com>';
const pubspecWithSingleAuthor = '''
name: pubspec_yaml
author: "$author"
author: '$author'
''';

const pubspecWithoutAuthorSpecification = '''
Expand All @@ -128,7 +128,7 @@ const anotherAuthor = 'Abigail Larsen';
const pubspecWithTwoAuthors = '''
name: pubspec_yaml
authors:
- "$author"
- '$author'
- $anotherAuthor
''';

Expand Down
2 changes: 1 addition & 1 deletion test/metadata_fields/package_documentation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void main() {
const documentation = 'https://example-pet-store.com/newtify/docs';
const pubspecWithDocumentationDefined = '''
name: pubspec_yaml
documentation: "$documentation"
documentation: $documentation
''';

const pubspecWithoutDocumentation = '''
Expand Down
2 changes: 1 addition & 1 deletion test/metadata_fields/package_homepage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void main() {
const homepage = 'https://example-pet-store.com/newtify';
const pubspecWithHomepageDefined = '''
name: pubspec_yaml
homepage: "$homepage"
homepage: $homepage
''';

const pubspecWithoutHomepage = '''
Expand Down
2 changes: 1 addition & 1 deletion test/metadata_fields/package_issue_tracker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void main() {
const issueTracker = 'https://example-pet-store.com/newtify/issues';
const pubspecWithIssueTrackerDefined = '''
name: pubspec_yaml
issue_tracker: "$issueTracker"
issue_tracker: $issueTracker
''';

const pubspecWithoutIssueTracker = '''
Expand Down
2 changes: 1 addition & 1 deletion test/metadata_fields/package_repository_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void main() {
const repository = 'https://example-pet-store.com/newtify';
const pubspecWithRepositoryDefined = '''
name: pubspec_yaml
repository: "$repository"
repository: $repository
''';

const pubspecWithoutRepository = '''
Expand Down

0 comments on commit f56c4f7

Please sign in to comment.