-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature/easier plugin creation #31
base: beatrice
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
add_subdirectory( witness ) | ||
add_subdirectory( account_history ) | ||
add_subdirectory( accounts_list ) | ||
add_subdirectory( affiliate_stats ) | ||
add_subdirectory( market_history ) | ||
add_subdirectory( delayed_node ) | ||
add_subdirectory( bookie ) | ||
add_subdirectory( generate_genesis ) | ||
add_subdirectory( generate_uia_sharedrop_genesis ) | ||
add_subdirectory( debug_witness ) | ||
add_subdirectory( snapshot ) | ||
# for each subdirectory containing a CMakeLists.txt, add that subdirectory | ||
file( GLOB children RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} * ) | ||
foreach( child ${children} ) | ||
if( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${child}" ) | ||
if( EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${child}/CMakeLists.txt" ) | ||
add_subdirectory( "${child}" ) | ||
endif() | ||
endif() | ||
endforeach() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugin_name": "TestPlugin1", | ||
"plugin_namespace": "TestPlugin1", | ||
"plugin_project": "TestPlugin1_plugin" | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugin_name": "TestPlugin2", | ||
"plugin_namespace": "TestPlugin2", | ||
"plugin_project": "TestPlugin2_plugin" | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#!/usr/bin/python | ||
|
||
# Copyright (c) 2018 Blockchain B.V. | ||
# | ||
# The MIT License | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
|
||
################################################################################# | ||
# This script is called everytime cmake is called. It generates code to certain # | ||
# files which are related to the creation of plugins. # | ||
# The working directory of the script is the project root directory. # | ||
# Script is called from libraries/utilities/CMakeLists.txt # | ||
################################################################################# | ||
|
||
|
||
import json | ||
import os | ||
|
||
|
||
# list of files where generated code is used (relative to project root) | ||
# libraries/app/include/graphene/api.hpp | ||
# libraries/app/api.cpp | ||
# libraries/app/application.cpp | ||
|
||
generated_files_root_dir = "libraries/utilities/include/graphene/utilities/" | ||
|
||
plugin_root_dir = "libraries/plugins/" | ||
|
||
generated_code_warning = "\ | ||
/*****************************************/\n\ | ||
/* This code is generated automatically. */\n\ | ||
/* To generate new plugin code create a */\n\ | ||
/* plugin.json file in a subdirectory */\n\ | ||
/* of the plugin root directory. */\n\ | ||
/*****************************************/\n\ | ||
\n\ | ||
\n" | ||
|
||
# this is a dictionary of the generated filename and the generated code | ||
# variables in the code are replaced by the variables from the plugin.json file | ||
templates = { | ||
"generated_api_cpp_case_code.hpp": | ||
"else if( api_name == \"{plugin_name}_api\" ) {{ \n\ | ||
_{plugin_name}_api = std::make_shared< {plugin_name}_api >( std::ref(_app) );\n\ | ||
}} \n", | ||
|
||
"generated_api_cpp_login_api_code.hpp": | ||
"fc::api<graphene::{plugin_namespace}::{plugin_name}_api> login_api::{plugin_name}() const \n\ | ||
{{ \n\ | ||
FC_ASSERT(_{plugin_name}_api); \n\ | ||
return *_{plugin_name}_api; \n\ | ||
}}\n\n", | ||
|
||
"generated_api_hpp_include_code.hpp": | ||
"#include <graphene/{plugin_name}/{plugin_name}_api.hpp> \n", | ||
|
||
"generated_api_hpp_login_api_method_def_code.hpp": | ||
"/// @brief Retrieve the {plugin_name} API \n\ | ||
fc::api<graphene::{plugin_namespace}::{plugin_name}_api> {plugin_name}()const;\n", | ||
|
||
"generated_api_hpp_login_api_apiobj_code.hpp": | ||
"optional< fc::api<graphene::{plugin_namespace}::{plugin_name}_api> > _{plugin_name}_api;\n", | ||
|
||
"generated_api_hpp_reflect_code.hpp": | ||
"({plugin_name})\n", | ||
|
||
"generated_application_cpp_allowed_apis_code.hpp": | ||
"wild_access.allowed_apis.push_back( \"{plugin_name}_api\" );\n", | ||
|
||
} | ||
|
||
|
||
# searches for all plugin.json files in the plugin directory | ||
# parses them and returns a list of dictionarys with the parsed data | ||
def get_all_plugin_data(): | ||
|
||
# holds content of all plugin.json files as dictionary | ||
plugin_data_list = [] | ||
# traverse plugin root dir search for plugin.json files | ||
for root, dirs, files in os.walk( "./" + plugin_root_dir ): | ||
for file in files: | ||
if file == "plugin.json": | ||
with open( os.path.join( root, file ) ) as f: | ||
parsed_data = json.load( f ) | ||
data_dic = dict() | ||
data_dic['plugin_name'] = parsed_data['plugin_name'] | ||
data_dic['plugin_namespace'] = parsed_data['plugin_namespace'] | ||
data_dic['plugin_project'] = parsed_data['plugin_project'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a python expert - is it really necessary to copy the fields instead of appending There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it would be possible to iterate inside there. But my thought was, we don't have much data hence the speed isn't really necessary here. And like this it's just better separated. |
||
plugin_data_list.append( data_dic ) | ||
|
||
return plugin_data_list | ||
|
||
|
||
def generate_all_files( plugin_data_list ): | ||
print "--" | ||
# t_fn = template_filename, t_con = template_content | ||
for t_fn, t_con in templates.items(): | ||
f_dir = os.path.join( generated_files_root_dir, t_fn ) | ||
with open( f_dir, 'w') as f: | ||
f.write( generated_code_warning ) | ||
# p_data = plugin_data | ||
for p_data in plugin_data_list: | ||
# **p_data = values of the dic | ||
content = t_con.format( **p_data ) | ||
f.write( content ) | ||
print( "-- plugin_code_generator: " + t_fn + " successfully generated" ) | ||
|
||
|
||
def main(): | ||
|
||
plugin_data_list = get_all_plugin_data() | ||
|
||
generate_all_files( plugin_data_list ) | ||
|
||
print "-- plugin_code_generator: plugin code generation complete\n--" | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generated files are not really headers (except
generated_api_hpp_include_code.hpp
maybe), therefore they shouldn't be put into theinclude
directory. Also don't use.hpp
for them, perhaps.hxx
or.cxx
depending on content.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will change that for more clearance.
But generally would you say this is way of solving this issue? I think it looks kinda weird to do includes in the middle of the code. But I heard in some projects this is common. Still wouldn't call it good practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I wouldn't call it beautiful. :-)
But if it gets the job done it's ok I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's okay for me. :)