diff --git a/TGS.Server/Helpers.cs b/TGS.Server/Helpers.cs index 9b3a163054..4dc3f4c1e9 100644 --- a/TGS.Server/Helpers.cs +++ b/TGS.Server/Helpers.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -49,6 +50,26 @@ namespace TGS.Server NormalizeAndDelete(di, excludeRoot, !ContentsOnly).Wait(); } + /// + /// Find all files in a directory with a given extension + /// + /// The directory to search + /// The extension to look for + /// An of s containing the full paths to files with the given in + public static IEnumerable GetFilesWithExtensionInDirectory(string directory, string extension) + { + if (directory == null) + throw new ArgumentNullException(nameof(directory)); + if (extension == null) + throw new ArgumentNullException(nameof(extension)); + + var di = new DirectoryInfo(directory); + if (!di.Exists) + yield break; + + foreach (var F in di.EnumerateFiles(String.Format(CultureInfo.InvariantCulture, "*.{0}", extension))) + yield return F.FullName; + } /// /// Properly unlinks directory if it is a symlink 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); diff --git a/TGS.Tests/Server/TestHelpers.cs b/TGS.Tests/Server/TestHelpers.cs new file mode 100644 index 0000000000..b9cf624171 --- /dev/null +++ b/TGS.Tests/Server/TestHelpers.cs @@ -0,0 +1,35 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.IO; +using System.Linq; +using TGServiceTests; + +namespace TGS.Server.Tests +{ + /// + /// Tests for + /// + [TestClass] + public sealed class TestHelpers : TempDirectoryRequiredTest + { + [TestMethod] + public void TestGetFilesWithExtensionInDirectory() + { + Assert.ThrowsException(() => Helpers.GetFilesWithExtensionInDirectory(null, null).ToList()); + Assert.ThrowsException(() => Helpers.GetFilesWithExtensionInDirectory("", null).ToList()); + Assert.ThrowsException(() => Helpers.GetFilesWithExtensionInDirectory(null, "").ToList()); + + Assert.AreEqual(0, Helpers.GetFilesWithExtensionInDirectory("Z:/", "dm").Count()); + + Assert.AreEqual(0, Helpers.GetFilesWithExtensionInDirectory(TempPath, "dm").Count()); + + File.WriteAllText(Path.Combine(TempPath, "somefile.txt"), "asdf"); + + Assert.AreEqual(0, Helpers.GetFilesWithExtensionInDirectory(TempPath, "dm").Count()); + + File.WriteAllText(Path.Combine(TempPath, "somefile.dm"), "asdf"); + + Assert.AreEqual(1, Helpers.GetFilesWithExtensionInDirectory(TempPath, "dm").Count()); + } + } +} diff --git a/TGS.Tests/TGS.Tests.csproj b/TGS.Tests/TGS.Tests.csproj index 1c470a65a3..df98e93aeb 100644 --- a/TGS.Tests/TGS.Tests.csproj +++ b/TGS.Tests/TGS.Tests.csproj @@ -66,6 +66,7 @@ + diff --git a/TGS.Tests/TempDirectoryRequiredTest.cs b/TGS.Tests/TempDirectoryRequiredTest.cs index 8a2dbbb4e7..b18858ccff 100644 --- a/TGS.Tests/TempDirectoryRequiredTest.cs +++ b/TGS.Tests/TempDirectoryRequiredTest.cs @@ -17,7 +17,7 @@ namespace TGServiceTests /// /// Construct a /// - internal TempDirectoryRequiredTest() { } + protected TempDirectoryRequiredTest() { } /// /// Setup