Skip to content
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

Create plugin script #1302

Merged
merged 7 commits into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions libraries/plugins/make_new_plugin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

# Find next available space id by checking all current plugins
next_space_id () {
SPACE_IDS=()
for i in * ; do
if [ -d "$i" ] && [ "$i" != "CMakeFiles" ]; then
cd "$i/include/graphene/$i"
result=$(grep -rnw '.' -e '#define[[:space:]]*[[:alnum:]]*_SPACE_ID')
B=$(echo $result | cut -d ' ' -f 3)
if [[ $B =~ [[:digit:]] ]]; then
SPACE_IDS+=($B)
fi
cd "../../../.."
fi
done
max=$( printf "%d\n" "${SPACE_IDS[@]}" | sort -n | tail -1 )
next=$(($max + 1))
return $next
}

## create new plugin
if [ $# -ne 1 ]; then
echo "Usage: $0 my_new_plugin"
echo "... where my_new_plugin is the name of the plugin you want to create"
exit 1
fi

pluginName="$1"

echo "Copying template..."
cp -r template_plugin $pluginName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please quote $pluginName here and in the following lines.


echo "Renaming files/directories..."
mv $pluginName/include/graphene/template_plugin $pluginName/include/graphene/$pluginName
for file in `find $pluginName -type f -name '*template_plugin*'`; do mv $file `sed s/template_plugin/$pluginName/g <<< $file`; done;
echo "Renaming in files..."
find $pluginName -type f -exec sed -i "s/template_plugin/$pluginName/g" {} \;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please quote $pluginName

echo "Assigning next available SPACE_ID..."
next_space_id
find $pluginName -type f -exec sed -i "s/@SPACE_ID@/$?/g" {} \;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please quote $pluginName


echo "Done! $pluginName is ready."
echo "Next steps:"
echo "1- Add 'add_subdirectory( $pluginName )' to CmakeLists.txt in this directory."
echo "2- Add 'graphene_$pluginName' to ../../programs/witness_node/CMakeLists.txt with the other plugins."
echo "3- Include plugin header file '#include <graphene/$pluginName/$pluginName.hpp>' to ../../programs/witness_node/main.cpp."
echo "4- Initialize plugin with the others with 'auto ${pluginName}_plug = node->register_plugin<$pluginName::$pluginName>();' in ../../programs/witness_node/main.cpp"
echo "5- cmake and make"
echo "6- Start plugin with './../programs/witness_node/witness_node --plugins \"$pluginName\"'. After the seed nodes are added you start to see see a msgs from the plugin 'onBlock' "
22 changes: 22 additions & 0 deletions libraries/plugins/template_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
file(GLOB HEADERS "include/graphene/template_plugin/*.hpp")

add_library( graphene_template_plugin
template_plugin.cpp
)

target_link_libraries( graphene_template_plugin graphene_chain graphene_app )
target_include_directories( graphene_template_plugin
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" )

if(MSVC)
set_source_files_properties(template_plugin.cpp PROPERTIES COMPILE_FLAGS "/bigobj" )
endif(MSVC)

install( TARGETS
graphene_template_plugin

RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
INSTALL( FILES ${HEADERS} DESTINATION "include/graphene/template_plugin" )
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2018 template_plugin and contributors.
*
* 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.
*/
#pragma once

#include <graphene/app/plugin.hpp>
#include <graphene/chain/database.hpp>

namespace graphene { namespace template_plugin {
using namespace chain;

//
// Plugins should #define their SPACE_ID's so plugins with
// conflicting SPACE_ID assignments can be compiled into the
// same binary (by simply re-assigning some of the conflicting #defined
// SPACE_ID's in a build script).
//
// Assignment of SPACE_ID's cannot be done at run-time because
// various template automagic depends on them being known at compile
// time.
//
#ifndef template_plugin_SPACE_ID
#define template_plugin_SPACE_ID @SPACE_ID@
#endif


namespace detail
{
class template_plugin_impl;
}

class template_plugin : public graphene::app::plugin
{
public:
template_plugin();
virtual ~template_plugin();

std::string plugin_name()const override;
std::string plugin_description()const override;
virtual void plugin_set_program_options(
boost::program_options::options_description& cli,
boost::program_options::options_description& cfg) override;
virtual void plugin_initialize(const boost::program_options::variables_map& options) override;
virtual void plugin_startup() override;

friend class detail::template_plugin_impl;
std::unique_ptr<detail::template_plugin_impl> my;
};

} } //graphene::template
112 changes: 112 additions & 0 deletions libraries/plugins/template_plugin/template_plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2018 template_plugin and contributors.
*
* 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.
*/

#include <graphene/template_plugin/template_plugin.hpp>

namespace graphene { namespace template_plugin {

namespace detail
{

class template_plugin_impl
{
public:
template_plugin_impl(template_plugin& _plugin)
: _self( _plugin )
{ }
virtual ~template_plugin_impl();

void onBlock( const signed_block& b );

graphene::chain::database& database()
{
return _self.database();
}

template_plugin& _self;

std::string _plugin_option = "";

private:

};

void template_plugin_impl::onBlock( const signed_block& b )
{
wdump((b.block_num()));
}

template_plugin_impl::~template_plugin_impl()
{
return;
}

} // end namespace detail

template_plugin::template_plugin() :
my( new detail::template_plugin_impl(*this) )
{
}

template_plugin::~template_plugin()
{
}

std::string template_plugin::plugin_name()const
{
return "template_plugin";
}
std::string template_plugin::plugin_description()const
{
return "template_plugin description";
}

void template_plugin::plugin_set_program_options(
boost::program_options::options_description& cli,
boost::program_options::options_description& cfg
)
{
cli.add_options()
("template_plugin_option", boost::program_options::value<std::string>(), "template_plugin option")
;
cfg.add(cli);
}

void template_plugin::plugin_initialize(const boost::program_options::variables_map& options)
{
database().applied_block.connect( [&]( const signed_block& b) {
my->onBlock(b);
} );

if (options.count("template_plugin")) {
my->_plugin_option = options["template_plugin"].as<std::string>();
}
}

void template_plugin::plugin_startup()
{
ilog("template_plugin: plugin_startup() begin");
}

} }