Skip to content

Commit a73e1b3

Browse files
committed
Add containing project and test info
1 parent 77d00cb commit a73e1b3

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

docs/msbuild/customize-builds-for-generated-files.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Files generated during execution don't exist during the evaluation phase, theref
3232

3333
<ItemGroup>
3434
<!-- If your generated file was placed in `obj\` -->
35-
<None Include="$(IntermediateOutputPath)GeneratedFile.cs" CopyToOutputDirectory="PreserveNewest"/>
35+
<None Include="$(IntermediateOutputPath)GeneratedFile.cs" TargetPath="GeneratedFile.cs" CopyToOutputDirectory="PreserveNewest"/>
3636
<!-- If you know exactly where that file is going to be, you can hard code the path. -->
3737
<None Include="some\specific\path\my-generatedfile" CopyToOutputDirectory="PreserveNewest"/>
3838

@@ -49,6 +49,25 @@ Files generated during execution don't exist during the evaluation phase, theref
4949

5050
Adding your generated file to `None` or `Content` is sufficient for the build process to see it. You also want to ensure it gets added at the right time. Ideally, your target runs before `BeforeBuild`. `AssignTargetPaths` is another possible target, as it is the final opportunity to modify `None` and `Content` items (among others) before they are transformed into new items. See [Common Item Types](common-msbuild-project-items.md).
5151

52+
If you put the above into a file and call it `buildcodegen.targets`, you can run `dotnet new console`, import the file, and then build it to test everything:
53+
54+
```xml
55+
<Project Sdk="Microsoft.NET.Sdk">
56+
<Import Project="buildcodegen.targets"/>
57+
<PropertyGroup>
58+
<OutputType>Exe</OutputType>
59+
<TargetFramework>net9.0</TargetFramework>
60+
<ImplicitUsings>enable</ImplicitUsings>
61+
<Nullable>enable</Nullable>
62+
</PropertyGroup>
63+
64+
</Project>
65+
```
66+
67+
Run *msbuild.exe* and look at the output to verify that your file was generated and copied to the output folder. You can use *ildasm.exe* to confirm that your output binaries include the generated code `MyEnum`:
68+
69+
`ildasm CodeGen.dll`
70+
5271
## Next steps
5372

5473
This example could be improved to support more realistic use cases. For example, to support [incremental builds](./incremental-builds.md) when the generated code depends on an input file, `Inputs` and `Outputs` should be provided to the target. Such a target would only regenerate the file if the date of the input file or files is more recent than the output file. Often when customizing for code generation, it's recommended to create a custom task. See [Create a custom task for code generation](./tutorial-custom-task-code-generation.md).

0 commit comments

Comments
 (0)