Skip to content

Commit c2b1a4b

Browse files
Support for SDK constraints
1 parent d7754b6 commit c2b1a4b

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

lib/src/internal/load_from_yaml.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ PubspecYaml loadFromYaml(String content) {
5757
dependencies: _loadDependenciesIfRequired(jsonMap, Tokens.dependencies),
5858
devDependencies: _loadDependenciesIfRequired(jsonMap, Tokens.devDependencies),
5959
dependencyOverrides: _loadDependenciesIfRequired(jsonMap, Tokens.dependencyOverrides),
60+
environment: jsonMap.containsKey(Tokens.environment) && jsonMap[Tokens.environment] != null
61+
? _loadEnvironment(jsonMap[Tokens.environment] as Map<String, dynamic>)
62+
: {},
6063
executables: jsonMap.containsKey(Tokens.executables) && jsonMap[Tokens.executables] != null
6164
? _loadExecutables(jsonMap[Tokens.executables] as Map<String, dynamic>)
6265
: {},
@@ -129,6 +132,9 @@ HostedPackageDependencySpec _loadGenericHostedDependency(String package, Map<Str
129132
);
130133
}
131134

135+
Map<String, String> _loadEnvironment(Map<String, dynamic> environment) =>
136+
environment.map((key, dynamic value) => MapEntry(key, value as String));
137+
132138
Map<String, Optional<String>> _loadExecutables(Map<String, dynamic> executables) =>
133139
executables.map((key, dynamic value) => MapEntry(
134140
key,
@@ -149,4 +155,5 @@ const _knownTokens = [
149155
Tokens.executables,
150156
Tokens.devDependencies,
151157
Tokens.dependencyOverrides,
158+
Tokens.environment,
152159
];

lib/src/internal/tokens.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ class Tokens {
4242
static const executables = 'executables';
4343
static const devDependencies = 'dev_dependencies';
4444
static const dependencyOverrides = 'dependency_overrides';
45+
static const environment = 'environment';
4546
}

lib/src/internal/yaml_formatter.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ String formatToYaml(PubspecYaml pubspecYaml) => '${[
4040
_dependenciesToYaml(pubspecYaml.devDependencies, Tokens.devDependencies),
4141
if (pubspecYaml.dependencyOverrides.isNotEmpty)
4242
_dependenciesToYaml(pubspecYaml.dependencyOverrides, Tokens.dependencyOverrides),
43+
if (pubspecYaml.environment.isNotEmpty) _environmentToYaml(pubspecYaml.environment),
4344
if (pubspecYaml.executables.isNotEmpty) _executablesToYaml(pubspecYaml.executables),
4445
for (final customField in pubspecYaml.customFields.entries)
4546
json2yaml(Map<String, dynamic>.fromEntries({customField}), yamlStyle: YamlStyle.pubspecYaml),
@@ -111,6 +112,11 @@ MapEntry<String, dynamic> _hostedPackageDependencyToJson(HostedPackageDependency
111112
: dep.version.valueOr(() => null),
112113
);
113114

115+
String _environmentToYaml(Map<String, String> environment) => json2yaml(
116+
<String, dynamic>{Tokens.environment: environment},
117+
yamlStyle: YamlStyle.pubspecYaml,
118+
);
119+
114120
String _executablesToYaml(Map<String, Optional<String>> executables) => json2yaml(
115121
<String, dynamic>{
116122
Tokens.executables: <String, dynamic>{

lib/src/pubspec_yaml.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class PubspecYaml extends $PubspecYaml {
5757
this.dependencies = const [],
5858
this.devDependencies = const [],
5959
this.dependencyOverrides = const [],
60+
this.environment = const {},
6061
this.executables = const {},
6162
this.customFields = const <String, dynamic>{},
6263
});
@@ -112,6 +113,11 @@ class PubspecYaml extends $PubspecYaml {
112113
@CustomEquality(DeepCollectionEquality())
113114
final Iterable<PackageDependencySpec> dependencyOverrides;
114115

116+
/// SDK constraints
117+
/// https://dart.dev/tools/pub/pubspec#sdk-constraints
118+
@CustomEquality(DeepCollectionEquality())
119+
final Map<String, String> environment;
120+
115121
/// Executables that can be run directly from the command line
116122
/// https://dart.dev/tools/pub/pubspec#executables
117123
@CustomEquality(DeepCollectionEquality())

lib/src/pubspec_yaml.g.dart

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/environment_test.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2019 Alexei Sintotski
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
*/
25+
26+
import 'package:pubspec_yaml/pubspec_yaml.dart';
27+
import 'package:test/test.dart';
28+
29+
void main() {
30+
group('given pubspec.yaml with SDK constraints', () {
31+
final pubspecYaml = PubspecYaml.loadFromYamlString(pubspecYamlWithSdkConstraints);
32+
33+
group('$PubspecYaml.loadFromYamlString()', () {
34+
test('provides object with non-empty environment', () {
35+
expect(pubspecYaml.environment, isNotEmpty);
36+
});
37+
test('provides object correct SDK names', () {
38+
expect(pubspecYaml.environment.keys, [dartSdk, flutterSdk]);
39+
});
40+
test('provides object correct SDK entries', () {
41+
expect(pubspecYaml.environment[dartSdk], dartSdkConstraint);
42+
expect(pubspecYaml.environment[flutterSdk], flutterSdkConstraint);
43+
});
44+
test('provides object without custom fields', () {
45+
expect(pubspecYaml.customFields, isEmpty);
46+
});
47+
});
48+
49+
group('$PubspecYaml.toYamlString()', () {
50+
test('produces output identical to the original string', () {
51+
expect(pubspecYaml.toYamlString(), pubspecYamlWithSdkConstraints);
52+
});
53+
});
54+
});
55+
}
56+
57+
const dartSdk = 'sdk';
58+
const dartSdkConstraint = '>=1.19.0 <3.0.0';
59+
const flutterSdk = 'flutter';
60+
const flutterSdkConstraint = '^0.1.2';
61+
const pubspecYamlWithSdkConstraints = '''
62+
name: pubspec_yaml
63+
64+
environment:
65+
$dartSdk: "$dartSdkConstraint"
66+
$flutterSdk: $flutterSdkConstraint
67+
''';

0 commit comments

Comments
 (0)