Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add commands to VisualStudio console?

Entity Framework and Nuget both do this. They add powershell commandlets that can be launched from the visual studio package manager console.

Comands in the Visual Studio Console

It would be really great if I could write some project-centric utilities that could be committed to my source control and be available for all developers from that console.

How do I do this?

Note that I'm not looking for a 3rd party solution (eg StudioShell), and that I'm aware that I can just write normal powershell scripts to do many things. I'm interested specifically in how to write functions that are first-class citizens in the Visual Studio package manager console like Get-Package and Update-Database.

like image 304
George Mauer Avatar asked Oct 31 '22 11:10

George Mauer


1 Answers

EntityFramework's integration is done via NuGet, so you can't really have it in a project in your solution easily. You'd have to go through a NuGet package. Although, you could probably make it work using a local folder for the package. Essentially, EF includes a normal powershell module in its package's tools folder along with an init.ps1 that loads the module. Its contents:

param($installPath, $toolsPath, $package, $project)

if (Get-Module | ?{ $_.Name -eq 'EntityFramework' })
{
    Remove-Module EntityFramework
}

Import-Module (Join-Path $toolsPath EntityFramework.psd1)

init.ps1 is run by NuGet/VS when opening the solution file. From the NuGet docs:

Init.ps1 runs the first time a package is installed in a solution.
* If the same package is installed into additional projects in the solution, the script is not run during those installations.
* The script also runs every time the solution is opened (Package Manager Console window has to be open at the same for the script to run). For example, if you install a package, close Visual Studio, and then start Visual Studio and open the solution with Package Manager Console window, the Init.ps1 script runs again.

like image 96
Mike Zboray Avatar answered Nov 09 '22 09:11

Mike Zboray