Skip to content

Commit

Permalink
even cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed Jan 12, 2025
1 parent 46c1cfa commit 454c7a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
20 changes: 11 additions & 9 deletions json_serializable/lib/src/check_dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'constants.dart';
const _productionDirectories = {'lib', 'bin'};
const _annotationPkgName = 'json_annotation';
final _supportLanguageRange =
VersionConstraint.parse(supportLanguageConstraint);
VersionConstraint.parse(supportedLanguageConstraint);
final requiredJsonAnnotationMinVersion = Version.parse('4.9.0');

Future<void> pubspecHasRightVersion(BuildStep buildStep) async {
Expand Down Expand Up @@ -52,23 +52,25 @@ Future<void> _validatePubspec(bool production, BuildStep buildStep) async {
//
// Ensure the current package language version is at least the minimum.
//

final currentPackageName = pubspec.name;

final packageConfig = await buildStep.packageConfig;

final thisPackage = packageConfig[currentPackageName]!;

// build_runner will error out without an SDK version - so this "should" be
// fine.
// build_runner will error out without an SDK version - so assuming
// `languageVersion` is not null.
final thisPackageVersion = thisPackage.languageVersion!;

final thisPackageVer = Version.parse('$thisPackageVersion.0');
if (!_supportLanguageRange.allows(thisPackageVer)) {
log.warning(
'The language version ($thisPackageVer) of this package '
'($currentPackageName) does not match the required range '
'`$supportLanguageConstraint`.',
'''
The language version ($thisPackageVer) of this package ($currentPackageName) does not match the required range `$supportedLanguageConstraint`.
Edit pubspec.yaml to include an SDK constraint of at least $supportedLanguageConstraint.
environment:
sdk: $supportedLanguageConstraint
''',
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ const converterOrKeyInstructions = r'''

/// This package generates code that uses case statements, which were introduced
/// in Dart 3.0.
const supportLanguageConstraint = '^3.0.0';
const supportedLanguageConstraint = '^3.0.0';
15 changes: 10 additions & 5 deletions json_serializable/test/annotation_version_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ void main() {
await _structurePackage(
environment: const {'sdk': '^$sdkLowerBound'},
dependencies: {'json_annotation': _annotationLowerBound},
message: 'The language version ($sdkLowerBound) of this package '
'($_testPkgName) does not match the required range '
'`$supportLanguageConstraint`.',
message: '''
The language version ($sdkLowerBound) of this package ($_testPkgName) does not match the required range `$supportedLanguageConstraint`.
Edit pubspec.yaml to include an SDK constraint of at least $supportedLanguageConstraint.
environment:
sdk: $supportedLanguageConstraint
''',
);
});

test('is at least the required `$supportLanguageConstraint`', () async {
test('is at least the required `$supportedLanguageConstraint`', () async {
await _structurePackage(
dependencies: {'json_annotation': _annotationLowerBound},
message: null,
Expand Down Expand Up @@ -135,7 +140,7 @@ const _testPkgName = '_test_pkg';
Future<void> _structurePackage({
String sourceDirectory = 'lib',
required String? message,
Map<String, dynamic> environment = const {'sdk': supportLanguageConstraint},
Map<String, dynamic> environment = const {'sdk': supportedLanguageConstraint},
Map<String, dynamic> dependencies = const {},
Map<String, dynamic> devDependencies = const {},
}) async {
Expand Down

0 comments on commit 454c7a4

Please sign in to comment.