Swift dynamic loading API

Ok, I've put together a standalone basic dynamic plugin API based on the approach of @stackotter and other related posts and blogs I've scrapped up while googling which works on macOS 12+ and Linux without any Foundation or Obj-C dependencies (uses actors though so only recent OS versions).

It's not doing the fancy type filtering, but provides a simple approach of supporting multiple types of plugins by specialising the factory based on the factory type used to create instances. It's also possible of course (and recommended really) to have different types of plugins in different locations.

Example:

  let pluginManager = try await PluginManager<PluginExampleAPIFactory>(withPath: validPath)

  for (_, plugin) in await pluginManager.plugins {
    var myInstance = plugin.factory.create()
    myInstance.setPluginManagerExampleAPI(PluginManagerExampleAPIProvider())
    print(myInstance.callFunctionThatUsesHostingApplicationAPI())
  }

I have the idea to use SPM:s diagnose-api-breaking-changes during compile time to generate a semver automatically in the future for both hosting API and plugin API to have more robust compatibility checking. For the time being, the onus is on the user to make sure compatible plugins are deployed for a given hosting application.

It's early days and no formal version is cut yet and API is subject to change, but if someone wants to play with it I put it out there - the main package for hosting apps can be found here: GitHub - hassila/swift-plugin-manager: Dynamic loading of Swift plug-ins for extensible architectures

The unit tests are required to be run from the command line as I didn't figure out how to find whether it was run from Xcode or where the output directory is there.

To download and run the sample application:

mkdir plugin-test
cd plugin-test
git clone https://github.com/hassila/swift-plugin-manager-example
git clone https://github.com/hassila/swift-plugin-example
cd swift-plugin-example
swift build
cd ../swift-plugin-manager-example
swift run

To download main package and run test suite on both macOS and Linux:

git clone https://github.com/hassila/swift-plugin-manager
cd swift-plugin-manager/
swift test
...

Hope it can be useful for someone, but for me its just a basic starting point for plugin support without Foundation / ObjC.

Merry Christmas to everyone!

Joakim

12 Likes