cake-build / cake

:cake: Cake (C# Make) is a cross platform build automation system.
https://cakebuild.net
MIT License
3.91k stars 729 forks source link

Add Version, AssemblyVersion, FileVersion, and AssemblyInformationalVersion properties to DotNetCoreMSBuildSettings #3449

Closed augustoproiete closed 3 years ago

augustoproiete commented 3 years ago

Discussed in https://github.com/cake-build/cake/discussions/3448

Originally posted by **augustoproiete** August 31, 2021 Similar to https://github.com/cake-build/cake/discussions/3437 which aims to adds `ContinuousIntegrationBuild` as a known property, `Version`, `AssemblyVersion`, and `FileVersion` are also common property we override on MSBuild on CI builds, for example: ```cakescript DotNetCoreBuild("./Project.csproj", new DotNetCoreBuildSettings { MSBuildSettings = new DotNetCoreMSBuildSettings() .WithProperty("Version", buildVersion.Version) .WithProperty("AssemblyVersion", buildVersion.AssemblyVersion) .WithProperty("FileVersion", buildVersion.FileVersion) .WithProperty("AssemblyInformationalVersion", buildVersion. AssemblyInformationalVersion) .WithProperty("ContinuousIntegrationBuild", BuildSystem.IsLocalBuild ? "false" : "true") }); ```

Relates to https://github.com/cake-build/cake/issues/3447 and could be done together in the same PR

Jericho commented 3 years ago

What's odd is that I have been adding version information to my nuget packages during the "pack" step like so:

var settings = new DotNetCorePackSettings
{
    Configuration = configuration,
    IncludeSource = false,
    IncludeSymbols = true,
    NoBuild = true,
    NoRestore = true,
    NoDependencies = true,
    OutputDirectory = outputDir,
    SymbolPackageFormat = "snupkg",
    ArgumentCustomization = (args) =>
    {
        return args
            .Append("/p:PackageReleaseNotes=\"{0}\"", releaseNotesUrl)
            .Append("/p:Version={0}", versionInfo.LegacySemVerPadded)
            .Append("/p:AssemblyVersion={0}", versionInfo.MajorMinorPatch)
            .Append("/p:FileVersion={0}", versionInfo.MajorMinorPatch)
            .Append("/p:AssemblyInformationalVersion={0}", versionInfo.InformationalVersion);
    }
};

DotNetCorePack(sourceProject, settings);

and the result is exactly as I expect:

image See here

Maybe we can set the version information during build or pack steps interchangeably???

augustoproiete commented 3 years ago

Interesting. Have you tried removing these lines from the build.cake file to see if it makes any difference?

You have NoBuild = true so that should prevent dotnet pack from calling dotnet build (which is the real "consumer" of these assembly properties)... AFAIK dotnet pack itself doesn't make any changes to assemblies.

Thus, my first guess is that something else in your build process (SourceLink?) is setting the assembly version, file version, etc. for you and these properties in the build script are being ignored.

My second guess is that something is causing NoBuild = true to be ignored, and your project is being built twice, once when you call DotNetCoreBuild and again when you call DotNetCorePack. Might be easy to check with diagnostic verbosity turned on.

Maybe we can set the version information during build or pack steps interchangeably???

Yes, if we add to DotNetCoreMSBuildSettings, it will be available for build, pack, publish, etc. all the commands that can end up calling MSBuild.

Usage could be something similar to:

DotNetCorePack("./Project.csproj", new DotNetCoreBuildSettings
{
    MSBuildSettings = new DotNetCoreMSBuildSettings()
        .SetVersion(versionInfo.LegacySemVerPadded)
        .SetAssemblyVersion(versionInfo.MajorMinorPatch)
        .SetFileVersion(versionInfo.MajorMinorPatch)
        .SetInformationalVersion(versionInfo.InformationalVersion)
});

// or

var msBuildSettings = new DotNetCoreMSBuildSettings
{
    Version = versionInfo.LegacySemVerPadded,
    AssemblyVersion = versionInfo.MajorMinorPatch,
    FileVersion = versionInfo.MajorMinorPatch,
    InformationalVersion = versionInfo.InformationalVersion,
};

DotNetCorePack("./Project.csproj", new DotNetCoreBuildSettings
{
    MSBuildSettings = msBuildSettings,
});

But, of course, the same principle apply i.e. If you set these version properties in DotNetCorePack you can't also set NoBuild = true... You need to let dotnet pack call dotnet build on your behalf to make the updates to the assemblies.

augustoproiete commented 3 years ago

Final list of properties to be added:

MSBuild Settings Property MSBuild Settings Extension Description
Version SetVersion Default version e.g. 1.2.3-pre
VersionPrefix SetVersionPrefix Version prefix e.g. 1.2.3
VersionSuffix SetVersionSuffix Version suffix e.g. pre
FileVersion SetFileVersion File version e.g. 1.2.3.0
AssemblyVersion SetAssemblyVersion Assembly version e.g. 1.0.0.0
InformationalVersion SetInformationalVersion Informational version e.g. 1.2.3-pre+7ad03d0
PackageVersion SetPackageVersion NuGet package version e.g. 1.2.3-pre
PackageReleaseNotes SetPackageReleaseNotes NuGet package release notes e.g. https://...

Example usage with DotNetCoreBuild:

DotNetCoreBuild("./Project.csproj", new DotNetCoreBuildSettings
{
    Configuration = configuration,
    NoRestore = true,
    NoIncremental = false,
    MSBuildSettings = new DotNetCoreMSBuildSettings()
        .SetVersion("1.2.3-pre")
        .SetAssemblyVersion("1.0.0.0")
        .SetFileVersion("1.2.3.0")
        .InformationalVersion("1.2.3-pre+7ad03d0")
});

Example usage with DotNetCorePack:

DotNetCorePack("Project.csproj", new DotNetCorePackSettings
{
    Configuration = "Release",
    NoRestore = true,
    NoBuild = true,
    OutputDirectory = "./artifact/nuget",
    // ...
    MSBuildSettings = new DotNetCoreMSBuildSettings()
        .SetPackageVersion("1.2.3-pre")
        .SetPackageReleaseNotes("https://github.com/cake-build/cake/releases")
});
cake-build-bot commented 3 years ago

:tada: This issue has been resolved in version v1.3.0 :tada:

The release is available on:

Your GitReleaseManager bot :package::rocket: