visual-studio – 如何向VisualStudio控制台添加命令?

Entity Framework和Nuget都这样做.他们添加了Power
Shell命令行开关,可以从visual studio包管理器控制台启动.

如果我能编写一些可以提交给我的源代码控制并且可以从该控制台为所有开发人员提供的以项目为中心的实用程序,那将是非常好的.

我该怎么做呢?

请注意,我不是在寻找第三方解决方案(例如StudioShell),而且我知道我可以编写正常的PowerShell脚本来执行许多操作.我特别感兴趣的是如何在Visual Studio包管理器控制台(如Get-Package和Update-Database)中编写作为一等公民的函数.

最佳答案 EntityFramework的集成是通过NuGet完成的,因此您无法轻松地在解决方案的项目中实现它.你必须通过一个NuGet包.虽然,您可能可以使用包的本地文件夹.从本质上讲,EF在其软件包的tools文件夹中包含一个普通的PowerShell模块以及一个加载模块的init.ps1.其内容:

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

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

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

打开解决方案文件时,init.ps1由NuGet / VS运行.从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.

点赞