From de75351c93dfc2ea17ecd03956d524001c6dc159 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Wed, 10 Jan 2018 13:06:56 -0500 Subject: [PATCH] Adds a system for including static .dm files --- TGS.Server/Instance/Compiler.cs | 64 +++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/TGS.Server/Instance/Compiler.cs b/TGS.Server/Instance/Compiler.cs index 33a9c370c1..db03e40940 100644 --- a/TGS.Server/Instance/Compiler.cs +++ b/TGS.Server/Instance/Compiler.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; using System.IO; +using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; @@ -140,6 +142,60 @@ namespace TGS.Server Directory.Delete(rgdl); } + /// + /// Copies .dm files from to and returns #include lines for them + /// + /// The directory to copy .dm files to + /// DM #include lines for .dm files in + IEnumerable GetAndCopyIncludeLines(string stagingDir) + { + if (stagingDir == null) + throw new ArgumentNullException(nameof(stagingDir)); + var baseURI = new Uri(stagingDir); + foreach (var I in Helpers.GetFilesWithExtensionInDirectory(RelativePath(StaticDirs), "dm")) + { + var fileURI = new Uri(I); + var fileName = Path.GetFileName(I); + File.Copy(I, Path.Combine(stagingDir, fileName)); + yield return String.Format(CultureInfo.InvariantCulture, "#include \"{0}\"", fileName); + } + } + + /// + /// Adds includes for .dm files in + /// + /// The directory to operate on + /// The full path the the .dme in + void HandleDMEModifications(string stagingDir, string dmePath) + { + if (stagingDir == null) + throw new ArgumentNullException(nameof(stagingDir)); + if (dmePath == null) + throw new ArgumentNullException(nameof(dmePath)); + if (!File.Exists(dmePath)) + return; //someone else will deal with this + var newInclusions = new List(); + var newLines = new List(); + + var lines = File.ReadAllLines(dmePath).ToList(); + var initalCount = lines.Count; + var enumerator = GetAndCopyIncludeLines(stagingDir); + for (var I = 0; I < lines.Count; ++I) + { + var line = lines[I]; + if (line.Contains("BEGIN_INCLUDE")) + { + lines.InsertRange(I + 1, enumerator); + break; + } + } + if (lines.Count == initalCount) + lines.InsertRange(0, enumerator); + + if (lines.Count > initalCount) + File.WriteAllLines(dmePath, lines); + } + //Initializing thread public void InitializeImpl() { @@ -324,6 +380,7 @@ namespace TGS.Server } string CurrentSha; + string dmeName, dmePath; try { bool silent; @@ -369,6 +426,9 @@ namespace TGS.Server //just the tip const string GitLogsDir = "/.git/logs"; Helpers.CopyDirectory(RelativePath(RepoPath + GitLogsDir), resurrectee + GitLogsDir); + dmeName = String.Format(CultureInfo.InvariantCulture, "{0}.dme", ProjectName()); + dmePath = Path.Combine(resurrectee, dmeName); + HandleDMEModifications(resurrectee, dmePath); try { File.Copy(RelativePath(PRJobFile), Path.Combine(resurrectee, PRJobFile)); @@ -389,9 +449,7 @@ namespace TGS.Server compilerCurrentStatus = CompilerStatus.Initialized; return; } - - var dmeName = ProjectName() + ".dme"; - var dmePath = resurrectee + "/" + dmeName; + if (!File.Exists(dmePath)) { var errorMsg = String.Format("Could not find {0}!", dmeName);