PowerShell Script to Create Visual Studio Project Template Extension Zip Issue

前端 未结 1 984
-上瘾入骨i
-上瘾入骨i 2021-01-19 14:45

I\'m trying to write a PowerShell script to write a Visual Studio extension which will simply add a project template. Here is a trimmed down version of the script to demonst

1条回答
  •  独厮守ぢ
    2021-01-19 15:31

    For some reason, [System.IO.Compression.ZipFile]::CreateFromDirectory will not create a zip/vsix file that works correctly, even though it will show as installed. The template does not show in the new project UI.

    Use 7zip instead to create zip files.

    While I tried to investigate this issue, I did not like that the code was not using fully qualified paths, and the strings were hard to look at. I refactored your code a bit.

    Based on my testing, this now works as expected.

    CODE

    <#
    http://stackoverflow.com/questions/40462544/powershell-script-to-create-visual-studio-project-template-extension-zip-issue
    
    For some reason, [System.IO.Compression.ZipFile]::CreateFromDirectory will not create a zip/vsix file that works correctly,
    even though it will show as installed.  The template does not show in the new project UI.
    
    Use 7zip instead to create zip files.
    #>
    
    Set-StrictMode -Version Latest
    $VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
    
    # Makes debugging from ISE easier.
    if ($PSScriptRoot -eq "")
    {
        $root = Split-Path -Parent $psISE.CurrentFile.FullPath
    }
    else
    {
        $root = $PSScriptRoot
    }
    
    Set-Location $root
    
    <#
    Create a zip file with items under Path in the root of the zip file.
    #>
    function New-ZipFile([string]$Path, [string]$FileName)
    {
        $zipExe = 'C:\Program Files\7-Zip\7z.exe'
        $currentLocation = Get-Location
        Set-Location $Path
        & $zipExe a -tzip $FileName * -r
        Set-Location $currentLocation
    }
    
    # Create temporary directories for the zip archives
    
    "Extension", "Template" | % {New-Item (Join-Path $root $_) -ItemType Directory}
    
    # Build up the contents of the template file
    
    $templateContent = @'
    
    
        
            MyExtension
            MyExtension
            MyExtension.ico
            CSharp
            
            2.0
            1000
            61251892-9605-4816-846b-858352383c38
            true
            MyExtension
            true
        
        
            
        
    
    '@
    
    # Save the template file
    
    $templateContent | Out-File (Join-Path $root "Template\MyExtension.vstemplate") -Encoding "UTF8" #-NoNewline
    
    # Build up the contents of the proj file
    
    $projContent = @'
    
    
        
        
        Debug
        AnyCPU
        
        
        2.0
        {403C08FA-9E44-4A8A-A757-1662142E1334}
        {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}
        Library
        Properties
        $safeprojectname$
        $safeprojectname$
        v4.5
        false
        
        
        
        
        
        
        true
        full
        false
        bin\
        DEBUG;TRACE
        prompt
        4
        
        
        pdbonly
        true
        bin\
        TRACE
        prompt
        4
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        10.0
        $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
        
        
        
        
        
        
            
            
                False
                True
                58060
                /
                
                
                False
                True
                http://localhost/
                False
            
            
        
        
        
    
    '@
    
    # Save the proj file
    
    $projContent | Out-File (Join-Path $root "Template\MyExtension.csproj") -Encoding "UTF8" #-NoNewline
    
    # Create the template zip file
    
    New-Item (Join-Path $root "Extension\ProjectTemplates\CSharp\Web\1033") -ItemType Directory
    New-ZipFile (Join-Path $root "Template") (Join-Path $root "Extension\ProjectTemplates\CSharp\Web\1033\MyExtension.zip")
    
    # Create a content types xml file (an error will be thrown if this does not exist)
    
    $conentTypesContent = @'
    
    '@
    
    # Save the content types file
    
    $conentTypesContent | Out-File -literalPath (Join-Path $root "Extension\[Content_Types].xml") -Encoding "UTF8" #-NoNewline
    
    # Now create an extension manifest for the visual studio template
    
    $extensionContent = @'
    
        
            
            MyExtension Project Template
            MyExtension Project Template Extension
        
        
            
        
        
            
        
        
            
        
    
    '@
    
    # Save the extension file
    
    $extensionContent | Out-File (Join-Path $root "Extension\extension.vsixmanifest") -Encoding "UTF8" #-NoNewline
    
    # Create the extension zip file
    
    New-ZipFile (Join-Path $root "Extension") (Join-Path $root "MyExtension.vsix")
    
    # Delete the temporary directories
    "Extension", "Template" | % {Remove-Item (Join-Path $root $_) -Recurse -Force}
    

    0 讨论(0)
提交回复
热议问题