Skip to content

Instantly share code, notes, and snippets.

@PaulStovell
Created June 17, 2012 13:52
Show Gist options
  • Save PaulStovell/2944609 to your computer and use it in GitHub Desktop.
Save PaulStovell/2944609 to your computer and use it in GitHub Desktop.
OctoPack.targets
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildDependsOn>
$(BuildDependsOn);
OctopusEstablishContext
</BuildDependsOn>
</PropertyGroup>
<PropertyGroup>
<_CopyWebApplicationDependsOn>
$(_CopyWebApplicationDependsOn);
OctopusEstablishContext
</_CopyWebApplicationDependsOn>
</PropertyGroup>
<!--
Configuration properties - you can override these from the command line
-->
<PropertyGroup>
<OctopusProjectRoot Condition="'$(OctopusProjectRoot)' == ''">$(MSBuildProjectDirectory.TrimEnd('\'))</OctopusProjectRoot>
<OctopusPackageConfiguration Condition="'$(OctopusPackageConfiguration)' == ''">Release</OctopusPackageConfiguration>
<OctopusNuSpecFileName Condition="'$(OctopusNuSpecFileName)' == ''">$(MSBuildProjectName.Replace('.csproj', '').Replace('.vbproj', '')).nuspec</OctopusNuSpecFileName>
<OctopusTemporaryDirectory Condition="'$(OctopusTemporaryDirectory)' == ''">$(OutputPath.TrimEnd('\'))\NuGet-temp</OctopusTemporaryDirectory>
<OctopusPackageVersion Condition="'$(OctopusPackageVersion)' == ''">1.0.0</OctopusPackageVersion>
<OctopusWebConfigFile Condition="'$(OctopusWebConfigFile)' == ''">$(OctopusProjectRoot)\Web.config</OctopusWebConfigFile>
<OctopusOutputDirectory Condition="'$(OctopusOutputDirectory)' == ''">$(OutputPath)</OctopusOutputDirectory>
</PropertyGroup>
<!--
Establsh Context
This target figures out whether this is a web project or normal application, and whether it is being built in release mode.
It then discovers the location of NuGet.exe.
-->
<Target Name="OctopusEstablishContext">
<PropertyGroup>
<OctopusContextIsReleaseBuild Condition="'$(OctopusPackageConfiguration)' == '$(Configuration)' Or '$(WebProjectOutputDir)' == ''">True</OctopusContextIsReleaseBuild>
<OctopusContextIsWebApplication Condition="Exists('$(OctopusWebConfigFile)')">True</OctopusContextIsWebApplication>
</PropertyGroup>
<Message Text="IsReleaseBuild: $(OctopusContextIsReleaseBuild)" />
<Message Text="IsWebApp: $(OctopusContextIsWebApplication)" />
<Message Text="This is not a $(OctopusPackageConfiguration) build, so any Octopus packaging will be skipped" Importance="High" Condition="'$(OctopusContextIsReleaseBuild)' != 'True'" />
<Message Text="This is a $(OctopusPackageConfiguration) build of a web application, so an Octopus package will be created" Importance="High" Condition="'$(OctopusContextIsReleaseBuild)' == 'True' And '$(OctopusContextIsWebApplication)' == 'True'" />
<Message Text="This is a $(OctopusPackageConfiguration) build of an executable, so an Octopus package will be created" Importance="High" Condition="'$(OctopusContextIsReleaseBuild)' == 'True' And '$(OctopusContextIsWebApplication)' != 'True'" />
<Message Text="OctopusProjectRoot: $(OctopusProjectRoot)" />
<Message Text="OctopusNuGetExePath: $(OctopusNuGetExePath)" />
<CallTarget Targets="OctopusPackageWeb" Condition="'$(OctopusContextIsReleaseBuild)' == 'True' And '$(OctopusContextIsWebApplication)' == 'True'" />
<CallTarget Targets="OctopusPackageApp" Condition="'$(OctopusContextIsReleaseBuild)' == 'True' And '$(OctopusContextIsWebApplication)' != 'True'" />
</Target>
<!--
Find NuGet.exe and the project NuSpec
-->
<Target Name="OctopusFindNuGetSettings">
<Error
Code="OCTO: 1001"
Text="OctoPack: Expected to find a NuGet spec file named $(OctopusNuSpecFileName) under $(MSBuildProjectDirectory). You can fix this error by adding a NuGet spec file with that name, or override the name of the file by setting a property called OctopusNuSpecFileName in your project."
Condition="!Exists('$(OctopusNuSpecFileName)')"
/>
<ItemGroup>
<FindNuGet Include="$(OctopusNuGetExePath)" />
<FindNuGet Include="$(OctopusProjectRoot)\packages\NuGet.CommandLine*\**\Tools\NuGet.exe" />
<FindNuGet Include="$(OctopusProjectRoot)\.nuget\NuGet*.exe" />
<FindNuGet Include="$(OctopusProjectRoot)\..\packages\NuGet.CommandLine*\**\Tools\NuGet.exe" />
<FindNuGet Include="$(OctopusProjectRoot)\..\.nuget\NuGet*.exe" />
<FindNuGet Include="$(OctopusProjectRoot)\..\..\packages\NuGet.CommandLine*\**\Tools\NuGet.exe" />
<FindNuGet Include="$(OctopusProjectRoot)\..\..\.nuget\NuGet*.exe" />
<FindNuGet Include="$(OctopusProjectRoot)\..\..\..\packages\NuGet.CommandLine*\**\Tools\NuGet.exe" />
<FindNuGet Include="$(OctopusProjectRoot)\..\..\..\packages\.nuget*\NuGet.exe" />
</ItemGroup>
<PropertyGroup>
<NuGetExe Condition="'$(OctopusNuGetExePath)' == ''">@(FindNuGet)</NuGetExe>
<NuGetExe Condition="'$(OctopusNuGetExePath)' != ''">$(OctopusNuGetExePath)</NuGetExe>
</PropertyGroup>
<Error
Code="OCTO: 1002"
Text="OctoPack: Could not locate NuGet.exe. Please add a NuGet package reference for NuGet.CommandLine, or set the property OctopusNuGetExePath in your project. Current search path is: $(NuGetExe)"
Condition="!Exists('$(NuGetExe)')"
/>
<Message Importance="High" Text="NuGet.exe found: $(NuGetExe)"/>
</Target>
<!--
Packaging Web Applications
-->
<Target Name="OctopusPackageWeb" DependsOnTargets="$(OnBefore_CopyWebApplicationDefault);OctopusFindNuGetSettings">
<Message Text="Project was published to: $(OutDir)" />
<Copy SourceFiles="$(OctopusNuSpecFileName)" DestinationFolder="$(WebProjectOutputDir)" />
<MakeDir Directories="$(OctopusOutputDirectory.TrimEnd(&quot;\&quot;))"/>
<Exec Command='"$(NuGetExe)" pack "$(WebProjectOutputDir.TrimEnd(&quot;\&quot;))\$(OctopusNuSpecFileName)" -OutputDirectory "$(OctopusOutputDirectory.TrimEnd(&quot;\&quot;))" -basePath "$(WebProjectOutputDir.TrimEnd(&quot;\&quot;))" -Version "$(OctopusPackageVersion)" -NoPackageAnalysis' />
<ItemGroup>
<PackagesBuilt Include="$(OctopusOutputDirectory.TrimEnd(&quot;\&quot;))\*.nupkg" />
</ItemGroup>
<Message Importance="High" Text="Copy packages: @(PackagesBuilt) to $(OutDir)"/>
<Copy SourceFiles="@(PackagesBuilt)" DestinationFolder="$(OutDir)" />
</Target>
<!--
Packaging windows services and console applications
-->
<Target Name="OctopusPackageApp" DependsOnTargets="OctopusFindNuGetSettings">
<RemoveDir Directories="$(OctopusTemporaryDirectory)" ContinueOnError="true" />
<MakeDir Directories="$(OctopusTemporaryDirectory)" />
<ItemGroup>
<ContentToPackage Include="$(OutputPath)\**\*" Exclude="$(OutputPath)\**\*.vshost.*;$(OutputPath)\**\*.nupkg" />
</ItemGroup>
<Copy
SourceFiles="@(ContentToPackage)"
DestinationFiles="@(ContentToPackage->'$(OctopusTemporaryDirectory)\%(RecursiveDir)%(Filename)%(Extension)')"
/>
<Copy SourceFiles="$(OctopusNuSpecFileName)" DestinationFolder="$(OctopusTemporaryDirectory)" />
<MakeDir Directories="$(OctopusOutputDirectory.TrimEnd(&quot;\&quot;))"/>
<Exec Command='"$(NuGetExe)" pack "$(OctopusTemporaryDirectory)\$(OctopusNuSpecFileName)" -OutputDirectory "$(OctopusOutputDirectory.TrimEnd(&quot;\&quot;))" -basePath "$(OctopusTemporaryDirectory)" -Version "$(OctopusPackageVersion)" -NoPackageAnalysis' />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment