Convert Automation to something actually cross-platform

This commit is contained in:
NGnius (Graham) 2020-05-03 15:14:13 -04:00
parent 5cb27b05c0
commit 4e5cc40be4
5 changed files with 640 additions and 371 deletions

View file

@ -1,66 +0,0 @@
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace Automation
{
class Automation
{
private static readonly string Name = "automation";
private static readonly string XmlPrefix = "<!--Start Dependencies-->\n <ItemGroup>\n";
private static readonly string XmlSuffix = " </ItemGroup>\n<!--End Dependencies-->";
private static readonly string GamecraftModdingAPI_csproj = @"\GamecraftModdingAPI.csproj";
static void Main(string[] args)
{
if (args.Length != 2 || args[0] == "-h" || args[0] == "--help")
{
Console.WriteLine($"Usage : {Name}.exe <path to Gamecraft DLLs> <path to GamecraftModdingAPI.csproj>");
Console.WriteLine("Other arguments:");
Console.WriteLine(" --help : display this message");
Console.WriteLine($" --version : display {Name}'s version");
return;
}
Console.WriteLine("Building Assembly references");
string asmXml = BuildAssemblyReferencesXML(args[0]);
//Console.WriteLine(asmXml);
string csprojPath = args[1].EndsWith(GamecraftModdingAPI_csproj)? args[1] : args[1]+GamecraftModdingAPI_csproj;
Console.WriteLine($"Parsing {csprojPath}");
string csprojContents = File.ReadAllText(csprojPath);
Match dependenciesStart = (new Regex(@"<\s*!--\s*Start\s+Dependencies\s*--\s*>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline)).Match(csprojContents);
Match dependenciesEnd = (new Regex(@"<\s*!--\s*End\s+Dependencies\s*--\s*>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline)).Match(csprojContents);
if (!dependenciesEnd.Success || !dependenciesStart.Success)
{
Console.WriteLine("Unable to find dependency XML comments, aborting!");
return;
}
csprojContents = csprojContents.Substring(0, dependenciesStart.Index) + asmXml + csprojContents.Substring(dependenciesEnd.Index+dependenciesEnd.Length);
//Console.WriteLine(csprojContents);
Console.WriteLine($"Writing Assembly references into {csprojPath}");
File.WriteAllText(csprojPath, csprojContents);
Console.WriteLine("Successfully generated Gamecraft assembly DLL references");
}
static string BuildAssemblyReferencesXML(string path)
{
StringBuilder result = new StringBuilder();
result.Append(XmlPrefix);
string[] files = Directory.GetFiles(path, "*.dll");
for(int i = 0; i<files.Length; i++)
{
if (files[i].Contains(@"\System.") || files[i].EndsWith(@"mscorlib.dll") || files[i].Contains(@"\Mono."))
{
// no
}
else
{
result.Append($" <Reference Include=\"{files[i].Substring(files[i].LastIndexOf(@"\") + 1).Replace(".dll", "")}\">\n <HintPath>{files[i]}</HintPath>\n </Reference>\n");
}
}
result.Append(XmlSuffix);
return result.ToString();
}
}
}

View file

@ -1,8 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

55
Automation/gen_csproj.py Executable file
View file

@ -0,0 +1,55 @@
#!/usr/bin/python3
import argparse
from pathlib import Path, PurePath
import re
DLL_EXCLUSIONS_REGEX = r"(System|Microsoft|Mono)\."
def getAssemblyReferences(path):
asmDir = Path(path)
result = list()
for child in asmDir.iterdir():
if child.is_file() and re.search(DLL_EXCLUSIONS_REGEX, str(child), re.I) is None:
result.append(str(child).replace("\\", "/"))
return result
def buildReferencesXml(path):
assemblyPathes = getAssemblyReferences(path)
result = list()
for asm in assemblyPathes:
asmPath = str(asm)
xml = " <Reference Include=\"" + asmPath[asmPath.rfind("/") + 1:].replace(".dll", "") + "\">\n" \
+ " <HintPath>" + asmPath.replace("/", "\\") + "</HintPath>\n" \
+ " <HintPath>..\\" + asmPath.replace("/", "\\") + "</HintPath>\n" \
+ " </Reference>\n"
result.append(xml)
return "".join(result)
#return "<!--Start Dependencies-->\n <ItemGroup>\n" + "".join(result) + " </ItemGroup>\n<!--End Dependencies-->"
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate GamecraftModdingAPI.csproj")
# TODO (maybe?): add params for custom csproj read and write locations
args = parser.parse_args()
print("Building Assembly references")
asmXml = buildReferencesXml("../ref/Gamecraft_Data/Managed")
print(asmXml)
with open("../GamecraftModdingAPI/GamecraftModdingAPI.csproj", "r") as xmlFile:
print("Parsing GamecraftModdingAPI.csproj")
fileStr = xmlFile.read()
print(fileStr)
depsStart = re.search(r"\<!--\s*Start\s+Dependencies\s*--\>", fileStr)
depsEnd = re.search(r"\<!--\s*End\s+Dependencies\s*--\>", fileStr)
if depsStart is None or depsEnd is None:
print("Unable to find dependency XML comments, aborting!")
exit(1)
newFileStr = fileStr[:depsStart.end()] + "\n" + asmXml + "\n" + fileStr[depsEnd.start():]
with open("../GamecraftModdingAPI/GamecraftModdingAPI.csproj", "w") as xmlFile:
print("Writing Assembly references (not)")
#xmlFile.seek(0)
xmlFile.write(newFileStr)
print(newFileStr)

View file

@ -3,9 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 16
VisualStudioVersion = 16.0.29411.108 VisualStudioVersion = 16.0.29411.108
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GamecraftModdingAPI", "GamecraftModdingAPI\GamecraftModdingAPI.csproj", "{7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GamecraftModdingAPI", "GamecraftModdingAPI\GamecraftModdingAPI.csproj", "{7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Automation", "Automation\Automation.csproj", "{1DF6E538-4DA4-4708-A567-781AF788921A}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -17,10 +15,6 @@ Global
{7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}.Debug|Any CPU.Build.0 = Debug|Any CPU {7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}.Release|Any CPU.ActiveCfg = Release|Any CPU {7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}.Release|Any CPU.Build.0 = Release|Any CPU {7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}.Release|Any CPU.Build.0 = Release|Any CPU
{1DF6E538-4DA4-4708-A567-781AF788921A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1DF6E538-4DA4-4708-A567-781AF788921A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DF6E538-4DA4-4708-A567-781AF788921A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1DF6E538-4DA4-4708-A567-781AF788921A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

File diff suppressed because it is too large Load diff