@@ -2668,16 +2668,11 @@ int calculateLineNumber(String contents, int offset) {
26682668 return result;
26692669}
26702670
2671- List < Error > _validateDependencies ({
2671+ String ? _findTargetPubspecPath ({
26722672 required String ? appDirectory,
26732673 required String ? dartOutPath,
26742674 String ? basePath,
2675- required String apiName,
2676- required List <String > requiredDeps,
2677- List <String > requiredDevDeps = const < String > [],
26782675}) {
2679- final errors = < Error > [];
2680-
26812676 String ? pubspecPath;
26822677 if (dartOutPath != null && dartOutPath.isNotEmpty) {
26832678 final String fullDartOut =
@@ -2701,6 +2696,169 @@ List<Error> _validateDependencies({
27012696 (dartOutPath == null || dartOutPath.isEmpty)) {
27022697 pubspecPath = findPubspecPath (Directory .current);
27032698 }
2699+ return pubspecPath;
2700+ }
2701+
2702+ /// Checks whether the `objective_c` dependency constraint in `pubspec.yaml`
2703+ /// matches the `ObjCVersionCheck(major, minor)` emitted by `ffigen` in the
2704+ /// generated `.ffi.dart` file.
2705+ ///
2706+ /// Returns a warning message if the constraint allows incompatible versions, or
2707+ /// `null` if the constraint is compatible or cannot be checked.
2708+ String ? checkObjectiveCPubspecVersion (InternalPigeonOptions options) {
2709+ return _checkGeneratedBindingsPubspecVersion (
2710+ options: options,
2711+ appDirectory: options.swiftOptions? .appDirectory ?? options.appDirectory,
2712+ generatedFileSuffix: '.ffi.dart' ,
2713+ versionCheckName: 'ObjCVersionCheck' ,
2714+ packageName: 'objective_c' ,
2715+ bindingTypeLabel: 'FFI' ,
2716+ );
2717+ }
2718+
2719+ /// Checks whether the `jni` dependency constraint in `pubspec.yaml`
2720+ /// matches the `JniVersionCheck(major, minor)` emitted by `jnigen` in the
2721+ /// generated `.jni.dart` file.
2722+ ///
2723+ /// Returns a warning message if the constraint allows incompatible versions, or
2724+ /// `null` if the constraint is compatible or cannot be checked.
2725+ String ? checkJniPubspecVersion (InternalPigeonOptions options) {
2726+ return _checkGeneratedBindingsPubspecVersion (
2727+ options: options,
2728+ appDirectory: options.kotlinOptions? .appDirectory ?? options.appDirectory,
2729+ generatedFileSuffix: '.jni.dart' ,
2730+ versionCheckName: 'JniVersionCheck' ,
2731+ packageName: 'jni' ,
2732+ bindingTypeLabel: 'JNI' ,
2733+ );
2734+ }
2735+
2736+ String ? _checkGeneratedBindingsPubspecVersion ({
2737+ required InternalPigeonOptions options,
2738+ required String ? appDirectory,
2739+ required String generatedFileSuffix,
2740+ required String versionCheckName,
2741+ required String packageName,
2742+ required String bindingTypeLabel,
2743+ }) {
2744+ final String ? dartOutPath = options.dartOptions? .dartOut;
2745+ if (dartOutPath == null || dartOutPath.isEmpty) {
2746+ return null ;
2747+ }
2748+ final String basePath = options.basePath ?? '' ;
2749+ final String fullDartOut = (basePath.isNotEmpty && ! dartOutPath.startsWith (basePath))
2750+ ? path.join (basePath, dartOutPath)
2751+ : dartOutPath;
2752+ final bindingsDartFile = File ('${path .withoutExtension (fullDartOut )}$generatedFileSuffix ' );
2753+ if (! bindingsDartFile.existsSync ()) {
2754+ return null ;
2755+ }
2756+
2757+ final String bindingsContent;
2758+ try {
2759+ bindingsContent = bindingsDartFile.readAsStringSync ();
2760+ } catch (_) {
2761+ return null ;
2762+ }
2763+
2764+ final RegExpMatch ? match = RegExp (
2765+ '$versionCheckName \\ (\\ s*(\\ d+)\\ s*,\\ s*(\\ d+)\\ s*\\ )' ,
2766+ ).firstMatch (bindingsContent);
2767+ if (match == null ) {
2768+ return null ;
2769+ }
2770+
2771+ final int major = int .parse (match.group (1 )! );
2772+ final int minor = int .parse (match.group (2 )! );
2773+ final requiredMinVersion = Version (major, minor, 0 );
2774+ final nextMajorVersion = Version (major + 1 , 0 , 0 );
2775+
2776+ final String ? pubspecPath = _findTargetPubspecPath (
2777+ appDirectory: appDirectory,
2778+ dartOutPath: fullDartOut,
2779+ basePath: options.basePath,
2780+ );
2781+ if (pubspecPath == null ) {
2782+ return null ;
2783+ }
2784+
2785+ final resolvedPubspec = File (pubspecPath);
2786+ if (! resolvedPubspec.existsSync ()) {
2787+ return null ;
2788+ }
2789+
2790+ try {
2791+ final dynamic doc = yaml.loadYaml (resolvedPubspec.readAsStringSync ());
2792+ if (doc is ! yaml.YamlMap ) {
2793+ return null ;
2794+ }
2795+ final dependencies = doc['dependencies' ] as yaml.YamlMap ? ;
2796+ final dependencyOverrides = doc['dependency_overrides' ] as yaml.YamlMap ? ;
2797+ final bool hasOverride =
2798+ dependencyOverrides != null && dependencyOverrides.containsKey (packageName);
2799+ final bool hasDependency =
2800+ hasOverride || (dependencies != null && dependencies.containsKey (packageName));
2801+ if (! hasDependency) {
2802+ return null ;
2803+ }
2804+ final Object ? rawDependency = hasOverride
2805+ ? dependencyOverrides[packageName]
2806+ : dependencies! [packageName];
2807+ final Object ? rawConstraint;
2808+ if (rawDependency == null ) {
2809+ rawConstraint = 'any' ;
2810+ } else if (rawDependency is Map ) {
2811+ if (! rawDependency.containsKey ('version' ) &&
2812+ (rawDependency.containsKey ('path' ) ||
2813+ rawDependency.containsKey ('git' ) ||
2814+ rawDependency.containsKey ('sdk' ))) {
2815+ return null ;
2816+ }
2817+ rawConstraint = rawDependency['version' ] ?? 'any' ;
2818+ } else {
2819+ rawConstraint = rawDependency;
2820+ }
2821+ if (rawConstraint is ! String ) {
2822+ return null ;
2823+ }
2824+
2825+ final constraint = VersionConstraint .parse (rawConstraint);
2826+ final bool allowsValidVersion = constraint.allowsAny (
2827+ VersionRange (min: requiredMinVersion, max: nextMajorVersion, includeMin: true ),
2828+ );
2829+ final bool allowsLowerVersion = constraint.allowsAny (VersionRange (max: requiredMinVersion));
2830+ final bool allowsHigherMajorVersion = constraint.allowsAny (
2831+ VersionRange (min: nextMajorVersion.firstPreRelease, includeMin: true ),
2832+ );
2833+
2834+ if (! allowsValidVersion || allowsLowerVersion || allowsHigherMajorVersion) {
2835+ return 'Generated $bindingTypeLabel bindings in "${path .basename (bindingsDartFile .path )}" require '
2836+ 'package:$packageName ^$major .$minor .0 (from $versionCheckName ($major , $minor )), '
2837+ 'but "$pubspecPath " specifies "$packageName : $rawConstraint ". '
2838+ 'Consider updating pubspec.yaml to "$packageName : ^$major .$minor .0".' ;
2839+ }
2840+ } catch (_) {
2841+ return null ;
2842+ }
2843+
2844+ return null ;
2845+ }
2846+
2847+ List <Error > _validateDependencies ({
2848+ required String ? appDirectory,
2849+ required String ? dartOutPath,
2850+ String ? basePath,
2851+ required String apiName,
2852+ required List <String > requiredDeps,
2853+ List <String > requiredDevDeps = const < String > [],
2854+ }) {
2855+ final errors = < Error > [];
2856+
2857+ final String ? pubspecPath = _findTargetPubspecPath (
2858+ appDirectory: appDirectory,
2859+ dartOutPath: dartOutPath,
2860+ basePath: basePath,
2861+ );
27042862
27052863 if (pubspecPath == null ) {
27062864 errors.add (
0 commit comments