Using WriteCodeFragment MSBuild Task
Asked Answered
A

1

5

I am trying to use the WriteCodeFragment MSBuild task to create an AssemblyVersion attribute. I'm having a problem creating a property group to correctly pass the ITaskItem array required for processing. Can someone help with an example?

Apposition answered 29/11, 2010 at 2:7 Comment(0)
A
18

This creates a file called BuildVersion.cs with an AssemblyVersion attribute of 123.123.123.123. If OutputFile is removed then a randomly generated file name will be used instead. The Compile item name automatically adds the item to the Compile items (includes BuildVersion.cs in the build). The FileWrites item name allows the file to be removed during Clean.

<Target Name="BeforeBuild">
  <ItemGroup>
     <AssemblyAttributes Include="AssemblyVersion">
       <_Parameter1>123.123.123.123</_Parameter1>
     </AssemblyAttributes>
  </ItemGroup>
  <WriteCodeFragment AssemblyAttributes="@(AssemblyAttributes)"
                     Language="C#"
                     OutputDirectory="$(IntermediateOutputPath)"
                     OutputFile="BuildVersion.cs">
    <Output TaskParameter="OutputFile" ItemName="Compile" />
    <Output TaskParameter="OutputFile" ItemName="FileWrites" />
  </WriteCodeFragment>
</Target>
Apposition answered 29/11, 2010 at 17:11 Comment(6)
Thanks, this helped me. It's worth noting also that you need to add BuildVersion.cs to the Compile item as well. <ItemGroup><Compile Include="BuildVersion.cs" /></ItemGroup>Lalittah
I've updated the code to automatically add the output to the Compile items.Huelva
In a C# project, adding Output TaskParameter="OutputFile" ItemName="FileWrites" /> to the content of the WriteCodeFragment element lets the file be removed upon Clean.Improvement
Without specifying OutputFile="BuildVersion.cs", the task will generate a new unique file name each time it is called.Improvement
How can I do this but in .net core 3.1?Mud
how would you add [assembly: InternalsVisibleTo("Namespace.OfYourUnitTest.Project")] if you needed to add testing of internals when using moq to mock up a dependancy? You cant keep making everything public :/Sidle

© 2022 - 2024 — McMap. All rights reserved.