|
1 |
| -# Unity-AltSourceGenerator |
2 |
| -Ease-of-Use Source Generator Alternative for Unity. |
| 1 | +<!-- omit in toc --> |
| 2 | +Alternative Source Generator for Unity |
| 3 | +====================================== |
| 4 | + |
| 5 | +Alternative Source Generator is built on the Unity native functions. |
| 6 | + |
| 7 | +- ✅ Unity Package Manager Support |
| 8 | +- ✅ No Complicated IDE Environment Setup |
| 9 | +- ✅ No Additional Package Installation |
| 10 | + |
| 11 | + |
| 12 | +As you already know, Roslyn's source generator is too sophisticated. This framework provides more simple, ease of use and good enough functions for source code generation. |
| 13 | + |
| 14 | +<p><details lang="ja" --open><summary><small>日本語 / JA</small></summary> |
| 15 | + |
| 16 | +超簡単に使える Unity 向けソースジェネレーターです。 |
| 17 | + |
| 18 | +<!------- End of Details JA Tag -------></details></p> |
| 19 | + |
| 20 | + |
| 21 | +<p><details lang="en" --open><summary>Table of Contents</summary> |
| 22 | + |
| 23 | +- [Sample Code](#sample-code) |
| 24 | + - [Result](#result) |
| 25 | + - [Output Path](#output-path) |
| 26 | +- [Utility Functions for Build](#utility-functions-for-build) |
| 27 | +- [Technical Notes](#technical-notes) |
| 28 | + - [Naming Convention](#naming-convention) |
| 29 | +- [Installation](#installation) |
| 30 | +- [Editor Extensions](#editor-extensions) |
| 31 | +- [Copyright](#copyright) |
| 32 | +- [License](#license) |
| 33 | +- [Devnote](#devnote) |
| 34 | + |
| 35 | +<!------- End of Details EN Tag -------></details></p> |
| 36 | + |
| 37 | + |
| 38 | +# Sample Code |
| 39 | + |
| 40 | +Minimal implementation of source generator here. |
| 41 | + |
| 42 | +<p><details lang="ja" --open><summary><small>日本語 / JA</small></summary> |
| 43 | + |
| 44 | +最小限のソースジェネレーターの構成はこちら。`StringBuilder` が渡されるので書き込んで `true` を返せば `context.OutputPath` に内容を書き込みます。`false` を返せば書き込みを中止できます。 |
| 45 | + |
| 46 | +<!------- End of Details JA Tag -------></details></p> |
| 47 | + |
| 48 | + |
| 49 | +```csharp |
| 50 | +using System.Text; |
| 51 | +using SatorImaging.UnitySourceGenerator; |
| 52 | + |
| 53 | +[UnitySourceGenerator(OverwriteIfFileExists = false)] |
| 54 | +class MinimalGenerator |
| 55 | +{ |
| 56 | + static string OutputFileName() => "Test.cs"; // USG automatically update to -> Test.<ClassName>.g.cs |
| 57 | +
|
| 58 | + static bool Emit(USGContext context, StringBuilder sb) |
| 59 | + { |
| 60 | + // write content into passed StringBuilder. |
| 61 | + sb.AppendLine($"Asset Path: {context.AssetPath}"); |
| 62 | + sb.AppendLine($"Hello World from {typeof(MinimalGenerator)}"); |
| 63 | + |
| 64 | + // you can modify output path. initial file name is that USG updated. |
| 65 | + // NOTE: USG doesn't care the modified path is valid or not. |
| 66 | + context.OutputPath += "_MyFirstTest.txt"; |
| 67 | + |
| 68 | + // return true to tell USG to write content into OutputPath. false to do nothing. |
| 69 | + return true; |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +## Result |
| 77 | + |
| 78 | +USG automatically adds document tag at the beginning of generated file. You can remove this document tag by `sb.Clear()` in `Emit()` method. |
| 79 | + |
| 80 | + |
| 81 | +<p><details lang="ja" --open><summary><small>日本語 / JA</small></summary> |
| 82 | + |
| 83 | +書き出される内容は以下の通り。渡される `StringBuilder` の冒頭にはドキュメントタグが入ってます。 |
| 84 | + |
| 85 | +<!------- End of Details JA Tag -------></details></p> |
| 86 | + |
| 87 | +```csharp |
| 88 | +// <auto-generated>MinimalGenerator</auto-generated> |
| 89 | +Asset Path: Assets/Scripts/MinimalGenerator.cs |
| 90 | +Hello World from Sample.MinimalGenerator |
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +## Output Path |
| 96 | + |
| 97 | +USG creates `USG.g` folder next to generator script file. Resulting file path will be: |
| 98 | + |
| 99 | +- Assets/Scripts/<b>USG.g</b>/Test<b>.MinimalGenerator.g</b>.cs |
| 100 | + |
| 101 | +> NOTE: In above example, output path is modified so that resulting file name is `Test.MinimalGenerator.g.cs_MyFirstTest.txt` |
| 102 | +
|
| 103 | + |
| 104 | +<p><details lang="ja" --open><summary><small>日本語 / JA</small></summary> |
| 105 | + |
| 106 | +書き出し先は上記の通り。フォルダーとジェネレータークラス名が付与されます。 |
| 107 | + |
| 108 | +<!------- End of Details JA Tag -------></details></p> |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +# Utility Functions for Build |
| 114 | + |
| 115 | +There are utility functions to perform source code generation on build event. |
| 116 | + |
| 117 | +<p><details lang="ja" --open><summary><small>日本語 / JA</small></summary> |
| 118 | + |
| 119 | +`IPostprocessBuildWithReport` も実装しようかと思ったものの、ビルドイベントに勝手に処理追加するのはなんか訳わからんが動かんの原因だし、BuildReport として渡される全てのファイル名を走査する処理は効率も良くない。ということで。 |
| 120 | + |
| 121 | +<!------- End of Details JA Tag -------></details></p> |
| 122 | + |
| 123 | + |
| 124 | +```csharp |
| 125 | +// perform by known asset path. |
| 126 | +USGEngine.IgnoreOverwriteSettingByAttribute = true; // force overwrite |
| 127 | +USGEngine.ProcessFile(pathToGeneratorScriptFile); |
| 128 | + |
| 129 | +// search by class name. |
| 130 | +USGUtility.ForceGenerate(nameof(MinimalGenerator)); |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +# Technical Notes |
| 136 | + |
| 137 | +As of C# 9.0, it doesn't allow to define `abstract static` methods in interface, USG reports error when source generator class doesn't implement required static methods. |
| 138 | + |
| 139 | +<p><details lang="ja" --open><summary><small>日本語 / JA</small></summary> |
| 140 | + |
| 141 | +理想はアトリビュートとインターフェイスによるフィルタリングですが、Unity 2021 は C# 9.0 で `abstract static` を含んだインターフェイスが使えません。 |
| 142 | + |
| 143 | +しょうがないのでメソッドのシグネチャを確認して存在しなければエラーをコンソールに出します。 |
| 144 | + |
| 145 | +<!------- End of Details JA Tag -------></details></p> |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | +## Naming Convention |
| 153 | + |
| 154 | +- Generator class name and filename must be matched. |
| 155 | +- Class name must be unique in whole project. |
| 156 | +- Classes are ignored if defined in assembly which name starts with: |
| 157 | + - `Unity` (no trailing dot) |
| 158 | + - `System.` |
| 159 | + - `Mono.` |
| 160 | + |
| 161 | + |
| 162 | +<p><details lang="ja" --open><summary><small>日本語 / JA</small></summary> |
| 163 | + |
| 164 | +- ジェネレータークラスの名前はファイル名と一致 |
| 165 | +- ジェネレータクラスの名前はプロジェクト内で一意 |
| 166 | +- クラスが以下で始まる名前のアセンブリで宣言されている場合は対象としない |
| 167 | + - `Unity` (末尾ドット無し) |
| 168 | + - `System.` |
| 169 | + - `Mono.` |
| 170 | + |
| 171 | +<!------- End of Details JA Tag -------></details></p> |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | +# Installation |
| 176 | + |
| 177 | +Use the following git URL in Unity Package Manager (UPM). |
| 178 | + |
| 179 | +- Latest: https://github.com/sator-imaging/Unity-AltSourceGenerator.git |
| 180 | +- v1.0.0: https://github.com/sator-imaging/Unity-AltSourceGenerator.git#v1.0.0 |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +# Editor Extensions |
| 185 | + |
| 186 | +<p><details lang="ja" --open><summary><small>日本語 / JA</small></summary> |
| 187 | + |
| 188 | +手動でソースコード生成イベントの発火も可能です。「ジェネレーターのスクリプトファイル」か「生成されたファイル」を選択して、Project ウインドウで `Reimport` か `Unity Source Generator` 以下のメニューを実行します。 |
| 189 | + |
| 190 | +`Force Generate...` はクラスアトリビュートの設定に関わらず強制的に上書き生成します。 |
| 191 | + |
| 192 | +<!------- End of Details JA Tag -------></details></p> |
| 193 | + |
| 194 | +There is an ability to invoke source code generation by hand. With generator script file or generated file selected in Project window: |
| 195 | + |
| 196 | +- `Reimport` |
| 197 | + - This command respects `OverwriteIfFileExists` setting by generator class attribute. |
| 198 | + |
| 199 | +- `Unity Source Generator > Force Generate` |
| 200 | + - This command will force re-generate source code even if overwrite setting is disabled. |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | +Copyright |
| 210 | +========= |
| 211 | + |
| 212 | +Copyright © 2023 Sator Imaging, all rights reserved. |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +License |
| 217 | +======= |
| 218 | + |
| 219 | +<p> |
| 220 | +<details> |
| 221 | +<summary>The above copyright notice and this permission notice shall be included in all |
| 222 | +copies or substantial portions of the Software.</summary> |
| 223 | + |
| 224 | +```text |
| 225 | +MIT License |
| 226 | +
|
| 227 | +Copyright (c) 2023 Sator Imaging |
| 228 | +
|
| 229 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 230 | +of this software and associated documentation files (the "Software"), to deal |
| 231 | +in the Software without restriction, including without limitation the rights |
| 232 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 233 | +copies of the Software, and to permit persons to whom the Software is |
| 234 | +furnished to do so, subject to the following conditions: |
| 235 | +
|
| 236 | +The above copyright notice and this permission notice shall be included in all |
| 237 | +copies or substantial portions of the Software. |
| 238 | +
|
| 239 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 240 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 241 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 242 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 243 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 244 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 245 | +SOFTWARE. |
| 246 | +``` |
| 247 | + |
| 248 | +</details> |
| 249 | +</p> |
| 250 | + |
| 251 | + |
| 252 | + |
| 253 | + |
| 254 | + |
| 255 | + |
| 256 | +# Devnote |
| 257 | + |
| 258 | +Unity doesn't invoke import event if Visual Studio is not launch by current session of Unity...? |
0 commit comments