packageSwiftTemplate function
Generates the SPM Package.swift for pluginName.
platformSpec is e.g. BuildVersions.iosPlatformSpec or
BuildVersions.macosPlatformSpec.
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: ${BuildVersions.swiftTools}
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(["${BuildVersions.spmCxxFlag}"])
]
),
// Swift implementation + generated bridge.
.target(
name: "$pluginName",
dependencies: ["${className}Cpp"],
path: "Sources/$className"
),
]
)
''';
}