How can I install Visual Studio Code extensions offline?

后端 未结 12 1157
终归单人心
终归单人心 2020-11-27 09:12

I have installed Visual Studio Code on a machine that is not, and cannot be, connected to the Internet. According to the documentation, I can install an extension from the c

相关标签:
12条回答
  • 2020-11-27 10:03

    As of today the download URL for the latest version of the extension is embedded verbatim in the source of the page on Marketplace, e.g. source at URL:

    https://marketplace.visualstudio.com/items?itemName=lukasz-wronski.ftp-sync

    contains string:

    https://lukasz-wronski.gallerycdn.vsassets.io/extensions/lukasz-wronski/ftp-sync/0.3.3/1492669004156/Microsoft.VisualStudio.Services.VSIXPackage

    I use following Python regexp to extract dl URL:

    urlre = re.search(r'source.+(http.+Microsoft\.VisualStudio\.Services\.VSIXPackage)', content)
    if urlre:
        return urlre.group(1)
    
    0 讨论(0)
  • 2020-11-27 10:05

    I wanted to throw a PowerShell download option on the pile in case anyone else comes across this. I have several offline scenarios and I run this in a loop to download and update all of the extensions I use offline.

    $page = Invoke-WebRequest -Uri 'https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell'
    
    $details = ( $page.Scripts | ? {$_.class -eq 'vss-extension'}).innerHTML | Convertfrom-Json
    
    $extensionName = $details.extensionName 
    $publisher     = $details.publisher.publisherName
    $version       = $details.versions.version
    
    Invoke-WebRequest -uri "$($details.versions.fallbackAssetUri)/Microsoft.VisualStudio.Services.VSIXPackage" `
                      -OutFile "C:\Scripts\extensions\$publisher.$extensionName.$version.VSIX"
    
    0 讨论(0)
  • 2020-11-27 10:07

    Now you can download an extension directly in the "Resources" section, there's a "Download extension" link, I hope this information is still useful.

    0 讨论(0)
  • 2020-11-27 10:08

    For Python users the pattern to use with t3chbot's excellent answer looks like:

    https://marketplace.visualstudio.com/_apis/public/gallery/publishers/ms-python/vsextensions/python/{version_number}/vspackage
    

    be sure to scroll right to see where you have to enter the version number.

    0 讨论(0)
  • 2020-11-27 10:10

    I've stored a script in my gist to download an extension from the marketplace using a PowerShell script. Feel free to comment of share it.

    https://gist.github.com/azurekid/ca641c47981cf8074aeaf6218bb9eb58

    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [string] $Publisher,
    
        [Parameter(Mandatory = $true)]
        [string] $ExtensionName,
    
        [Parameter(Mandatory = $true)]
        [ValidateScript( {
                If ($_ -match "^([0-9].[0-9].[0-9])") {
                    $True
                }
                else {
                    Throw "$_ is not a valid version number. Version can only contain digits"
                }
            })]
        [string] $Version,
    
        [Parameter(Mandatory = $true)]
        [string] $OutputLocation
    )
    
    Set-StrictMode -Version Latest
    $ErrorActionPreference = "Stop"
    
    Write-Output "Publisher:        $($Publisher)"
    Write-Output "Extension name:   $($ExtensionName)"
    Write-Output "Version:          $($Version)"
    Write-Output "Output location   $($OutputLocation)"
    
    $baseUrl = "https://$($Publisher).gallery.vsassets.io/_apis/public/gallery/publisher/$($Publisher)/extension/$($ExtensionName)/$($Version)/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage"
    $outputFile = "$($Publisher)-$($ExtensionName)-$($Version).visx"
    
    if (Test-Path $OutputLocation) {
        try {
            Write-Output "Retrieving extension..."
            [uri]::EscapeUriString($baseUrl) | Out-Null
            Invoke-WebRequest -Uri $baseUrl -OutFile "$OutputLocation\$outputFile"
        }
        catch {
            Write-Error "Unable to find the extension in the marketplace"
        }
    }
    else {
        Write-Output "The Path $($OutputLocation) does not exist"
    }
    
    0 讨论(0)
  • 2020-11-27 10:12

    If you are looking for a scripted solution:

    1. Get binary download URL: you can use an API, but be warned that there is no documentation for it. This API can return an URL to download .vsix files (see example below)
    2. Download the binary
    3. Carefully unzip the binary into ~/.vscode/extensions/: you need to modify unzipped directory name, remove one file and move/rename another one.

    For API start by looking at following example, and for hints how to modify request head to https://github.com/Microsoft/vscode/blob/master/src/vs/platform/extensionManagement/node/extensionGalleryService.ts.

    POST https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery?api-version=5.1-preview HTTP/1.1
    content-type: application/json
    
    {
        "filters": [
            {
            "criteria": [
                {
                    "filterType": 8,
                    "value": "Microsoft.VisualStudio.Code",
                },
                {
                    "filterType": 7,
                    "value": "ms-python.python",
                }
            ],
            "pageNumber": 1,
            "pageSize": 10,
            "sortBy": 0,
            "sortOrder": 0,
            }
        ],
        "assetTypes": ["Microsoft.VisualStudio.Services.VSIXPackage"],
        "flags": 514,
    }
    

    Explanations to the above example:

    • "filterType": 8 - FilterType.Target more FilterTypes
    • "filterType": 7 - FilterType.ExtensionName more FilterTypes
    • "flags": 514 - 0x2 | 0x200 - Flags.IncludeFiles | Flags.IncludeLatestVersionOnly - more Flags
      • to get flag decimal value you can run python -c "print(0x2|0x200)"
    • "assetTypes": ["Microsoft.VisualStudio.Services.VSIXPackage"] - to get only link to .vsix file more AssetTypes
    0 讨论(0)
提交回复
热议问题