From 525bf1bde0b04d0e2d57fbac0cdfa798bdfd3d8c Mon Sep 17 00:00:00 2001
From: Igor Velikorossov <igor.velikorossov@microsoft.com>
Date: Thu, 14 Nov 2024 13:37:49 +1100
Subject: [PATCH] Generalise how package version isn't stabilized for
 dev/preview stages

Follow up on #5632
---
 eng/MSBuild/ProjectStaging.targets               | 15 ++++++++++-----
 eng/Versions.props                               | 16 ++++++++++++----
 .../Directory.Build.props                        |  1 -
 .../Directory.Build.props                        | 10 ++++++++++
 .../Microsoft.Extensions.AI.Abstractions.csproj  |  2 --
 .../Directory.Build.props                        | 10 ++++++++++
 ...crosoft.Extensions.AI.AzureAIInference.csproj |  2 --
 .../Directory.Build.props                        | 10 ++++++++++
 .../Microsoft.Extensions.AI.Ollama.csproj        |  2 --
 .../Directory.Build.props                        | 10 ++++++++++
 .../Microsoft.Extensions.AI.OpenAI.csproj        |  2 --
 .../Directory.Build.props                        | 10 ++++++++++
 .../Microsoft.Extensions.AI.csproj               |  2 --
 .../Directory.Build.props                        | 11 +++++++++++
 .../Microsoft.Extensions.Caching.Hybrid.csproj   |  5 +----
 .../Directory.Build.props                        |  1 -
 .../Directory.Build.props                        |  1 -
 .../Directory.Build.props                        |  1 -
 18 files changed, 84 insertions(+), 27 deletions(-)
 create mode 100644 src/Libraries/Microsoft.Extensions.AI.Abstractions/Directory.Build.props
 create mode 100644 src/Libraries/Microsoft.Extensions.AI.AzureAIInference/Directory.Build.props
 create mode 100644 src/Libraries/Microsoft.Extensions.AI.Ollama/Directory.Build.props
 create mode 100644 src/Libraries/Microsoft.Extensions.AI.OpenAI/Directory.Build.props
 create mode 100644 src/Libraries/Microsoft.Extensions.AI/Directory.Build.props
 create mode 100644 src/Libraries/Microsoft.Extensions.Caching.Hybrid/Directory.Build.props

diff --git a/eng/MSBuild/ProjectStaging.targets b/eng/MSBuild/ProjectStaging.targets
index e3a89d03542..01bcbeb47fb 100644
--- a/eng/MSBuild/ProjectStaging.targets
+++ b/eng/MSBuild/ProjectStaging.targets
@@ -4,6 +4,10 @@
     <!-- Preview packages: do not use stable branding and do not warn about lack of [Experimental] -->
     <SuppressFinalPackageVersion Condition="'$(Stage)' == 'preview'">true</SuppressFinalPackageVersion>
     <NoWarn Condition="'$(Stage)' == 'preview'">$(NoWarn);LA0003</NoWarn>
+
+    <!-- Amend the description based on stage -->
+    <Description Condition="'$(Stage)' == 'dev'">Experimental package. $(Description)</Description>
+    <Description Condition="'$(Stage)' == 'obsolete'">Obsolete Package. $(Description)</Description>
   </PropertyGroup>
 
   <!-- Produce errors if we don't have all the right properties defined -->
@@ -22,10 +26,11 @@
     <Error Condition="'$(MinMutationScore)' != 'n/a' AND ('$(MinMutationScore)' &lt; 50)" Text="MinMutationScore property must be >= 50 for normal stage." />
   </Target>
 
-  <!-- Amend the description based on stage -->
-  <PropertyGroup>
-    <Description Condition="'$(Stage)' == 'dev'">Experimental package. $(Description)</Description>
-    <Description Condition="'$(Stage)' == 'obsolete'">Obsolete Package. $(Description)</Description>
-  </PropertyGroup>
+  <Target Name="_ValidateVersion" AfterTargets="GenerateNuspec" Condition="'$(Stage)' == 'dev' or '$(Stage)' == 'preview'">
+    <PropertyGroup>
+      <_ExpectedVersionSuffix>$(_PreReleaseLabel)$(_BuildNumberLabels)</_ExpectedVersionSuffix>
+    </PropertyGroup>
 
