Մι∩z•thedev · Follow
Published in Coding · 6 min read · 1 day ago
__
👏65k 💬321 🔖 ⤴️
__
During the Debug phase, multiple build/debug/fix iterations may be needed in a complex solution where a technical debt was accumulated, or where a difficult to debug legacy is present, especially where a loose plugin architecture is used.
Plugin architecture is great because it is open and expandable, however debugging highly introspective and decorrelated code can be tricky.
As a developer, you quickly feel you should do better than the basic build process to save time, so you will be able to focus on the solution bugs. That was the driving idea.
However MSBuild is quite coarse, so I offer a few insights to help reach a more comfortable Debug build process.
.csproj
<?xml version="1.0" encoding="utf-8"?>
<!--
This .csproj exerpt demonstrates how to leverage msbuild and some of its extensions to perform atypical build tasks
-->
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
MSBuild Extension Pack to be added with Nuget
-->
<Import Project="..\..\packages\MSBuild.Extension.Pack.1.9.1\build\net40\MSBuild.Extension.Pack.targets" Condition="Exists('..\..\packages\MSBuild.Extension.Pack.1.9.1\build\net40\MSBuild.Extension.Pack.targets')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<Message Text="=== Check MSBuild.Extension.Pack ===" Importance="High" />
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\MSBuild.Extension.Pack.1.9.1\build\net40\MSBuild.Extension.Pack.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\MSBuild.Extension.Pack.1.9.1\build\net40\MSBuild.Extension.Pack.targets'))" />
</Target>
<!--
in the following Target, I specify a Copy task to be performed before the build.
Observe the Message statement, which allow to report to the build console what we are doing, before or after the task itself.
This is much useful to troubleshoot complex build mechanics.
You will see in some further sections Message where I include 
 to break lines, indeed the build console is sometimes to much condensed to easily spot where the build process has gone, so it is helpful to give some space around critical steps you want to keep an eye on.
here, it is a hotfix to get WCFCore work in a legacy .Net Framework application
-->
<Target Name="CopySPSM" BeforeTargets="Build">
<Message Text="=== Copy System.Private.ServiceModel ===" Importance="High" />
<Copy SourceFiles="G:\devnetx\packages\System.Private.ServiceModel.4.10.3\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(OutputPath)" />
</Target>
<!--
in the following Target, I prepare a dll landscape for a plugin distribution
I execute a powershell inline script that runs a oneliner dos command.
The tricky part here is to escape appropriately and to concatenate it all.
Escaping characters involves several levels (Dos -> Powershell -> Xml).
This proved to be very annoying to put up, but once tuned, the build process is seamless.
&& for &&
\" for "
enclose paths into "..." thus \"...\"
-->
<Target Name="CopyDllsToDist" AfterTargets="CopySPSM">
<Message Importance="High" Text="
 
 
=== Copy ThePlugin dlls to dist ===

 ...
" />
<Exec Command="powershell -Command "Start-Process cmd -ArgumentList('/C', ' (if not exist \"$(ProjectDir)$(OutDir)dist\extralibs\" mkdir \"$(ProjectDir)$(OutDir)dist\extralibs\") &&(if not exist \"$(ProjectDir)$(OutDir)dist\fr\" mkdir \"$(ProjectDir)$(OutDir)dist\fr\") &&copy /Y \"$(ProjectDir)$(OutDir)*.dll\" \"$(ProjectDir)$(OutDir)dist\extralibs\" &&copy /Y \"$(ProjectDir)$(OutDir)fr\ThePlugin.resources.dll\" \"$(ProjectDir)$(OutDir)dist\fr\" &&copy /Y \"$(ProjectDir)$(OutDir)*.config\" \"$(ProjectDir)$(OutDir)dist\extralibs\" &&copy /Y \"$(ProjectDir)$(OutDir)*.pdb\" \"$(ProjectDir)$(OutDir)dist\extralibs\" &&del /s \"$(ProjectDir)$(OutDir)dist\extralibs\IDontWanThis.*\" &&del /s \"$(ProjectDir)$(OutDir)dist\extralibs\IDontWantThat.*\" &&del /s \"$(ProjectDir)$(OutDir)dist\extralibs\NoThanks.dll\" &&del /s \"$(ProjectDir)$(OutDir)dist\extralibs\NoThanks.dll.config\" &&del /s \"$(ProjectDir)$(OutDir)dist\extralibs\NoThanks.pdb\" &&move /Y \"$(ProjectDir)$(OutDir)dist\extralibs\LetsFlattenThis.*\" \"$(ProjectDir)$(OutDir)dist\" ')"" />
<Message Importance="High" Text="
 
 ThePlugin dlls copied to $(ProjectDir)$(OutDir)dist
 

" />
</Target>
<!--
In 'Debug' configuration, I want an early deployment of the plugin to the program files
a message is displayed if adjourning because not in debug. See the condition.
next step is critical and you will see, is protected by user confirmation
-->
<Target Name="MessageCopyDllsToProg" AfterTargets="CopySPSM" Condition="'$(Configuration)' != 'Debug'">
<Message Importance="High" Text="
 
 
=== Copy ThePlugin dlls to program files ===

 ADJOURNED.

 Happens only for Configuration 'Debug'


" />
</Target>
<Target Name="AskStopTheProg" AfterTargets="CopyDllsToDist" Condition="'$(Configuration)' == 'Debug'">
<!--
grasp the developer attention, emitting a beep, indeed next dialog appears minimized
-->
<MSBuild.ExtensionPack.UI.Console TaskAction="Beep" Repeat="1" Duration="250" Frequency="425" />
<!--
ask confirmation to deploy
-->
<MSBuild.ExtensionPack.UI.Dialog TaskAction="Show" Text="Stop TheProg to Copy ThePlugin Debug dlls to Program Files" Button1Text="Continue" Button2Text="Cancel" Title="ThePlugin Debug Deploy">
<Output TaskParameter="ButtonClickedText" PropertyName="answerStopTheProg" />
</MSBuild.ExtensionPack.UI.Dialog>
</Target>
<!--
check the previous dialog returned a Continue answer
then elevate powershell to kill the program that will receive the plugin update
We would not kill the prog if not authorized by the user otherwise work could be lost.
-->
<Target Name="StopTheProgAndCopyDllsToProg" AfterTargets="AskStopTheProg" Condition="'$(Configuration)' == 'Debug' And '$(answerStopTheProg)' == 'Continue'">
<Message Importance="High" Text="
 
 
=== Stopping TheProg ===

 ...
" />
<Exec Command="powershell -Command "Stop-Process -Name TheProg -Force" ContinueOnError="WarnAndContinue" />
<Message Importance="High" Text="
 
 Stopped TheProg.


" />
<PropertyGroup>
<!--
That's the way to declare a custom variable that can later be refered to as $(TheProg) in a string
-->
<TheProg>C:\Program Files\Thecorp\Theprog\</TheProg>
</PropertyGroup>
<Message Importance="High" Text="
 
 
=== Copy ThePlugin Debug dlls to $(TheProg) ===

 ...
" />
<!--
then elevate powershell to copy the prepared plugin distribution to program files
-->
<Exec Command="powershell -Command "Start-Process cmd -ArgumentList('/C', ' @echo off &&echo Copy ThePlugin Debug dlls to $(TheProg) &&(if not exist \"$(TheProg)extralibs\" mkdir \"$(TheProg)extralibs\") &&(if not exist \"$(TheProg)fr\" mkdir \"$(TheProg)fr\") &&copy /Y \"$(ProjectDir)$(OutDir)dist\extralibs\*.*\" \"$(TheProg)extralibs\" &&copy /Y \"$(ProjectDir)$(OutDir)dist\*.*\" \"$(TheProg)" &&copy /Y \"$(ProjectDir)$(OutDir)dist\fr\*.*\" \"$(TheProg)fr\" ') -Verb RunAs"" />
<Message Importance="High" Text="
 
 ThePlugin Debug dlls copied to C:\Program Files\Thecorp\Theprog
 

" />
</Target>
</Project>