@@ -5,13 +5,13 @@ import 'dart:convert';
5
5
import 'dart:io' ;
6
6
7
7
import 'package:aft/aft.dart' ;
8
+ import 'package:aft/src/constraints_checker.dart' ;
8
9
import 'package:aft/src/options/glob_options.dart' ;
9
10
import 'package:collection/collection.dart' ;
10
11
import 'package:pub_api_client/pub_api_client.dart' ;
11
12
import 'package:pub_semver/pub_semver.dart' ;
12
- import 'package:pubspec_parse/pubspec_parse.dart' ;
13
13
14
- enum _ConstraintsAction {
14
+ enum ConstraintsAction {
15
15
check (
16
16
'Checks whether all constraints in the repo match the global config' ,
17
17
'All constraints matched!' ,
@@ -26,7 +26,7 @@ enum _ConstraintsAction {
26
26
'Constraints successfully applied!' ,
27
27
);
28
28
29
- const _ConstraintsAction (this .description, this .successMessage);
29
+ const ConstraintsAction (this .description, this .successMessage);
30
30
31
31
final String description;
32
32
final String successMessage;
@@ -35,8 +35,8 @@ enum _ConstraintsAction {
35
35
/// Command to manage dependencies across all Dart/Flutter packages in the repo.
36
36
class ConstraintsCommand extends AmplifyCommand {
37
37
ConstraintsCommand () {
38
- addSubcommand (_ConstraintsSubcommand (_ConstraintsAction .check));
39
- addSubcommand (_ConstraintsSubcommand (_ConstraintsAction .apply));
38
+ addSubcommand (_ConstraintsSubcommand (ConstraintsAction .check));
39
+ addSubcommand (_ConstraintsSubcommand (ConstraintsAction .apply));
40
40
addSubcommand (_ConstraintsUpdateCommand ());
41
41
addSubcommand (_ConstraintsPubVerifyCommand ());
42
42
}
@@ -52,129 +52,29 @@ class ConstraintsCommand extends AmplifyCommand {
52
52
class _ConstraintsSubcommand extends AmplifyCommand with GlobOptions {
53
53
_ConstraintsSubcommand (this .action);
54
54
55
- final _ConstraintsAction action;
55
+ final ConstraintsAction action;
56
56
57
57
@override
58
58
String get description => action.description;
59
59
60
60
@override
61
61
String get name => action.name;
62
62
63
- final _mismatchedDependencies = < String > [];
64
-
65
- /// Checks the [local] constraint against the [global] and returns whether
66
- /// an update is required.
67
- void _checkConstraint (
68
- PackageInfo package,
69
- List <String > dependencyPath,
70
- VersionConstraint global,
71
- VersionConstraint local,
72
- ) {
73
- // Packages are not allowed to diverge from `aft.yaml`, even to specify
74
- // more precise constraints.
75
- final satisfiesGlobalConstraint = global == local;
76
- if (satisfiesGlobalConstraint) {
77
- return ;
78
- }
79
- switch (action) {
80
- case _ConstraintsAction .check:
81
- final dependencyName = dependencyPath.last;
82
- _mismatchedDependencies.add (
83
- '${package .path }\n '
84
- 'Mismatched `$dependencyName `:\n '
85
- 'Expected $global \n '
86
- 'Found $local \n ' ,
87
- );
88
- return ;
89
- case _ConstraintsAction .apply:
90
- case _ConstraintsAction .update:
91
- package.pubspecInfo.pubspecYamlEditor.update (
92
- dependencyPath,
93
- global.toString (),
94
- );
95
- }
96
- }
97
-
98
- /// Checks the package's environment constraints against the global config.
99
- void _checkEnvironment (
100
- PackageInfo package,
101
- Map <String , VersionConstraint ?> environment,
102
- Environment globalEnvironment,
103
- ) {
104
- // Check Dart SDK contraint
105
- final globalSdkConstraint = globalEnvironment.sdk;
106
- final localSdkConstraint = environment['sdk' ] ?? VersionConstraint .any;
107
- _checkConstraint (
108
- package,
109
- ['environment' , 'sdk' ],
110
- globalSdkConstraint,
111
- localSdkConstraint,
112
- );
113
-
114
- // Check Flutter SDK constraint
115
- if (package.flavor == PackageFlavor .flutter) {
116
- final globalFlutterConstraint = globalEnvironment.flutter;
117
- final localFlutterConstraint =
118
- environment['flutter' ] ?? VersionConstraint .any;
119
- _checkConstraint (
120
- package,
121
- ['environment' , 'flutter' ],
122
- globalFlutterConstraint,
123
- localFlutterConstraint,
124
- );
125
- }
126
- }
127
-
128
- /// Checks the package's dependency constraints against the global config.
129
- void _checkDependency (
130
- PackageInfo package,
131
- Map <String , Dependency > dependencies,
132
- DependencyType dependencyType,
133
- MapEntry <String , VersionConstraint > globalDep,
134
- ) {
135
- final dependencyName = globalDep.key;
136
- final globalDepConstraint = globalDep.value;
137
- final localDep = dependencies[dependencyName];
138
- if (localDep is ! HostedDependency ) {
139
- return ;
140
- }
141
- final localDepConstraint = localDep.version;
142
- _checkConstraint (
143
- package,
144
- [dependencyType.key, dependencyName],
145
- globalDepConstraint,
146
- localDepConstraint,
147
- );
148
- }
149
-
150
- Future <void > _run (_ConstraintsAction action) async {
151
- final globalDependencyConfig = aftConfig.dependencies;
152
- final globalEnvironmentConfig = aftConfig.environment;
63
+ Future <void > _run (ConstraintsAction action) async {
64
+ final constraintsCheckers = [
65
+ GlobalConstraintChecker (
66
+ action,
67
+ repo.aftConfig.dependencies.asMap (),
68
+ repo.aftConfig.environment,
69
+ ),
70
+ PublishConstraintsChecker (
71
+ action,
72
+ repo.getPackageGraph (includeDevDependencies: true ),
73
+ ),
74
+ ];
153
75
for (final package in commandPackages.values) {
154
- _checkEnvironment (
155
- package,
156
- package.pubspecInfo.pubspec.environment ?? const {},
157
- globalEnvironmentConfig,
158
- );
159
- for (final globalDep in globalDependencyConfig.entries) {
160
- _checkDependency (
161
- package,
162
- package.pubspecInfo.pubspec.dependencies,
163
- DependencyType .dependency,
164
- globalDep,
165
- );
166
- _checkDependency (
167
- package,
168
- package.pubspecInfo.pubspec.dependencyOverrides,
169
- DependencyType .dependencyOverride,
170
- globalDep,
171
- );
172
- _checkDependency (
173
- package,
174
- package.pubspecInfo.pubspec.devDependencies,
175
- DependencyType .devDependency,
176
- globalDep,
177
- );
76
+ for (final constraintsChecker in constraintsCheckers) {
77
+ constraintsChecker.checkConstraints (package);
178
78
}
179
79
180
80
if (package.pubspecInfo.pubspecYamlEditor.edits.isNotEmpty) {
@@ -183,9 +83,17 @@ class _ConstraintsSubcommand extends AmplifyCommand with GlobOptions {
183
83
);
184
84
}
185
85
}
186
- if (_mismatchedDependencies.isNotEmpty) {
187
- for (final mismatched in _mismatchedDependencies) {
188
- logger.error (mismatched);
86
+ final mismatchedDependencies = constraintsCheckers.expand (
87
+ (checker) => checker.mismatchedDependencies,
88
+ );
89
+ if (mismatchedDependencies.isNotEmpty) {
90
+ for (final mismatched in mismatchedDependencies) {
91
+ final (: package, : dependencyName, : message) = mismatched;
92
+ logger.error (
93
+ '${package .path }\n '
94
+ 'Mismatched `$dependencyName `:\n '
95
+ '$message \n ' ,
96
+ );
189
97
}
190
98
exit (1 );
191
99
}
@@ -200,7 +108,7 @@ class _ConstraintsSubcommand extends AmplifyCommand with GlobOptions {
200
108
}
201
109
202
110
class _ConstraintsUpdateCommand extends _ConstraintsSubcommand {
203
- _ConstraintsUpdateCommand () : super (_ConstraintsAction .update);
111
+ _ConstraintsUpdateCommand () : super (ConstraintsAction .update);
204
112
205
113
@override
206
114
Future <void > run () async {
@@ -336,7 +244,7 @@ class _ConstraintsUpdateCommand extends _ConstraintsSubcommand {
336
244
}
337
245
338
246
if (hasUpdates) {
339
- await _run (_ConstraintsAction .apply);
247
+ await _run (ConstraintsAction .apply);
340
248
}
341
249
}
342
250
}
0 commit comments