+    <Error Condition=" '$(VersionSuffix)' != '$(_ExpectedVersionSuffix)' " Text="Unexpected package version suffix. Expected suffix for '$(Stage)' stage: '$(_ExpectedVersionSuffix)', received: '$(VersionSuffix)'." />
+  </Target>
 </Project>
diff --git a/eng/Versions.props b/eng/Versions.props
index bbf47bbf857..4b870cc539d 100644
--- a/eng/Versions.props
+++ b/eng/Versions.props
@@ -8,13 +8,21 @@
     <VersionPrefix>$(MajorVersion).$(MinorVersion).$(PatchVersion)</VersionPrefix>
     <ValidateBaseline>true</ValidateBaseline>
     <AssemblyVersion>$(MajorVersion).$(MinorVersion).0.0</AssemblyVersion>
-    <!--
-        When DotNetFinalVersionKind is set to 'release', this branch will produce stable outputs for 'Shipping' packages
-    -->
-    <DotNetFinalVersionKind />
+    <DotNetFinalVersionKind>release</DotNetFinalVersionKind>
+
     <!-- Enabling this rule will cause build failures on undocumented public APIs. -->
     <SkipArcadeNoWarnCS1591>true</SkipArcadeNoWarnCS1591>
   </PropertyGroup>
+  
+  <PropertyGroup>
+    <!--
+      Makes it such that the package version won't be stabilized even when the rest of the repo is going stable.
+      https://github.com/dotnet/arcade/blob/main/Documentation/CorePackages/Versioning.md#package-version
+     -->
+    <SuppressFinalPackageVersion />
+    <SuppressFinalPackageVersion Condition="'$(Stage)' == 'dev' or '$(Stage)' == 'preview'">true</SuppressFinalPackageVersion>
+  </PropertyGroup>
+
   <!--
 
     These versions should ONLY be updated by automation.
diff --git a/src/Generators/Microsoft.Gen.MetricsReports/Directory.Build.props b/src/Generators/Microsoft.Gen.MetricsReports/Directory.Build.props
index f739a758633..142bcd3852c 100644
--- a/src/Generators/Microsoft.Gen.MetricsReports/Directory.Build.props
+++ b/src/Generators/Microsoft.Gen.MetricsReports/Directory.Build.props
@@ -4,7 +4,6 @@
   the project itself. -->
   <PropertyGroup>
     <Stage>dev</Stage>
-    <SuppressFinalPackageVersion>true</SuppressFinalPackageVersion>
   </PropertyGroup>
   <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
 </Project>
\ No newline at end of file
diff --git a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Directory.Build.props b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Directory.Build.props
new file mode 100644
index 00000000000..7de04b91028
--- /dev/null
+++ b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Directory.Build.props
@@ -0,0 +1,10 @@
+<Project>
+  <!-- In order to get the right package versions for projects that shouldn't stabilize, we need to set this property before
+  importing the root level Directory.Build.props file. This property should be kept in here, as opposed to moving it to
+  the project itself. -->
+  <PropertyGroup>
+    <Stage>preview</Stage>
+  </PropertyGroup>
+
+  <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
+</Project>
\ No newline at end of file
diff --git a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Microsoft.Extensions.AI.Abstractions.csproj b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Microsoft.Extensions.AI.Abstractions.csproj
index 8b8541688ef..4bbf65d5fd1 100644
--- a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Microsoft.Extensions.AI.Abstractions.csproj
+++ b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Microsoft.Extensions.AI.Abstractions.csproj
@@ -7,8 +7,6 @@
   </PropertyGroup>
 
   <PropertyGroup>
