Pages

Sunday, June 30, 2024

Adding "Hard Clean" option to Visual Studio when Clean&Rebuild don't work

At some point, while we code, we need to start clean, rebuild our projects, and throw away all old irrelevant artifacts, to ensure that the version we are testing includes our most recent changes.

While Visual Studio (like any other decent IDE) provides such an option to clean the artifacts, and empty the bin and obj folders, I found it not perfect all the time, like changes made in the appsettings.json file, don't always get updated after the rebuild.
I needed a way to run my .NET projects with the confidence that they include my most recent changes, so I don't stare at the screen and wonder if what I see is really what I just changed.
The safest option to ensure that the artifacts are being rebuilt is to manually delete them. So I found a way to automate this. Bear with me...

Deleting the bin and obj folders using a PowerShell script

I don't remember how I got it, did I write it? did I ask ChatGPT to write it... I don't remember, the important thing here is that it's working exactly as I want it, you can run it on any path and be assured that it will delete any nested bin/obj folders, it's a simple script:

Get-ChildItem -Path "." .\ -include bin, obj -Recurse | ForEach-Object ($_) { remove-item $_.fullname -Force -Recurse }

Remember to set the path of the PowerShell terminal to the root path you want to scrape its bin/obj folders. Running it on the default path of the PowerShell window will probably take a hell amount of time to finish to go through all the nested bin/obj folders.

Adding the Script to Visual Studio external tools

One of the least-known features of Visual Studio is the External Tools, it allows you to plugin any external tool directly into Visual Studio, and it is such a perfect place to add the script so that we run it easily and conveniently.

From the Tools menu select> External Tools...
In the Window, click Add to add a new "external tool", fill in the fields with these values:

Title: Hard Clean (up to you)
Command: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Arguments: -ExecutionPolicy RemoteSigned -Command "&{ Get-ChildItem -Path "." .\ -include bin, obj -Recurse | ForEach-Object ($_) { remove-item $_.fullname -Force -Recurse }}
Initial directory: $(SolutionDir)
Tick the "Close on exit" option, then click OK to save.

Now, the "Hard Clean" option will appear under the tools menu and it will do the trick> recursively deleting all bin/obj folders under the current solution path.



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Dynamically load jndi.properties when JMeter is running

An approach to having all test configurations in one place is using the jndi.properties file of JMeter. When maintaining many JMeter test su...