源生成器:有关引用项目的信息?

Chr*_*all 2 c# code-generation sourcegenerators

我开始使用 C# 源生成器。

我想要的是启动一个git describe --tags --long进程并GitVersion使用当前标签和哈希代码作为属性填充静态类。

问题是,我没有关于引用项目的目录的信息,所以我不知道在哪里运行 git 进程。GeneratorExecutionContext我在函数的参数中找不到任何有用的信息Execute

AppDomain.CurrentDomain指向 csc.exe 进程,所以我想没有办法知道它在那里?

Chr*_*all 9

我最终使用了:

public void Execute(GeneratorExecutionContext context) {
     var mainSyntaxTree = context.Compilation.SyntaxTrees
                          .First(x => x.HasCompilationUnitRoot);

     var directory = Path.GetDirectoryName(mainSyntaxTree.FilePath);
Run Code Online (Sandbox Code Playgroud)