packageSwiftTemplate function
Generates the SPM Package.swift for pluginName.
platformSpec is e.g. 'iOS(.v13)' or 'macOS(.v10_15)'.
isMacos switches the Xcode flag path between iOS and macOS conventions.
Implementation
String packageSwiftTemplate(
String pluginName,
String className,
String platformSpec, {
bool isMacos = false,
}) {
return '''
// swift-tools-version: 5.9
import PackageDescription
let package = Package(
name: "$pluginName",
platforms: [.$platformSpec],
products: [
.library(name: "${pluginName.replaceAll('_', '-')}", targets: ["$pluginName"]),
],
targets: [
// C/C++ bridge — SPM requires Swift and C++ in separate targets.
// nitro headers (nitro.h, dart_api_dl.h …) are copied into include/
// by `nitrogen link`, so no extra header search path is needed.
.target(
name: "${className}Cpp",
path: "Sources/${className}Cpp",
publicHeadersPath: "include",
cxxSettings: [
.headerSearchPath("include"),
.unsafeFlags(["-std=c++17"])
]
),
// Swift implementation + generated bridge.
.target(
name: "$pluginName",
dependencies: ["${className}Cpp"],
path: "Sources/$className"
),
]
)
''';
}