Skip to content

DefineでSourceGenerartorによるSource生成を制御 & ManualGeneratorのMac対応#9

Open
AtaruMatsudaira wants to merge 5 commits intoruccho:masterfrom
AtaruMatsudaira:feat/ataru/misc
Open

DefineでSourceGenerartorによるSource生成を制御 & ManualGeneratorのMac対応#9
AtaruMatsudaira wants to merge 5 commits intoruccho:masterfrom
AtaruMatsudaira:feat/ataru/misc

Conversation

@AtaruMatsudaira
Copy link

概要

SourceGeneratorが動作するUnityバージョンでもManual Generatorを利用したいモチベーションがあり、

  1. ユーザ定義によるSourceGenerartorを無効にするDefineSymbolの追加と適応
  2. Macにてdotnetのpath解決が失敗する不具合の修正
  3. dotnetのランタイムが.NET Core3.1以外でもManualGenerartorを動作させる

の対応を行いました。

補足

ユーザ定義によるSourceGenerartorを無効にするDefineSymbolの追加と適応

ProjectReflectionGeneratorによって生成される成果物はDefineの対象外としています。
Manual Generatorを利用したいシチュエーションが、ローカルにAnimatorControllerなどのアセットが存在せずリモートで取得する場合などを想定しているため、
TagやLayerはManualGenerartorによる生成を必要としないためです。

Macにてdotnetのpath解決が失敗する不具合の修正

下記記事を参考に対応させていただきました。

macOS の Unity エディタでは、(明示的にCLIから起動しない限り)シェルで使えるものとは異なる PATH が設定されています。

https://zenn.dev/ruccho/articles/a5b58dc2cfecd6

dotnetのランタイムが.NET Core3.1以外で動作させる

.NET Core3.1は既にサポートされていないための対応となります。
dotnetコマンドのオプションに --roll-forward Major を指定することでメジャーバージョンを無視して、DLL等を実行するコトができるようになります。

Comment on lines +75 to +84
if (roslynContext.Compilation.AssemblyName != "UniTyped")
{
result = $"""
#if !UNITYPED_USER_DEFINE_DISABLE_SOURCE_GENERATOR
{result}
#endif
""";
}


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manual GeneratorとSource Generatorの間で同一入力を与えれば同一のコードが生成される状態を維持したいので、生成コード側に #if をつけるより、Source Generatorの実行時に定義済みのシンボルを参照して生成結果を変えるようにしたいんですが、どうでしょうか?

ちなみにRoslynではSyntaxTree.OptionsCSharpParseOptionsにダウンキャストすると、定義済みのシンボルを引っ張ってくることができます。
https://learn.microsoft.com/ja-jp/dotnet/api/microsoft.codeanalysis.csharp.csharpparseoptions.preprocessorsymbolnames?view=roslyn-dotnet-4.3.0#microsoft-codeanalysis-csharp-csharpparseoptions-preprocessorsymbolnames

};
var fullProjectPath = Path.GetFullPath(item.projectPath);
var fullOutputFilePath = Path.GetFullPath(item.outputFile);
var args = $"--roll-forward Major {fullGeneratorPath} --project {fullProjectPath} --output {fullOutputFilePath}";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このオプション初めて知りました👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants