Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 29.0.11

* [swift] Warns after FFI code generation if the `objective_c` version constraint in `pubspec.yaml` does not match the generated `ObjCVersionCheck`.
* [kotlin] Warns after JNI code generation if the `jni` version constraint in `pubspec.yaml` does not match the generated `JniVersionCheck`.

## 29.0.10

* [swift] [kotlin] Allows `sealed` classes in native interop files.
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/src/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import 'generator.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '29.0.10';
const String pigeonVersion = '29.0.11';

/// Default plugin package name.
const String defaultPluginPackageName = 'dev.flutter.pigeon';
Expand Down
8 changes: 8 additions & 0 deletions packages/pigeon/lib/src/pigeon_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,10 @@ ${_argParser.usage}''';
return exitCode;
}
}
final String? objcVersionWarning = checkObjectiveCPubspecVersion(internalOptions);
if (objcVersionWarning != null) {
stderr.writeln('Warning: $objcVersionWarning');
}
}

if (useJni) {
Expand Down Expand Up @@ -947,6 +951,10 @@ ${_argParser.usage}''';
return 1;
}
}
final String? jniVersionWarning = checkJniPubspecVersion(internalOptions);
if (jniVersionWarning != null) {
stderr.writeln('Warning: $jniVersionWarning');
}
}

return 0;
Expand Down
170 changes: 164 additions & 6 deletions packages/pigeon/lib/src/pigeon_lib_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2668,16 +2668,11 @@ int calculateLineNumber(String contents, int offset) {
return result;
}

