Tidy up assembly referencing

This commit is contained in:
UnknownShadow200 2024-07-01 22:41:43 +10:00
parent 96ebcf29bb
commit a818e54b85
4 changed files with 20 additions and 19 deletions

View File

@ -25,5 +25,6 @@ runs:
steps: steps:
- name: Notify failure - name: Notify failure
shell: sh shell: sh
if: ${{ inputs.WEBHOOK_URL != '' }}
run: | run: |
curl ${{ inputs.WEBHOOK_URL }} -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"username\": \"${{ inputs.BOT_USERNAME }}\", \"avatar_url\": \"${{ inputs.BOT_AVATAR }}\", \"content\": \"${{ inputs.NOTIFY_MESSAGE }}\" }" curl ${{ inputs.WEBHOOK_URL }} -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"username\": \"${{ inputs.BOT_USERNAME }}\", \"avatar_url\": \"${{ inputs.BOT_AVATAR }}\", \"content\": \"${{ inputs.NOTIFY_MESSAGE }}\" }"

View File

@ -126,7 +126,7 @@ namespace MCGalaxy.Modules.Compiling
/// <summary> Converts source file paths to full paths, /// <summary> Converts source file paths to full paths,
/// then returns list of parsed referenced assemblies </summary> /// then returns list of parsed referenced assemblies </summary>
public static List<string> ProcessInput(string[] srcPaths, string commentPrefix) { protected List<string> ProcessInput(string[] srcPaths, string commentPrefix) {
List<string> referenced = new List<string>(); List<string> referenced = new List<string>();
for (int i = 0; i < srcPaths.Length; i++) for (int i = 0; i < srcPaths.Length; i++)
@ -142,9 +142,9 @@ namespace MCGalaxy.Modules.Compiling
return referenced; return referenced;
} }
static void AddReferences(string path, string commentPrefix, List<string> referenced) { void AddReferences(string path, string commentPrefix, List<string> referenced) {
// Allow referencing other assemblies using '//reference [assembly name]' at top of the file // Allow referencing other assemblies using '//reference [assembly name]' at top of the file
using (StreamReader r = new StreamReader(path)) { using (StreamReader r = new StreamReader(path)) {
string refPrefix = commentPrefix + "reference "; string refPrefix = commentPrefix + "reference ";
string plgPrefix = commentPrefix + "pluginref "; string plgPrefix = commentPrefix + "pluginref ";
string line; string line;
@ -156,18 +156,16 @@ namespace MCGalaxy.Modules.Compiling
} else if (line.CaselessStarts(plgPrefix)) { } else if (line.CaselessStarts(plgPrefix)) {
path = Path.Combine(IScripting.PLUGINS_DLL_DIR, GetDLL(line)); path = Path.Combine(IScripting.PLUGINS_DLL_DIR, GetDLL(line));
referenced.Add(Path.GetFullPath(path)); referenced.Add(Path.GetFullPath(path));
#if NETSTANDARD
} else if (line.CaselessStarts(commentPrefix + "dotnetref")) {
referenced.Add(GetDLL(line));
#endif
} else { } else {
continue; ProcessInputLine(line, referenced);
} }
} }
} }
} }
static string GetDLL(string line) { protected virtual void ProcessInputLine(string line, List<string> referenced) { }
protected static string GetDLL(string line) {
int index = line.IndexOf(' ') + 1; int index = line.IndexOf(' ') + 1;
// For consistency with C#, treat '//reference X.dll;' as '//reference X.dll' // For consistency with C#, treat '//reference X.dll;' as '//reference X.dll'
return line.Substring(index).Replace(";", ""); return line.Substring(index).Replace(";", "");

View File

@ -46,13 +46,12 @@ namespace MCGalaxy.Modules.Compiling
/// <summary> Compiles source code files from a particular language, using a CodeDomProvider for the compiler </summary> /// <summary> Compiles source code files from a particular language, using a CodeDomProvider for the compiler </summary>
public static class ICodeDomCompiler public static class ICodeDomCompiler
{ {
public static CompilerParameters PrepareInput(string[] srcPaths, string dstPath, string commentPrefix) { public static CompilerParameters PrepareInput(string[] srcPaths, string dstPath, List<string> referenced) {
CompilerParameters args = new CompilerParameters(); CompilerParameters args = new CompilerParameters();
args.GenerateExecutable = false; args.GenerateExecutable = false;
args.IncludeDebugInformation = true; args.IncludeDebugInformation = true;
args.OutputAssembly = dstPath; args.OutputAssembly = dstPath;
List<string> referenced = ICompiler.ProcessInput(srcPaths, commentPrefix);
foreach (string assembly in referenced) foreach (string assembly in referenced)
{ {
args.ReferencedAssemblies.Add(assembly); args.ReferencedAssemblies.Add(assembly);
@ -97,12 +96,8 @@ namespace MCGalaxy.Modules.Compiling
{ {
static Regex outputRegWithFileAndLine; static Regex outputRegWithFileAndLine;
static Regex outputRegSimple; static Regex outputRegSimple;
public static List<string> PrepareInput(string[] srcPaths) {
return ICompiler.ProcessInput(srcPaths, "//");
}
public static ICompilerErrors Compile(string[] srcPaths, string dstPath, List<string> referenced) { public static ICompilerErrors Compile(string[] srcPaths, string dstPath, List<string> referenced) {
string args = GetCommandLineArguments(srcPaths, dstPath, referenced); string args = GetCommandLineArguments(srcPaths, dstPath, referenced);
string netPath = GetBinaryFile("MCG_DOTNET_PATH", "'dotnet' executable - e.g. /home/test/.dotnet/dotnet"); string netPath = GetBinaryFile("MCG_DOTNET_PATH", "'dotnet' executable - e.g. /home/test/.dotnet/dotnet");
string cscPath = GetBinaryFile("MCG_COMPILER_PATH", "'csc.dll' file - e.g. /home/test/.dotnet/sdk/6.0.300/Roslyn/bincore/csc.dll"); string cscPath = GetBinaryFile("MCG_COMPILER_PATH", "'csc.dll' file - e.g. /home/test/.dotnet/sdk/6.0.300/Roslyn/bincore/csc.dll");

View File

@ -34,7 +34,8 @@ namespace MCGalaxy.Modules.Compiling
CodeDomProvider compiler; CodeDomProvider compiler;
protected override ICompilerErrors DoCompile(string[] srcPaths, string dstPath) { protected override ICompilerErrors DoCompile(string[] srcPaths, string dstPath) {
CompilerParameters args = ICodeDomCompiler.PrepareInput(srcPaths, dstPath, "//"); List<string> referenced = ProcessInput(srcPaths, "//");
CompilerParameters args = ICodeDomCompiler.PrepareInput(srcPaths, dstPath, referenced);
args.CompilerOptions += " /unsafe"; args.CompilerOptions += " /unsafe";
// NOTE: Make sure to keep CompilerOptions in sync with RoslynCSharpCompiler // NOTE: Make sure to keep CompilerOptions in sync with RoslynCSharpCompiler
@ -42,9 +43,15 @@ namespace MCGalaxy.Modules.Compiling
return ICodeDomCompiler.Compile(args, srcPaths, compiler); return ICodeDomCompiler.Compile(args, srcPaths, compiler);
} }
#else #else
protected override ICompilerErrors DoCompile(string[] srcPaths, string dstPath) { protected override ICompilerErrors DoCompile(string[] srcPaths, string dstPath) {
List<string> referenced = RoslynCSharpCompiler.PrepareInput(srcPaths); List<string> referenced = ProcessInput(srcPaths, "//");
return RoslynCSharpCompiler.Compile(srcPaths, dstPath, referenced); return RoslynCSharpCompiler.Compile(srcPaths, dstPath, referenced);
}
protected override void ProcessInputLine(string line, List<string> referenced) {
if (!line.CaselessStarts("//dotnetref")) return;
referenced.Add(GetDLL(line));
} }
#endif #endif