Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit 06d31db

Browse files
authored
Merge pull request #82 from amis92/patch-1
Update README for v0.4.49
2 parents d45d9fc + 92e1588 commit 06d31db

File tree

1 file changed

+31
-42
lines changed

1 file changed

+31
-42
lines changed

README.md

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,30 @@ This includes design-time support, such that code generation can respond to
88
changes made in hand-authored code files by generating new code that shows
99
up to Intellisense as soon as the file is saved to disk.
1010

11+
## Table of Contents
12+
13+
* [How to write your own code generator][]
14+
* [Apply code generation][]
15+
* [Developing your code generator][]
16+
* [Packaging up your code generator for others' use][]
17+
1118
## How to write your own code generator
19+
[How to write your own code generator]: #how-to-write-your-own-code-generator
1220

1321
In this walkthrough, we will define a code generator that replicates any class
1422
your code generation attribute is applied to, but with a suffix appended to its name.
1523

1624
### Define code generator
25+
[Define code generator]: #define-code-generator
1726

18-
This must be done in a library that targets netstandard1.5 or net462 (or later).
27+
This must be done in a library that targets netstandard1.6 or net461 (or later;
28+
net461 is supported if you have .NET Core SDK v2.0+ installed, [see docs for details][netstandard-table]).
1929
Your generator cannot be defined in the same project that will have code generated
2030
for it because code generation runs *before* the receiving project is itself compiled.
2131

2232
Install the [CodeGeneration.Roslyn][NuPkg] NuGet Package.
2333

24-
Define the generator class:
34+
Define the generator class (*note: constructor accepting `AttributeData` parameter is required*):
2535

2636
```csharp
2737
using CodeGeneration.Roslyn;
@@ -67,7 +77,7 @@ public class DuplicateWithSuffixGenerator : ICodeGenerator
6777
To activate your code generator, you need to define an attribute that can be
6878
applied to the class to be copied. This attribute may be defined in the same
6979
assembly as defines your code generator, but since your code generator must
70-
be defined in a netstandard1.3 or net46 library, this may limit which projects
80+
be defined in a netstandard1.6+ or net461+ library, this may limit which projects
7181
can apply your attribute. So define your attribute in another assembly if
7282
it must be applied to projects that target older platforms.
7383

@@ -76,7 +86,7 @@ If your attributes are in their own project, you must install the
7686

7787
Define your attribute class.
7888
For this walkthrough, we will assume that the attributes are defined in the same
79-
netstandard1.3 project that defines the generator which allows us to use the more
89+
netstandard1.6 project that defines the generator which allows us to use the more
8090
convenient `typeof` syntax when declaring the code generator type.
8191
If the attributes and code generator classes were in separate assemblies, you must
8292
specify the assembly-qualified name of the generator type as a string instead.
@@ -110,10 +120,13 @@ instead as just a compile-time hint to code generation, and allowing you to not
110120
with a dependency on your code generation assembly.
111121

112122
## Apply code generation
123+
[Apply code generation]: #apply-code-generation
113124

114125
The attribute may not be applied in the same assembly that defines the generator.
115126
This is because the code generator must be compiled in order to execute before compiling
116-
the project that applies the attribute.
127+
the project that applies the attribute. Also, the consuming project (where the code
128+
will be generated) must use SDK-style csproj, which implies using VS2017+ or `dotnet`
129+
CLI tooling (VS Code with omnisharp, for example).
117130

118131
Applying code generation is incredibly simple. Just add the attribute on any type
119132
or member supported by the attribute and generator you wrote. Note you will need to
@@ -128,7 +141,14 @@ public class Foo
128141

129142
Install the [CodeGeneration.Roslyn.BuildTime][BuildTimeNuPkg] package into the
130143
project that uses your attribute. You may set `PrivateAssets="all"` on this reference
131-
because this is a build-time only package.
144+
because this is a build-time only package. You must also add this item to an `<ItemGroup>` in the project
145+
that will execute the code generator as part of your build:
146+
147+
```xml
148+
<DotNetCliToolReference Include="dotnet-codegen" Version="0.4.12" />
149+
```
150+
151+
You should adjust the version in the above xml to match the version of this tool you are using.
132152

133153
You can then consume the generated code at design-time:
134154

@@ -146,43 +166,23 @@ If you execute Go To Definition on it, Visual Studio will open the generated cod
146166
that actually defines `FooA`, and you'll notice it's exactly like `Foo`, just renamed
147167
as our code generator defined it to be.
148168

149-
### Using `dotnet build`
150-
151-
If you build with `dotnet build`, you need to take a couple extra steps. First, define a `nuget.config` file with an extra package source:
152-
153-
```xml
154-
<?xml version="1.0" encoding="utf-8"?>
155-
<configuration>
156-
<packageSources>
157-
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
158-
<add key="corefxlab" value="https://dotnet.myget.org/F/dotnet-corefxlab/api/v3/index.json" />
159-
</packageSources>
160-
</configuration>
161-
```
162-
163-
Second, add this item to an `<ItemGroup>` in the project that will execute the code generator as part of your build:
164-
165-
```xml
166-
<DotNetCliToolReference Include="dotnet-codegen" Version="0.4.12" />
167-
```
168-
169-
You should adjust the version in the above xml to match the version of this tool you are using.
170-
171169
### Shared Projects
172170

173171
When using shared projects and partial classes across the definitions of your class in shared and platform projects:
174172

175173
* The code generation attributes should be applied only to the files in the shared project
176174
(or in other words, the attribute should only be applied once per type to avoid multiple generator invocations).
177-
* The MSBuild:GenerateCodeFromAttributes custom tool must be applied to every file we want to auto generate code from.
175+
* The `MSBuild:GenerateCodeFromAttributes` custom tool must be applied to every file we want to auto generate code from.
178176

179177
## Developing your code generator
178+
[Developing your code generator]: #developing-your-code-generator
180179

181180
Your code generator can be defined in a project in the same solution as the solution with
182181
the project that consumes it. You can edit your code generator and build the solution
183182
to immediately see the effects of your changes on the generated code.
184183

185184
## Packaging up your code generator for others' use
185+
[Packaging up your code generator for others' use]: #packaging-up-your-code-generator-for-others-use
186186

187187
You can also package up your code generator as a NuGet package for others to install
188188
and use. Your NuGet package should include a dependency on the `CodeGeneration.Roslyn.BuildTime`
@@ -210,7 +210,7 @@ For example your package should have a `build\MyPackage.targets` file with this
210210
Then your package should also have a `tools` folder that contains your code generator and any of the runtime
211211
dependencies it needs *besides those delivered by the `CodeGeneration.Roslyn.BuildTime` package*.
212212

213-
Your attributes assembly should be placed under your package's `lib` folder` so consuming projects
213+
Your attributes assembly should be placed under your package's `lib` folder so consuming projects
214214
can apply those attributes.
215215

216216
Your consumers should depend on your package, and the required dotnet CLI tool,
@@ -223,21 +223,10 @@ so that the MSBuild Task can invoke the `dotnet codegen` command line tool:
223223
</ItemGroup>
224224
```
225225

226-
Also, any consumer must have a nuget.config file with at least this content:
227-
228-
```xml
229-
<?xml version="1.0" encoding="utf-8"?>
230-
<configuration>
231-
<packageSources>
232-
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
233-
<add key="corefxlab" value="https://dotnet.myget.org/F/dotnet-corefxlab/api/v3/index.json" />
234-
</packageSources>
235-
</configuration>
236-
```
237-
238226
Make sure that the DotNetCliToolReference version matches the version of the
239227
`CodeGeneration.Roslyn` package your package depends on.
240228

241229
[NuPkg]: https://nuget.org/packages/CodeGeneration.Roslyn
242230
[BuildTimeNuPkg]: https://nuget.org/packages/CodeGeneration.Roslyn.BuildTime
243231
[AttrNuPkg]: https://nuget.org/packages/CodeGeneration.Roslyn.Attributes
232+
[netstandard-table]: https://docs.microsoft.com/dotnet/standard/net-standard#net-implementation-support

0 commit comments

Comments
 (0)