mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-26 22:48:20 +01:00
Merge pull request #468 from tgstation/467-ConfigurableDefines
Adds a system for including custom .dm files
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find all files in a directory with a given extension
|
||||
/// </summary>
|
||||
/// <param name="directory">The directory to search</param>
|
||||
/// <param name="extension">The extension to look for</param>
|
||||
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="string"/>s containing the full paths to files with the given <paramref name="extension"/> in <paramref name="directory"/></returns>
|
||||
public static IEnumerable<string> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Properly unlinks directory <paramref name="di"/> if it is a symlink
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies .dm files from <see cref="StaticDirs"/> to <paramref name="stagingDir"/> and returns #include lines for them
|
||||
/// </summary>
|
||||
/// <param name="stagingDir">The directory to copy .dm files to</param>
|
||||
/// <returns>DM #include lines for .dm files in <see cref="StaticDirs"/></returns>
|
||||
IEnumerable<string> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds includes for .dm files in <see cref="StaticDirs"/>
|
||||
/// </summary>
|
||||
/// <param name="stagingDir">The directory to operate on</param>
|
||||
/// <param name="dmePath">The full path the the .dme in <paramref name="stagingDir"/></param>
|
||||
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<string>();
|
||||
var newLines = new List<string>();
|
||||
|
||||
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);
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using TGServiceTests;
|
||||
|
||||
namespace TGS.Server.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests for <see cref="Helpers"/>
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public sealed class TestHelpers : TempDirectoryRequiredTest
|
||||
{
|
||||
[TestMethod]
|
||||
public void TestGetFilesWithExtensionInDirectory()
|
||||
{
|
||||
Assert.ThrowsException<ArgumentNullException>(() => Helpers.GetFilesWithExtensionInDirectory(null, null).ToList());
|
||||
Assert.ThrowsException<ArgumentNullException>(() => Helpers.GetFilesWithExtensionInDirectory("", null).ToList());
|
||||
Assert.ThrowsException<ArgumentNullException>(() => 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,7 @@
|
||||
<Compile Include="ControlPanel\TestInstanceSelector.cs" />
|
||||
<Compile Include="OutputProcOverriderTest.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Server\TestHelpers.cs" />
|
||||
<Compile Include="ServiceInterface\TestHelpers.cs" />
|
||||
<Compile Include="ServiceInterface\TestInterface.cs" />
|
||||
<Compile Include="TempDirectoryRequiredTest.cs" />
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace TGServiceTests
|
||||
/// <summary>
|
||||
/// Construct a <see cref="TempDirectoryRequiredTest"/>
|
||||
/// </summary>
|
||||
internal TempDirectoryRequiredTest() { }
|
||||
protected TempDirectoryRequiredTest() { }
|
||||
|
||||
/// <summary>
|
||||
/// Setup <see cref="TempPath"/>
|
||||
|
||||
Reference in New Issue
Block a user