我们使用Nuget来管理内部工具链并安装一些PowerShell脚本,目前我们通过自定义“外部工具...”菜单选项将这些脚本手动包含在Visual Studio中。

如果有一个PowerShell脚本自动添加通常需要“外部工具...”对话框的菜单选项,我将喜欢它。是否有API可以做到这一点?

我已经编写了大部分的Dev On-boarding过程脚本,以自动配置dev机器,但有一些奇怪的地方。

任何想法表示赞赏。

最佳答案

这个有可能。

在可以访问DTE对象的NuGet包的init.ps1文件中,可以从包含用户设置配置的动态创建文件中导入新工具,然后在导入用户设置后将其删除。

这是执行导入命令的示例。该示例启动Powershell,并在项目的根目录中执行脚本。请注意,我已经使用VS v9.0来实现最大的向后兼容性,但是可以仅导出v15.0(或更高版本)中的现有工具菜单配置,并相应地替换下面文件中的内容。

init.ps1文件的内容

# NuGet Package installation script. Run
# first time NuGet package installed to solution.
param($installPath, $toolsPath, $package, $project)

# Determine fully qualified path to a temp file
$fileName = [System.IO.Path]::GetTempPath() + [System.Guid]::NewGuid().ToString() + ".vssettings";

# Create User Settings file:
'<UserSettings>
    <ApplicationIdentity version="9.0"/>
    <ToolsOptions/>
    <Category name="Environment_Group" RegisteredName="Environment_Group">
        <Category name="Environment_ExternalTools" Category="{E8FAE9E8-FBA2-4474-B134-AB0FFCFB291D}" Package="{DA9FB551-C724-11d0-AE1F-00A0C90FFFC3}" RegisteredName="Environment_ExternalTools" PackageName="Visual Studio Environment Package">
            <PropertyValue name="Launch Powershell.Command">powershell.exe</PropertyValue>
            <PropertyValue name="Launch Powershell.Arguments">"&amp; ''$(ProjectDir)\Myscript.ps1''"</PropertyValue>
            <PropertyValue name="Launch Powershell.InitialDirectory">"$(ProjectDir)"</PropertyValue>
            <PropertyValue name="Launch Powershell.SourceKeyName"/>
            <PropertyValue name="Launch Powershell.UseOutputWindow">true</PropertyValue>
            <PropertyValue name="Launch Powershell.PromptForArguments">false</PropertyValue>
            <PropertyValue name="Launch Powershell.CloseOnExit">true</PropertyValue>
            <PropertyValue name="Launch Powershell.IsGUIapp">false</PropertyValue>
            <PropertyValue name="Launch Powershell.SaveAllDocs">true</PropertyValue>
            <PropertyValue name="Launch Powershell.UseTaskList">false</PropertyValue>
            <PropertyValue name="Launch Powershell.Unicode">false</PropertyValue>
            <PropertyValue name="Launch Powershell.Package">{00000000-0000-0000-0000-000000000000}</PropertyValue>
            <PropertyValue name="Launch Powershell.NameID">0</PropertyValue>
            <PropertyValue name="ToolNames">Launch Powershell</PropertyValue>
        </Category>
    </Category>
</UserSettings>' >> $fileName


# Perform the import of the custom tool
$project.DTE.ExecuteCommand("Tools.ImportandExportSettings", "/import:""$fileName""");

"--Remove file"
Remove-Item -path $fileName

MyProject.nuspec(部分)内容

.nuspec文件中,确保包括init.ps1文件。在以下情况下,init.ps1的源位于“脚本”文件夹中名为“MyProject”的Visual Studio项目中。
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
      ...
    </metadata>
    <files>
        ...
        <!-- NuGet Package installation script. Run first time NuGet package installed to solution. -->
        <file src="Scripts\init.ps1" target="tools\init.ps1" />
    </files>
</package>

10-01 02:39
查看更多