ensureFlutterFrameworkSymlink function

bool ensureFlutterFrameworkSymlink(
  1. String packageSwiftPath,
  2. String pluginBaseDir
)

Attempts to ensure the FlutterFramework package referenced in packageSwiftPath is resolvable by creating a symlink at the expected path.

Search order (relative to pluginBaseDir):

  1. example/ios/Flutter/ephemeral/Packages/.packages/FlutterFramework (iOS)
  2. example/macos/Flutter/ephemeral/Packages/.packages/FlutterFramework (macOS)

Returns true when a symlink was created. Returns false when the path already resolves, no FlutterFramework could be located, or the operation fails (e.g. symlinks not supported on this OS).

Implementation

bool ensureFlutterFrameworkSymlink(String packageSwiftPath, String pluginBaseDir) {
  if (flutterFrameworkPathExists(packageSwiftPath)) return false;

  final file = File(packageSwiftPath);
  if (!file.existsSync()) return false;
  final content = file.readAsStringSync();

  final match = RegExp(
    r'\.package\s*\(\s*name\s*:\s*"FlutterFramework"\s*,\s*path\s*:\s*"([^"]+)"\s*\)',
  ).firstMatch(content);
  if (match == null) return false;

  final relativePath = match.group(1)!;
  final packageDir = file.parent.path;
  final targetPath = p.normalize(p.join(packageDir, relativePath));

  // Candidate FlutterFramework locations (both iOS and macOS variants).
  final candidates = [
    p.join(pluginBaseDir, 'example', 'ios', 'Flutter', 'ephemeral', 'Packages', '.packages', 'FlutterFramework'),
    p.join(pluginBaseDir, 'example', 'macos', 'Flutter', 'ephemeral', 'Packages', '.packages', 'FlutterFramework'),
  ];

  for (final candidate in candidates) {
    if (Directory(candidate).existsSync()) {
      try {
        Directory(p.dirname(targetPath)).createSync(recursive: true);
        final relLink = p.relative(candidate, from: p.dirname(targetPath));
        Link(targetPath).createSync(relLink);
        return true;
      } catch (_) {}
    }
  }
  return false;
}