37 #include <RobotAPI/interface/aron.h>
51 int main(
int argc,
char* argv[])
55 cxxopts::Options options(
"AronArmemCodeGenerator",
"An application interface for the aron and armem code generation");
57 std::string input_default =
"/path/to/some/xml/file.xml";
58 std::string output_default =
"/path/to/some/output/folder/";
61 (
"v,verbose",
"Enable verbose mode", cxxopts::value<bool>()->default_value(
"false"))
62 (
"h,help",
"Print usage");
65 (
"i,input",
"XML file name", cxxopts::value<std::string>()->default_value(input_default))
66 (
"o,output",
"The output path", cxxopts::value<std::string>()->default_value(output_default))
67 (
"I,include",
"include path",
cxxopts::value<std::vector<std::string>>());
70 (
"f,force",
"Enforce the generation", cxxopts::value<bool>()->default_value(
"false"));
72 auto result = options.
parse(argc, argv);
74 if (result.count(
"help"))
76 std::cout << options.
help() << std::endl;
80 bool verbose = result[
"v"].as<
bool>();
81 bool force = result[
"f"].as<
bool>();
83 std::string
filename = result[
"i"].as<std::string>();
84 std::string output = result[
"o"].as<std::string>();
86 if (
filename == input_default || output == output_default)
88 std::cout << options.
help() << std::endl;
94 std::cout <<
"Welcome to the AronCodeGenerator!" << std::endl;
95 std::cout <<
"Received the following parameters:" << std::endl;
96 std::cout <<
"- IO:" << std::endl;
97 std::cout <<
"-- Input: " <<
filename << std::endl;
98 std::cout <<
"-- Output: " << output << std::endl;
99 std::cout <<
"- Generation:" << std::endl;
100 std::cout <<
"-- Force: " << force << std::endl;
101 std::cout << std::endl;
106 std::cout <<
"Generating a new cpp file out of <" +
filename +
">" << std::endl;
109 std::filesystem::path input_file(
filename);
110 std::filesystem::path output_folder(output);
112 std::vector<std::filesystem::path> includePaths;
114 if(result.count(
"I") > 0)
116 for(
const auto& path: result[
"I"].as<std::vector<std::string>>())
118 includePaths.emplace_back(path);
123 if (!std::filesystem::exists(input_file) || !std::filesystem::is_regular_file(input_file))
125 std::cerr <<
"The input file does not exist or is not a regular file." << std::endl;
129 if (!std::filesystem::exists(output_folder) || !std::filesystem::is_directory(output_folder))
131 std::cerr <<
"The output folder does not exist or is not an directory." << std::endl;
135 if (input_file.extension().string() !=
".xml")
137 std::cerr <<
"The file you passed has the wrong type." << std::endl;
143 std::cout <<
"Parsing the XML file..." << std::endl;
147 reader.
parseFile(input_file, includePaths);
150 std::cout <<
"Parsing the XML file... done!" << std::endl;
151 std::cout <<
"--> Found " << reader.
getGenerateObjects().size() <<
" types." << std::endl;
152 std::cout <<
"--> They are: " << std::endl;
155 std::cout <<
"----> " << generateType.typeName << std::endl;
163 std::cout <<
"Running the type class generator..." << std::endl;
171 std::cout <<
"Running the type class generator... done!" << std::endl;
172 std::cout <<
"--> Found " << writer.
getTypeClasses().size() <<
" type objects." << std::endl;
173 std::cout <<
"--> They are: " << std::endl;
176 std::cout <<
"----> " <<
c->getName() << std::endl;
180 std::time_t current_time = std::time(0);
181 std::tm* now = std::localtime(¤t_time);
184 std::string fileDoc = std::string(
"* This file is part of ArmarX. \n") +
186 "* Copyright (C) 2012-" + current_year +
", High Performance Humanoid Technologies (H2T), \n" +
187 "* Karlsruhe Institute of Technology (KIT), all rights reserved. \n" +
189 "* ArmarX is free software; you can redistribute it and/or modify \n" +
190 "* it under the terms of the GNU General Public License version 2 as \n" +
191 "* published by the Free Software Foundation. \n" +
193 "* ArmarX is distributed in the hope that it will be useful, but \n" +
194 "* WITHOUT ANY WARRANTY; without even the implied warranty of \n" +
195 "* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the \n" +
196 "* GNU General Public License for more details. \n" +
198 "* You should have received a copy of the GNU General Public License \n" +
199 "* along with this program. If not, see <http://www.gnu.org/licenses/>. \n" +
201 "* @copyright http://www.gnu.org/licenses/gpl-2.0.txt \n" +
202 "* GNU General Public License \n" +
203 "* *********************************************************************** \n" +
204 "* WARNING: This file is autogenerated. \n" +
205 "* Original file: " +
filename +
" \n" +
206 "* Please do not edit since your changes may be overwritten on the next generation \n" +
207 "* If you have any questions please contact: Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu) \n" +
208 "* ***********************************************************************";
218 std::cout <<
"Now exporting enums..." << std::endl;
221 w->body.line(
"/*************************************************************************");
222 w->body.line(
" * ALL GENERATED ENUMS ***************************************************");
223 w->body.line(
" ************************************************************************/");
228 std::string enum_file_generation_content = w->getString();
232 std::cout <<
"Now exporting enums... done!" << std::endl;
235 if (enums.size() > 0 && enum_file_generation_content ==
"")
237 std::cerr <<
"\033[31m" <<
"Error code 11 - Found error in code generation. Aborting!" <<
"\033[0m" << std::endl;
245 std::cout <<
"Now exporting classes..." << std::endl;
248 w->body.line(
"/* ************************************************************************");
249 w->body.line(
" * ALL GENERATED CLASSES *************************************************");
250 w->body.line(
" * ***********************************************************************/");
255 std::string class_file_generation_content = simox::alg::remove_prefix(w->getString(), enum_file_generation_content);
259 std::cout <<
"Now exporting classes... done!" << std::endl;
262 if (classes.size() > 0 && class_file_generation_content ==
"")
264 std::cerr <<
"\033[31m" <<
"Error code 21 - Found error in code generation. Aborting!" <<
"\033[0m" << std::endl;
268 std::string new_file_full_content = w->getString();
270 std::string new_name_with_extension = input_file.filename().replace_extension(
"").string();
271 new_name_with_extension +=
".aron.generated.h";
272 std::string output_file_path = output +
"/" + new_name_with_extension;
273 std::filesystem::path output_file(output_file_path);
278 if (std::filesystem::exists(output_file))
280 std::ifstream ifs(output_file);
281 std::string file_content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
283 if (file_content == new_file_full_content)
287 std::cout <<
"\033[31m" <<
"Error code 31 - File content not changed for <" + output_file.string() +
">" <<
"\033[0m" << std::endl;
295 ofs.open(output_file);
296 ofs << new_file_full_content;
301 std::cout <<
"\033[32m" <<
"Finished generating <" + output_file.string() +
">. The new file ist called <" << output_file.string() <<
">" <<
"\033[0m" << std::endl;
306 std::cerr <<
"\033[31m" <<
"Error code 01 - Error in parsing cxxopts options: " << e.
what() <<
"\033[0m" << std::endl;