23 #include "plugin_generator.h"
25 #include <core/exception.h>
27 #include <utils/misc/string_conversions.h>
50 const std::string &author,
51 const std::string &year,
52 const std::string &creation_date,
53 const std::string &plugin_name,
54 const std::string &description)
58 _creation_date(creation_date),
59 _plugin_name(plugin_name),
60 _description(description)
62 if (_dir.find_last_of(
"/") != (_dir.length() - 1)) {
66 _filename_thread_cpp = plugin_name +
"_thread.cpp";
67 _filename_thread_h = plugin_name +
"_thread.h";
68 _filename_plugin_cpp = plugin_name +
"_plugin.cpp";
69 _filename_makefile =
"Makefile";
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"
112 _plugin_name.c_str(),
113 (_creation_date.length() > 0) ?
" * Created: " :
"",
114 (_creation_date.length() > 0) ? _creation_date.c_str() :
"",
126 "#*****************************************************************************\n"
127 "# Makefile Build System for Fawkes: %s Plugin\n"
128 "# -------------------\n"
130 "# Copyright (C) %s by %s\n"
132 "#*****************************************************************************\n"
134 "# This program is free software; you can redistribute it and/or modify\n"
135 "# it under the terms of the GNU General Public License as published by\n"
136 "# the Free Software Foundation; either version 2 of the License, or\n"
137 "# (at your option) any later version.\n"
139 "#*****************************************************************************\n\n",
140 _plugin_name.c_str(),
141 _creation_date.c_str(),
152 fprintf(f,
"#ifndef %s\n", _deflector.c_str());
153 fprintf(f,
"#define %s\n\n", _deflector.c_str());
164 "#include \"%s\"\n\n"
165 "using namespace fawkes;\n\n"
166 "/** @class %s '%s' \n"
170 _filename_thread_h.c_str(),
171 _class_name_thread.c_str(),
172 _filename_thread_h.c_str(),
173 _description.c_str(),
177 "/** Constructor. */\n"
179 " : Thread(\"%s\", Thread::OPMODE_WAITFORWAKEUP),\n"
180 " BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_ACT) \n{\n}\n\n",
182 _class_name_thread.c_str(),
183 _class_name_thread.c_str(),
184 _class_name_thread.c_str());
186 fprintf(f,
"void\n%s::init()\n{\n}\n\n", _class_name_thread.c_str());
188 fprintf(f,
"void\n%s::loop()\n{\n}\n\n", _class_name_thread.c_str());
190 fprintf(f,
"void\n%s::finalize()\n{\n}\n\n", _class_name_thread.c_str());
203 "#include <core/threading/thread.h>\n"
204 "#include <aspect/blocked_timing.h>\n"
205 "#include <aspect/logging.h>\n"
206 "#include <aspect/blackboard.h>\n"
207 "#include <aspect/configurable.h>\n\n"
209 "namespace fawkes {\n"
210 " // add forward declarations here, e.g., interfaces\n"
213 ": public fawkes::Thread,\n"
214 " public fawkes::BlockedTimingAspect,\n"
215 " public fawkes::LoggingAspect,\n"
216 " public fawkes::ConfigurableAspect,\n"
217 " public fawkes::BlackBoardAspect\n"
221 " virtual void init();\n"
222 " virtual void finalize();\n"
223 " virtual void loop();\n\n"
224 " /** Stub to see name in backtrace for easier debugging. @see Thread::run() */\n"
225 " protected: virtual void run() { Thread::run(); }\n\n"
227 " //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");
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(),
252 _filename_plugin_cpp.c_str(),
253 _description.c_str(),
256 "class %s : public fawkes::Plugin\n"
259 " /** Constructor.\n"
260 " * @param config Fakwes configuration\n"
262 " %s(Configuration *config)\n"
263 " : Plugin(config)\n"
265 " thread_list.push_back(new %s());\n"
268 _class_name_plugin.c_str(),
269 _class_name_plugin.c_str(),
270 _class_name_thread.c_str());
272 "PLUGIN_DESCRIPTION(\"%s\")\n"
274 _description.c_str(),
275 _class_name_plugin.c_str());
285 std::string filename_plugin_o = _plugin_name +
"_plugin.o";
286 std::string filename_thread_o = _plugin_name +
"_thread.o";
288 "BASEDIR = ../../..\n"
289 "include $(BASEDIR)/etc/buildsys/config.mk\n\n"
290 "LIBS_%s = m fawkescore fawkesutils fawkesaspects fawkesbaseapp \\\n"
291 " fawkesblackboard fawkesinterface\n\n"
292 "OBJS_%s = %s %s\n\n",
293 _plugin_name_underscore.c_str(),
294 _plugin_name_underscore.c_str(),
295 filename_plugin_o.c_str(),
296 filename_thread_o.c_str());
298 "PLUGINS_all = $(PLUGINDIR)/%s.$(SOEXT)\n\n"
299 "OBJS_all = $(OBJS_%s)\n\n"
300 "include $(BUILDSYSDIR)/base.mk",
301 _plugin_name.c_str(),
302 _plugin_name.c_str());
313 for (std::string::size_type i = 0; (i = source.find(
"-", i)) != std::string::npos;) {
314 source.replace(i, 1,
"_");
329 std::string class_name;
331 std::size_t underline_position = plugin_name.find(
'_');
332 if (underline_position != std::string::npos) {
334 std::istringstream stream(plugin_name);
336 std::vector<std::string> splitted;
337 while (std::getline(stream, item,
'_')) {
338 splitted.push_back(item);
341 for (
auto element : splitted) {
342 element[0] = std::toupper(element[0]);
343 class_name.append(element);
345 class_name.append(appendix);
348 plugin_name[0] = std::toupper(plugin_name[0]);
349 class_name.append(plugin_name);
350 class_name.append(appendix);
367 if (!(stat(_dir.c_str(), &info) == 0 && S_ISDIR(info.st_mode))) {
368 if (mkdir(_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1) {
369 throw fawkes::Exception(errno,
"Failed to generate plugin, cannot create directory");
372 thread_h = fopen(
string(_dir + _filename_thread_h).c_str(),
"w");
373 thread_cpp = fopen(
string(_dir + _filename_thread_cpp).c_str(),
"w");
374 plugin_cpp = fopen(
string(_dir + _filename_plugin_cpp).c_str(),
"w");
375 makefile = fopen(
string(_dir + _filename_makefile).c_str(),
"w");
377 if (thread_h == NULL) {
378 printf(
"Cannot open thread_h file %s%s\n", _dir.c_str(), _filename_thread_h.c_str());
380 if (thread_cpp == NULL) {
381 printf(
"Cannot open thread_cpp file %s%s\n", _dir.c_str(), _filename_thread_cpp.c_str());
383 if (plugin_cpp == NULL) {
384 printf(
"Cannot open plugin_cpp file %s%s\n", _dir.c_str(), _filename_plugin_cpp.c_str());
386 if (makefile == NULL) {
387 printf(
"Cannot open makefile %s%s\n", _dir.c_str(), _filename_makefile.c_str());
400 printf(
"Plugin %s successfully created!\n", _plugin_name.c_str());
void write_header(FILE *f, std::string filename)
Write header to file.
void write_makefile(FILE *f)
Write Makefile.
void write_thread_cpp(FILE *f)
Write cpp file.
~PluginGenerator()
Destructor.
void write_makefile_header(FILE *f)
Write makefile header.
std::string replace_dash_w_undescore(std::string source)
Replace dash with underscore.
void generate()
Generator cpp and h files.
std::string format_class_name(std::string plugin_name, std::string append)
Format a lowercase plugin name to CamelCase class.
PluginGenerator(const std::string &directory, const std::string &author, const std::string &year, const std::string &creation_date, const std::string &plugin_name, const std::string &description)
Constructor.
void write_deflector(FILE *f)
Write header deflector.
void write_plugin_cpp(FILE *f)
Write plugin cpp file.
void write_thread_h(FILE *f)
Write h file.
Base class for exceptions in Fawkes.
static std::string to_upper(std::string str)
Convert string to all-uppercase string.