-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
137 additions
and
18 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
...arp/Extension/HotExecutor/Natasha.CSharp.Extension.HotExecutor.SG/HotExecutorGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
|
||
namespace Natasha.CSharp.Extension.HotExecutor.SG | ||
{ | ||
[Generator] | ||
public class HotExecutorGenerator : ISourceGenerator | ||
{ | ||
|
||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
// No initialization required for this one | ||
} | ||
|
||
void ISourceGenerator.Execute(GeneratorExecutionContext context) | ||
{ | ||
//Debugger.Launch(); | ||
var mainFile = string.Empty; | ||
var lineNumber = 0; | ||
var characterPosition = 0; | ||
var syntaxTrees = context.Compilation.SyntaxTrees; | ||
foreach (var tree in syntaxTrees) | ||
{ | ||
var methods = tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>(); | ||
if (methods.Any(method => method.Identifier.Text == "Main")) | ||
{ | ||
var mainMethod = methods.FirstOrDefault(method => method.Identifier.Text == "Main"); | ||
mainFile = tree.FilePath; | ||
|
||
var lineSpan = mainMethod.GetLocation().GetLineSpan(); | ||
lineNumber = lineSpan.StartLinePosition.Line + 1; | ||
characterPosition = lineSpan.StartLinePosition.Character + 1; | ||
//Debugger.Launch(); | ||
string proxyMethodContent = $@" | ||
using System.Runtime.CompilerServices; | ||
namespace System{{ | ||
public static class InterceptMain | ||
{{ | ||
[InterceptsLocation( | ||
filePath: @""{mainFile}"", | ||
line: {lineNumber}, | ||
character: {characterPosition})] | ||
public static void InterceptMethodMain(string args[]) | ||
{{ | ||
Console.WriteLine(11); | ||
}} | ||
}} | ||
}} | ||
// <auto-generated/> | ||
namespace System.Runtime.CompilerServices | ||
{{ | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
file sealed class InterceptsLocationAttribute(string filePath, int line, int character) : Attribute | ||
{{ | ||
}} | ||
}} | ||
"; | ||
|
||
context.AddSource($"NatashaHotExecutorProxy.g.cs", proxyMethodContent); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...or/Natasha.CSharp.Extension.HotExecutor.SG/Natasha.CSharp.Extension.HotExecutor.SG.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<LangVersion>preview</LangVersion> | ||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
</Project> |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters