Skip to content

Commit

Permalink
Prefer "TargetFramework" when specified globally
Browse files Browse the repository at this point in the history
- Prefer "TargetFramework" over its plural counterpart
  when specified globally, like on the command-line
  and in the MSBuild task, Properties metadata.

- This also adds a "GetGlobalPropertyValueTask" for getting global properties.
  • Loading branch information
Nirmal4G committed Dec 11, 2021
1 parent 8f08ac3 commit 4a56316
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
34 changes: 30 additions & 4 deletions Sources/NuGet.Build.Sdk/Core/NuGet.Restore.targets
Original file line number Diff line number Diff line change
Expand Up @@ -597,18 +597,20 @@
============================================================
-->
<Target Name="_GetRestoreTargetFrameworksOutput"
DependsOnTargets="_GetRestoreProjectStyle"
DependsOnTargets="_GetRestoreProjectStyle;_CheckIfGlobalTargetFramework"
Returns="@(_RestoreTargetFrameworksOutputFiltered)">

<PropertyGroup>
<_RestoreProjectFramework/>
<_PreferredTargetFrameworks Condition="'$(_IsGlobalTargetFramework)' == 'true'">$(TargetFramework)</_PreferredTargetFrameworks>
<_PreferredTargetFrameworks Condition="'$(_IsGlobalTargetFramework)' != 'true'">$(TargetFrameworks)</_PreferredTargetFrameworks>
</PropertyGroup>

<!-- For project.json projects target frameworks will be read from project.json -->
<GetProjectTargetFrameworksTask
Condition="'$(RestoreProjectStyle)' != 'ProjectJson'"
ProjectPath="$(MSBuildProjectFullPath)"
TargetFrameworks="$(TargetFrameworks)"
TargetFrameworks="$(_PreferredTargetFrameworks)"
TargetFramework="$(TargetFramework)"
TargetFrameworkMoniker="$(TargetFrameworkMoniker)"
TargetPlatformIdentifier="$(TargetPlatformIdentifier)"
Expand All @@ -630,13 +632,16 @@
Read $(TargetFrameworks) from the project as items.
Projects that do not have $(TargetFrameworks) will noop.
If $(TargetFramework) is specified globally, it'll be preferred over $(TargetFrameworks)
============================================================
-->
<Target Name="_GetRestoreTargetFrameworksAsItems"
DependsOnTargets="_GetRestoreProjectStyle"
DependsOnTargets="_GetRestoreProjectStyle;_CheckIfGlobalTargetFramework"
Returns="@(_RestoreTargetFrameworkItems)">
<ItemGroup Condition="'$(TargetFrameworks)' != ''">
<_RestoreTargetFrameworkItems Include="$(TargetFrameworks.Split(';'))"/>
<_RestoreTargetFrameworkItems Include="$(TargetFrameworks.Split(';'))" Condition="'$(_IsGlobalTargetFramework)' != 'true'"/>
<!-- Use $(TargetFramework) instead of $(TargetFrameworks) when it is specified globally (i.e., on the command-line) -->
<_RestoreTargetFrameworkItems Include="$(TargetFramework)" Condition="'$(_IsGlobalTargetFramework)' == 'true'"/>
</ItemGroup>
</Target>

Expand Down Expand Up @@ -1376,6 +1381,27 @@
</PropertyGroup>
</Target>

<!--
============================================================
_CheckIfGlobalTargetFramework
============================================================
-->
<Target Name="_CheckIfGlobalTargetFramework"
Condition="'$(_DisableNuGetRestoreTargetFrameworksOverride)' != 'true'"
Returns="$(_IsGlobalTargetFramework)">

<!-- Check if $(TargetFramework) is a global property (i.e., specified on the command-line) -->
<GetGlobalPropertyValueTask
PropertyName="TargetFramework"
Condition="'$(TargetFramework)' != ''">
<Output TaskParameter="GlobalPropertyValue" PropertyName="_GlobalTargetFramework"/>
</GetGlobalPropertyValueTask>

<PropertyGroup Condition="'$(_GlobalTargetFramework)' != ''">
<_IsGlobalTargetFramework Condition="'$(_GlobalTargetFramework)' == '$(TargetFramework)'">true</_IsGlobalTargetFramework>
</PropertyGroup>
</Target>

<!--
============================================================
_IsProjectRestoreSupported
Expand Down
1 change: 1 addition & 0 deletions Sources/NuGet.Build.Sdk/Tasks/NuGet.Restore.tasks
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<!-- NuGet's Common Tasks -->
<UsingTask AssemblyFile="$(NuGetRestoreTasksAssemblyFile)" TaskName="NuGet.Build.Tasks.NuGetMessageTask"/>
<UsingTask AssemblyFile="$(NuGetRestoreTasksAssemblyFile)" TaskName="NuGet.Build.Tasks.GetGlobalPropertyValueTask"/>
<UsingTask AssemblyFile="$(NuGetRestoreTasksAssemblyFile)" TaskName="NuGet.Build.Tasks.CheckForDuplicateNuGetItemsTask"/>
<UsingTask AssemblyFile="$(NuGetRestoreTasksAssemblyFile)" TaskName="NuGet.Build.Tasks.WarnForInvalidProjectsTask"/>
<UsingTask AssemblyFile="$(NuGetRestoreTasksAssemblyFile)" TaskName="NuGet.Build.Tasks.GetCentralPackageVersionsTask"/>
Expand Down

0 comments on commit 4a56316

Please sign in to comment.