From c2631da8e4795c967d85bbe8df207620e3025c36 Mon Sep 17 00:00:00 2001
From: Tom Lambert <tom.lambert@picosmos.de>
Date: Sat, 11 Jan 2020 20:02:44 +0100
Subject: [PATCH] TestWriter scaffold

---
 .gitignore                     |  1 +
 IWriter.cs                     |  7 +++++++
 MonitorPyWriter.cs             | 18 ++++++++++++++++++
 ObstaclePyWriter.cs            | 18 ++++++++++++++++++
 Program.cs                     | 29 +++++++++++++++++------------
 Properties/launchSettings.json |  2 +-
 TestWriter.cs                  | 16 +++++++++++++++-
 WorldWriter.cs                 | 17 +++++++++++++++++
 compiler.csproj                | 13 +++++++++++++
 9 files changed, 107 insertions(+), 14 deletions(-)
 create mode 100644 IWriter.cs
 create mode 100644 MonitorPyWriter.cs
 create mode 100644 ObstaclePyWriter.cs
 create mode 100644 WorldWriter.cs

diff --git a/.gitignore b/.gitignore
index 3804965..d043fb5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 /obj/
 /bin/
 /.vs/
+/output/
\ No newline at end of file
diff --git a/IWriter.cs b/IWriter.cs
new file mode 100644
index 0000000..b159d1e
--- /dev/null
+++ b/IWriter.cs
@@ -0,0 +1,7 @@
+namespace compiler
+{
+    public interface IWriter
+    {
+        void Write(string path, TestParser.Node rootNode);
+    }
+}
diff --git a/MonitorPyWriter.cs b/MonitorPyWriter.cs
new file mode 100644
index 0000000..73ab933
--- /dev/null
+++ b/MonitorPyWriter.cs
@@ -0,0 +1,18 @@
+using System.IO;
+
+namespace compiler
+{
+    public class MonitorPyWriter : IWriter
+    {
+        public void Write(string path, TestParser.Node rootNode)
+        {
+            var targetFile = Path.Combine(path, "monitor.py");
+            File.WriteAllText(targetFile, $@"
+# ToDo: Add Python Code
+
+
+
+");
+        }
+    }
+}
diff --git a/ObstaclePyWriter.cs b/ObstaclePyWriter.cs
new file mode 100644
index 0000000..f942ac8
--- /dev/null
+++ b/ObstaclePyWriter.cs
@@ -0,0 +1,18 @@
+using System.IO;
+
+namespace compiler
+{
+    public class ObstaclePyWriter : IWriter
+    {
+        public void Write(string path, TestParser.Node rootNode)
+        {
+            var targetFile = Path.Combine(path, "obstacle.py");
+            File.WriteAllText(targetFile, $@"
+# ToDo: Add Python Code
+
+
+
+");
+        }
+    }
+}
diff --git a/Program.cs b/Program.cs
index db12467..97dfc5f 100644
--- a/Program.cs
+++ b/Program.cs
@@ -1,4 +1,5 @@
 using System;
+using System.IO;
 using System.Linq;
 
 namespace compiler
@@ -16,23 +17,27 @@ namespace compiler
             }
 
             var sourceDirectory = args[0];
-            var sourceFiles = System.IO.Directory.GetFiles(sourceDirectory, "*.uml");
+            var targetDirectory = args[1];
+
+            if (Directory.Exists(targetDirectory))
+            {
+                Directory.Delete(targetDirectory, true);
+            }
+
+            var sourceFiles = Directory.GetFiles(sourceDirectory, "*.uml", SearchOption.AllDirectories);
             foreach (var sourceFile in sourceFiles)
             {
-                Console.WriteLine("New file: " + sourceFile);
+                var baseName = sourceFile.Substring(sourceDirectory.Length + 1);
+
+                Console.WriteLine("Processing file: " + baseName);
                 var rootNodes = TestParser.ReadFile(sourceFile).ToList();
 
-                // thw following is just for testing
-                foreach (var rootNode in rootNodes)
+                for (var i = 0; i < rootNodes.Count; i++)
                 {
-                    Console.WriteLine("New rootNode");
-                    var node = rootNode;
-                    while (node != null)
-                    {
-                        Console.WriteLine(node.Text);
-                        Console.WriteLine(node.Data?.Select(x => "  " + x)?.Aggregate((x, y) => x + "\n" + y));
-                        node = node.Next;
-                    }
+                    var rootNode = rootNodes[i];
+                    var path = Path.Combine(targetDirectory, @$"{baseName}-{i}");
+                    Directory.CreateDirectory(path);
+                    TestWriter.WriteAll(path, rootNode);
                 }
             }
         }
diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json
index ec02d06..889107a 100644
--- a/Properties/launchSettings.json
+++ b/Properties/launchSettings.json
@@ -2,7 +2,7 @@
   "profiles": {
     "compiler": {
       "commandName": "Project",
-      "commandLineArgs": "../../../tests ../../../output"
+      "commandLineArgs": "../../../TestSpec ../../../output"
     }
   }
 }
\ No newline at end of file
diff --git a/TestWriter.cs b/TestWriter.cs
index 0250c71..6b5cf6e 100644
--- a/TestWriter.cs
+++ b/TestWriter.cs
@@ -1,6 +1,20 @@
-namespace compiler
+using System.Collections.Generic;
+
+namespace compiler
 {
     public static class TestWriter
     {
+        public static void WriteAll(string path, TestParser.Node rootNode)
+        {
+            var writers = new List<IWriter>();
+            writers.Add(new MonitorPyWriter());
+            writers.Add(new ObstaclePyWriter());
+            writers.Add(new WorldWriter());
+
+            foreach (var writer in writers)
+            {
+                writer.Write(path, rootNode);
+            }
+        }
     }
 }
diff --git a/WorldWriter.cs b/WorldWriter.cs
new file mode 100644
index 0000000..f410760
--- /dev/null
+++ b/WorldWriter.cs
@@ -0,0 +1,17 @@
+using System.IO;
+
+namespace compiler
+{
+    public class WorldWriter : IWriter
+    {
+        public void Write(string path, TestParser.Node rootNode)
+        {
+            var targetFile = Path.Combine(path, "test.world");
+            File.WriteAllText(targetFile, $@"
+
+
+
+");
+        }
+    }
+}
diff --git a/compiler.csproj b/compiler.csproj
index d453e9a..70f7e77 100644
--- a/compiler.csproj
+++ b/compiler.csproj
@@ -5,4 +5,17 @@
     <TargetFramework>netcoreapp3.1</TargetFramework>
   </PropertyGroup>
 
+  <ItemGroup>
+    <Compile Remove="Tests\**" />
+    <EmbeddedResource Remove="Tests\**" />
+    <None Remove="Tests\**" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
+      <PrivateAssets>all</PrivateAssets>
+      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+    </PackageReference>
+  </ItemGroup>
+
 </Project>
-- 
GitLab