Skip to content

Commit 52f874c

Browse files
committed
Load plugins in CLI, beginin of the final refactor in the CLI.
1 parent 98fc66f commit 52f874c

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

source/cli/metacallcli/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ include(SecurityFlags)
2626

2727
set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
2828
set(inline_path "${CMAKE_CURRENT_SOURCE_DIR}/inline/${target}")
29-
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
29+
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
3030

3131
set(headers
3232
${include_path}/tokenizer.hpp
@@ -139,6 +139,15 @@ target_compile_options(${target}
139139
${DEFAULT_COMPILE_OPTIONS}
140140
)
141141

142+
#
143+
# Compile features
144+
#
145+
146+
target_compile_features(${target}
147+
PRIVATE
148+
cxx_std_17 # Required for filesystem
149+
)
150+
142151
#
143152
# Linker options
144153
#

source/cli/metacallcli/include/metacallcli/application.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ class application
301301
/* -- Private Member Data -- */
302302

303303
bool exit_condition; /**< Condition for main loop */
304+
void *plugin_cli_handle; /**< Handle containing all loaded plugins */
304305
arg_list arguments; /**< Vector containing a list of arguments */
305306
script_list scripts; /**< Vector containing a list of script names */
306307
command_table commands; /**< Hash table from command strings to command handlers */

source/cli/metacallcli/source/application.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <metacallcli/tokenizer.hpp>
1414

1515
#include <algorithm>
16+
#include <filesystem>
1617
#include <functional>
1718
#include <iostream>
1819

@@ -621,7 +622,7 @@ bool application::clear(const std::string &tag, const std::string &script)
621622
}
622623

623624
application::application(int argc, char *argv[]) :
624-
exit_condition(false)
625+
exit_condition(false), plugin_cli_handle(NULL)
625626
{
626627
/* Set locale */
627628
setlocale(LC_CTYPE, "C");
@@ -667,6 +668,33 @@ application::application(int argc, char *argv[]) :
667668
std::for_each(&argv[1], argv + /*argc*/ 2, param_it);
668669
}
669670

671+
/* Get core plugin path and handle in order to load cli plugins */
672+
const char *plugin_path = metacall_plugin_path();
673+
void *plugin_extension_handle = metacall_plugin_extension();
674+
675+
/* Define the cli plugin path as string (core plugin path plus cli) */
676+
namespace fs = std::filesystem;
677+
fs::path plugin_cli_path(plugin_path);
678+
plugin_cli_path /= "cli";
679+
std::string plugin_cli_path_str(plugin_cli_path.string());
680+
681+
/* Load cli plugins into plugin cli handle */
682+
void *args[] = {
683+
metacall_value_create_string(plugin_cli_path_str.c_str(), plugin_cli_path_str.length()),
684+
metacall_value_create_ptr(&plugin_cli_handle)
685+
};
686+
687+
void *ret = metacallhv_s(plugin_extension_handle, "plugin_load_from_path", args, sizeof(args) / sizeof(args[0]));
688+
689+
if (ret == NULL || (ret != NULL && metacall_value_to_int(ret) != 0))
690+
{
691+
std::cout << "Failed to load CLI plugins from folder: " << plugin_cli_path_str << std::endl;
692+
}
693+
694+
metacall_value_destroy(args[0]);
695+
metacall_value_destroy(args[1]);
696+
metacall_value_destroy(ret);
697+
670698
/* Define available commands */
671699
define("help", &command_cb_help);
672700

@@ -833,8 +861,15 @@ void application::command_inspect(const char *str, size_t size, void *allocator)
833861
value_array_for_each(v_args_array, [&iterator, &count](void *arg) {
834862
void **v_arg_map = metacall_value_to_map(arg);
835863
void **v_arg_name_tupla = metacall_value_to_array(v_arg_map[0]);
864+
std::string parameter_name(metacall_value_to_string(v_arg_name_tupla[1]));
865+
866+
if (parameter_name.empty())
867+
{
868+
parameter_name += "arg";
869+
parameter_name += std::to_string(iterator);
870+
}
836871

837-
std::cout << metacall_value_to_string(v_arg_name_tupla[1]);
872+
std::cout << parameter_name;
838873

839874
if (iterator + 1 < count)
840875
{

source/metacall/include/metacall/metacall.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,15 @@ METACALL_API int metacall_clear(void *handle);
14051405
*/
14061406
METACALL_API void *metacall_plugin_extension(void);
14071407

1408+
/**
1409+
* @brief
1410+
* Get the plugin extension path to be used for accessing the plugins folder
1411+
*
1412+
* @return
1413+
* String containing the core plugin path, or null if it failed to load the plugin extension
1414+
*/
1415+
METACALL_API const char *metacall_plugin_path(void);
1416+
14081417
/**
14091418
* @brief
14101419
* Destroy MetaCall library

source/metacall/source/metacall.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ static int metacall_config_flags = 0;
6161
static int metacall_initialize_argc = 0;
6262
static char **metacall_initialize_argv = NULL;
6363
static void *plugin_extension_handle = NULL;
64+
static loader_path plugin_path = { 0 };
6465

6566
/* -- Private Methods -- */
6667

@@ -94,7 +95,6 @@ int metacall_plugin_extension_load(void)
9495
};
9596
static const char plugin_suffix[] = "plugins";
9697
const char *library_path = loader_library_path();
97-
loader_path plugin_path;
9898
size_t plugin_path_size;
9999
void *args[2];
100100
void *ret;
@@ -2209,6 +2209,16 @@ void *metacall_plugin_extension(void)
22092209
return plugin_extension_handle;
22102210
}
22112211

2212+
const char *metacall_plugin_path(void)
2213+
{
2214+
if (plugin_extension_handle == NULL)
2215+
{
2216+
return NULL;
2217+
}
2218+
2219+
return plugin_path;
2220+
}
2221+
22122222
int metacall_destroy(void)
22132223
{
22142224
if (metacall_initialize_flag == 0)

0 commit comments

Comments
 (0)