From 8508a35e2e2582d0f5498bd90ff6a3c1fd4aafa7 Mon Sep 17 00:00:00 2001 From: cferreiragonz Date: Tue, 13 May 2025 10:41:52 +0200 Subject: [PATCH 1/2] Refs #23150: Apply user template to included IDL files Signed-off-by: cferreiragonz --- .../java/com/eprosima/fastdds/fastddsgen.java | 109 ++++++++++++++---- 1 file changed, 84 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/eprosima/fastdds/fastddsgen.java b/src/main/java/com/eprosima/fastdds/fastddsgen.java index eb1e8a63..5411c9fb 100644 --- a/src/main/java/com/eprosima/fastdds/fastddsgen.java +++ b/src/main/java/com/eprosima/fastdds/fastddsgen.java @@ -589,7 +589,7 @@ private boolean isIncludePathDuplicated(String pathToCheck) { } return false; } - + /* * ---------------------------------------------------------------------------------------- * Arguments @@ -869,14 +869,19 @@ private Project parseIDL( for (Map.Entry entry : m_customStgOutput.entrySet()) { System.out.println("Loading custom template " + entry.getKey() + "..."); - Path path = Paths.get(entry.getKey()); - String templateName = path.getFileName().toString().substring(0, path.getFileName().toString().lastIndexOf('.')); - try { - String content = new String(Files.readAllBytes(path)); - tmanager.addGroupFromString(templateName, content); - } catch(IOException e){ - System.out.println(ColorMessage.error( - "IOException") + "Cannot read content from " + path.toString()); + loadAndAddTemplate(entry.getKey(), tmanager); + } + } + else + { + // Check if there is a '$' in the output_file_name + for (Map.Entry entry : m_customStgOutput.entrySet()) + { + if (entry.getValue().contains("$")) + { + System.out.println("Loading custom template " + + entry.getKey() + " for included IDL file " + idlFilename + "..."); + loadAndAddTemplate(entry.getKey(), tmanager); } } } @@ -919,25 +924,31 @@ private Project parseIDL( { for (Map.Entry entry : m_customStgOutput.entrySet()) { - Path path = Paths.get(entry.getKey()); - String templateName = path.getFileName().toString().substring(0, path.getFileName().toString().lastIndexOf('.')); - System.out.println("Generating from custom " + templateName + " to " + entry.getValue()); - - if (returnedValue = Utils.writeFile(output_dir + entry.getValue(), maintemplates.getTemplate(templateName), m_replace)) + String templatePath = entry.getKey(); + String outputName = entry.getValue().replace("$", ""); + if (! (returnedValue = createOutputCustomTemplate( + templatePath, outputName, output_dir, relative_dir, ctx.getFilename(), + maintemplates, m_replace, project))) { - // Try to determine if the file is a header file. - if (entry.getValue().contains(".hpp") || entry.getValue().contains(".h")) - { - project.addCommonIncludeFile(relative_dir + entry.getValue()); - } - else - { - project.addCommonSrcFile(relative_dir + ctx.getFilename() + entry.getValue()); - } + break; } - else + } + } + else + { + // Check if there is a '$' in the output_file_name + for (Map.Entry entry : m_customStgOutput.entrySet()) + { + if (entry.getValue().contains("$")) { - break; + String templatePath = entry.getKey(); + String outputName = entry.getValue().replace("$", idlFilename.substring(0, idlFilename.lastIndexOf('.'))); + if (! (returnedValue = createOutputCustomTemplate( + templatePath, outputName, output_dir, relative_dir, ctx.getFilename(), + maintemplates, m_replace, project))) + { + break; + } } } } @@ -1177,6 +1188,54 @@ private Project parseIDL( return returnedValue ? project : null; } + private void loadAndAddTemplate( + String templatePath, + TemplateManager tmanager) + { + try + { + Path path = Paths.get(templatePath); + String templateName = path.getFileName().toString(); + templateName = templateName.substring(0, templateName.lastIndexOf('.')); + String content = new String(Files.readAllBytes(path)); + tmanager.addGroupFromString(templateName, content); + } + catch (IOException e) + { + System.out.println(ColorMessage.error("IOException") + "Cannot read content from " + templatePath); + } + } + + private boolean createOutputCustomTemplate( + String templatePath, + String outputName, + String outputDir, + String relativeDir, + String contextFilename, + TemplateGroup maintemplates, + boolean replace, + Project project) + { + Path path = Paths.get(templatePath); + String templateName = path.getFileName().toString(); + templateName = templateName.substring(0, templateName.lastIndexOf('.')); + System.out.println("Generating from custom " + templateName + " to " + outputName); + + boolean ret_val = Utils.writeFile(outputDir + outputName, maintemplates.getTemplate(templateName), replace); + if (ret_val) + { + if (outputName.contains(".hpp") || outputName.contains(".h")) + { + project.addCommonIncludeFile(relativeDir + outputName); + } + else + { + project.addCommonSrcFile(relativeDir + contextFilename + outputName); + } + } + return ret_val; + } + private boolean genSolution( Solution solution) { From 042028510a8851b75857c9a396722903d5445c54 Mon Sep 17 00:00:00 2001 From: cferreiragonz Date: Tue, 13 May 2025 14:11:07 +0200 Subject: [PATCH 2/2] Refs #23150: Apply Review Signed-off-by: cferreiragonz --- .../java/com/eprosima/fastdds/fastddsgen.java | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/eprosima/fastdds/fastddsgen.java b/src/main/java/com/eprosima/fastdds/fastddsgen.java index 5411c9fb..c4f5118d 100644 --- a/src/main/java/com/eprosima/fastdds/fastddsgen.java +++ b/src/main/java/com/eprosima/fastdds/fastddsgen.java @@ -874,10 +874,10 @@ private Project parseIDL( } else { - // Check if there is a '$' in the output_file_name + // Check if there is a '@' in the output_file_name for (Map.Entry entry : m_customStgOutput.entrySet()) { - if (entry.getValue().contains("$")) + if (entry.getValue().contains("@")) { System.out.println("Loading custom template " + entry.getKey() + " for included IDL file " + idlFilename + "..."); @@ -924,10 +924,8 @@ private Project parseIDL( { for (Map.Entry entry : m_customStgOutput.entrySet()) { - String templatePath = entry.getKey(); - String outputName = entry.getValue().replace("$", ""); if (! (returnedValue = createOutputCustomTemplate( - templatePath, outputName, output_dir, relative_dir, ctx.getFilename(), + entry, idlFilename, output_dir, relative_dir, ctx.getFilename(), maintemplates, m_replace, project))) { break; @@ -939,12 +937,10 @@ private Project parseIDL( // Check if there is a '$' in the output_file_name for (Map.Entry entry : m_customStgOutput.entrySet()) { - if (entry.getValue().contains("$")) + if (entry.getValue().contains("@")) { - String templatePath = entry.getKey(); - String outputName = entry.getValue().replace("$", idlFilename.substring(0, idlFilename.lastIndexOf('.'))); if (! (returnedValue = createOutputCustomTemplate( - templatePath, outputName, output_dir, relative_dir, ctx.getFilename(), + entry, idlFilename, output_dir, relative_dir, ctx.getFilename(), maintemplates, m_replace, project))) { break; @@ -1207,8 +1203,8 @@ private void loadAndAddTemplate( } private boolean createOutputCustomTemplate( - String templatePath, - String outputName, + Map.Entry entry, + String idlFilename, String outputDir, String relativeDir, String contextFilename, @@ -1216,9 +1212,10 @@ private boolean createOutputCustomTemplate( boolean replace, Project project) { - Path path = Paths.get(templatePath); + Path path = Paths.get(entry.getKey()); String templateName = path.getFileName().toString(); templateName = templateName.substring(0, templateName.lastIndexOf('.')); + String outputName = entry.getValue().replace("@", idlFilename.substring(0, idlFilename.lastIndexOf('.'))); System.out.println("Generating from custom " + templateName + " to " + outputName); boolean ret_val = Utils.writeFile(outputDir + outputName, maintemplates.getTemplate(templateName), replace);