diff --git a/message_generation/src/main/java/org/ros/internal/message/GenerateInterfaces.java b/message_generation/src/main/java/org/ros/internal/message/GenerateInterfaces.java index 00bff33d..f02e454d 100644 --- a/message_generation/src/main/java/org/ros/internal/message/GenerateInterfaces.java +++ b/message_generation/src/main/java/org/ros/internal/message/GenerateInterfaces.java @@ -20,6 +20,8 @@ import com.google.common.collect.Sets; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.SystemUtils; +import org.omg.SendingContext.RunTime; import org.ros.exception.RosMessageRuntimeException; import org.ros.internal.message.definition.MessageDefinitionProviderChain; import org.ros.internal.message.definition.MessageDefinitionTupleParser; @@ -36,8 +38,10 @@ import org.ros.message.MessageFactory; import org.ros.message.MessageIdentifier; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; import java.util.Collection; import java.util.List; import java.util.ListIterator; @@ -96,12 +100,45 @@ private void writeTopicInterfaces(File outputDirectory, Collection packa } } for (MessageIdentifier topicType : topicTypes) { - String definition = messageDefinitionProviderChain.get(topicType.getType()); + String definition = getDefinition(topicType); MessageDeclaration messageDeclaration = new MessageDeclaration(topicType, definition); writeInterface(messageDeclaration, outputDirectory, true); } } + private String getDefinition(MessageIdentifier topicType) { + String definition; + final File directory = topicDefinitionFileProvider.getDirectory(topicType.getPackage()); + final String cmd = "rosrun roslib gendeps --cat " + directory.getAbsolutePath() + File.separator + "msg" + + File.separator + topicType.getName() + ".msg"; + final StringBuilder sb = new StringBuilder(); + Process p = null; + try { + p = Runtime.getRuntime().exec(cmd); + final BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream())); + final BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream())); + String s; + while ((s = bri.readLine()) != null) { + sb.append(s); + sb.append("\n"); + } + p.waitFor(); + if (p.exitValue() != 0) { + throw new RuntimeException(bre.readLine()); + } + definition = sb.toString(); + } catch (Exception e) { + System.out.println("ERROR: Cannot generate child definitions for " + topicType.getType() + + ". Failed to run '" + cmd + "': " + e.getMessage()); + definition = messageDefinitionProviderChain.get(topicType.getType()); + } finally { + if (p != null) { + p.destroy(); + } + } + return definition; + } + /** * @param packages * a list of packages containing the topic types to generate diff --git a/message_generation/src/main/java/org/ros/internal/message/StringFileProvider.java b/message_generation/src/main/java/org/ros/internal/message/StringFileProvider.java index 2f9ddfaf..4cdf524f 100644 --- a/message_generation/src/main/java/org/ros/internal/message/StringFileProvider.java +++ b/message_generation/src/main/java/org/ros/internal/message/StringFileProvider.java @@ -124,6 +124,10 @@ public String get(File file) { return strings.get(file); } + public Collection getDirectories() { + return directories; + } + public boolean has(File file) { return strings.containsKey(file); } diff --git a/message_generation/src/main/java/org/ros/internal/message/definition/MessageDefinitionFileProvider.java b/message_generation/src/main/java/org/ros/internal/message/definition/MessageDefinitionFileProvider.java index 4aa03891..829ffef0 100644 --- a/message_generation/src/main/java/org/ros/internal/message/definition/MessageDefinitionFileProvider.java +++ b/message_generation/src/main/java/org/ros/internal/message/definition/MessageDefinitionFileProvider.java @@ -92,6 +92,15 @@ public void addDirectory(File directory) { stringFileProvider.addDirectory(directory); } + public File getDirectory(String pkg) { + for (File dir : stringFileProvider.getDirectories()) { + if (dir.getName().equals(pkg)) { + return dir; + } + } + return null; + } + @Override public Collection getPackages() { return messageIdentifiers.keySet(); diff --git a/message_generation/src/main/java/org/ros/internal/message/definition/MessageDefinitionParser.java b/message_generation/src/main/java/org/ros/internal/message/definition/MessageDefinitionParser.java index e0bbe1e6..10bd8f28 100644 --- a/message_generation/src/main/java/org/ros/internal/message/definition/MessageDefinitionParser.java +++ b/message_generation/src/main/java/org/ros/internal/message/definition/MessageDefinitionParser.java @@ -103,6 +103,9 @@ public void parse(String messageType, String messageDefinition) { if (line.startsWith("#")) { continue; } + if (line.equals(MessageDefinitionSeparators.defaultSeparator)) { + break; + } if (line.length() > 0) { parseField(messageType, line); } diff --git a/message_generation/src/main/java/org/ros/internal/message/definition/MessageDefinitionSeparators.java b/message_generation/src/main/java/org/ros/internal/message/definition/MessageDefinitionSeparators.java new file mode 100644 index 00000000..12a6abe5 --- /dev/null +++ b/message_generation/src/main/java/org/ros/internal/message/definition/MessageDefinitionSeparators.java @@ -0,0 +1,5 @@ +package org.ros.internal.message.definition; + +public class MessageDefinitionSeparators { + public static final String defaultSeparator = "================================================================================"; +}