I got inspired by a post on stackoverflow to automatically generate Versions for my project output. However this does not work for nuget packages.
I attached all relevant config files and log output. I tried different targets in the BeforeTargets argument in the definition of the SetVersion-target. The logs show that the target is also executed before the nugetpackage is created. Also the version properties get set via the executed command.
.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;</TargetFrameworks>
<Nullable>enable</Nullable>
<LangVersion>9</LangVersion>
</PropertyGroup>
<PropertyGroup>
<AfterTransform></AfterTransform>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>SourceGenerator</PackageId>
<Authors>author</Authors>
<Company>company</Company>
</PropertyGroup>
<ItemGroup>
<PackageReference ...>
</PackageReference>
</ItemGroup>
<!-- to be sure code generator output is in correct output dir -->
<ItemGroup>
<None Include="$(OutputPath)\netstandard2.0\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>
</Project>
directory.build.probs:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project>
<Target Name="SetVersion" BeforeTargets="InitializeSourceControlInformation" Condition=" '$(OS)' == 'Windows_NT' ">
<Message Text="The command was executed '$(VersionNumber)'" Importance="high" />
<Message Text="The command was executed '$(Version)'" Importance="high" />
<Message Text="The command was executed '$(AssemblyVersion)'" Importance="high" />
<Message Text="The command was executed '$(FileVersion)'" Importance="high" />
<Message Text="The command was executed '$(PackageVersion)'" Importance="high" />
<Exec
Command="git describe --tags --match 'Version-[0-9].[0-9].[0-9]' --abbrev=6 | cut -d '-' -f 2,3 | tr '-' '.'"
ConsoleToMSBuild="True"
IgnoreExitCode="False" >
<Output PropertyName="VersionNumber" TaskParameter="ConsoleOutput"/>
<Output PropertyName="Version" TaskParameter="ConsoleOutput"/>
<Output PropertyName="AssemblyVersion" TaskParameter="ConsoleOutput"/>
<Output PropertyName="FileVersion" TaskParameter="ConsoleOutput"/>
<Output PropertyName="PackageVersion" TaskParameter="ConsoleOutput"/>
</Exec>
<Message Text="The command was executed '$(VersionNumber)'" Importance="high" />
<Message Text="The command was executed '$(Version)'" Importance="high" />
<Message Text="The command was executed '$(AssemblyVersion)'" Importance="high" />
<Message Text="The command was executed '$(FileVersion)'" Importance="high" />
<Message Text="The command was executed '$(PackageVersion)'" Importance="high" />
</Target>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<VersionSuffix>debug</VersionSuffix>
</PropertyGroup>
</Project>
Build Output:
Rebuild started...
1>------ Rebuild All started: Project: SourceGenerator, Configuration: Debug Any CPU ------
Restored C:\Users\user\source\repos\trading\C_Hashtag\Tools\SourceGenerator\SourceGenerator.csproj (in 79 ms).
1>The command was executed ''
1>The command was executed '1.0.0-debug'
1>The command was executed '1.0.0.0'
1>The command was executed '1.0.0.0'
1>The command was executed '1.0.0-debug'
1>0.0.1.3
1>The command was executed '0.0.1.3'
1>The command was executed '0.0.1.3'
1>The command was executed '0.0.1.3'
1>The command was executed '0.0.1.3'
1>The command was executed '0.0.1.3'
1>SourceGenerator -> C:\Users\user\source\repos\SourceGenerator\bin\Debug\netstandard2.0\SourceGenerator.dll
1>Successfully created package 'C:\Users\user\source\repos\SourceGenerator\bin\Debug\SourceGenerator.1.0.0-debug.nupkg'.
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
as you can see all Version properties get set, however the output file has the wrong filename bin\Debug\SourceGenerator.1.0.0-debug.nupkg'
I think the generation of ´obj\Debug\SourceGenerator.1.0.0-debug.nuspec` already is wrong:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>SourceGenerator</id>
<version>1.0.0-debug</version>
<authors>author</authors>
<description>Package Description</description>
<dependencies>
<group targetFramework=".NETStandard2.0">
<dependency id="Microsoft.CodeAnalysis.CSharp" version="3.11.0" exclude="Build,Analyzers" />
<dependency id="Microsoft.CodeAnalysis.Common" version="3.11.0" exclude="Build,Analyzers" />
<dependency id="Microsoft.CodeAnalysis.Compilers" version="3.11.0" exclude="Build,Analyzers" />
</group>
</dependencies>
</metadata>
<files>
<file src="C:\Users\user\source\repos\SourceGenerator\bin\Debug\netstandard2.0\SourceGenerator.dll" target="lib\netstandard2.0\SourceGenerator.dll" />
<file src="C:\Users\user\source\repos\SourceGenerator\bin\Debug\netstandard2.0\SourceGenerator.dll" target="analyzers\dotnet\cs\SourceGenerator.dll" />
</files>
</package>