11import 'dart:io' ;
22
3- import 'package:colorize/colorize.dart' ;
43import 'package:pub_semver/pub_semver.dart' ;
54
65import '../../../api_tool.dart' ;
76
7+ class VersionCheckResult {
8+ final bool success;
9+ final Version oldVersion;
10+ final Version newVersion;
11+ final Version ? neededVersion;
12+ final String explanation;
13+
14+ VersionCheckResult .success ({
15+ required this .oldVersion,
16+ required this .newVersion,
17+ Version ? neededVersion,
18+ required this .explanation,
19+ }) : success = true ,
20+ neededVersion = neededVersion ?? newVersion;
21+
22+ VersionCheckResult .failure ({
23+ required this .oldVersion,
24+ required this .newVersion,
25+ required this .neededVersion,
26+ required this .explanation,
27+ }) : success = false ;
28+ }
29+
830/// helper class to check if the version change matches the changes
931abstract class VersionCheck {
1032 /// checks if the version change between [oldPackageApi] and [newPackageApi] matches the changes in [diffResult]
11- static bool versionChangeMatchesChanges ({
33+ static VersionCheckResult check ({
1234 required PackageApiDiffResult diffResult,
1335 required PackageApi oldPackageApi,
1436 required PackageApi newPackageApi,
1537 required bool ignorePrerelease,
1638 required VersionCheckMode versionCheckMode,
1739 }) {
18- stdout.writeln ('' );
1940 stdout.writeln ('Checking Package version' );
20- stdout.writeln ('' );
2141 if (oldPackageApi.packageVersion == null ) {
2242 throw PackageApiDiffError (
2343 message: 'Old package doesn\' t contain a version]' );
@@ -36,14 +56,21 @@ abstract class VersionCheck {
3656 diffResult.apiChanges.any ((change) => ! change.type.requiresMinorBump);
3757
3858 if (versionCheckMode == VersionCheckMode .none) {
39- stdout.writeln ('Skipping version check completely' );
40- return true ;
59+ return VersionCheckResult .success (
60+ oldVersion: oldVersion,
61+ newVersion: newVersion,
62+ explanation:
63+ 'Skipping version check completely as the version check mode is $versionCheckMode ' ,
64+ );
4165 }
4266 if (versionCheckMode == VersionCheckMode .onlyBreakingChanges &&
4367 ! containsBreakingChanges) {
44- stdout.writeln (
45- 'Skipping version check because there are no breaking changes' );
46- return true ;
68+ return VersionCheckResult .success (
69+ oldVersion: oldVersion,
70+ newVersion: newVersion,
71+ explanation:
72+ 'Skipping version check because there are no breaking changes' ,
73+ );
4774 }
4875
4976 if (ignorePrerelease) {
@@ -57,28 +84,35 @@ abstract class VersionCheck {
5784 final oldVersionWithoutPreRelease = Version .parse (oldVersion.toString ());
5885 oldVersionWithoutPreRelease.preRelease.clear ();
5986 if (oldVersionWithoutPreRelease <= newVersion) {
60- stdout.writeln (
61- 'Skipping version check because the old version is a pre-release and the new version is the same or higher without the pre-release part' );
62- return true ;
87+ return VersionCheckResult .success (
88+ oldVersion: oldVersion,
89+ newVersion: newVersion,
90+ explanation:
91+ 'Skipping version check because the old version is a pre-release and the new version is the same or higher without the pre-release part' ,
92+ );
6393 }
6494 }
6595
6696 if (newVersion.isPreRelease) {
6797 // pre-release. We don't look at differentiation between breaking and non-breaking changes
68- stdout. writeln (
69- 'We got a pre release. We only check if there are any changes' ) ;
98+ final prefix =
99+ 'We got a pre release. We only check if there are any changes.' ;
70100 if (containsAnyChanges && oldVersion >= newVersion) {
71- stdout.writeln (
72- 'Got "${Colorize (newVersion .toString ()).bold ()}" expected > "${Colorize (oldVersion .toString ()).bold ()}" (pre-release but changes)' );
73- return false ;
101+ return VersionCheckResult .failure (
102+ oldVersion: oldVersion,
103+ newVersion: newVersion,
104+ neededVersion: null ,
105+ explanation:
106+ '$prefix Got "$newVersion " expected > "$oldVersion " (pre-release but changes)' ,
107+ );
74108 }
75- stdout.writeln (Colorize ('New version is OK!' ).green ());
76- final explaination = containsAnyChanges
77- ? 'which is > "${Colorize (oldVersion .toString ()).bold ()}" (pre-release but changes)'
109+ final explanation = containsAnyChanges
110+ ? 'which is > "$oldVersion " (pre-release but changes)'
78111 : 'and no changes' ;
79- stdout.writeln (
80- 'Got "${Colorize (newVersion .toString ()).bold ()}" $explaination ' );
81- return true ;
112+ return VersionCheckResult .success (
113+ oldVersion: oldVersion,
114+ newVersion: newVersion,
115+ explanation: '$prefix Got "$newVersion " $explanation ' );
82116 }
83117
84118 Version expectedMinVersion =
@@ -99,19 +133,22 @@ abstract class VersionCheck {
99133 }
100134 }
101135
102- stdout.writeln ('Old version: "$oldVersion "' );
103- stdout.writeln (
104- 'Expecting minimum version: "$expectedMinVersion " ($versionExplanation )' );
105136 if (newVersion < expectedMinVersion) {
106- stdout.writeln (Colorize ('New Version is too low!' ).red ());
107- stdout.writeln (
108- 'Got "${Colorize (newVersion .toString ()).bold ()}" expected >= "${Colorize (expectedMinVersion .toString ()).bold ()}"' );
109- return false ;
137+ return VersionCheckResult .failure (
138+ oldVersion: oldVersion,
139+ newVersion: newVersion,
140+ neededVersion: expectedMinVersion,
141+ explanation:
142+ 'Got "$newVersion " expected >= "$expectedMinVersion " ($versionExplanation )' ,
143+ );
110144 } else {
111- stdout.writeln (Colorize ('New version is OK!' ).green ());
112- stdout.writeln (
113- 'Got "${Colorize (newVersion .toString ()).bold ()}" which is >= "${Colorize (expectedMinVersion .toString ()).bold ()}"' );
114- return true ;
145+ return VersionCheckResult .success (
146+ oldVersion: oldVersion,
147+ newVersion: newVersion,
148+ neededVersion: expectedMinVersion,
149+ explanation:
150+ 'Got "$newVersion " which is >= "$expectedMinVersion " ($versionExplanation )' ,
151+ );
115152 }
116153 }
117154}
0 commit comments