Skip to content

Instantly share code, notes, and snippets.

@leniency
Created April 19, 2014 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leniency/11095547 to your computer and use it in GitHub Desktop.
Save leniency/11095547 to your computer and use it in GitHub Desktop.
MsBuild proj file to create a Nuget package. Uses msbuildtask library for git stuff. This assumes your git tags are in the form of #.# (ie, 1.2) for versioning.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Package">
<PropertyGroup>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<Configuration Condition="$(Configuration) == ''">Release</Configuration>
<Platform Condition="$(Platform) == ''">AnyCPU</Platform>
<BuildDir>bin\$(Configuration)\</BuildDir>
<OutputPath>bin\$(Configuration)\</OutputPath>
<ProjectName Condition="$(ProjectName) == ''"></ProjectName>
<ProjectFile>$(ProjectName).csproj</ProjectFile>
<PackageDir>c:\Packages</PackageDir>
</PropertyGroup>
<PropertyGroup>
<MSBuildCommunityTasksPath>$(SolutionDir).build</MSBuildCommunityTasksPath>
</PropertyGroup>
<!--
Update all the assembly info files with generated version info
This requires Git tags and $(BuildNumber).
-->
<Target Name="UpdateAssemblyInfo">
<Message Text="- Modifying AssemblyInfo files ---------------------------" Importance="high" />
<!-- Fetch the git information. -->
<GitVersion LocalPath="$(SolutionDir)">
<Output TaskParameter="CommitHash" PropertyName="CommitHash" />
</GitVersion>
<GitDescribe LocalPath="$(SolutionDir)" Arguments="--tags">
<Output TaskParameter="CommitCount" PropertyName="CommitCount" />
<Output TaskParameter="Tag" PropertyName="Tag" />
</GitDescribe>
<!-- Fetch all AssemblyInfo files -->
<CreateItem Include="$(SolutionDir)**\AssemblyInfo.cs">
<Output TaskParameter="Include" ItemName="AssemblyInfoFiles"/>
</CreateItem>
<!-- Build the full assebly version. -->
<PropertyGroup>
<Version>$(Tag).$(CommitCount)</Version>
<InformationVersion>$(BuildNumber) ($(CommitHash))</InformationVersion>
</PropertyGroup>
<!-- Clear any read-only attributes on the files. -->
<Attrib Files="@(AssemblyInfoFiles)" Normal="true" />
<!-- Update AssemblyVersion -->
<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="AssemblyVersion\(&quot;.*&quot;\)\]"
ReplacementText="AssemblyVersion(&quot;$(Version)&quot;)]" />
<!-- Update AssemblyInformationalVersion -->
<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="AssemblyInformationalVersion\(&quot;.*&quot;\)\]"
ReplacementText="AssemblyInformationalVersion(&quot;$(InformationVersion)&quot;)]" />
<Message Text="AssemblyInfo files updated to version &quot;$(Version)&quot;" />
<Message Text="AssemblyInformationVersion files updated to version &quot;$(InformationVersion)&quot;" />
<Message Text="----------------------------------------------------------" Importance="high" />
</Target>
<Target Name="BuildLibrary" DependsOnTargets="UpdateAssemblyInfo">
<Message Text="-------------------------------------------------------" Importance="high" />
<Message Text="Building $(ProjectFile)." Importance="high" />
<MSBuild Projects="$(SolutionDir)$(ProjectName)\$(ProjectFile)" Targets="Clean;Rebuild" />
</Target>
<Target Name="Package" DependsOnTargets="BuildLibrary">
<Message Text="-------------------------------------------------------" Importance="high" />
<Message Text="Packaging $(ProjectFile)." Importance="high" />
<Message Text=" Vars: SolutionDir | $(SolutionDir)" Importance="high" />
<Message Text=" ProjectName | $(ProjectName)" Importance="high" />
<Message Text=" ProjectFile | $(ProjectFile)" Importance="high" />
<Message Text=" PackageDir | $(PackageDir)" Importance="high" />
<Message Text=" MSBuildToolsPath | $(MSBuildToolsPath)" Importance="high" />
<Message Text=" MSBuildCommunityTasksPath | $(MSBuildCommunityTasksPath)" Importance="high" />
<!--
Execute the NuGet pack command.
Nuget.exe pack {Project.csproj}
-OutputDirectory {Packages}
-Verbose
-Symbols
-Build
-Properties Configuration=Release
-->
<Exec
Command="&quot;$(SolutionDir).nuget\NuGet.exe&quot; pack &quot;$(SolutionDir)$(ProjectName)\$(ProjectFile)&quot; -OutputDirectory &quot;$(PackageDir)&quot; -Verbose -Symbols -Build -Properties Configuration=Release"/>
</Target>
<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.targets"/>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir).nuget\nuget.targets" />
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment