You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+33-29
Original file line number
Diff line number
Diff line change
@@ -32,33 +32,35 @@ Our templates use a [Hybrid model](https://github.com/Drizin/CodegenCS/tree/mast
32
32
33
33
To sum our hybrid model provides the best of both worlds: you can write templates using your favorite language (C#), in your favorite IDE (Visual Studio) with full **debugging** capabilities - and you can **leverage the power of .NET** and any **.NET library** (think LINQ, Dapper, Newtonsoft, Swashbuckle, RestSharp, Humanizer, etc.)
34
34
35
-
## Entrypoint and subtemplates
35
+
## Template Entrypoint
36
36
37
-
Templates are C# programs so they need an entrypoint (`Main()` or `TemplateMain()`).
37
+
Templates are C# programs so they should have an entrypoint method (named `Main()` or `TemplateMain()`).
38
38
39
-
Entry-points can be markup-based (they just have to return an interpolated string):
39
+
Methods can be "markup-based" which means that they just return an interpolated string:
40
40
41
41
```cs
42
42
classMyTemplate
43
43
{
44
44
FormattableStringMain() =>$"My first template";
45
45
}
46
46
```
47
-
... or they can be programmatic:
47
+
Or they can be programmatic:
48
48
49
49
```cs
50
50
classMyTemplate
51
51
{
52
52
voidMain(ICodegenOutputFilewriter)
53
53
{
54
-
writer.WriteLine($"My first template");
54
+
writer.Write($"My first template");
55
55
}
56
56
}
57
57
```
58
58
59
59
## Subtemplates (aka "partials")
60
60
61
-
In regular C# string interpolation you can only interpolate types that can be directly converted to string (`string`, `FormattableString`, `int`, `DateTime`, etc.). In our hybrid model you can interpolate many other types including delegates (methods) so it's very easy to "embed" a subtemplate (a different method) right within an interpolated string.
61
+
In a programmatic method it's pretty obvious that you can just invoke other methods to break down a template into smaller (and more organized blocks), but when you have a markup-based block (i.e. a large string) it's not so obvious how you can include a "subtemplate":
62
+
63
+
In regular C# string interpolation we can only interpolate types that can be directly converted to string (`string`, `FormattableString`, `int`, `DateTime`, etc.), but in our magic TextWriter allows interpolating many other types including delegates (methods) so it's very easy to "embed" a subtemplate (a different method) right within an interpolated string.
62
64
63
65
In the example below template starts with a markup entry-point and then it "escapes" from the markup and switches to a programmatic subtemplate (another method that might have more complex logic):
64
66
@@ -87,6 +89,10 @@ class MyTemplate
87
89
}
88
90
```
89
91
92
+
(`WriteClass` method will get the `ICodegenOutputFile` parameter automatically injected, as explained later).
93
+
94
+
Switching back and forth between programmatic-mode (C# methods) or markup mode (large C# strings) is what enables our hybrid model.
95
+
90
96
## Indentation Control
91
97
92
98
Our clever [Indent Control](https://github.com/Drizin/CodegenCS/tree/master/docs/Indent-Control.md) automatically captures the current indent (whatever number of spaces or tabs you have in current line) and will preserve it when you interpolate a large object (multiline) or when you interpolate a subtemplate.
@@ -95,37 +101,29 @@ Generating code with the correct indentation (even if there are multiple nested
95
101
96
102
Explicit indent control is also supported.
97
103
104
+
98
105
## Command-line Tool (dotnet-codegencs)
99
106
100
-
[dotnet-codegencs](https://github.com/Drizin/CodegenCS/tree/master/src/Tools/dotnet-codegencs/) is a cross-platform .NET tool (command-line tool) that can be used to download, **build**, and **run templates**.
101
-
It can also be used to extract models (reverse engineer schema) from existing databases.
102
-
In other words this is our **"batteries-included"** tool, and probably the best place to get started.
103
-
It's available for Windows/Linux/MacOS.
107
+
[dotnet-codegencs](https://github.com/Drizin/CodegenCS/tree/master/src/Tools/dotnet-codegencs/) is a cross-platform .NET tool (command-line tool).
108
+
It's available for Windows/Linux/MacOS.
109
+
110
+
Besides running templates, it can also be used to extract models from existing databases (**reverse engineer db schema**), or download our our [sample templates](https://github.com/CodegenCS/Templates).
111
+
So if you want to quickly start generating code (e.g. based on your database) this is our **"batteries-included"** tool.
112
+
113
+
You can run it manually or you can automate into your build pipeline. See [this example](/Samples/PrebuildEvent/RunTemplates.ps1) of a prebuild script that will install dotnet-codegencs, refresh a database schema, and run a template that generates POCOs.
104
114
105
115
## Visual Studio Extension
106
116
107
-
Our [Visual Studio Extension](https://github.com/Drizin/CodegenCS/tree/master/src/VisualStudio/) allows **building and running templates** directly from Visual Studio.
108
-
Output files are automatically added to the project (nested under the template item), so it's easy to use (but it doesn't have all features available in `dotnet-codegencs`).
117
+
Our [Visual Studio Extension](https://github.com/Drizin/CodegenCS/tree/master/src/VisualStudio/) allows running templates directly from Visual Studio.
109
118
It's available for [Visual Studio 2022](https://marketplace.visualstudio.com/items?itemName=Drizin.CodegenCS) or [Visual Studio 2019/2017](https://marketplace.visualstudio.com/items?itemName=Drizin.CodegenCS-Compatibility).
110
119
120
+
Output files are automatically added to the project (nested under the template item), so it's easy to use (but it doesn't have all features available in `dotnet-codegencs`).
121
+
111
122
## Roslyn Source Generator
112
123
113
-
Our [Source Generator](https://nuget.org/packages/CodegenCS.SourceGenerator) allows running templates on-the-fly during compilation.
124
+
Our [Source Generator](https://github.com/Drizin/CodegenCS/tree/master/src/SourceGenerator/) (nuget[here](https://nuget.org/packages/CodegenCS.SourceGenerator)) allows running templates on-the-fly during compilation.
114
125
It's possible to render physical files on disk or just render in-memory (no need to add to source-control or put into ignore lists).
115
126
116
-
How to use:
117
-
1. Install nuget CodegenCS.SourceGenerator to your project
<!-- Use "Memory" or "File", where "File" will save the output files to disk -->
124
-
<!-- you can include as many templates as you want -->
125
-
</ItemGroup>
126
-
```
127
-
3. If you want to augment on existing classes (using Roslyn Syntax Tree), just inject GeneratorExecutionContext (see [Template3.csx](/src/SourceGenerator/SampleProjectWithSourceGenerator/Template3.csx) example).
128
-
129
127
## Important Classes
130
128
131
129
Before explaining more features it's important to first learn about 3 important classes:
- To start using the command-line tool, check out [dotnet-codegencs Quickstart](https://github.com/Drizin/CodegenCS/tree/master/src/Tools/dotnet-codegencs#quickstart)
1141
1139
- To start using the Visual Studio Extension, check out [Visual Studio Extension Quickstart](https://github.com/Drizin/CodegenCS/tree/master/src/VisualStudio#quickstart)
1140
+
- To start using the Source Generator, check out [Roslyn Source Generator Quickstart](https://github.com/Drizin/CodegenCS/tree/master/src/SourceGenerator#quickstart)
1142
1141
- To generate code based on a database, check out [DbSchema Quickstart](https://github.com/Drizin/CodegenCS/tree/master/src/Models/CodegenCS.Models.DbSchema#quickstart)
1143
1142
- To generate code based on a REST API, check out [NSwagAdapter Quickstart](https://github.com/Drizin/CodegenCS/tree/master/src/Models/CodegenCS.Models.NSwagAdapter#quickstart)
1144
1143
- Our [Templates repository (https://github.com/CodegenCS/Templates)](https://github.com/CodegenCS/Templates) contains some **sample fully-functional templates** that you can use, customize, or use as a reference for building your own templates.
Check out [CodegenCS vs T4 Templates](https://github.com/Drizin/CodegenCS/tree/master/docs/Comparison-T4.md)
1197
1196
(Spoiler: CodegenCS is much much better)
1198
1197
1199
-
## How does CodegenCS compare to Roslyn?
1198
+
## How does CodegenCS compare to Roslyn Source Generators?
1199
+
1200
+
Source Generators are only required when you're building code based on existing code.
1201
+
CodegenCS has a Source Generator plugin that can be used to invoke templates, and those templates will also have access to `GeneratorExecutionContext` (to read the syntax trees) - so you don't have to write your own Source Generator (which is not an easy task).
1202
+
1203
+
But if you're not building code based on existing code (e.g. if you're generating based on a database or a json file) then you don't need a Source Generator - you can just use our command-line tool `dotnet-codegencs` to run our templates from your prebuild scripts.
1204
+
1205
+
For more details check out [CodegenCS vs Roslyn Source Generators](https://github.com/Drizin/CodegenCS/tree/master/docs/Comparison-Roslyn.md)
1200
1206
1201
-
Check out [CodegenCS vs Roslyn](https://github.com/Drizin/CodegenCS/tree/master/docs/Comparison-Roslyn.md)
1202
-
(Spoiler: That's not an apples-to-apples comparison. Roslyn should be used to analyze compilation syntax-trees and eventually generate code **based** on that syntax tree - but generating code using syntax-trees is painful and nonsense. CodegenCS was created for generating human-readable code and let the hard-work of syntax trees be done by a compiler).
1203
1207
1204
1208
## Why yet another Code Generator? Why not T4, Liquid, Razor, etc?
0 commit comments