From a818e54b854c494848cc10a1cb949b0452124198 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 1 Jul 2024 22:41:43 +1000 Subject: [PATCH] Tidy up assembly referencing --- .github/actions/notify_failure/action.yml | 1 + MCGalaxy/Modules/Compiling/Compiler.cs | 16 +++++++--------- MCGalaxy/Modules/Compiling/CompilerBackends.cs | 9 ++------- MCGalaxy/Modules/Compiling/CompilerFrontends.cs | 13 ++++++++++--- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/.github/actions/notify_failure/action.yml b/.github/actions/notify_failure/action.yml index d67d0ec28..bb5fc2900 100644 --- a/.github/actions/notify_failure/action.yml +++ b/.github/actions/notify_failure/action.yml @@ -25,5 +25,6 @@ runs: steps: - name: Notify failure shell: sh + if: ${{ inputs.WEBHOOK_URL != '' }} 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 }}\" }" \ No newline at end of file diff --git a/MCGalaxy/Modules/Compiling/Compiler.cs b/MCGalaxy/Modules/Compiling/Compiler.cs index 6a8e81247..7a7b81481 100644 --- a/MCGalaxy/Modules/Compiling/Compiler.cs +++ b/MCGalaxy/Modules/Compiling/Compiler.cs @@ -126,7 +126,7 @@ namespace MCGalaxy.Modules.Compiling /// Converts source file paths to full paths, /// then returns list of parsed referenced assemblies - public static List ProcessInput(string[] srcPaths, string commentPrefix) { + protected List ProcessInput(string[] srcPaths, string commentPrefix) { List referenced = new List(); for (int i = 0; i < srcPaths.Length; i++) @@ -142,9 +142,9 @@ namespace MCGalaxy.Modules.Compiling return referenced; } - static void AddReferences(string path, string commentPrefix, List referenced) { + void AddReferences(string path, string commentPrefix, List referenced) { // 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 plgPrefix = commentPrefix + "pluginref "; string line; @@ -156,18 +156,16 @@ namespace MCGalaxy.Modules.Compiling } else if (line.CaselessStarts(plgPrefix)) { path = Path.Combine(IScripting.PLUGINS_DLL_DIR, GetDLL(line)); referenced.Add(Path.GetFullPath(path)); -#if NETSTANDARD - } else if (line.CaselessStarts(commentPrefix + "dotnetref")) { - referenced.Add(GetDLL(line)); -#endif } else { - continue; + ProcessInputLine(line, referenced); } } } } - static string GetDLL(string line) { + protected virtual void ProcessInputLine(string line, List referenced) { } + + protected static string GetDLL(string line) { int index = line.IndexOf(' ') + 1; // For consistency with C#, treat '//reference X.dll;' as '//reference X.dll' return line.Substring(index).Replace(";", ""); diff --git a/MCGalaxy/Modules/Compiling/CompilerBackends.cs b/MCGalaxy/Modules/Compiling/CompilerBackends.cs index 22b47a617..f721f3254 100644 --- a/MCGalaxy/Modules/Compiling/CompilerBackends.cs +++ b/MCGalaxy/Modules/Compiling/CompilerBackends.cs @@ -46,13 +46,12 @@ namespace MCGalaxy.Modules.Compiling /// Compiles source code files from a particular language, using a CodeDomProvider for the compiler public static class ICodeDomCompiler { - public static CompilerParameters PrepareInput(string[] srcPaths, string dstPath, string commentPrefix) { + public static CompilerParameters PrepareInput(string[] srcPaths, string dstPath, List referenced) { CompilerParameters args = new CompilerParameters(); args.GenerateExecutable = false; args.IncludeDebugInformation = true; args.OutputAssembly = dstPath; - List referenced = ICompiler.ProcessInput(srcPaths, commentPrefix); foreach (string assembly in referenced) { args.ReferencedAssemblies.Add(assembly); @@ -97,12 +96,8 @@ namespace MCGalaxy.Modules.Compiling { static Regex outputRegWithFileAndLine; static Regex outputRegSimple; - - public static List PrepareInput(string[] srcPaths) { - return ICompiler.ProcessInput(srcPaths, "//"); - } - public static ICompilerErrors Compile(string[] srcPaths, string dstPath, List referenced) { + public static ICompilerErrors Compile(string[] srcPaths, string dstPath, List referenced) { string args = GetCommandLineArguments(srcPaths, dstPath, referenced); 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"); diff --git a/MCGalaxy/Modules/Compiling/CompilerFrontends.cs b/MCGalaxy/Modules/Compiling/CompilerFrontends.cs index 3b31af4fa..35a874aaf 100644 --- a/MCGalaxy/Modules/Compiling/CompilerFrontends.cs +++ b/MCGalaxy/Modules/Compiling/CompilerFrontends.cs @@ -34,7 +34,8 @@ namespace MCGalaxy.Modules.Compiling CodeDomProvider compiler; protected override ICompilerErrors DoCompile(string[] srcPaths, string dstPath) { - CompilerParameters args = ICodeDomCompiler.PrepareInput(srcPaths, dstPath, "//"); + List referenced = ProcessInput(srcPaths, "//"); + CompilerParameters args = ICodeDomCompiler.PrepareInput(srcPaths, dstPath, referenced); args.CompilerOptions += " /unsafe"; // NOTE: Make sure to keep CompilerOptions in sync with RoslynCSharpCompiler @@ -42,9 +43,15 @@ namespace MCGalaxy.Modules.Compiling return ICodeDomCompiler.Compile(args, srcPaths, compiler); } #else - protected override ICompilerErrors DoCompile(string[] srcPaths, string dstPath) { - List referenced = RoslynCSharpCompiler.PrepareInput(srcPaths); + protected override ICompilerErrors DoCompile(string[] srcPaths, string dstPath) { + List referenced = ProcessInput(srcPaths, "//"); return RoslynCSharpCompiler.Compile(srcPaths, dstPath, referenced); + } + + protected override void ProcessInputLine(string line, List referenced) { + if (!line.CaselessStarts("//dotnetref")) return; + + referenced.Add(GetDLL(line)); } #endif