Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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">
<ParameterGroup>
<AssemblyPath ParameterType="System.String" Required="true" />
<Version ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Diagnostics" />
<Code Type="Fragment" Language="cs">
<![CDATA[
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;
]]>
</Code>
</Task>
</UsingTask>
<!-- 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">
<ItemGroup>
<AssemblyToCheck Include="%(SitecoreAssemblies.Name)" Condition=" Exists('$(TargetDir)%(SitecoreAssemblies.Name)') ">
<Path>$(TargetDir)%(SitecoreAssemblies.Name)</Path>
<FileVersion>%(SitecoreAssemblies.FileVersion)</FileVersion>
</AssemblyToCheck>
</ItemGroup>
</Target>
<!-- 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" />
</GetFileVersion>
<Message Condition=" '$(AssemblyVersion)' != '%(AssemblyToCheck.FileVersion)' " Importance="High" Text="Assembly %(AssemblyToCheck.Identity): Found Version: $(AssemblyVersion), Expected: %(AssemblyToCheck.FileVersion)" />
<PropertyGroup>
<!-- Only create the AssemblyVersionCheckFailed property if there is a mismatch. This indicates that the build should fail -->
<AssemblyVersionCheckFailed Condition=" '$(AssemblyVersion)' != '%(AssemblyToCheck.FileVersion)' ">true</AssemblyVersionCheckFailed>
</PropertyGroup>
</Target>
<!-- 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. " />
</Target>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.