PowerShell script to auto-add Visual Studio “External Tools…” menu options?

后端 未结 2 371
北海茫月
北海茫月 2021-01-14 07:45

We use Nuget to manage our internal toolchain and installing some PowerShell scripts, which we currently manually include in Visual Studio via custom \"External Tools...\" m

2条回答
  •  -上瘾入骨i
    2021-01-14 08:15

    This is possible.

    In the NuGet package's init.ps1 file, which has access to the DTE object, a new tool can be imported from a dynamically created file containing User Settings configuration that's then deleted after the user settings are imported.

    Here's an example of a command to do imports. The example launches Powershell, executing a script in the root directory of a project. Note that I've used VS v9.0 for greatest backwards-compatibility, but it'd be possible to just export the existing tools menu configuration in v15.0 (or newer), and just replace the content in the file below accordingly.

    init.ps1 file content

    # 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:
    '
        
        
        
            
                powershell.exe
                "& ''$(ProjectDir)\Myscript.ps1''"
                "$(ProjectDir)"
                
                true
                false
                true
                false
                true
                false
                false
                {00000000-0000-0000-0000-000000000000}
                0
                Launch Powershell
            
        
    ' >> $fileName
    
    
    # Perform the import of the custom tool
    $project.DTE.ExecuteCommand("Tools.ImportandExportSettings", "/import:""$fileName""");
    
    "--Remove file"
    Remove-Item -path $fileName
    

    MyProject.nuspec (partial) content

    In the .nuspec file, ensure that the init.ps1 file is included. In the case below, the source for the init.ps1 is in a Visual Studio project named 'MyProject' in the 'Scripts' folder.

    
    
        
          ...
        
        
            ...
            
            
        
    
    

提交回复
热议问题