linkPodspec function
void
linkPodspec(})
Implementation
void linkPodspec(
String pluginName,
List<String> moduleLibs, {
String baseDir = '.',
List<ModuleInfo>? moduleInfos,
}) {
final nitroNativePath = resolveNitroNativePath(baseDir);
final podspecFile = File(p.join(baseDir, 'ios', '$pluginName.podspec'));
if (!podspecFile.existsSync()) return;
var content = podspecFile.readAsStringSync();
bool modified = false;
// Normalize source_files to 'Classes/**/*'.
// Flutter's SPM-first template generates paths like '<plugin>/Sources/<plugin>/**/*'
// which point to non-existent directories when CocoaPods is the build system,
// causing "No files found matching ..." warnings and empty pod targets.
final sourceFilesMatch = RegExp(r"s\.source_files\s*=\s*'([^']+)'").firstMatch(content);
if (sourceFilesMatch != null && sourceFilesMatch.group(1) != 'Classes/**/*') {
final badPath = sourceFilesMatch.group(1)!;
// Fix any non-Classes path. Flutter's SPM-first template generates paths like
// '<plugin>/Sources/<plugin>/**/*'; for SPM-layout plugins the first directory
// segment exists on disk even though the glob matches nothing, so we cannot
// rely on existsSync() to detect the bad path — always normalize.
final firstSegment = badPath.split('/').first;
if (firstSegment != 'Classes') {
content = content.replaceFirst(
sourceFilesMatch.group(0)!,
"s.source_files = 'Classes/**/*'",
);
modified = true;
}
}
if (!content.contains("s.swift_version = '${BuildVersions.podSwiftVersion}'")) {
content = content.replaceFirst(
RegExp(r"s\.swift_version\s*=\s*'.+?'"),
"s.swift_version = '${BuildVersions.podSwiftVersion}'",
);
modified = true;
}
if (!content.contains("s.platform = :ios, '${BuildVersions.iosDeployment}.0'")) {
content = content.replaceFirst(
RegExp(r"s\.platform\s*=\s*:ios,\s*'.+?'"),
"s.platform = :ios, '${BuildVersions.iosDeployment}.0'",
);
modified = true;
}
final xcResult = _patchApplePodspecXcconfig(content, isMacos: false);
content = xcResult.content;
if (xcResult.modified) modified = true;
// Sync generated Swift bridges into ios/Classes/ so Xcode can compile them
// in the same module scope as the plugin's other Swift files.
// Using a podspec source_files glob to ../lib/src/generated/swift/ does NOT
// reliably work — types defined there are not always in scope for Classes/ files.
if (modified) podspecFile.writeAsStringSync(content);
createSharedHeaders(nitroNativePath, baseDir: baseDir);
final classesDir = Directory(p.join(baseDir, 'ios', 'Classes'))..createSync(recursive: true);
File(
p.join(classesDir.path, 'dart_api_dl.c'),
).writeAsStringSync(classesDartApiDlForwarder);
syncBridgeFiles(baseDir);
_copySwiftBridgesToClasses(classesDir, baseDir);
// Remove the outer lib/src/generated/swift glob from the podspec if present,
// since the bridge is now copied directly into Classes/ (avoids duplicate symbols).
_removeSwiftGlobFromPodspec(podspecFile);
// Link the main project source files.
final cppInSrc = File(p.join(baseDir, 'src', '$pluginName.cpp'));
if (cppInSrc.existsSync()) {
cleanRedundantIncludes(cppInSrc);
File(p.join(classesDir.path, '$pluginName.cpp')).writeAsStringSync(
managedCppForwarder('../../src/$pluginName.cpp'),
);
}
final cInSrc = File(p.join(baseDir, 'src', '$pluginName.c'));
if (cInSrc.existsSync()) {
cleanRedundantIncludes(cInSrc);
File(
p.join(classesDir.path, '$pluginName.c'),
).writeAsStringSync(classesCForwarder(pluginName));
}
// Link C++ module implementation files for iOS.
// On Android each module is a separate .so via CMake. On iOS everything is
// compiled into one pod binary, so only ios:NativeImpl.cpp modules need
// a Hybrid*.cpp forwarder in ios/Classes/.
// Windows-only or macos-only C++ modules must NOT get a forwarder here.
_writeAppleModuleForwarders(moduleInfos, classesDir, baseDir, isIosCppModule);
ensureIosPackageSwift(pluginName, baseDir: baseDir, moduleInfos: moduleInfos);
// Re-affirm the correct ../../src/ relative paths AFTER ensureIosPackageSwift,
// which may write forwarders into Sources/NitroPubTestCpp/ with ../../../src/.
// These are two different files, but belt-and-suspenders: always end with the
// definitive Classes/ versions so a stale copy can never win.
File(
p.join(classesDir.path, 'dart_api_dl.c'),
).writeAsStringSync(classesDartApiDlForwarder);
if (cppInSrc.existsSync()) {
File(p.join(classesDir.path, '$pluginName.cpp')).writeAsStringSync(
managedCppForwarder('../../src/$pluginName.cpp'),
);
}
if (cInSrc.existsSync()) {
File(
p.join(classesDir.path, '$pluginName.c'),
).writeAsStringSync(classesCForwarder(pluginName));
}
}