Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 48 additions & 10 deletions src/main/java/com/eprosima/microxrcedds/microxrceddsgen.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Vector;
import java.util.jar.Manifest;

Expand Down Expand Up @@ -61,6 +62,7 @@ public class microxrceddsgen {
private final String m_defaultOutputDir = "." + File.separator;
private String m_outputDir = m_defaultOutputDir;
private String m_tempDir = null;
private boolean m_tempDirCleanup = false;
protected static String m_appName = "microxrceddsgen";
protected boolean m_test = false;
protected String m_defaultContainerPreallocSize = "50";
Expand Down Expand Up @@ -228,6 +230,13 @@ public boolean execute() {
}
}

if (returnedValue)
{
// be nice and clean up if no error; user may want files if there
// was an issue (though they have no real ability to find them...)
cleanUpTemp();
}

return returnedValue;

}
Expand Down Expand Up @@ -272,16 +281,15 @@ public static void printHelp()
public boolean globalInit() {
// Set the temporary folder
if (m_tempDir == null) {
if (m_os.contains("Windows")) {
String tempPath = System.getenv("TEMP");

if (tempPath == null) {
tempPath = System.getenv("TMP");
}

m_tempDir = tempPath;
} else if (m_os.contains("Linux") || m_os.contains("Mac")) {
m_tempDir = "/tmp/";
try
{
m_tempDir = java.nio.file.Files.createTempDirectory("microxrceddsgen").toString();
m_tempDirCleanup = true; // as we created it
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
}

Expand All @@ -292,6 +300,36 @@ public boolean globalInit() {
return true;
}

private void cleanUpTemp() {
if (!m_tempDirCleanup) {
return;
}

try
{
// https://stackoverflow.com/a/27917071 . it's "very simple"
java.nio.file.Path directory = java.nio.file.Paths.get(m_tempDir);
java.nio.file.Files.walkFileTree(directory, new java.nio.file.SimpleFileVisitor<java.nio.file.Path>() {
@Override
public java.nio.file.FileVisitResult visitFile(java.nio.file.Path file, java.nio.file.attribute.BasicFileAttributes attrs) throws IOException {
java.nio.file.Files.delete(file);
return java.nio.file.FileVisitResult.CONTINUE;
}

@Override
public java.nio.file.FileVisitResult postVisitDirectory(java.nio.file.Path dir, IOException exc) throws IOException {
java.nio.file.Files.delete(dir);
return java.nio.file.FileVisitResult.CONTINUE;
}
});
}
catch (IOException e)
{
e.printStackTrace();
// we tried!
}
}

private Project process(String idlFilename) {
Project project = null;
System.out.println("Processing the file " + idlFilename + "...");
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/com/eprosima/microxrcedds/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.eprosima.idl.generator.manager.TemplateST;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;

public class Utils
{
Expand Down Expand Up @@ -88,13 +90,31 @@ public static boolean writeFile(String file, ST template, boolean replace)
try
{
File handle = new File(file);
Path handlePath = handle.toPath();

if(!handle.exists() || replace)
{
FileWriter fw = new FileWriter(file);
// assume we might race with someone else writing the same contents

// create in same directory as output so we can do an atomic rename
Path temp = Files.createTempFile(handlePath.getParent(), ".microxrceddsgen", null);

FileWriter fw = new FileWriter(temp.toFile());
String data = template.render();
fw.write(data, 0, data.length());
fw.close();

handle.delete(); // get rid of any old file, assuming it might be outdated
try
{
Files.move(temp, handlePath, java.nio.file.StandardCopyOption.ATOMIC_MOVE);
}
catch(IOException e)
{
// assume destination exists, in which case somebody wrote
// the correct contents before us, so we don't need to do
// anything
}
}
else
{
Expand Down