|
1 | 1 | <Project> |
2 | 2 |
|
3 | | - <Target Name="NewtonsoftJsonVersionCheck" AfterTargets="ResolveReferences"> |
| 3 | + <Target Name="NewtonsoftJsonVersionCheck" AfterTargets="ResolveReferences" Condition="'$(CheckNewtonsoftJsonVersion)' != 'false'"> |
4 | 4 | <ItemGroup> |
5 | 5 | <!-- |
6 | 6 | Any NuGet packages that are referenced (even transitive references) are in the `Reference` |
7 | 7 | item. For NuGet packages, there will be `NuGetPackageId` and `NuGetPackageVersion` metadata. |
8 | 8 | Filter the list of references to get the version of Newtonsoft.Json that is referenced (if any). |
9 | 9 | --> |
10 | 10 | <CVST_NewtonsoftJsonReferenceVersion Include="%(Reference.NuGetPackageVersion)" Condition="'%(Reference.NuGetPackageId)' == 'Newtonsoft.Json'"/> |
| 11 | + |
| 12 | + <!-- |
| 13 | + We also need the version of the `Microsoft.VisualStudio.SDK`package. That is a |
| 14 | + meta package, so it doesn't end up in the `Reference` items. We can get it from the |
| 15 | + `PackageReference` items instead, however that does rely on it being explicitly referenced. |
| 16 | + --> |
| 17 | + <CVST_VisualStudioSdkReferenceVersion Include="%(PackageReference.Version)" Condition="'%(PackageReference.Identity)' == 'Microsoft.VisualStudio.SDK'"/> |
11 | 18 | </ItemGroup> |
12 | 19 |
|
13 | 20 | <PropertyGroup> |
14 | 21 | <!-- Turn the item list of version numbers into a single property value. --> |
15 | 22 | <CVST_NewtonsoftJsonVersion>@(CVST_NewtonsoftJsonReferenceVersion)</CVST_NewtonsoftJsonVersion> |
16 | | - |
| 23 | + <CVST_VisualStudioSdkVersion>@(CVST_VisualStudioSdkReferenceVersion)</CVST_VisualStudioSdkVersion> |
| 24 | + |
17 | 25 | <!-- |
| 26 | + The latest versions of the Visual Studio SDK explicitly reference `Newtonsoft.Json`, |
| 27 | + which means NuGet will raise warnings if a newer version of `Newtonsoft.Json` is installed. |
| 28 | + That means we can skip the version check if the Visual Studio SDK is above a certain |
| 29 | + version. We can skip the check by clearing the `CVST_NewtonsoftJsonVersion` property. |
| 30 | + --> |
| 31 | + <CVST_NewtonsoftJsonSdkMaxVersion>17.5.33428.388</CVST_NewtonsoftJsonSdkMaxVersion> |
| 32 | + <CVST_NewtonsoftJsonHasModernSdk Condition="( '$(CVST_NewtonsoftJsonSdkMaxVersion)' != '' ) AND ( '$(CVST_VisualStudioSdkVersion)' != '' ) AND ( '$(CVST_VersionPropertyFunctionsExist)' == 'true' )">$([MSBuild]::VersionGreaterThan('$(CVST_VisualStudioSdkVersion)', '$(CVST_NewtonsoftJsonSdkMaxVersion)'))</CVST_NewtonsoftJsonHasModernSdk> |
| 33 | + <CVST_NewtonsoftJsonHasModernSdk Condition="( '$(CVST_NewtonsoftJsonSdkMaxVersion)' != '' ) AND ( '$(CVST_VisualStudioSdkVersion)' != '' ) AND ( '$(CVST_VersionPropertyFunctionsExist)' != 'true' ) AND ( $(CVST_VisualStudioSdkVersion) > $(CVST_NewtonsoftJsonSdkMaxVersion) )">true</CVST_NewtonsoftJsonHasModernSdk> |
| 34 | + <CVST_NewtonsoftJsonVersion Condition="$(CVST_NewtonsoftJsonHasModernSdk) == 'true'"></CVST_NewtonsoftJsonVersion> |
| 35 | + |
| 36 | + <!-- |
18 | 37 | The property functions for version comparisons are the preferred way to compare versions, |
19 | 38 | but they are not available in older versions of MSBuild. In earlier versions we can compare |
20 | 39 | the version values directly, although that is not always accurate. |
21 | 40 | See: https://docs.microsoft.com/visualstudio/msbuild/msbuild-conditions#comparing-versions |
22 | 41 | --> |
| 42 | + <CVST_NewtonsoftJsonIsValid>true</CVST_NewtonsoftJsonIsValid> |
23 | 43 | <CVST_NewtonsoftJsonIsValid Condition="( '$(CVST_NewtonsoftJsonVersion)' != '' ) AND ( '$(CVST_VersionPropertyFunctionsExist)' == 'true' )">$([MSBuild]::VersionLessThanOrEquals('$(CVST_NewtonsoftJsonVersion)', '$(CVST_NewtonsoftJsonMaxVersion)'))</CVST_NewtonsoftJsonIsValid> |
24 | 44 | <CVST_NewtonsoftJsonIsValid Condition="( '$(CVST_NewtonsoftJsonVersion)' != '' ) AND ( '$(CVST_VersionPropertyFunctionsExist)' != 'true' ) AND ( $(CVST_NewtonsoftJsonVersion) <= $(CVST_NewtonsoftJsonMaxVersion) )">true</CVST_NewtonsoftJsonIsValid> |
25 | 45 |
|
|
0 commit comments