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

后端 未结 2 370
北海茫月
北海茫月 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条回答
  • 2021-01-14 08:13

    I am looking for same thing and so far it looks like there is no way how to interact with IDE from PowerShell. But I found this package http://www.nuget.org/packages/StudioShell.Provider/ and at the first sight it allows you to do advanced stuff which you can do only with plugins and extensions. But in my opinion it is little bit overkill for this problem.

    0 讨论(0)
  • 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:
    '<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 (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.

    <?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>
    
    0 讨论(0)
提交回复
热议问题