-    <Stage>preview</Stage>
-    <SuppressFinalPackageVersion>true</SuppressFinalPackageVersion>
     <MinCodeCoverage>0</MinCodeCoverage>
     <MinMutationScore>0</MinMutationScore>
   </PropertyGroup>
diff --git a/src/Libraries/Microsoft.Extensions.AI.AzureAIInference/Directory.Build.props b/src/Libraries/Microsoft.Extensions.AI.AzureAIInference/Directory.Build.props
new file mode 100644
index 00000000000..7de04b91028
--- /dev/null
+++ b/src/Libraries/Microsoft.Extensions.AI.AzureAIInference/Directory.Build.props
@@ -0,0 +1,10 @@
+<Project>
+  <!-- In order to get the right package versions for projects that shouldn't stabilize, we need to set this property before
+  importing the root level Directory.Build.props file. This property should be kept in here, as opposed to moving it to
+  the project itself. -->
+  <PropertyGroup>
+    <Stage>preview</Stage>
+  </PropertyGroup>
+
+  <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
+</Project>
\ No newline at end of file
diff --git a/src/Libraries/Microsoft.Extensions.AI.AzureAIInference/Microsoft.Extensions.AI.AzureAIInference.csproj b/src/Libraries/Microsoft.Extensions.AI.AzureAIInference/Microsoft.Extensions.AI.AzureAIInference.csproj
index 0e3f60b8db3..10cc37ea55b 100644
--- a/src/Libraries/Microsoft.Extensions.AI.AzureAIInference/Microsoft.Extensions.AI.AzureAIInference.csproj
+++ b/src/Libraries/Microsoft.Extensions.AI.AzureAIInference/Microsoft.Extensions.AI.AzureAIInference.csproj
@@ -7,8 +7,6 @@
   </PropertyGroup>
 
   <PropertyGroup>
-    <Stage>preview</Stage>
-    <SuppressFinalPackageVersion>true</SuppressFinalPackageVersion>
     <MinCodeCoverage>0</MinCodeCoverage>
     <MinMutationScore>0</MinMutationScore>
   </PropertyGroup>
diff --git a/src/Libraries/Microsoft.Extensions.AI.Ollama/Directory.Build.props b/src/Libraries/Microsoft.Extensions.AI.Ollama/Directory.Build.props
new file mode 100644
index 00000000000..7de04b91028
--- /dev/null
+++ b/src/Libraries/Microsoft.Extensions.AI.Ollama/Directory.Build.props
@@ -0,0 +1,10 @@
+<Project>
+  <!-- In order to get the right package versions for projects that shouldn't stabilize, we need to set this property before
+  importing the root level Directory.Build.props file. This property should be kept in here, as opposed to moving it to
+  the project itself. -->
+  <PropertyGroup>
+    <Stage>preview</Stage>
+  </PropertyGroup>
+
+  <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
+</Project>
\ No newline at end of file
diff --git a/src/Libraries/Microsoft.Extensions.AI.Ollama/Microsoft.Extensions.AI.Ollama.csproj b/src/Libraries/Microsoft.Extensions.AI.Ollama/Microsoft.Extensions.AI.Ollama.csproj
index 018184d6bf0..d08fbe83be6 100644
--- a/src/Libraries/Microsoft.Extensions.AI.Ollama/Microsoft.Extensions.AI.Ollama.csproj
+++ b/src/Libraries/Microsoft.Extensions.AI.Ollama/Microsoft.Extensions.AI.Ollama.csproj
@@ -7,8 +7,6 @@
   </PropertyGroup>
 
   <PropertyGroup>
-    <Stage>preview</Stage>
-    <SuppressFinalPackageVersion>true</SuppressFinalPackageVersion>
     <MinCodeCoverage>0</MinCodeCoverage>
     <MinMutationScore>0</MinMutationScore>
   </PropertyGroup>
diff --git a/src/Libraries/Microsoft.Extensions.AI.OpenAI/Directory.Build.props b/src/Libraries/Microsoft.Extensions.AI.OpenAI/Directory.Build.props
new file mode 100644
index 00000000000..7de04b91028
--- /dev/null
+++ b/src/Libraries/Microsoft.Extensions.AI.OpenAI/Directory.Build.props
@@ -0,0 +1,10 @@
+<Project>
+  <!-- In order to get the right package versions for projects that shouldn't stabilize, we need to set this property before
+  importing the root level Directory.Build.props file. This property should be kept in here, as opposed to moving it to
+  the project itself. -->
+  <PropertyGroup>
+    <Stage>preview</Stage>
+  </PropertyGroup>
+
+  <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
+</Project>
\ No newline at end of file
diff --git a/src/Libraries/Microsoft.Extensions.AI.OpenAI/Microsoft.Extensions.AI.OpenAI.csproj b/src/Libraries/Microsoft.Extensions.AI.OpenAI/Microsoft.Extensions.AI.OpenAI.csproj
index 1d400389af0..44260cec565 100644
--- a/src/Libraries/Microsoft.Extensions.AI.OpenAI/Microsoft.Extensions.AI.OpenAI.csproj
+++ b/src/Libraries/Microsoft.Extensions.AI.OpenAI/Microsoft.Extensions.AI.OpenAI.csproj
@@ -7,8 +7,6 @@
   </PropertyGroup>
 
   <PropertyGroup>
-    <Stage>preview</Stage>
-    <SuppressFinalPackageVersion>true</SuppressFinalPackageVersion>
     <MinCodeCoverage>0</MinCodeCoverage>
     <MinMutationScore>0</MinMutationScore>
   </PropertyGroup>
diff --git a/src/Libraries/Microsoft.Extensions.AI/Directory.Build.props b/src/Libraries/Microsoft.Extensions.AI/Directory.Build.props
new file mode 100644
index 00000000000..7de04b91028
--- /dev/null
+++ b/src/Libraries/Microsoft.Extensions.AI/Directory.Build.props
@@ -0,0 +1,10 @@
+<Project>
+  <!-- In order to get the right package versions for projects that shouldn't stabilize, we need to set this property before
+  importing the root level Directory.Build.props file. This property should be kept in here, as opposed to moving it to
+  the project itself. -->
+  <PropertyGroup>
+    <Stage>preview</Stage>
+  </PropertyGroup>
+
+  <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
+</Project>
\ No newline at end of file
diff --git a/src/Libraries/Microsoft.Extensions.AI/Microsoft.Extensions.AI.csproj b/src/Libraries/Microsoft.Extensions.AI/Microsoft.Extensions.AI.csproj
index 33628f2562a..822bfb50ccf 100644
--- a/src/Libraries/Microsoft.Extensions.AI/Microsoft.Extensions.AI.csproj
+++ b/src/Libraries/Microsoft.Extensions.AI/Microsoft.Extensions.AI.csproj
@@ -9,8 +9,6 @@
   </PropertyGroup>
 
   <PropertyGroup>
-    <Stage>preview</Stage>
-    <SuppressFinalPackageVersion>true</SuppressFinalPackageVersion>
     <MinCodeCoverage>0</MinCodeCoverage>
     <MinMutationScore>0</MinMutationScore>
   </PropertyGroup>
