Commit f28e220f authored by Alexander David Hellwig's avatar Alexander David Hellwig
Browse files

Moved to ftl templates

parent 436b1694
Pipeline #116343 passed with stages
in 1 minute and 46 seconds
......@@ -3,7 +3,9 @@ package de.monticore.lang.monticar.generator.rosmsg;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAPortInstanceSymbol;
import de.monticore.lang.monticar.common2._ast.ASTCommonMatrixType;
import de.monticore.lang.monticar.generator.rosmsg.util.CMakeListsViewModel;
import de.monticore.lang.monticar.generator.rosmsg.util.FileContent;
import de.monticore.lang.monticar.generator.rosmsg.util.RosMsgTemplates;
import de.monticore.lang.monticar.struct._symboltable.StructFieldDefinitionSymbol;
import de.monticore.lang.monticar.struct._symboltable.StructSymbol;
import de.monticore.lang.monticar.ts.MCASTTypeSymbol;
......@@ -16,7 +18,6 @@ import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
......@@ -81,7 +82,7 @@ public class GeneratorRosMsg {
String definition = structSymbol.getStructFieldDefinitions().stream()
.filter(sfds -> sfds.getType().existsReferencedSymbol())
.map(sfds -> getInMsgRosType(currentPackageName, sfds.getType().getReferencedSymbol(), ros2mode) + " " + getFieldName(sfds,ros2mode))
.map(sfds -> getInMsgRosType(currentPackageName, sfds.getType().getReferencedSymbol(), ros2mode) + " " + getFieldName(sfds, ros2mode))
.collect(Collectors.joining("\n"));
FileContent fc = new FileContent();
......@@ -269,80 +270,15 @@ public class GeneratorRosMsg {
}
private FileContent getRos2CMakeLists(ArrayList<FileContent> res) {
FileContent cmakeLists = new FileContent();
cmakeLists.setFileName("CMakeLists.txt");
StringBuilder content = new StringBuilder();
content.append("cmake_minimum_required(VERSION 3.5)\n");
content.append("project(struct_msgs)\n");
content.append("find_package(ament_cmake REQUIRED)\n");
content.append("find_package(rosidl_default_generators REQUIRED)\n");
content.append("# declare the message files to generate code for\n");
for (FileContent fc : res) {
content.append("LIST(APPEND msg_files \"")
.append(fc.getFileName())
.append("\")\n");
}
content.append("rosidl_generate_interfaces(${PROJECT_NAME} ${msg_files})\n");
content.append("ament_export_dependencies(rosidl_default_runtime)\n");
content.append("ament_package()\n");
content.append("set(struct_msgs_LIBRARIES struct_msgs__rosidl_typesupport_cpp struct_msgs__rosidl_typesupport_fastrtps_cpp struct_msgs__rosidl_typesupport_introspection_cpp PARENT_SCOPE)\n");
content.append("export(TARGETS struct_msgs__rosidl_typesupport_cpp struct_msgs__rosidl_typesupport_fastrtps_cpp struct_msgs__rosidl_typesupport_introspection_cpp FILE struct_msgs.cmake)\n");
cmakeLists.setFileContent(content.toString());
return cmakeLists;
return new FileContent("CMakeLists.txt", RosMsgTemplates.generateRos2CMakeLists(new CMakeListsViewModel(res)));
}
private FileContent getRos2PackageXml(){
String res = "<?xml version=\"1.0\"?>\n" +
"<?xml-model href=\"http://download.ros.org/schema/package_format2.xsd\" schematypens=\"http://www.w3.org/2001/XMLSchema\"?>\n" +
"<package format=\"3\">\n" +
" <name>struct_msgs</name>\n" +
" <version>0.0.0</version>\n" +
" <description>Generated Messages from Struct</description>\n" +
" <maintainer email=\"unknown@unknown.com\">Unknown</maintainer>\n" +
" <license>Unknown</license>\n" +
"\n" +
" <buildtool_depend>ament_cmake</buildtool_depend>\n" +
" <buildtool_depend>rosidl_default_generators</buildtool_depend>\n" +
" <exec_depend>rosidl_default_runtime</exec_depend>\n" +
"\n" +
" <export>\n" +
" <build_type>ament_cmake</build_type>\n" +
" </export>\n" +
" <member_of_group>rosidl_interface_packages</member_of_group>\n" +
"</package>";
FileContent fileContent = new FileContent();
fileContent.setFileName("package.xml");
fileContent.setFileContent(res);
return fileContent;
private FileContent getRos2PackageXml() {
return new FileContent("package.xml", RosMsgTemplates.generateRos2Package());
}
private FileContent getRosCMakeLists(ArrayList<FileContent> res) {
FileContent cmakeLists = new FileContent();
cmakeLists.setFileName("CMakeLists.txt");
StringBuilder content = new StringBuilder();
content.append("cmake_minimum_required(VERSION 2.8.12)\n");
content.append("project(struct_msgs)\n");
content.append("find_package(genmsg REQUIRED)\n\n");
res.stream()
.map(FileContent::getFileName)
.map(fn -> Paths.get(fn).getFileName().toString())
.map(fn -> "add_message_files(FILES " + fn + ")\n")
.forEach(content::append);
content.append("generate_messages()\n");
content.append("set(struct_msgs_INCLUDE_DIRS ${struct_msgs_INCLUDE_DIRS} PARENT_SCOPE)\n");
cmakeLists.setFileContent(content.toString());
return cmakeLists;
return new FileContent("CMakeLists.txt", RosMsgTemplates.generateRosCMakeLists(new CMakeListsViewModel(res)));
}
public List<File> generateProject(List<MCTypeReference<? extends MCTypeSymbol>> typeReferences) throws IOException {
......
package de.monticore.lang.monticar.generator.rosmsg.util;
import java.nio.file.Paths;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class CMakeListsViewModel{
public List<String> getFileNamesWithPath() {
return fileContents.stream()
.map(FileContent::getFileName)
.collect(Collectors.toList());
}
public List<String> getFileNamesOnly() {
return fileContents.stream()
.map(FileContent::getFileName)
.map(fn -> Paths.get(fn).getFileName().toString())
.collect(Collectors.toList());
}
private List<FileContent> fileContents;
public CMakeListsViewModel(List<FileContent> fileContents) {
this.fileContents = fileContents;
}
}
......@@ -4,6 +4,14 @@ public class FileContent {
private String fileName;
private String fileContent;
public FileContent() {
}
public FileContent(String fileName, String fileContent) {
this.fileName = fileName;
this.fileContent = fileContent;
}
public String getFileName() {
return fileName;
}
......
package de.monticore.lang.monticar.generator.rosmsg.util;
import de.se_rwth.commons.logging.Log;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
public class RosMsgTemplates {
private RosMsgTemplates(){
}
private static final Template ROS2_CMAKELISTS;
private static final Template ROS2_PACKAGE_XML;
private static final Template ROS_CMAKELISTS;
static {
Configuration conf = new Configuration(Configuration.VERSION_2_3_23);
conf.setDefaultEncoding("UTF-8");
conf.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
conf.setLogTemplateExceptions(false);
conf.setClassForTemplateLoading(RosMsgTemplates.class, "/templates");
try {
ROS2_CMAKELISTS = conf.getTemplate("ros2.MsgGen.CMakeLists.ftl");
ROS_CMAKELISTS = conf.getTemplate("ros.MsgGen.CMakeLists.ftl");
ROS2_PACKAGE_XML = conf.getTemplate("ros2.MsgGen.Package.ftl");
} catch (IOException e) {
String msg = "could not load templates";
Log.error(msg, e);
throw new RuntimeException(msg, e);
}
}
public static String generateRos2CMakeLists(CMakeListsViewModel viewModel){
HashMap<String, Object> data = new HashMap<>();
data.put("viewModel", viewModel);
return generate(ROS2_CMAKELISTS, data);
}
public static String generateRosCMakeLists(CMakeListsViewModel viewModel){
HashMap<String, Object> data = new HashMap<>();
data.put("viewModel", viewModel);
return generate(ROS_CMAKELISTS, data);
}
public static String generateRos2Package(){
return generate(ROS2_PACKAGE_XML, new HashMap<>());
}
private static String generate(Template template, Object dataForTemplate) {
Log.errorIfNull(template);
Log.errorIfNull(dataForTemplate);
StringWriter sw = new StringWriter();
try {
template.process(dataForTemplate, sw);
} catch (TemplateException | IOException e) {
Log.error("template generation failed, template: " + template.getName(), e);
}
return sw.toString();
}
}
# generated by ros.MsgGen.CMakeLists.ftl
cmake_minimum_required(VERSION 2.8.12)
project(struct_msgs)
find_package(genmsg REQUIRED)
<#list viewModel.getFileNamesOnly() as fileOnly>
add_message_files(FILES ${fileOnly})
</#list>
<#noparse>
generate_messages()
set(struct_msgs_INCLUDE_DIRS ${struct_msgs_INCLUDE_DIRS} PARENT_SCOPE)
</#noparse>
\ No newline at end of file
# generated by ros2.MsgGen.CMakeLists.ftl
cmake_minimum_required(VERSION 3.5)
project(struct_msgs)
find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)
# declare the message files to generate code for
<#list viewModel.getFileNamesWithPath() as fileWithPath>
LIST(APPEND msg_files "${fileWithPath}")
</#list>
<#noparse>
rosidl_generate_interfaces(${PROJECT_NAME} ${msg_files})
ament_export_dependencies(rosidl_default_runtime)
ament_package()
set(struct_msgs_LIBRARIES struct_msgs__rosidl_typesupport_cpp struct_msgs__rosidl_typesupport_fastrtps_cpp struct_msgs__rosidl_typesupport_introspection_cpp PARENT_SCOPE)
export(TARGETS struct_msgs__rosidl_typesupport_cpp struct_msgs__rosidl_typesupport_fastrtps_cpp struct_msgs__rosidl_typesupport_introspection_cpp FILE struct_msgs.cmake)
</#noparse>
\ No newline at end of file
<!--Generated by ros2.MsgGen.Package.ftl-->
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>struct_msgs</name>
<version>0.0.0</version>
<description>Generated Messages from Struct</description>
<maintainer email="unknown@unknown.com">Unknown</maintainer>
<license>Unknown</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>rosidl_default_generators</buildtool_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
<member_of_group>rosidl_interface_packages</member_of_group>
</package>
\ No newline at end of file
......@@ -17,6 +17,7 @@ import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.symboltable.GlobalScope;
import de.monticore.symboltable.Scope;
import de.se_rwth.commons.logging.Log;
import org.apache.commons.io.FilenameUtils;
import org.junit.Assert;
import java.io.File;
......
# generated by ros.MsgGen.CMakeLists.ftl
cmake_minimum_required(VERSION 2.8.12)
project(struct_msgs)
find_package(genmsg REQUIRED)
......
# generated by ros2.MsgGen.CMakeLists.ftl
cmake_minimum_required(VERSION 3.5)
project(struct_msgs)
......
<!--Generated by ros2.MsgGen.Package.ftl-->
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment