Skip to content

Commit

Permalink
Simplify MSBuild logic in project files
Browse files Browse the repository at this point in the history
- Add necessary guard to check for pack.
- Remove redundant properties and values.
- Remove and adjust quotes in property functions.
- Use wildcards to generalize and reduce items declared.
  • Loading branch information
Nirmal4G committed Jan 2, 2023
1 parent c5547bc commit 8473017
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 92 deletions.
1 change: 1 addition & 0 deletions CommunityToolkit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Root", "Root", "{CFA75BE0-5
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{CD16E790-7B7B-411E-9CE7-768E759CC22D}"
ProjectSection(SolutionItems) = preProject
eng\AssemblyInfo.Shared.cs = eng\AssemblyInfo.Shared.cs
eng\Sign-Package.ps1 = eng\Sign-Package.ps1
eng\SignClientSettings.json = eng\SignClientSettings.json
eng\Toolkit.Common.props = eng\Toolkit.Common.props
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<!-- Embed source files that are not tracked by the source control manager to the PDB -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<!-- Include PDB in the built .nupkg -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<AllowedOutputExtensionsInPackageBuildOutputFolder>.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
Expand Down
16 changes: 16 additions & 0 deletions eng/AssemblyInfo.Shared.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// This file contains assembly and module attributes that is shared across projects.
// Include it in `Directory.Build.targets` near to all projects that need this file.

using System.Runtime.CompilerServices;

/*
Using `[module: SkipLocalsInit]` suppresses the .init flag for local variables for the entire module.
This doesn't affect the correctness of the methods in this assembly, as none of them are relying on
JIT ensuring that all local memory is zeroed out to work. Doing this can provide some minor
performance benefits, depending on the workload.
*/
[module: SkipLocalsInit]
12 changes: 9 additions & 3 deletions eng/Toolkit.Common.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>

<PropertyGroup>
<PropertyGroup Condition="'$(IsPackable)' == 'true'">
<Company>Microsoft</Company>
<Authors>Microsoft</Authors>
<Product>.NET Community Toolkit</Product>
Expand All @@ -13,19 +13,25 @@
<PackageIcon>Icon.png</PackageIcon>
</PropertyGroup>

<PropertyGroup>
<PropertyGroup Condition="'$(IsPackable)' == 'true'">
<!-- TODO: Dynamically generate Title if one wasn't set -->
<Title Condition="'$(Title)' == ''">$(Product) Asset</Title>
<PackageTags Condition="'$(PackageTags)' != ''">$(CommonTags);$(PackageTags)</PackageTags>
<PackageTags Condition="'$(PackageTags)' == ''">$(CommonTags)</PackageTags>
</PropertyGroup>

<ItemGroup Condition="$(IsPackable)">
<ItemGroup Condition="'$(IsPackable)' == 'true'">
<None Pack="true" PackagePath="\" Visible="False" Include="$(BuildToolsDirectory)Icon.png" />
<None Pack="true" PackagePath="\" Visible="False" Include="$(RepositoryDirectory)License.md" />
<None Pack="true" PackagePath="\" Visible="False" Include="$(RepositoryDirectory)ThirdPartyNotices.txt" />
</ItemGroup>

<!-- Checked Version properties to use down the line -->
<PropertyGroup Condition="'$([MSBuild]::GetTargetFrameworkIdentifier($(TargetFramework)))' == '.NETCoreApp'">
<_ToolkitTargetFrameworkVersion>$([MSBuild]::GetTargetFrameworkVersion($(TargetFramework)))</_ToolkitTargetFrameworkVersion>
<_NET_6_OR_GREATER>$([MSBuild]::VersionGreaterThanOrEquals($(_ToolkitTargetFrameworkVersion), '6.0'))</_NET_6_OR_GREATER>
</PropertyGroup>

<!--
Add additional Pre-Processor symbols to $(DefineConstants) and $(FinalDefineConstants) in VB.
This overrides the same target in the .NET SDK. Since, that version has issues in DesignTime.
Expand Down
35 changes: 4 additions & 31 deletions eng/Toolkit.TextTemplates.targets
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,17 @@
</ItemGroup>

<ItemGroup>
<None Update="Generated\Guard.Comparable.Numeric.tt">
<None Update="**\*.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Guard.Comparable.Numeric.g.cs</LastGenOutput>
</None>
<None Update="Generated\Guard.Collection.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Guard.Collection.g.cs</LastGenOutput>
</None>
<None Update="Generated\ThrowHelper.Collection.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>ThrowHelper.Collection.g.cs</LastGenOutput>
</None>
<None Update="Generated\TypeInfo.ttinclude">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>TypeInfo.g.cs</LastGenOutput>
<LastGenOutput>%(Filename).g.cs</LastGenOutput>
</None>
</ItemGroup>

<ItemGroup>
<Compile Update="Generated\Guard.Comparable.Numeric.g.cs">
<DesignTime>True</DesignTime>
<Compile Update="**\*.g.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Guard.Comparable.Numeric.tt</DependentUpon>
</Compile>
<Compile Update="Generated\Guard.Collection.g.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Guard.Collection.tt</DependentUpon>
</Compile>
<Compile Update="Generated\ThrowHelper.Collection.g.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>ThrowHelper.Collection.tt</DependentUpon>
</Compile>
<Compile Update="Generated\TypeInfo.g.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>TypeInfo.ttinclude</DependentUpon>
<DependentUpon>$([System.IO.Path]::ChangeExtension(%(Filename), '.tt'))</DependentUpon>
</Compile>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

<!-- On .NET Standard 2.0, the unit test project also needs access to internals -->
<ItemGroup>
<InternalsVisibleTo Include="CommunityToolkit.HighPerformance.UnitTests, PublicKey=$(AssemblySignPublicKey)" />
<InternalsVisibleTo Include="CommunityToolkit.HighPerformance.UnitTests" />
</ItemGroup>

<!-- .NET Standard 2.0 doesn't have the Span<T>, HashCode and ValueTask types -->
Expand Down
2 changes: 1 addition & 1 deletion src/CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

<!-- Add the [InternalsVisibleTo] attribute for the test project -->
<ItemGroup>
<InternalsVisibleTo Include="CommunityToolkit.Mvvm.Internals.UnitTests, PublicKey=$(AssemblySignPublicKey)" />
<InternalsVisibleTo Include="CommunityToolkit.Mvvm.Internals.UnitTests" />
</ItemGroup>

<!--
Expand Down
2 changes: 1 addition & 1 deletion src/CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.targets
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
the same version) but Microsoft.Build.Tasks.CodeAnalysis.dll is where MSBuild loads the compiler tasks from so if
someone is getting creative with msbuild tasks/targets this is the "most correct" assembly to check.
-->
<GetAssemblyIdentity AssemblyFiles="$([System.IO.Path]::Combine(`$([System.IO.Path]::GetDirectoryName($(CSharpCoreTargetsPath)))`,`Microsoft.Build.Tasks.CodeAnalysis.dll`))">
<GetAssemblyIdentity AssemblyFiles="$([System.IO.Path]::Combine($([System.IO.Path]::GetDirectoryName($(CSharpCoreTargetsPath))), 'Microsoft.Build.Tasks.CodeAnalysis.dll'))">
<Output TaskParameter="Assemblies" ItemName="MVVMToolkitCurrentCompilerAssemblyIdentity"/>
</GetAssemblyIdentity>

