[Gambas-user] How to create a plugin system for your Gambas project

Benoît Minisini g4mba5 at gmail.com
Wed Feb 17 18:27:46 CET 2021


Hi,

I saw the thread about plugin management system, but didn't actually
read it in detail.

So I decided to start a new thread about that, and concerned people will
tell me if it helps them, if they already did that way, and so on...

Regards,

------------------------------------------------------------------------

(0) HOW TO CREATE A PLUGIN SYSTEM IN GAMBAS ?

First, there is (almost) no technical difference between a project
executable and an executable library.

So let's suppose our main project is named "MainProject", and that we
want to develop a plugin for that main project, from another project
named "MyPlugin".

------------------------------------------------------------------------

(1) LOADING THE PLUGIN FROM THE MAIN PROJECT

You must know the absolute path of the executable of the plugin, and you
load it by doing:

   Component.Load(PluginAbsolutePath)

So your plugin becames a component for the project. The name of that
component is the name of the plugin executable file without the
extension. So, in our case, "MyPlugin".

*Note: you cannot unload a component, this is not supported by Gambas at 
the moment.*

------------------------------------------------------------------------

(2) TO KNOW IF THE PLUGIN IS ALREADY LOADED

Just do that:

   If Component.IsLoaded("MyPlugin") Then ...

------------------------------------------------------------------------

(3) COMMUNICATION BETWEEN THE MAIN PROJECT AND THE PLUGIN AT RUNTIME

This must be done by using exported classes.

The plugin can call any class exported by the main project.

The main project can call any class exported by the plugin project.

Why? Because there is one symbol table per project, that is private
except for exported classes, and one global symbol table for all
exported classes of all projects.

This is the way for components (native components, components written in
Gambas, libraries, any executable) to share things (i.e. classes).

*NOTE: Any piece of code can call any public method of any class as soon
as it as a reference on an object of that class, so exported class is
not the only way of share things between projects. But using exported
classes make things explicit and clear.*

That means that you must be careful with the name of your exported
classes (no namespace, only one global symbol table!).

Usually, you will define a common public interface for all your plugins.

One exported class may be enough for that. As said before, the name of
that exported class must be unique, so you should put inside the name of
the plugin.

For example, you can decide that all plugins must export a class named
"MainProjectPlugin_<NameOfThePlugin>" or something better than that.
Then you will have a public function in that class ("Main"?) that will
be called by the main project to initialize the plugin.

------------------------------------------------------------------------

(4) HOW TO GET THE EXPORTED CLASSES OF THE MAIN PROJECT DURING PLUGIN
DEVELOPMENT

To let the IDE know which exported classes your project can use, you
must go the "Libraries" tab of the plugin project property dialog.

Then you can add to the "Additional references" list the path of your
main project executable.

Each exported classes of each executable in that list will be available
to your project (with automatic completion) without raising "unknown
identifier" errors.

-- 
Benoît Minisini


More information about the User mailing list