Just to be super clear, here's the setup with three separate SPM packages:
- DataModel (providing the plugin, the test targets uses the plugin internally here but also explicitly adds dependency on flatbuffers)
// swift-tools-version: 5.6
import PackageDescription
let package = Package(
name: "package-data-model",
platforms: [
.macOS(.v12),
],
products: [
.executable(
name: "DataModelGenerator",
targets: ["DataModelGenerator"]
),
.plugin(name: "DataModel",
targets: ["DataModel"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMajor(from: "1.1.0")),
.package(url: "https://github.com/apple/swift-log", .upToNextMajor(from: "1.0.0")),
.package(url: "https://github.com/apple/swift-system", .upToNextMajor(from: "1.0.0")),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/mustiikhalil/flatbuffers", branch: "swift"),
],
targets: [
.plugin(
name: "DataModel",
capability: .buildTool(),
dependencies: ["DataModelGenerator"]
),
.executableTarget(
name: "DataModelGenerator",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Logging", package: "swift-log"),
.product(name: "SystemPackage", package: "swift-system"),
"DataModelDefinition",
],
path: "Sources/DataModelGenerator"
),
.target(name: "DataModelDefinition", path: "Sources/DataModelDefinition"),
.testTarget(
name: "DataModelGeneratorTests",
dependencies: ["DataModelGenerator",
"DataModelDefinition",
.product(name: "FlatBuffers", package: "flatbuffers")],
plugins: [
.plugin(name: "DataModel")
]
),
.testTarget(
name: "DataModelTests",
dependencies: [
.product(name: "FlatBuffers", package: "flatbuffers"),
],
plugins: [
.plugin(name: "DataModel")
]
),
]
)
- Core API - se comments for the dependencies, two slightly different issues that are unexpected
// swift-tools-version: 5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "api-core",
platforms: [
.macOS(.v12),
],
products: [
.library(
name: "Core",
targets: ["Core"]
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/ordo-one/package-data-model", branch: "feature/sc-347/add-build-tool-for-data-model-generation"),
.package(url: "https://github.com/mustiikhalil/flatbuffers", branch: "swift"),
],
targets: [
.target(
name: "Core",
dependencies: [
.product(name: "DataModelGenerator", package: "package-data-model"), // this tool is needed to build the build plugin source
.product(name: "FlatBuffers", package: "flatbuffers"), // this is needed to be able to build the generated code
],
plugins: [
.plugin(name: "DataModel", package: "package-data-model")
]
),
.testTarget(
name: "CoreTests",
dependencies: ["Core"]
),
]
)
- Final app consuming the Core API
// swift-tools-version: 5.6
import PackageDescription
let package = Package(
name: "prototype-app",
platforms: [
.macOS(.v12),
],
products: [
.library(
name: "xxx",
type: .dynamic,
targets: ["xxx"]
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/ordo-one/api-app", branch: "feature/sc-347/add-build-tool-for-data-model-generation"),
.package(url: "https://github.com/ordo-one/api-core", branch: "feature/sc-347/add-build-tool-for-data-model-generation"),
],
targets: [
.target(
name: "xxx",
dependencies: [
.product(name: "Application", package: "api-app"),
.product(name: "Core", package: "api-core"),
]
),
.testTarget(
name: "xxxTests",
dependencies: ["xxx"]
),
]
)