Skip to content

Commit e180325

Browse files
Apply user template to included IDL files (#472)
* Refs #23150: Apply user template to included IDL files Signed-off-by: cferreiragonz <[email protected]> * Refs #23150: Apply Review Signed-off-by: cferreiragonz <[email protected]> --------- Signed-off-by: cferreiragonz <[email protected]>
1 parent 7d9011d commit e180325

File tree

1 file changed

+81
-25
lines changed

1 file changed

+81
-25
lines changed

src/main/java/com/eprosima/fastdds/fastddsgen.java

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ private boolean isIncludePathDuplicated(String pathToCheck) {
589589
}
590590
return false;
591591
}
592-
592+
593593
/*
594594
* ----------------------------------------------------------------------------------------
595595
* Arguments
@@ -875,14 +875,19 @@ private Project parseIDL(
875875
for (Map.Entry<String, String> entry : m_customStgOutput.entrySet())
876876
{
877877
System.out.println("Loading custom template " + entry.getKey() + "...");
878-
Path path = Paths.get(entry.getKey());
879-
String templateName = path.getFileName().toString().substring(0, path.getFileName().toString().lastIndexOf('.'));
880-
try {
881-
String content = new String(Files.readAllBytes(path));
882-
tmanager.addGroupFromString(templateName, content);
883-
} catch(IOException e){
884-
System.out.println(ColorMessage.error(
885-
"IOException") + "Cannot read content from " + path.toString());
878+
loadAndAddTemplate(entry.getKey(), tmanager);
879+
}
880+
}
881+
else
882+
{
883+
// Check if there is a '@' in the output_file_name
884+
for (Map.Entry<String, String> entry : m_customStgOutput.entrySet())
885+
{
886+
if (entry.getValue().contains("@"))
887+
{
888+
System.out.println("Loading custom template " +
889+
entry.getKey() + " for included IDL file " + idlFilename + "...");
890+
loadAndAddTemplate(entry.getKey(), tmanager);
886891
}
887892
}
888893
}
@@ -925,25 +930,27 @@ private Project parseIDL(
925930
{
926931
for (Map.Entry<String, String> entry : m_customStgOutput.entrySet())
927932
{
928-
Path path = Paths.get(entry.getKey());
929-
String templateName = path.getFileName().toString().substring(0, path.getFileName().toString().lastIndexOf('.'));
930-
System.out.println("Generating from custom " + templateName + " to " + entry.getValue());
931-
932-
if (returnedValue = Utils.writeFile(output_dir + entry.getValue(), maintemplates.getTemplate(templateName), m_replace))
933+
if (! (returnedValue = createOutputCustomTemplate(
934+
entry, idlFilename, output_dir, relative_dir, ctx.getFilename(),
935+
maintemplates, m_replace, project)))
933936
{
934-
// Try to determine if the file is a header file.
935-
if (entry.getValue().contains(".hpp") || entry.getValue().contains(".h"))
936-
{
937-
project.addCommonIncludeFile(relative_dir + entry.getValue());
938-
}
939-
else
940-
{
941-
project.addCommonSrcFile(relative_dir + ctx.getFilename() + entry.getValue());
942-
}
937+
break;
943938
}
944-
else
939+
}
940+
}
941+
else
942+
{
943+
// Check if there is a '$' in the output_file_name
944+
for (Map.Entry<String, String> entry : m_customStgOutput.entrySet())
945+
{
946+
if (entry.getValue().contains("@"))
945947
{
946-
break;
948+
if (! (returnedValue = createOutputCustomTemplate(
949+
entry, idlFilename, output_dir, relative_dir, ctx.getFilename(),
950+
maintemplates, m_replace, project)))
951+
{
952+
break;
953+
}
947954
}
948955
}
949956
}
@@ -1226,6 +1233,55 @@ private Project parseIDL(
12261233
return returnedValue ? project : null;
12271234
}
12281235

1236+
private void loadAndAddTemplate(
1237+
String templatePath,
1238+
TemplateManager tmanager)
1239+
{
1240+
try
1241+
{
1242+
Path path = Paths.get(templatePath);
1243+
String templateName = path.getFileName().toString();
1244+
templateName = templateName.substring(0, templateName.lastIndexOf('.'));
1245+
String content = new String(Files.readAllBytes(path));
1246+
tmanager.addGroupFromString(templateName, content);
1247+
}
1248+
catch (IOException e)
1249+
{
1250+
System.out.println(ColorMessage.error("IOException") + "Cannot read content from " + templatePath);
1251+
}
1252+
}
1253+
1254+
private boolean createOutputCustomTemplate(
1255+
Map.Entry<String, String> entry,
1256+
String idlFilename,
1257+
String outputDir,
1258+
String relativeDir,
1259+
String contextFilename,
1260+
TemplateGroup maintemplates,
1261+
boolean replace,
1262+
Project project)
1263+
{
1264+
Path path = Paths.get(entry.getKey());
1265+
String templateName = path.getFileName().toString();
1266+
templateName = templateName.substring(0, templateName.lastIndexOf('.'));
1267+
String outputName = entry.getValue().replace("@", idlFilename.substring(0, idlFilename.lastIndexOf('.')));
1268+
System.out.println("Generating from custom " + templateName + " to " + outputName);
1269+
1270+
boolean ret_val = Utils.writeFile(outputDir + outputName, maintemplates.getTemplate(templateName), replace);
1271+
if (ret_val)
1272+
{
1273+
if (outputName.contains(".hpp") || outputName.contains(".h"))
1274+
{
1275+
project.addCommonIncludeFile(relativeDir + outputName);
1276+
}
1277+
else
1278+
{
1279+
project.addCommonSrcFile(relativeDir + contextFilename + outputName);
1280+
}
1281+
}
1282+
return ret_val;
1283+
}
1284+
12291285
private boolean genSolution(
12301286
Solution solution)
12311287
{

0 commit comments

Comments
 (0)