List<Error> _validateDependencies({
String? _findTargetPubspecPath({
required String? appDirectory,
required String? dartOutPath,
String? basePath,
required String apiName,
required List<String> requiredDeps,
List<String> requiredDevDeps = const <String>[],
}) {
final errors = <Error>[];

String? pubspecPath;
if (dartOutPath != null && dartOutPath.isNotEmpty) {
final String fullDartOut =
Expand All @@ -2701,6 +2696,169 @@ List<Error> _validateDependencies({
(dartOutPath == null || dartOutPath.isEmpty)) {
pubspecPath = findPubspecPath(Directory.current);
}
return pubspecPath;
}

/// Checks whether the `objective_c` dependency constraint in `pubspec.yaml`
/// matches the `ObjCVersionCheck(major, minor)` emitted by `ffigen` in the
/// generated `.ffi.dart` file.
///
/// Returns a warning message if the constraint allows incompatible versions, or
/// `null` if the constraint is compatible or cannot be checked.
String? checkObjectiveCPubspecVersion(InternalPigeonOptions options) {
return _checkGeneratedBindingsPubspecVersion(
options: options,
appDirectory: options.swiftOptions?.appDirectory ?? options.appDirectory,
generatedFileSuffix: '.ffi.dart',
versionCheckName: 'ObjCVersionCheck',
packageName: 'objective_c',
bindingTypeLabel: 'FFI',
);
}

/// Checks whether the `jni` dependency constraint in `pubspec.yaml`
/// matches the `JniVersionCheck(major, minor)` emitted by `jnigen` in the
/// generated `.jni.dart` file.
///
/// Returns a warning message if the constraint allows incompatible versions, or
/// `null` if the constraint is compatible or cannot be checked.
String? checkJniPubspecVersion(InternalPigeonOptions options) {
return _checkGeneratedBindingsPubspecVersion(
options: options,
appDirectory: options.kotlinOptions?.appDirectory ?? options.appDirectory,
generatedFileSuffix: '.jni.dart',
versionCheckName: 'JniVersionCheck',
packageName: 'jni',
bindingTypeLabel: 'JNI',
);
}

String? _checkGeneratedBindingsPubspecVersion({
required InternalPigeonOptions options,
required String? appDirectory,
required String generatedFileSuffix,
required String versionCheckName,
required String packageName,
required String bindingTypeLabel,
}) {
final String? dartOutPath = options.dartOptions?.dartOut;
if (dartOutPath == null || dartOutPath.isEmpty) {
return null;
}
final String basePath = options.basePath ?? '';
final String fullDartOut = (basePath.isNotEmpty && !dartOutPath.startsWith(basePath))
? path.join(basePath, dartOutPath)
: dartOutPath;
final bindingsDartFile = File('${path.withoutExtension(fullDartOut)}$generatedFileSuffix');
if (!bindingsDartFile.existsSync()) {
return null;
}

final String bindingsContent;
try {
bindingsContent = bindingsDartFile.readAsStringSync();
} catch (_) {
return null;
}

final RegExpMatch? match = RegExp(
'$versionCheckName\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)',
).firstMatch(bindingsContent);
if (match == null) {
return null;
}

final int major = int.parse(match.group(1)!);
final int minor = int.parse(match.group(2)!);
final requiredMinVersion = Version(major, minor, 0);
final nextMajorVersion = Version(major + 1, 0, 0);

final String? pubspecPath = _findTargetPubspecPath(
appDirectory: appDirectory,
dartOutPath: fullDartOut,
basePath: options.basePath,
Comment thread
tarrinneal marked this conversation as resolved.
Resolved
);
if (pubspecPath == null) {
return null;
}

final resolvedPubspec = File(pubspecPath);
if (!resolvedPubspec.existsSync()) {
return null;
}

try {
final dynamic doc = yaml.loadYaml(resolvedPubspec.readAsStringSync());
if (doc is! yaml.YamlMap) {
return null;
}
final dependencies = doc['dependencies'] as yaml.YamlMap?;
final dependencyOverrides = doc['dependency_overrides'] as yaml.YamlMap?;
final bool hasOverride =
dependencyOverrides != null && dependencyOverrides.containsKey(packageName);
final bool hasDependency =
hasOverride || (dependencies != null && dependencies.containsKey(packageName));
if (!hasDependency) {
return null;
}
final Object? rawDependency = hasOverride
? dependencyOverrides[packageName]
: dependencies![packageName];
final Object? rawConstraint;
if (rawDependency == null) {
rawConstraint = 'any';
} else if (rawDependency is Map) {
if (!rawDependency.containsKey('version') &&
(rawDependency.containsKey('path') ||
rawDependency.containsKey('git') ||
rawDependency.containsKey('sdk'))) {
return null;
}
rawConstraint = rawDependency['version'] ?? 'any';
} else {
rawConstraint = rawDependency;
}
if (rawConstraint is! String) {
Comment thread
tarrinneal marked this conversation as resolved.
Resolved
return null;
}

final constraint = VersionConstraint.parse(rawConstraint);
final bool allowsValidVersion = constraint.allowsAny(
VersionRange(min: requiredMinVersion, max: nextMajorVersion, includeMin: true),
);
final bool allowsLowerVersion = constraint.allowsAny(VersionRange(max: requiredMinVersion));
final bool allowsHigherMajorVersion = constraint.allowsAny(
VersionRange(min: nextMajorVersion.firstPreRelease, includeMin: true),
);

if (!allowsValidVersion || allowsLowerVersion || allowsHigherMajorVersion) {
return 'Generated $bindingTypeLabel bindings in "${path.basename(bindingsDartFile.path)}" require '
'package:$packageName ^$major.$minor.0 (from $versionCheckName($major, $minor)), '
'but "$pubspecPath" specifies "$packageName: $rawConstraint". '
'Consider updating pubspec.yaml to "$packageName: ^$major.$minor.0".';
}
} catch (_) {
return null;
}

return null;
}

List<Error> _validateDependencies({
required String? appDirectory,
required String? dartOutPath,
String? basePath,
required String apiName,
required List<String> requiredDeps,
List<String> requiredDevDeps = const <String>[],
}) {
final errors = <Error>[];

final String? pubspecPath = _findTargetPubspecPath(
appDirectory: appDirectory,
dartOutPath: dartOutPath,
basePath: basePath,
);

if (pubspecPath == null) {
errors.add(
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: pigeon
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
version: 29.0.10 # This must match the version in lib/src/generator_tools.dart
version: 29.0.11 # This must match the version in lib/src/generator_tools.dart

environment:
sdk: ^3.11.0
Expand Down
Loading
Loading