7

離線安裝 VSCode 與 Extension .vsix 批次下載

 2 years ago
source link: https://blog.darkthread.net/blog/install-vscode-offline/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
離線安裝 VSCode 與 Extension .vsix 批次下載-黑暗執行緒

愈來愈多軟體採用隨選下載安裝,安裝程式只包含主程式,其他需要的模組、套件會等用到時再從 Internet 下載自動安裝,如此使用者不需要下載好幾 GB 的安裝程式,裡面包含一堆這輩子都不會用到內容,用多少下載多少,能節省可觀的頻寬與時間,說來聰明又環保。

VSCode 也依循這樣的設定哲學,剛安裝好時不包含任何 Extension (擴充套件),但除了手動尋找安裝,VSCode 還能識別副檔名建議下載 Extension。像是第一次編輯 .ps1 檔案便會出現以下提示:

不過,這種方便設計一旦遇到不能連上 Internet 的環境就要吃癟了。

所幸,除了直接下載安裝,VSCode Extension 也支援以 .vsix 檔形式安裝,Marketplace 網頁都有下載連結,方便預先下載再部署到目的主機安裝。

.vsix 可透過操作介面安裝:

或直接跑指令:

code --install-extension  <extension-vsix-path>

基本上,這樣就能做到離線安裝了。

且慢,所以我得列出 Extension 清單,瀏覽網站找到 Extension 一個個找到 Download 連結下載存成 .vsix 檔案?那不如一刀給我個痛快。

Yes,自動下載 VSCode 已安裝 Extension 的 PowerShell 腳本來惹!

# 列出已安裝 VSCode Extension 清單
$list = code --list-extensions

[System.IO.Directory]::CreateDirectory("$(Get-Location)\vsix") | Out-Null
# 下載  VSCode Extension .vsix
function DownloadVsix([string]$extName) {
    $reqBody = @"
    {
        "filters": [
            {
                "criteria": [
                    {
                        "filterType": 8,
                        "value": "Microsoft.VisualStudio.Code"
                    },
                    {
                        "filterType": 10,
                        "value": "$extName"
                    },
                    {
                        "filterType": 12,
                        "value": "4096"
                    }
                ],
                "pageNumber": 1,
                "pageSize": 50,
                "sortBy": 0,
                "sortOrder": 0
            }
        ],
        "assetTypes": [],
        "flags": 950
    }
"@
    $res = (Invoke-WebRequest -Uri https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery -Method POST -ContentType 'application/json' -Body $reqBody).Content | ConvertFrom-Json
    ($res.results.extensions.versions | Where-Object { !$_.targetPlatform -or $_.targetPlatform -eq 'win32-x64' }).files | 
        Where-Object { $_.assetType -eq 'Microsoft.VisualStudio.Services.VSIXPackage' }  | 
        Select-Object -ExpandProperty source -First 1 | ForEach-Object {
            Write-Host "Downloading $extName"
            Invoke-WebRequest -Uri $_ -OutFile "vsix\$extName.vsix"
        }
}
$list | ForEach-Object { DownloadVsix $_ }

以上腳本先用 code --list-extensions 列出 VSCode 已安裝的 Extension 清單,再呼叫 _apis/public/gallery/extensionquery WebAPI 查詢該 Extension 的 .vsix 下載網址(註:有些 Extension 會區分版號跟平台,我寫了一段簡單邏輯找出 win32-x64 的最新版本,這段沒有完整測試過,打算遇到問題再修),再透過 Invoke-WebRequest 下載,一氣喝成。

就醬,我們就能泡杯茶翹腳等 PowerShell 把用到的 Extension 一次下載回來,敲開勳。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK