|
| 1 | +# Hacking |
| 2 | + |
| 3 | +FIRST, read how to make [contributions](CONTRIBUTIONS.md). |
| 4 | + |
| 5 | +Now some tips on hacking in the DOTS repo. |
| 6 | + |
| 7 | +## T4 |
| 8 | + |
| 9 | +In our codebase we may have one or more files with a `.tt` extension. These are templates for the [Text Template Transformation Toolkit (T4)](https://en.wikipedia.org/wiki/Text_Template_Transformation_Toolkit) to generate code from. T4 was originally just a [Microsoft Visual Studio tool](https://docs.microsoft.com/en-us/visualstudio/modeling/generating-files-with-the-texttransform-utility), but there's now a [Mono version](https://github.com/mono/t4) that runs on .NET Core. |
| 10 | + |
| 11 | +### Configure Your Tools |
| 12 | + |
| 13 | +#### Visual Studio |
| 14 | + |
| 15 | +This is probably the best T4 experience. T4 processing is built in, you can get dual-language colorizing and completion, and hitting ctrl-s quickly runs codegen. |
| 16 | + |
| 17 | +<img src='Documentation~/images/t4-ide.png' alt='A nice VS experience' style='width:500px'/> |
| 18 | + |
| 19 | +Two extension options for richer T4 support: |
| 20 | + |
| 21 | + 1. Best: [ReSharper](https://www.jetbrains.com/resharper) + [ForTea](https://plugins.jetbrains.com/plugin/11634-fortea) (install from ReSharper|Extension Manager) |
| 22 | + 2. OK: [tangible T4 Editor](https://t4-editor.tangible-engineering.com/T4-Editor-Visual-T4-Editing.html) |
| 23 | + |
| 24 | +__Important__: Unity doesn't understand .tt files, and when it generates .csproj's it doesn't include any markup for them. So VS thinks of it as an ordinary text file and you don't get auto-codegen. |
| 25 | + |
| 26 | +To work around that, run this from Powershell in your DOTS repo: `Tools/Fix-ProjectsForT4 Samples/Samples.sln`. This will add the necessary markup. You'll need to run this _every time Unity regenerates the csproj's_, so keep it handy. |
| 27 | + |
| 28 | +Ask Powershell `help Tools/Fix-ProjectsForT4` for important info on using T4+Unity effectively in Visual Studio. |
| 29 | + |
| 30 | +#### Visual Studio Code |
| 31 | + |
| 32 | +VSCode does a passable job at T4. With a couple extensions, you get syntax highlighting, auto-regen-on-save, but no completion. |
| 33 | + |
| 34 | +To set up VSCode, run this from a command shell: |
| 35 | + |
| 36 | +```sh |
| 37 | +code --install-extension zbecknell.t4-support |
| 38 | +code --install-extension aisoftware.tt-processor |
| 39 | +``` |
| 40 | + |
| 41 | +One does colorizing, the other does auto-regen. You'll also need to go to your settings.json and add an entry for your T4 processor. |
| 42 | + |
| 43 | +```json |
| 44 | +"ttProcessor.TTPath": "path/to/my/t4", |
| 45 | +``` |
| 46 | + |
| 47 | +(But.. _"Wait, how do I get a T4 processor??"_ See instructions in "All Other Editors" below.) |
| 48 | + |
| 49 | +#### Rider (And Other Editors) |
| 50 | + |
| 51 | +For other editors, you'll have to either run the T4 processor manually on the command line, or configure an External Tool to do it. |
| 52 | + |
| 53 | +First, get a T4 processor: |
| 54 | + |
| 55 | +* Windows: you already should have a `TextTransform.exe` in a Visual Studio under `Common7\IDE` |
| 56 | +* All: [mono/t4](https://github.com/mono/t4) is available, actively maintained, and works in .NET or .NET Core. To install it, make sure you have .NET Core SDK [installed](https://dotnet.microsoft.com/download), and then run `dotnet tool install -g dotnet-t4`. |
| 57 | + |
| 58 | +Now you can just run either `TextTransform` or `t4` manually, passing in the path to the .tt file. Your editor probably has a way to run shell commands or "external tools", which you can configure to run the T4 processor and maybe even bind to a hotkey. |
| 59 | + |
| 60 | +For example, to add to Rider: |
| 61 | + |
| 62 | +* File|Settings -> Tools\External Tools |
| 63 | +* `+` to add |
| 64 | + * Name = Run T4 Codegen |
| 65 | + * Program = `path/to/my/t4` |
| 66 | + * Arguments = `$FilePath$` |
| 67 | + * Check boxes in Advanced Options for "Synchronize files", "Open console", and "Make active on stderr" |
| 68 | + |
| 69 | +Now you can run codegen using Tools|External Tools -> Run T4 Codegen. Not automatic, no codegen, but better than nothing..? |
| 70 | + |
| 71 | +(Note that Rider supposedly wants to engineer their own T4 implementation, go [vote for it](https://youtrack.jetbrains.com/issue/RIDER-5245)) |
| 72 | + |
| 73 | +### Adding New T4 Files |
| 74 | + |
| 75 | +Do the following to add a new T4 file to Unity: |
| 76 | + |
| 77 | + 1. Create a new .tt file however you want |
| 78 | + 2. Copy-paste in the text from the **Standard T4 Template** below |
| 79 | + 3. Go to Unity, let it notice the new .tt file and update the generated csproj so your editor sees it |
| 80 | + 4. Visual Studio users: also run `Tools/Fix-ProjectsForT4 -RunCodeGen Samples/Samples.sln` (for more info, see "Configure Your Tools" above) |
| 81 | + |
| 82 | +#### Standard T4 Template |
| 83 | + |
| 84 | +When creating a new .tt file, copy this into the top to get started. |
| 85 | + |
| 86 | +```xml |
| 87 | +<#/*THIS IS A T4 FILE - see HACKING.md for what it is and how to run codegen*/#> |
| 88 | +<#@ output extension=".gen.cs" #> |
| 89 | +<#@ assembly name="System.Collections" #> |
| 90 | +<#@ assembly name="System.Core" #> |
| 91 | +<#@ import namespace="System.Collections.Generic" #> |
| 92 | +//------------------------------------------------------------------------------ |
| 93 | +// <auto-generated> |
| 94 | +// This code was generated by a tool. |
| 95 | +// |
| 96 | +// Changes to this file may cause incorrect behavior and will be lost if |
| 97 | +// the code is regenerated. |
| 98 | +// </auto-generated> |
| 99 | +//------------------------------------------------------------------------------ |
| 100 | +``` |
| 101 | + |
| 102 | +Note that the assemblies list _needs to work in both .NET and .NET Core_ so we can run on multiple desktop platforms. Keep this in mind as you add more references. |
0 commit comments