As some of you may know, I regularly speak at conferences & create courses for Pluralsight. These types of talks and courses tend to include a lot of demos I distribute afterwards – and I can’t always just put ‘m on GitHub.
So, I need to clean everything up: remove unnecessary files, remove bin/obj folders, remove source control bindings, etc. Especially for courses this gets tedious very fast (the course I’m currently working on includes more than 100 different visual studio solutions), so I was looking into a way to automate cleaning up the exercise directories, removing source control bindings, and afterwards: check if NuGet package restore works & if the build still works.
Maarten Balliauw pointed me in the right direction: Powershell. So I started looking around, and found a great post by Daniel Thompson.
It didn’t completely fit my requirements, so I adjusted this somewhat:
Lastly, this specific set of demo files I’m currently working on contain mdf & ldf files (DB) – I don’t want to distribute those for each and every starter/finished demo, but only once so the file download size is as low as possible. Therefore, I also remove those.
get-childitem . -include *.vssscc,*.user,*.vspscc,
*.v12.suo,*.mdf,*.ldf,debug,packages,bin,obj -recurse -force |
%{
remove-item $_.fullname -force -recurse
}
Up next is removing all the source control bindings from solution & project files (these are coming from Daniel’s post). I tend to commit all the code I work on to (in this case) TFS Online, but it makes no sense to leave those bindings there when distributing the code: all the end user will get is an annoying “unauthorized to connect to TFS online” message.
# Remove the bindings from the sln files
get-childitem . -include *.sln -recurse |
%{
$file = $_;
$inVCSection = $False;
get-content $file |
%{
$line = $_.Trim();
if ($inVCSection -eq $False -and $line.StartsWith('GlobalSection') -eq $True -and $line.Contains('VersionControl') -eq $True) {
$inVCSection = $True
}
if ($inVCSection -eq $False) {
add-content ($file.fullname + '.new') $_
}
if ($inVCSection -eq $True -and $line -eq 'EndGlobalSection') {
$inVCSection = $False
}
}
mv ($file.fullname + '.new') $file.fullname -force
}
# Remove the bindings from the csproj files
get-childitem . -include *.csproj -recurse |
%{
$file = $_;
get-content $file |
%{
$line = $_.Trim();
if ($line.StartsWith('<Scc') -eq $False) {
add-content ($file.fullname + '.new') $_
}
}
mv ($file.fullname + '.new') $file.fullname -force
}
That’s it – clean solutions everywhere! 🙂
But, well, I kinda want to be sure package restore & a subsequent build will still work. So here’s two more scripts I run after having cleaned up everything, just to make sure the demos still work as expected.
First, test if NuGet restore effectively restores everything that has to be restored:
$nuget = "C:\nuget\nuget.exe"
get-childitem . -include *.sln -recurse |
%{
$restorepackages = "$nuget restore ""$_"""
invoke-expression $restorepackages
}
Next, test if the build still works (/p:VisualStudioVersion=12.0 is required in my case b/c I use WebDeploy to Azure):
$msbuild = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe"
get-childitem . -include *.sln -recurse |
%{
$buildsln = "$msbuild ""$_"" /p:VisualStudioVersion=12.0"
invoke-expression $buildsln
}
These two scripts are what I consider a failsafe – afterwards, I run the first set of scripts again to clean
everything up, and then I’m ready to distribute the demos. As always, your mileage may differ & you might need a few adjustment before using these scripts (these are definitely aimed at my own requirements) – but these work for me, and they should be enough to get you started.
Hope this helps some of you out 🙂