Finished compile time plugin loading. Refactored plugin loading. Extended build helper script
This commit is contained in:
30
Resources/Scripts/Helpers/CommandHelper.cs
Normal file
30
Resources/Scripts/Helpers/CommandHelper.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Scripts.Helpers;
|
||||
|
||||
public class CommandHelper
|
||||
{
|
||||
public async Task Run(string program, string arguments, string? workingDir = null)
|
||||
{
|
||||
var process = await RunRaw(program, arguments, workingDir);
|
||||
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
if (process.ExitCode != 0)
|
||||
throw new Exception($"The command '{program} {arguments}' failed with exit code: {process.ExitCode}");
|
||||
}
|
||||
|
||||
private Task<Process> RunRaw(string program, string arguments, string? workingDir = null)
|
||||
{
|
||||
var psi = new ProcessStartInfo()
|
||||
{
|
||||
FileName = program,
|
||||
Arguments = arguments,
|
||||
WorkingDirectory = string.IsNullOrEmpty(workingDir) ? Directory.GetCurrentDirectory() : workingDir
|
||||
};
|
||||
|
||||
var process = Process.Start(psi)!;
|
||||
|
||||
return Task.FromResult(process);
|
||||
}
|
||||
}
|
||||
46
Resources/Scripts/Helpers/StartupClassDetector.cs
Normal file
46
Resources/Scripts/Helpers/StartupClassDetector.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
|
||||
namespace Scripts.Helpers;
|
||||
|
||||
public class StartupClassDetector
|
||||
{
|
||||
public bool Check(string content, out string fullName)
|
||||
{
|
||||
var tree = CSharpSyntaxTree.ParseText(content);
|
||||
var root = tree.GetCompilationUnitRoot();
|
||||
|
||||
var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
|
||||
var compilation = CSharpCompilation.Create("MyAnalysis", [tree], [mscorlib]);
|
||||
|
||||
var model = compilation.GetSemanticModel(tree);
|
||||
|
||||
var classDeclarations = root.DescendantNodes().OfType<ClassDeclarationSyntax>();
|
||||
|
||||
foreach (var classDecl in classDeclarations)
|
||||
{
|
||||
var symbol = model.GetDeclaredSymbol(classDecl);
|
||||
|
||||
if(symbol == null)
|
||||
continue;
|
||||
|
||||
var hasAttribute = symbol.GetAttributes().Any(attribute =>
|
||||
{
|
||||
if (attribute.AttributeClass == null)
|
||||
return false;
|
||||
|
||||
return attribute.AttributeClass.Name.Contains("PluginStartup");
|
||||
});
|
||||
|
||||
if (hasAttribute)
|
||||
{
|
||||
fullName = symbol.ContainingNamespace.ToDisplayString() + "." + classDecl.Identifier.ValueText;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
fullName = "";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user