sdbus++-gendir: avoid stale data on re-run

The foo.md file is formed from both a foo.interface.yaml and
foo.errors.yaml, but it is possible that only one of them exists.  In
the case where only foo.errors.yaml existed, sdbus++-gendir will
concatentate output from multiple executions into the same markdown
file, due to the use of 'append' mode.

Improve sdbus++-gendir by adding a pass through the files that creates
an empty file for any files which are later written to in append mode.

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I38a11755a9d05358f4f76665ff855287a3c2da80
diff --git a/tools/sdbus++-gendir b/tools/sdbus++-gendir
index 32a340a..518d43b 100755
--- a/tools/sdbus++-gendir
+++ b/tools/sdbus++-gendir
@@ -83,7 +83,11 @@
 #   $4: relative output file
 #   $5: 'append-mode' if present.
 function generate_single {
-    if [ "x" == "x$5" ];
+
+    if [ "xempty" == "x$1" ];
+    then
+        echo -n > $outputdir/$4
+    elif [ "x" == "x$5" ];
     then
         $sdbuspp $1 $2 $3 > $outputdir/$4 &
     else
@@ -116,31 +120,41 @@
 for d in $@;
 do
     interfaces="$(find $d -name '*.interface.yaml')"
+    errors="$(find $d -name '*.errors.yaml')"
+
+    # Some files are created from multiple YAML sources, but we don't know
+    # which YAML sources are present.  For these files, we need to first
+    # create an empty file to ensure the file does not carry old data from
+    # a previous run.
+    for y in $interfaces $errors;
+    do
+        path="${y%.interface.yaml}"
+        path="${path%.errors.yaml}"
+        iface="${path//\//.}"
+
+        mkdir -p $outputdir/$path
+        generate_single empty markdown $iface $path.md
+    done
 
     for i in $interfaces;
     do
         path="${i%.interface.yaml}"
         iface="${path//\//.}"
 
-        mkdir -p $outputdir/$path
-
         generate_single interface server-header $iface $path/server.hpp
         generate_single interface server-cpp $iface $path/server.cpp
         generate_single interface client-header $iface $path/client.hpp
-        generate_single interface markdown $iface $path.md
+        generate_single interface markdown $iface $path.md append
 
     done
     wait # finish all before continuing
 
-    errors="$(find $d -name '*.errors.yaml')"
 
     for e in $errors;
     do
         path="${e%.errors.yaml}"
         iface="${path//\//.}"
 
-        mkdir -p $outputdir/$path
-
         generate_single error exception-header $iface $path/error.hpp
         generate_single error exception-cpp $iface $path/error.cpp
         generate_single error markdown $iface $path.md append