Skip to content

Instantly share code, notes, and snippets.

Created March 25, 2020 15:41
Show Gist options
  • Save cturano/9b81308e6f300aba6ea57a7514fc6333 to your computer and use it in GitHub Desktop.
Save cturano/9b81308e6f300aba6ea57a7514fc6333 to your computer and use it in GitHub Desktop.
Sitecore has released some NuGet packages with a list of Assemblies and Versions distributed as part of the Sitecore release. This MSBuild gist shows you how to use the contents of the SitecoreAssemblies package during your build to generate an error if you reference incorrect assemblies in your project.
<!-- This Using task gets the file version of the assembly passed in "AssemblyPath" and returns the "Version" -->
<UsingTask TaskName="GetFileVersion" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<AssemblyPath ParameterType="System.String" Required="true" />
<Version ParameterType="System.String" Output="true" />
<Using Namespace="System.Diagnostics" />
<Code Type="Fragment" Language="cs">
var fvi = FileVersionInfo.GetVersionInfo(this.AssemblyPath);
// This is done this way because the FileVersion property sometimes pads the private part with a leading zero
this.Version = fvi.FileMajorPart + "." + fvi.FileMinorPart + "." + fvi.FileBuildPart + "." + fvi.FilePrivatePart;
<!-- This Target creates an item group that only contains the assemblies referenced in the project based on the .dll's found in the $(TargetDir) -->
<Target Name="CalculateAssembliesToCheck" Inputs="@(SitecoreAssemblies)" Outputs="%(Name)/Check.nul">
<AssemblyToCheck Include="%(SitecoreAssemblies.Name)" Condition=" Exists('$(TargetDir)%(SitecoreAssemblies.Name)') ">
<!-- This Target verifies the assemblies are the correct version -->
<Target Name="DoVerifyAssemblyVersions" DependsOnTargets="CalculateAssembliesToCheck" Inputs="@(AssemblyToCheck)" Outputs="%(Identity)/Check.nul">
<Message Importance="Normal" Text="Verifying Assembly Version %(AssemblyToCheck.Path)" />
<GetFileVersion AssemblyPath="%(AssemblyToCheck.Path)">
<Output TaskParameter="Version" PropertyName="AssemblyVersion" />
<Message Condition=" '$(AssemblyVersion)' != '%(AssemblyToCheck.FileVersion)' " Importance="High" Text="Assembly %(AssemblyToCheck.Identity): Found Version: $(AssemblyVersion), Expected: %(AssemblyToCheck.FileVersion)" />
<!-- Only create the AssemblyVersionCheckFailed property if there is a mismatch. This indicates that the build should fail -->
<AssemblyVersionCheckFailed Condition=" '$(AssemblyVersion)' != '%(AssemblyToCheck.FileVersion)' ">true</AssemblyVersionCheckFailed>
<!-- This Target calls the Actual verification and only flags an error if there is a mismatched assembly.
This allows the build log to contain a list of all mismatches -->
<Target Name="VerifyAssemblyVersions" AfterTargets="AfterBuild" DependsOnTargets="DoVerifyAssemblyVersions">
<Error Condition=" '$(AssemblyVersionCheckFailed)' == 'true' " Text="One or more refereneced assemblies are not the expected version. Please see the build log for more details. " />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment