How to execute scripts when opening a Solution File

Question

Thursday, November 8, 2012 3:04 PM

Briefly said: I would like to know how to execute a script when opening a Visual Studio Solution File, ideally, the script being "referenced" in that Solution File ?

Here is the long story:

We are using TFS to store "good old" Web Site Projects that cannot be upgraded to Web Application Projects. Those are hosted in IIS on the development workstations (not in WebDev or IIS Express).

Those Web Sites Projects are branched in TFS (E.x.: Release 1.1, Release 1.2, ...)

When opening a Web Site Project for the first time (ex. from the branch "Release 1.1"), a Virtual Directory is created in IIS and mapped on the physical location corresponding to the local path of the branch.

When opening later this Web Site Project from another branch (ex. from the branch "Release 1.2"), Visual Studio can

- either reuse the existing Virtual Directory with its current physical location (corresponding to the local path of the "previous" branch) - Indeed, the physical location of the Virtual Directory is not updated as it happens with Web Application Projects!

- or create a new Virtual Directory with a suffix like "_1".

In the first case, Visual Studio silently creates mapping in your workspace to fetch the source from the current branch (Release 1.2) in the local path of the previous branch (Release 1.1), as far as you did only map the TFS server root ("$\) or the TFS Team Project Root ("$\TeamProject>") in your workspace. This is completely messing up your workspace as eventually working back on the project opened from the first branch (Release 1.1) will actually check-out files in the second branch (Release 1.2).

In the second case, the URL of the site run for debugging purpose won't be the expected one and you could experience issues if your have dependencies on that URL (ex.: in navigation scenario)

The only valid approach is therefore to delete any existing Virtual Directory before opening a Solution containing a related Web Site Projects. Visual Studio will then next re-create a Virtual Directory correctly mapped....

I could to this via a Script but I would need to run that script when opening a Visual Studio Solution File. Any idea how to proceed ?

Thx a lot in adv. for any suggestions. The simplest one will be my favorite especially the ones that do not require deployment of VS addins on all the development workstations (a solution based on scripts stored in TFS next to the Solution File would be great)

Valéry Letroye

All replies (8)

Saturday, February 7, 2015 6:06 PM ✅Answered | 1 vote

I know this is old, and I found it looking for this exact same thing, but one solution is to make a nuget package that only has a tools directory. Put any scripts you want in there, and add an init.ps1 file as well. init.ps1 will get run every time the solution opens. For example, to import a single module ps1 file (just because that's easier to show than a fully fleshed out module) I would write one like this:

param($installPath, $toolsPath, $package)
 
Import-Module (Join-Path $toolsPath SolutionUtilities.ps1)

Now SolutionUtilities.ps1 will be imported every time the solution opens.

To install it into a solution select Manage Nuget Packages on the solution, search for the package (I will leave learning how to create and deploy nuget packages as an excersize for the reader) and install it. If there is only a tools directory it will not ask what project to install it to, and will instead add a package.config folder to the solution itself.

The one caveat here is that running update package on it doesn't seem to work as expect for project level packages. It will actually just add a NEW reference to the package. So if you have 1.0 and you push out 1.1 and the developers hit "update" you will end up with references to BOTH 1.0 and 1.1. Both scripts will get ran. THe way modules work, this actually means the oldest module definitions will supercede the newer ones. So you have to always uninstall the package and re-install instead of just calling update.

Hope this helps someone.


Friday, November 9, 2012 8:25 AM

Hi Valery,

What scripts you want to execute?

>the script being "referenced" in that Solution File

I don't know how to execute a script as you said. However if we create an add-in, we can do something like represent a window box when the solution is opening.

Is it what you want?

Best regards, 

Ego [MSFT]
MSDN Community Support | Feedback to us


Friday, November 9, 2012 10:36 AM

The problem of an Add-In is that it must be deployed on all the development workstations and we have no process here to "push" such components to the workstations...

So, I would prefer to run something like a .bat or a .vps. It would actually be very similar to executing Pre-Build Events but when loading the solution...

Valéry Letroye


Monday, November 12, 2012 7:19 AM | 1 vote

Hi Valery,

You can add your batch file as a command in Tools | External Tools.

Once you do this, your batch file will appear as one of the items on your tools menu. You can run it before you open a Visual Studio Solution File in Visual Studio.

Can this help you?

Best regards,

Ego [MSFT]
MSDN Community Support | Feedback to us


Monday, November 12, 2012 8:38 AM

Thx a lot.

It can help the developers, giving them a direct access to the script from within VS IDE.

However, it's not yet a "transparent" solution as developers will all have to configure this "external tool" manually. I was hoping a solution without any manipulation...

If such a solution does not exist, yours is a smart and helpful one ;)

Valéry Letroye


Wednesday, November 14, 2012 9:09 AM

Hi Valery,

I marked my reply as answer.

If you found a better way, please post it here and mark yourself as answer.

Thank you for your understanding and support.

Best regards,

Ego [MSFT]
MSDN Community Support | Feedback to us


Monday, February 9, 2015 7:36 AM

That's a very good tip.

Thx for sharing!

Valéry Letroye


Thursday, May 10, 2018 3:09 PM

Only a hint: unfortenately init.ps1 is only executed in case of the user is showing the Nuget Packet Manager Console.