diff --git a/src/Libraries/Microsoft.Extensions.Caching.Hybrid/Directory.Build.props b/src/Libraries/Microsoft.Extensions.Caching.Hybrid/Directory.Build.props
new file mode 100644
index 00000000000..33081243fef
--- /dev/null
+++ b/src/Libraries/Microsoft.Extensions.Caching.Hybrid/Directory.Build.props
@@ -0,0 +1,11 @@
+<Project>
+  <!-- In order to get the right package versions for projects that shouldn't stabilize, we need to set this property before
+  importing the root level Directory.Build.props file. This property should be kept in here, as opposed to moving it to
+  the project itself. -->
+  <PropertyGroup>
+    <Stage>dev</Stage>
+    <StageDevDiagnosticId>EXTEXP0018</StageDevDiagnosticId>
+  </PropertyGroup>
+
+  <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
+</Project>
\ No newline at end of file
diff --git a/src/Libraries/Microsoft.Extensions.Caching.Hybrid/Microsoft.Extensions.Caching.Hybrid.csproj b/src/Libraries/Microsoft.Extensions.Caching.Hybrid/Microsoft.Extensions.Caching.Hybrid.csproj
index 05638bcea77..c77fdb327df 100644
--- a/src/Libraries/Microsoft.Extensions.Caching.Hybrid/Microsoft.Extensions.Caching.Hybrid.csproj
+++ b/src/Libraries/Microsoft.Extensions.Caching.Hybrid/Microsoft.Extensions.Caching.Hybrid.csproj
@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
     <Description>Multi-level caching implementation building on and extending IDistributedCache</Description>
@@ -24,9 +24,6 @@
   </PropertyGroup>
 
   <PropertyGroup>
-    <Stage>dev</Stage>
-    <SuppressFinalPackageVersion>true</SuppressFinalPackageVersion>
-    <StageDevDiagnosticId>EXTEXP0018</StageDevDiagnosticId>
     <MinCodeCoverage>75</MinCodeCoverage>
     <MinMutationScore>50</MinMutationScore>
   </PropertyGroup>
diff --git a/src/Libraries/Microsoft.Extensions.Diagnostics.Probes/Directory.Build.props b/src/Libraries/Microsoft.Extensions.Diagnostics.Probes/Directory.Build.props
index 0ea108580da..0f62eaa4953 100644
--- a/src/Libraries/Microsoft.Extensions.Diagnostics.Probes/Directory.Build.props
+++ b/src/Libraries/Microsoft.Extensions.Diagnostics.Probes/Directory.Build.props
@@ -3,7 +3,6 @@
   importing the root level Directory.Build.props file. This property should be kept in here, as opposed to moving it to
   the project itself. -->
   <PropertyGroup>
-    <SuppressFinalPackageVersion>true</SuppressFinalPackageVersion>
     <Stage>dev</Stage>
     <StageDevDiagnosticId>EXTEXP0015</StageDevDiagnosticId>
   </PropertyGroup>
diff --git a/src/Libraries/Microsoft.Extensions.Hosting.Testing/Directory.Build.props b/src/Libraries/Microsoft.Extensions.Hosting.Testing/Directory.Build.props
index 77a9a53a9e9..6ad6add3254 100644
--- a/src/Libraries/Microsoft.Extensions.Hosting.Testing/Directory.Build.props
+++ b/src/Libraries/Microsoft.Extensions.Hosting.Testing/Directory.Build.props
@@ -3,7 +3,6 @@
   importing the root level Directory.Build.props file. This property should be kept in here, as opposed to moving it to
   the project itself. -->
   <PropertyGroup>
-    <SuppressFinalPackageVersion>true</SuppressFinalPackageVersion>
     <Stage>dev</Stage>
     <StageDevDiagnosticId>EXTEXP0016</StageDevDiagnosticId>
   </PropertyGroup>
diff --git a/src/Libraries/Microsoft.Extensions.Options.Contextual/Directory.Build.props b/src/Libraries/Microsoft.Extensions.Options.Contextual/Directory.Build.props
index 59864c9c658..35ff2ae323e 100644
--- a/src/Libraries/Microsoft.Extensions.Options.Contextual/Directory.Build.props
+++ b/src/Libraries/Microsoft.Extensions.Options.Contextual/Directory.Build.props
@@ -3,7 +3,6 @@
   importing the root level Directory.Build.props file. This property should be kept in here, as opposed to moving it to
   the project itself. -->
   <PropertyGroup>
-    <SuppressFinalPackageVersion>true</SuppressFinalPackageVersion>
     <Stage>dev</Stage>
     <StageDevDiagnosticId>EXTEXP0017</StageDevDiagnosticId>
   </PropertyGroup>