Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift plug-in architecture

Tags:

plugins

swift

I'm looking for a way to implement at plug-in architecture in Swift.

Use Case

An application, fx. a CLI, has a folder from where it's executed where plug-ins can be placed and will be loaded when the application starts.

Requirements

  • Being able to define a protocol or a set of protocols that plug-ins must or can adhere to.
  • Pure Swift.
  • Introspect what protocols a given plug-in implements.
  • Loaded dynamically when the application starts up.
  • Communicate with delegate pattern.
  • Plug-in developers should be able to import protocols, implement them, compile and place the binary in the plug-in folder.

What I've tried

I've used Processto start an executable where I can also add arguments. But that doesn't satisfy my requirements.

I came across this post of how it could be done, but that requires use of NSBundle and Objective-C so that's no good either.

I'm not really sure how to go about this. Any help or pointers in the right direction would be greatly appreciated.

like image 908
Anton Gregersen Avatar asked Nov 19 '16 14:11

Anton Gregersen


People also ask

What is plug-in architecture?

A plug-in is a bundle that adds functionality to an application, called the host application, through some well-defined architecture for extensibility. This allows third-party developers to add functionality to an application without having access to the source code.

What is plugin framework?

The plugin framework is a NuGet package that allows you to customise and extend a .NET application. Plugins are loaded at runtime from a catalogue which can be a local source or via NuGet. Supports all the current . NET Core based applications, ranging from Blazor to ASP.NET Core and to Console, WPF and Windows Forms.

What is a primary benefit of a plug-in architecture?

The advantages of a plug-in architecture is obviously the increase in flexibility. This allows other developers to extend your application in ways that did not expect in the first place. Note that there are various plug-in architecture ranging from flexible to extreme flexible.


1 Answers

Swift currently doesn't have a stable ABI so you can't rely on native interfaces yet. (This is expected to be resolved by Swift 4.)

I can think of two options for now, but both rely on defining some sort of a communication protocol between host and plug-in. You'll also have to implement your requirements (introspection of features, etc.) by yourself if you can't use Objective-C.

  1. Lower to the C ABI. For every function you need, implement it on the called side in Swift with the @convention(c) annotation and declare it on the caller side in a header for external linkage. You won't be able to implement a function twice, so you should define a callback-based API that plug-ins register themselves into.

  2. Define a (e.g., socket-based) communication protocol and write the Swift source files to be imported in every plug-in. Make the plug-ins executable which, upon launch by the host application, start the communication.

I personally prefer option 2 because it doesn't require mingling with linkers and such and you get the added bonus of defining Swift types and functions for the plug-in's use. Also, since the plug-in is a forked child, it can crash without affecting the host.

like image 112
Constantino Tsarouhas Avatar answered Oct 21 '22 16:10

Constantino Tsarouhas