23 #include "plugin_generator.h" 25 #include <utils/misc/string_conversions.h> 26 #include <core/exception.h> 52 std::string year, std::string creation_date,
53 std::string plugin_name, std::string description
57 if ( _dir.find_last_of(
"/") != (_dir.length() - 1) ) {
62 _creation_date = creation_date;
63 _description = description;
65 _filename_thread_cpp = plugin_name +
"_thread.cpp";
66 _filename_thread_h = plugin_name +
"_thread.h";
67 _filename_plugin_cpp = plugin_name +
"_plugin.cpp";
68 _filename_makefile =
"Makefile";
70 _plugin_name = plugin_name;
71 _plugin_name_underscore = replace_dash_w_undescore(_plugin_name);
73 _class_name_thread = format_class_name(_plugin_name_underscore,
"Thread");
74 _class_name_plugin = format_class_name(_plugin_name_underscore,
"Plugin");
93 "\n/***************************************************************************\n" 97 " * Copyright %s %s\n" 98 " ****************************************************************************/\n\n" 99 "/* This program is free software; you can redistribute it and/or modify\n" 100 " * it under the terms of the GNU General Public License as published by\n" 101 " * the Free Software Foundation; either version 2 of the License, or\n" 102 " * (at your option) any later version.\n" 104 " * This program is distributed in the hope that it will be useful,\n" 105 " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" 106 " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" 107 " * GNU Library General Public License for more details.\n" 109 " * Read the full text in the LICENSE.GPL file in the doc directory.\n" 111 filename.c_str(), _plugin_name.c_str(),
112 (_creation_date.length() > 0 ) ?
" * Created: " :
"",
113 (_creation_date.length() > 0 ) ? _creation_date.c_str() :
"",
114 _year.c_str(), _author.c_str()
125 "#*****************************************************************************\n" 126 "# Makefile Build System for Fawkes: %s Plugin\n" 127 "# -------------------\n" 129 "# Copyright (C) %s by %s\n" 131 "#*****************************************************************************\n" 133 "# This program is free software; you can redistribute it and/or modify\n" 134 "# it under the terms of the GNU General Public License as published by\n" 135 "# the Free Software Foundation; either version 2 of the License, or\n" 136 "# (at your option) any later version.\n" 138 "#*****************************************************************************\n\n",
139 _plugin_name.c_str(), _creation_date.c_str(), _year.c_str(),
150 fprintf(f,
"#ifndef %s\n", _deflector.c_str());
151 fprintf(f,
"#define %s\n\n", _deflector.c_str());
161 write_header(f, _filename_thread_cpp);
163 "#include \"%s\"\n\n" 164 "using namespace fawkes;\n\n" 165 "/** @class %s '%s' \n" 169 _filename_thread_h.c_str(),
170 _class_name_thread.c_str(), _filename_thread_h.c_str(), _description.c_str(),
174 "/** Constructor. */\n" 176 " : Thread(\"%s\", Thread::OPMODE_WAITFORWAKEUP),\n" 177 " BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_ACT) \n{\n}\n\n",
179 _class_name_thread.c_str(), _class_name_thread.c_str(),
180 _class_name_thread.c_str());
183 "void\n%s::init()\n{\n}\n\n", _class_name_thread.c_str());
186 "void\n%s::loop()\n{\n}\n\n", _class_name_thread.c_str());
189 "void\n%s::finalize()\n{\n}\n\n", _class_name_thread.c_str());
198 write_header(f, _filename_thread_h);
202 "#include <core/threading/thread.h>\n" 203 "#include <aspect/blocked_timing.h>\n" 204 "#include <aspect/logging.h>\n" 205 "#include <aspect/blackboard.h>\n" 206 "#include <aspect/configurable.h>\n\n" 208 "namespace fawkes {\n" 209 " // add forward declarations here, e.g., interfaces\n" 212 ": public fawkes::Thread,\n" 213 " public fawkes::BlockedTimingAspect,\n" 214 " public fawkes::LoggingAspect,\n" 215 " public fawkes::ConfigurableAspect,\n" 216 " public fawkes::BlackBoardAspect\n" 220 " virtual void init();\n" 221 " virtual void finalize();\n" 222 " virtual void loop();\n\n" 223 " /** Stub to see name in backtrace for easier debugging. @see Thread::run() */\n" 224 " protected: virtual void run() { Thread::run(); }\n\n" 226 " //Define class member variables here\n" 228 _class_name_thread.c_str(),
229 _class_name_thread.c_str());
231 fprintf(f,
"\n};\n\n\n#endif");
240 write_header(f, _filename_plugin_cpp);
242 "#include <core/plugin.h>\n\n" 243 "#include \"%s\"\n\n" 244 "using namespace fawkes;\n\n",
245 _filename_thread_h.c_str());
247 "/** @class %s \"%s\"\n" 251 _class_name_plugin.c_str(), _filename_plugin_cpp.c_str(),
252 _description.c_str(), _author.c_str());
254 "class %s : public fawkes::Plugin\n" 257 " /** Constructor.\n" 258 " * @param config Fakwes configuration\n" 260 " %s(Configuration *config)\n" 261 " : Plugin(config)\n" 263 " thread_list.push_back(new %s());\n" 266 _class_name_plugin.c_str(), _class_name_plugin.c_str(),
267 _class_name_thread.c_str());
269 "PLUGIN_DESCRIPTION(\"%s\")\n" 271 _description.c_str(), _class_name_plugin.c_str());
280 write_makefile_header(f);
281 std::string filename_plugin_o = _plugin_name +
"_plugin.o";
282 std::string filename_thread_o = _plugin_name +
"_thread.o";
284 "BASEDIR = ../../..\n" 285 "include $(BASEDIR)/etc/buildsys/config.mk\n\n" 286 "LIBS_%s = m fawkescore fawkesutils fawkesaspects fawkesbaseapp \\\n" 287 " fawkesblackboard fawkesinterface\n\n" 288 "OBJS_%s = %s %s\n\n",
289 _plugin_name_underscore.c_str(), _plugin_name_underscore.c_str(), filename_plugin_o.c_str(),
290 filename_thread_o.c_str()
293 "PLUGINS_all = $(PLUGINDIR)/%s.$(SOEXT)\n\n" 294 "OBJS_all = $(OBJS_%s)\n\n" 295 "include $(BUILDSYSDIR)/base.mk",
296 _plugin_name.c_str(), _plugin_name.c_str());
307 for(std::string::size_type i = 0; (i = source.find(
"-", i)) != std::string::npos;)
309 source.replace(i, 1,
"_");
324 std::string class_name;
326 std::size_t underline_position = plugin_name.find(
'_');
327 if (underline_position!=std::string::npos){
329 std::istringstream stream(plugin_name);
331 std::vector<std::string> splitted;
332 while (std::getline(stream, item,
'_')) {
333 splitted.push_back(item);
336 for (
auto element:splitted){
337 element[0] = std::toupper(element[0]);
338 class_name.append(element);
340 class_name.append(appendix);
343 plugin_name[0] = std::toupper(plugin_name[0]);
344 class_name.append(plugin_name);
345 class_name.append(appendix);
363 if (!(stat(_dir.c_str(), &info) == 0 && S_ISDIR(info.st_mode))) {
364 if (mkdir(_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1) {
365 throw fawkes::Exception(errno,
"Failed to generate plugin, cannot create directory");
368 thread_h = fopen(
string(_dir + _filename_thread_h).c_str(),
"w");
369 thread_cpp = fopen(
string(_dir + _filename_thread_cpp).c_str(),
"w");
370 plugin_cpp = fopen(
string(_dir + _filename_plugin_cpp).c_str(),
"w");
371 makefile = fopen(
string(_dir + _filename_makefile).c_str(),
"w");
373 if ( thread_h == NULL ) {
374 printf(
"Cannot open thread_h file %s%s\n", _dir.c_str(), _filename_thread_h.c_str());
376 if ( thread_cpp == NULL ) {
377 printf(
"Cannot open thread_cpp file %s%s\n", _dir.c_str(), _filename_thread_cpp.c_str());
379 if ( plugin_cpp == NULL ) {
380 printf(
"Cannot open plugin_cpp file %s%s\n", _dir.c_str(), _filename_plugin_cpp.c_str());
382 if ( makefile == NULL ) {
383 printf(
"Cannot open makefile %s%s\n", _dir.c_str(), _filename_makefile.c_str());
386 write_thread_cpp(thread_cpp);
387 write_thread_h(thread_h);
388 write_plugin_cpp(plugin_cpp);
389 write_makefile(makefile);
396 printf(
"Plugin %s successfully created!\n", _plugin_name.c_str());
std::string format_class_name(std::string plugin_name, std::string append)
Format a lowercase plugin name to CamelCase class.
static std::string to_upper(std::string str)
Convert string to all-uppercase string.
void write_thread_h(FILE *f)
Write h file.
void write_thread_cpp(FILE *f)
Write cpp file.
void write_plugin_cpp(FILE *f)
Write plugin cpp file.
std::string replace_dash_w_undescore(std::string source)
Replace dash with underscore.
void write_makefile_header(FILE *f)
Write makefile header.
void generate()
Generator cpp and h files.
Base class for exceptions in Fawkes.
PluginGenerator(std::string directory, std::string author, std::string year, std::string creation_date, std::string plugin_name, std::string description)
Constructor.
void write_header(FILE *f, std::string filename)
Write header to file.
~PluginGenerator()
Destructor.
void write_makefile(FILE *f)
Write Makefile.
void write_deflector(FILE *f)
Write header deflector.