2. DNF5 Plugin Template

Below is a template code for a DNF5 plugin. For the complete tutorial on writing DNF5 plugins, refer to DNF5 Plugins.

dnf5-plugins/template_plugin/template_cmd_plugin.cpp

  1#include "template_cmd.hpp"
  2
  3#include <dnf5/iplugin.hpp>
  4
  5#include <cstring>
  6
  7using namespace dnf5;
  8
  9namespace {
 10
 11constexpr const char * PLUGIN_NAME{"dnf5_template_plugin"};
 12constexpr PluginVersion PLUGIN_VERSION{.major = 1, .minor = 0, .micro = 0};
 13constexpr PluginAPIVersion REQUIRED_PLUGIN_API_VERSION{.major = 2, .minor = 0};
 14
 15constexpr const char * attrs[]{"author.name", "author.email", "description", nullptr};
 16constexpr const char * attrs_value[]{"Fred Fedora", "dummy@email.com", "Plugin description."};
 17
 18class TemplateCmdPlugin : public IPlugin {
 19public:
 20    using IPlugin::IPlugin;
 21
 22    /// Fill in the API version of your plugin.
 23    /// This is used to check if the provided plugin API version is compatible with the application's plugin API version.
 24    /// MANDATORY to override.
 25    PluginAPIVersion get_api_version() const noexcept override { return REQUIRED_PLUGIN_API_VERSION; }
 26
 27    /// Enter the name of your new plugin.
 28    /// This is used in log messages when an action or error related to the plugin occurs.
 29    /// MANDATORY to override.
 30    const char * get_name() const noexcept override { return PLUGIN_NAME; }
 31
 32    /// Fill in the version of your plugin.
 33    /// This is utilized in informative and debugging log messages.
 34    /// MANDATORY to override.
 35    PluginVersion get_version() const noexcept override { return PLUGIN_VERSION; }
 36
 37    /// Add custom attributes, such as information about yourself and a description of the plugin.
 38    /// These can be used to query plugin-specific data through the API.
 39    /// Optional to override.
 40    const char * const * get_attributes() const noexcept override { return attrs; }
 41    const char * get_attribute(const char * attribute) const noexcept override {
 42        for (size_t i = 0; attrs[i]; ++i) {
 43            if (std::strcmp(attribute, attrs[i]) == 0) {
 44                return attrs_value[i];
 45            }
 46        }
 47        return nullptr;
 48    }
 49
 50    /// Export all the commands that plugin is implementing.
 51    /// MANDATORY to override.
 52    std::vector<std::unique_ptr<Command>> create_commands() override;
 53
 54    /// Initialization method called after the Base object is created and before command-line arguments are parsed.
 55    /// Optional to override.
 56    void init() override {}
 57
 58    /// Cleanup method called when plugin objects are garbage collected.
 59    /// Optional to override.
 60    void finish() noexcept override {}
 61};
 62
 63std::vector<std::unique_ptr<Command>> TemplateCmdPlugin::create_commands() {
 64    std::vector<std::unique_ptr<Command>> commands;
 65    commands.push_back(std::make_unique<TemplateCommand>(get_context()));
 66    return commands;
 67}
 68
 69
 70}  // namespace
 71
 72/// Below is a block of functions with C linkage used for loading the plugin binaries from disk.
 73/// All of these are MANDATORY to implement.
 74
 75/// Return plugin's API version.
 76PluginAPIVersion dnf5_plugin_get_api_version(void) {
 77    return REQUIRED_PLUGIN_API_VERSION;
 78}
 79
 80/// Return plugin's name.
 81const char * dnf5_plugin_get_name(void) {
 82    return PLUGIN_NAME;
 83}
 84
 85/// Return plugin's version.
 86PluginVersion dnf5_plugin_get_version(void) {
 87    return PLUGIN_VERSION;
 88}
 89
 90/// Return the instance of the implemented plugin.
 91IPlugin * dnf5_plugin_new_instance([[maybe_unused]] ApplicationVersion application_version, Context & context) try {
 92    return new TemplateCmdPlugin(context);
 93} catch (...) {
 94    return nullptr;
 95}
 96
 97/// Delete the plugin instance.
 98void dnf5_plugin_delete_instance(IPlugin * plugin_object) {
 99    delete plugin_object;
100}

dnf5-plugins/template_plugin/CMakeLists.txt

 1# set gettext domain for translations
 2set(GETTEXT_DOMAIN template-dnf5-cmd-plugin)
 3add_definitions(-DGETTEXT_DOMAIN=\"${GETTEXT_DOMAIN}\")
 4
 5#include_directories(${PROJECT_SOURCE_DIR}/libdnf5)
 6include_directories(${PROJECT_SOURCE_DIR}/dnf5/include)
 7
 8add_library(template_dnf5_cmd_plugin MODULE template_cmd.cpp template_cmd_plugin.cpp)
 9
10# disable the 'lib' prefix in order to create template_cmd_plugin.so
11set_target_properties(template_dnf5_cmd_plugin PROPERTIES PREFIX "")
12
13#target_link_libraries(template_cmd_plugin PRIVATE libdnf5 libdnf5-cli)
14target_link_libraries(template_dnf5_cmd_plugin PRIVATE dnf5)
15
16# install plugin library
17#install(TARGETS template_dnf5_cmd_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}/dnf5/plugins/)