You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2024 Jon Evans <jon@craftyjon.com>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef KICAD_API_PLUGIN_H
  21. #define KICAD_API_PLUGIN_H
  22. #include <memory>
  23. #include <optional>
  24. #include <set>
  25. #include <nlohmann/json_fwd.hpp>
  26. #include <wx/bmpbndl.h>
  27. #include <wx/filename.h>
  28. #include <wx/string.h>
  29. #include <api/plugin_action_scope.h>
  30. #include <kicommon.h>
  31. struct API_PLUGIN_CONFIG;
  32. class API_PLUGIN;
  33. class JSON_SCHEMA_VALIDATOR;
  34. struct PLUGIN_DEPENDENCY
  35. {
  36. wxString package_name;
  37. wxString version;
  38. };
  39. enum class PLUGIN_RUNTIME_TYPE
  40. {
  41. INVALID,
  42. PYTHON,
  43. EXEC
  44. };
  45. struct PLUGIN_RUNTIME
  46. {
  47. bool FromJson( const nlohmann::json& aJson );
  48. PLUGIN_RUNTIME_TYPE type;
  49. wxString min_version;
  50. std::vector<PLUGIN_DEPENDENCY> dependencies;
  51. };
  52. /**
  53. * An action performed by a plugin via the IPC API
  54. * (not to be confused with ACTION_PLUGIN, the old SWIG plugin system, which will be removed
  55. * in the future)
  56. */
  57. struct PLUGIN_ACTION
  58. {
  59. PLUGIN_ACTION( const API_PLUGIN& aPlugin ) :
  60. plugin( aPlugin )
  61. {}
  62. wxString identifier;
  63. wxString name;
  64. wxString description;
  65. bool show_button = false;
  66. wxString entrypoint;
  67. std::set<PLUGIN_ACTION_SCOPE> scopes;
  68. std::vector<wxString> args;
  69. wxBitmapBundle icon_light;
  70. wxBitmapBundle icon_dark;
  71. const API_PLUGIN& plugin;
  72. };
  73. /**
  74. * A plugin that is invoked by KiCad and runs as an external process; communicating with KiCad
  75. * via the IPC API. The plugin metadata is read from a JSON file containing things like which
  76. * actions the plugin is capable of and how to invoke each one.
  77. */
  78. class KICOMMON_API API_PLUGIN
  79. {
  80. public:
  81. API_PLUGIN( const wxFileName& aConfigFile, const JSON_SCHEMA_VALIDATOR& aValidator );
  82. ~API_PLUGIN();
  83. bool IsOk() const;
  84. static bool IsValidIdentifier( const wxString& aIdentifier );
  85. const wxString& Identifier() const;
  86. const wxString& Name() const;
  87. const wxString& Description() const;
  88. const PLUGIN_RUNTIME& Runtime() const;
  89. wxString BasePath() const;
  90. const std::vector<PLUGIN_ACTION>& Actions() const;
  91. wxString ActionSettingsKey( const PLUGIN_ACTION& aAction ) const;
  92. private:
  93. friend struct API_PLUGIN_CONFIG;
  94. std::optional<PLUGIN_ACTION> createActionFromJson( const nlohmann::json& aJson );
  95. wxFileName m_configFile;
  96. std::unique_ptr<API_PLUGIN_CONFIG> m_config;
  97. };
  98. /**
  99. * Comparison functor for ensuring API_PLUGINs have unique identifiers
  100. */
  101. struct CompareApiPluginIdentifiers
  102. {
  103. bool operator()( const std::unique_ptr<API_PLUGIN>& item1,
  104. const std::unique_ptr<API_PLUGIN>& item2 ) const
  105. {
  106. return item1->Identifier() < item2->Identifier();
  107. }
  108. };
  109. #endif //KICAD_API_PLUGIN_H