Skip to content

Instantly share code, notes, and snippets.

@mwwhited
Last active February 2, 2024 22:51
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 mwwhited/34dba580d47bb3b8eb82970c27b915a7 to your computer and use it in GitHub Desktop.
Save mwwhited/34dba580d47bb3b8eb82970c27b915a7 to your computer and use it in GitHub Desktop.
Custom build extensions for .Net
<Project>
<PropertyGroup Condition="'$(SolutionDir)' == ''">
<!-- Resolve solution dir if not set -->
<SolutionDir Condition="'$(SolutionDir)' == '' and $([System.IO.Directory]::GetFiles('../', '*.sln').get_Length()) != 0">../</SolutionDir>
<SolutionDir Condition="'$(SolutionDir)' == '' and $([System.IO.Directory]::GetFiles('../../', '*.sln').get_Length()) != 0">../../</SolutionDir>
<SolutionDir Condition="'$(SolutionDir)' == '' and $([System.IO.Directory]::GetFiles('../../../', '*.sln').get_Length()) != 0">../../../</SolutionDir>
<SolutionDir Condition="'$(SolutionDir)' == '' and $([System.IO.Directory]::GetFiles('../../../../', '*.sln').get_Length()) != 0">../../../../</SolutionDir>
<SolutionDir Condition="'$(SolutionDir)' == '' and $([System.IO.Directory]::GetFiles('../../../../../', '*.sln').get_Length()) != 0">../../../../../</SolutionDir>
<SolutionDir Condition="'$(SolutionDir)' != ''">$([System.IO.Path]::GetFullPath('$(SolutionDir)'))</SolutionDir>
</PropertyGroup>
<PropertyGroup>
<Company>Out-of-Band Development</Company>
<Authors>$(USERNAME)@$(USERDNSDOMAIN)</Authors>
<Copyright>$(Company), $([System.DateTime]::Now.ToString('yyyy'))</Copyright>
<TestResultsOutFolderPath>$(SolutionDir)TestResults\</TestResultsOutFolderPath>
<VSTestResultsDirectory>$(SolutionDir)TestResults\</VSTestResultsDirectory>
<RunSettingsFilePath>$(SolutionDir).runsettings</RunSettingsFilePath>
</PropertyGroup>
<PropertyGroup Condition="Exists('$(SolutionDir)LICENSE')">
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>
<ItemGroup Condition="Exists('$(SolutionDir)LICENSE')">
<LicenseFile Include="$(SolutionDir)LICENSE" />
<None Include="@(LicenseFile)" Pack="true" PackagePath="\" />
</ItemGroup>
<PropertyGroup Condition="'$(LOCAL_NUGET)' != ''">
<PackageOutputPath>$(LOCAL_NUGET.Trim('\'))\</PackageOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(LOCAL_NUGET)' == ''">
<PackageOutputPath>$(SolutionDir)publish\packages</PackageOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(DOCUMENT_PATH)' != ''">
<DocumentPath>$(DOCUMENT_PATH.Trim('\'))\</DocumentPath>
</PropertyGroup>
<PropertyGroup Condition="'$(DOCUMENT_PATH)' == ''">
<DocumentPath>$(SolutionDir)docs\</DocumentPath>
</PropertyGroup>
<PropertyGroup Condition="'$(CodeDocumentPath)' == ''">
<CodeDocumentPath>$(DocumentPath)code\</CodeDocumentPath>
</PropertyGroup>
<Target Name="Details">
<Message Text="===== SolutionDir: $(SolutionDir)" Importance="high"/>
<Message Text="===== TestResultsOutFolderPath: $(TestResultsOutFolderPath)" Importance="high"/>
<Message Text="===== DocumentPath: $(DocumentPath)" Importance="high"/>
<Message Text="===== CodeDocumentPath: $(CodeDocumentPath)" Importance="high"/>
<Message Text="===== LicenseFile: @(LicenseFile)" Importance="high"/>
<Message Text="===== PackageOutputPath: $(PackageOutputPath)" Importance="high"/>
<Message Text="===== PackageLicenseFile: $(PackageLicenseFile)" Importance="high"/>
</Target>
<ItemGroup>
<!-- GitVersion will automatically calculate and maintain the version of projects within your solution -->
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<!-- This added additional documentation to your build output -->
<CodeDocumentation Include="**\*.md" />
<CodeDocumentation Include="**\*.plantuml" />
<CodeDocumentation Include="**\*.puml" />
<CodeDocumentation Remove="bin\**\*.*" />
<CodeDocumentation Remove="obj\**\*.*" />
<CodeDocumentation Remove="TestResults\**\*.*" />
<!-- these items will be automatically included as embedded resources in your assembly. -->
<Examples Include="**\*.txt" />
<Examples Include="**\*.json" />
<Examples Include="**\*.html" />
<Examples Include="**\*.csv" />
<Examples Include="**\*.sql" />
<Examples Include="**\*.xml" />
<Examples Remove="bin\**\*.*" />
<Examples Remove="obj\**\*.*" />
<Examples Remove="TestResults\**\*.*" />
<Content Include="@(CodeDocumentation)" Pack="true" PackagePath="\docs" />
<EmbeddedResource Include="@(Examples)" Pack="true" PackagePath="\examples" />
</ItemGroup>
<ItemGroup Condition="'$(PackageReadmeFile)'!='' and Exists('$(PackageReadmeFile)')">
<None Remove="$(PackageReadmeFile)" />
<None Include="$(PackageReadmeFile)" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>
<Project>
<!--
when `dotnet clean` is not clean enough...
`dotnet clean /T:DeepClean` ensures all of the build and intermediate files are removed
-->
<Target Name="DeepClean">
<Message Text="===== Really cleaning project: $(Configuration) - &quot;$(MSBuildProjectName)&quot;" Importance="high"/>
<Message Text="===== SolutionDir: $(SolutionDir)" Importance="high"/>
<RemoveDir Directories="$(BaseIntermediateOutputPath)" Condition="Exists('$(BaseIntermediateOutputPath)')" ContinueOnError="true" />
<RemoveDir Directories="$(BaseOutputPath)" Condition="Exists('$(BaseOutputPath)')" ContinueOnError="true" />
<RemoveDir Directories="$(TestResultsOutFolderPath)" Condition="Exists('$(TestResultsOutFolderPath)')" ContinueOnError="true" />
</Target>
<Target Name="CleanPaths">
<Message Text="===== Check: $(SolutionDir)" Importance="high"/>
<ItemGroup>
<Directories Include="$([System.IO.Directory]::GetDirectories('$(SolutionDir)', '*', System.IO.SearchOption.AllDirectories))" />
<Directories>
<Files>$([System.IO.Directory]::GetFiles("%(Directories.Identity)", "*", System.IO.SearchOption.AllDirectories).get_Length())</Files>
</Directories>
</ItemGroup>
<RemoveDir Directories="@(Directories)" Condition="%(Files)=='0'" />
</Target>
<Target Name="GetDocumentation" Condition="'@(CodeDocumentation)' != ''">
<!--AfterTargets="PostBuildEvent"-->
<Message Text="===== Get Documentation: $(Configuration) - &quot;$(MSBuildProjectName)&quot;" Importance="high"/>
<RemoveDir Directories="$(CodeDocumentPath)$(MSBuildProjectName)" ContinueOnError="true" />
<Message Text="CodeDocumentation: @(CodeDocumentation) -> $(CodeDocumentPath)$(MSBuildProjectName)" Importance="high"/>
<Copy SourceFiles="@(CodeDocumentation)" DestinationFolder="$(CodeDocumentPath)$(MSBuildProjectName)" />
</Target>
<!-- Ensure the PackageReadmeFile is included with the projects to be packaged for nuget -->
<Target Name="CheckReadMe" Condition="'$(IsPackable)'=='true' and '$(PackageReadmeFile)'==''" BeforeTargets="Build">
<Warning Text="No PackageReadmeFile included for &quot;$(MSBuildProjectName)&quot;"
Code="ELIPK001"
File="$(MSBuildProjectName).csproj" />
</Target>
<Target Name="CheckReadMeExists" Condition="'$(PackageReadmeFile)'!='' and !Exists('$(PackageReadmeFile)')" BeforeTargets="Build">
<Error Text="&quot;$(PackageReadmeFile)&quot; missing from &quot;$(MSBuildProjectName)&quot;"
Code="ELIPK002"
File="$(MSBuildProjectName).csproj" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment