Skip to content

Instantly share code, notes, and snippets.

@Juansero29
Created November 21, 2022 16: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 Juansero29/da2aff107df9d27208a2b9735bc92577 to your computer and use it in GitHub Desktop.
Save Juansero29/da2aff107df9d27208a2b9735bc92577 to your computer and use it in GitHub Desktop.
A script to use in Visual Studio projects to delete bin/obj when VS gives weird compilation errors or warnings, useful cleaning script
# How to use this script
#
# - This scripts needs to be launched in a folder containing a Visual Studio solution (.sln)
#
# - It is recommended that Visual Studio is closed during the execution of this script
#
# - It is best if the latest changest have been pulled with git before using this script
#
# - All necessary environement variables must be set in order to the script to work appropriately
# - Team Explorer
# (C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer)
#
# - NuGet
# (Download the '.exe', for example in the folder "C:\path\nuget.exe". Download here: https://www.nuget.org/downloads (recommended latest version)
#
# - MSBuild [Optional, not used because line is commented]
# (C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\)
#
# This script can fix problems with MAUI/Xamarin solutions, for example :
#
# - Compilation issues tied to the 'Linker' or due to '.target' files.
$folderPath = '.\'
$value = $false
# Iterate through all files and force "IsReadOnly" flag to false
"Setting ReadOnly property to false"
Get-ChildItem -Path $folderPath -Include bin,obj -Recurse | ForEach-Object ($_) {
$item = Get-Item -Path $_.FullName
if($item -isnot [System.IO.DirectoryInfo])
{
# Information message
"Setting " + $item.Name + " ReadOnly property to false"
# Set flag to false
$item.IsReadOnly = $value
}
}
# Re-iterate through all files, but include only those named "bin" or "obj"
Get-ChildItem -Path $folderPath -Include bin,obj -Recurse | ForEach-Object ($_) {
if(!$_.FullName.Contains("References\bin"))
{
Remove-Item $_.FullName -Force -Recurse
$_.FullName + " deleted"
}
}
# Iterate the folder in which the script was launched and for each .sln execute the following actions
Get-ChildItem -Path $folderPath | Where-Object { $_.Extension -eq ".sln"} | ForEach-Object ($_) {
# Information message
"Getting Latest for " + $_.FullName
# Get latest if using TFS
#tf.exe get $tfPath /recursive
# Pull latest changes if using git
git pull
# Information message
"Nuget Restoring " + $_.FullName
# Restore nuget packages so everything is OK when we reopen the solution
nuget restore $_.FullName;
$confirmation = Read-Host "Do you want to build the solution? Type y for yes or n for no."
if ($confirmation -eq 'y')
{
# Information message
"Building " + $_.FullName
# Build solution
MsBuild $_.FullName -p:Configuration=Debug
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment