Skip to content

Commit

Permalink
(chocolateyGH-458) Conditional compilation symbols for .NET version
Browse files Browse the repository at this point in the history
.NET doesn't have compiler conditionals for the different
versions of the .NET Frameworks, so it's not possible out of the
box to target different functionality based on the .NET versions.
Add a property group in a targets file based on
http://stackoverflow.com/a/29001688/18475 and ensure that both
chocolatey and chocolatey.console VS projects use those targets
files.
  • Loading branch information
ferventcoder committed Oct 11, 2015
1 parent 0c171b3 commit fd09a01
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/chocolatey.console/chocolatey.console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
</None>
<None Include="packages.config" />
<None Include="targets\Microsoft.Application.targets" />
<None Include="targets\NET.Compile.targets">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\chocolatey.resources\chocolatey.resources.csproj">
Expand All @@ -139,6 +142,7 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project=".\targets\Microsoft.Application.targets" />
<Import Project=".\targets\NET.Compile.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
19 changes: 19 additions & 0 deletions src/chocolatey.console/targets/NET.Compile.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!--Based on http://stackoverflow.com/a/29001688/18475 -->
<!--
******************************************************************
Defines the Compile time symbols
Modelled from https://msdn.microsoft.com/en-us/library/ms171464.aspx
*********************************************************************
-->


<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5.2')))) &gt;= 0">$(DefineConstants);NETFX_452</DefineConstants>
<DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5.1')))) &gt;= 0">$(DefineConstants);NETFX_451</DefineConstants>
<DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5')))) &gt;= 0">$(DefineConstants);NETFX_45</DefineConstants>
<DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.0')))) &gt;= 0">$(DefineConstants);NETFX_40</DefineConstants>
<DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('3.5')))) &gt;= 0">$(DefineConstants);NETFX_35</DefineConstants>
<DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('3.0')))) &gt;= 0">$(DefineConstants);NETFX_30</DefineConstants>
</PropertyGroup>
</Project>
2 changes: 2 additions & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@
</EmbeddedResource>
<None Include="packages.config" />
<None Include="targets\Microsoft.Application.targets" />
<None Include="targets\NET.Compile.targets" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\docs\logo\chocolatey.ico">
Expand All @@ -291,6 +292,7 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project=".\targets\Microsoft.Application.targets" />
<Import Project=".\targets\NET.Compile.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
19 changes: 19 additions & 0 deletions src/chocolatey/targets/NET.Compile.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!--Based on http://stackoverflow.com/a/29001688/18475 -->
<!--
******************************************************************
Defines the Compile time symbols
Modelled from https://msdn.microsoft.com/en-us/library/ms171464.aspx
*********************************************************************
-->


<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5.2')))) &gt;= 0">$(DefineConstants);NETFX_452</DefineConstants>
<DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5.1')))) &gt;= 0">$(DefineConstants);NETFX_451</DefineConstants>
<DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5')))) &gt;= 0">$(DefineConstants);NETFX_45</DefineConstants>
<DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.0')))) &gt;= 0">$(DefineConstants);NETFX_40</DefineConstants>
<DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('3.5')))) &gt;= 0">$(DefineConstants);NETFX_35</DefineConstants>
<DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('3.0')))) &gt;= 0">$(DefineConstants);NETFX_30</DefineConstants>
</PropertyGroup>
</Project>

0 comments on commit fd09a01

Please sign in to comment.