Expand Down
7 changes: 2 additions & 5 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>

<Import Project="..\Directory.Build.props" />
<Import Project="..\$(MSBuildThisFile)" />

<PropertyGroup>
<IsPackable>true</IsPackable>
Expand All @@ -12,10 +12,7 @@

<!-- Include PolySharp to generate polyfills for all projects (on their .NET Standard 2.x targets) -->
<ItemGroup>
<PackageReference Include="PolySharp" Version="1.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>build; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="PolySharp" Version="1.8.1" PrivateAssets="All" IncludeAssets="Analyzers;Build" />
</ItemGroup>

</Project>
57 changes: 10 additions & 47 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,56 +1,19 @@
<Project>

<Import Project="..\Directory.Build.targets" />
<Import Project="..\$(MSBuildThisFile)" />

<!-- Define NETSTANDARD2_1_OR_GREATER for .NET Standard 2.1 targets and above -->
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0' OR '$(TargetFramework)' == 'net7.0'">
<DefineConstants>NETSTANDARD2_1_OR_GREATER</DefineConstants>
</PropertyGroup>

<!-- Configure trimming for projects on .NET 6 and above -->
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0' OR '$(TargetFramework)' == 'net7.0'">
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<PropertyGroup Condition="'$(_NET_6_OR_GREATER)' == 'true'">
<IsTrimmable>true</IsTrimmable>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<DefineConstants>NETSTANDARD2_1_OR_GREATER</DefineConstants>
</PropertyGroup>

<!--
The following target has been ported from TerraFX.Interop.Windows.
See: https://github.com/terrafx/terrafx.interop.windows.
Using [module: SkipLocalsInit] suppresses the .init flag for local variables for the entire module.
This doesn't affect the correctness of methods in this assembly, as none of them are relying on the
JIT ensuring that all local memory is zeroed out to work. Doing this can provide some minor
performance benefits, depending on the workload.
-->
<PropertyGroup>
<GeneratedSkipLocalsInitFile Condition="'$(GeneratedSkipLocalsInitFile)' == ''">$(IntermediateOutputPath)$(MSBuildProjectName).SkipLocalsInit.g.cs</GeneratedSkipLocalsInitFile>
<GeneratedSkipLocalsInitFileLines>
<![CDATA[//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
[module: global::System.Runtime.CompilerServices.SkipLocalsInitAttribute]]]>
</GeneratedSkipLocalsInitFileLines>
</PropertyGroup>

<Target Name="GenerateSkipLocalsInit"
BeforeTargets="BeforeCompile;CoreCompile"
DependsOnTargets="PrepareForBuild"
Condition="'$(Language)' == 'C#'"
Inputs="$(MSBuildAllProjects)"
Outputs="$(GeneratedSkipLocalsInitFile)">

<!-- Write the file with the attribute -->
<WriteLinesToFile Lines="$(GeneratedSkipLocalsInitFileLines)" Overwrite="true" WriteOnlyWhenDifferent="true" File="$(GeneratedSkipLocalsInitFile)" />
<ItemGroup>
<Compile Include="$(BuildToolsDirectory)AssemblyInfo.Shared.cs" LinkBase="Properties" Visible="False" />
</ItemGroup>

<!-- Include the generated file in the list of files to compile -->
<ItemGroup>
<Compile Include="$(GeneratedSkipLocalsInitFile)" />
</ItemGroup>
</Target>
<ItemGroup>
<InternalsVisibleTo Update="@(InternalsVisibleTo)" PublicKey="$(AssemblySignPublicKey)" Condition="@(InternalsVisibleTo->Count()) != 0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion tests/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>

<Import Project="..\Directory.Build.props" />
<Import Project="..\$(MSBuildThisFile)" />

<PropertyGroup>
<IsPackable>false</IsPackable>
Expand Down
2 changes: 1 addition & 1 deletion tests/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>

<Import Project="..\Directory.Build.targets" />
<Import Project="..\$(MSBuildThisFile)" />

<!-- MSTest v2 -->
<ItemGroup Condition="'$(IsTestProject)' == 'true'">
Expand Down

0 comments on commit 8473017

Please sign in to comment.