• Windows 利器 - 命令行安装程序 Scoop

    最近在公司申请了一台新的机器,Dell Precision 5820。在新申请时,就有想法在安装软件时,尽量通过脚本来一键安装。
    一开始是打算通过 Shell 脚本来做,偶然间看到 CS 自学指南里介绍的 Scoop,便决定好了在安装新机器时通过 Scoop 来完成这件事情。那,这篇文章就是对这件事情的一个总结,为装机这件事画上一个句号。

    什么是Scoop

    Scoop 是一款适用于 Windows 的命令行安装程序,可以帮你统一安装和管理常用的应用程序。
    Scoop 就相当于 macOS 或 Linux 上的 Homebrew,这个项目本身也是受 Homebrew 的启发。有意思的一点,他们的域名也是高度一致:brew.sh vs scoop.sh

    Scoop 的脚本可编程性很高,可以按需配置并能一键安装应用程序,这也是我为什么会使用它来安装常用软件的原因。

    在 Windows 上,还有其他的通过命令行来完成软件安装和管理的工具,如微软出品的开源 Windows 包管理器 winget,商用的 Chocolatey。他们之间最大的区别在于 Scoop 是默认将软件安装(解压)在 ~/scoop/ 文件夹内,不需要管理员权限去修改注册表等内容,具体可以参考 Chocolatey-and-Winget-Comparison

    如何使用 Scoop

    Scoop 依赖于 Powershell,在安装 Scoop 前需确保有最新的 Powershell

    安装 Scoop

    先配置一下 Powershell 的安全模式:Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
    然后通过 Powershell 的 cmdlet 命令来安装 Scoop 本身:irm get.scoop.sh | iex

    • irm: Invoke-RestMethod 的一个别名,是 PowerShell 中用来处理 HTTP 请求的 cmdlet
    • iex: Invoke-Expression 的别名,是 Powershell 中用来识别字符串命令并运行该命令的 cmdlet

    安装好 Scoop 后,就可以通过 Scoop 命令来安装其他的软件了。这里列出几个常用的命令,详细的可以看 Scoop 的 Quick Start · ScoopInstaller/Scoop Wiki (github.com)

    scoop help
    # find app
    scoop search curl
    # install app
    scoop install curl
    # update app
    scoop update curl
    # update scoop
    scoop update

    在 Scoop 里有几个概念,其中最主要的是 apps 和 buckets。 apps 比较好理解,就是我们想要通过 Scoop 安装的软件。而 buckets 呢,是存放 apps 的仓库,是人为做好分类的一个软件集合,比如 main, extras 等。它们的关系就相当于书和书架的关系。

    Scoop 网站提供了一个搜索工具来帮助我们来查找 scoop 已有的 buckets 中是否包含我们所需的软件。

    scoop search, git as an example

    使用 aria2

    在安装应用程序时,一定要利用上 aria2 这个工具,能让你的安装速度快上好多好多倍。aria2 是一款轻量级的多协议、多源、跨平台下载工具,采用命令行方式运行,其特点就是主打一个超快。

    scoop install aria2
    scoop config aria2-warning-enabled false

    脚本

    因为可以通过命令行来完成安装,那么我们就可以尝试把这些命令行放在一起,来完成批量安装了。
    在 Scoop 的帮助页面有一个示例的脚本,通过这基本就能知道怎么去定制化自己的安装脚本了。

    这里有几个需要注意的点:

    1. 需要先安装 Git,这是因为 Scoop 是通过 Git 来托管 Buckets 的(也就是 Repositories),比如 extras bucket 是托管在 https://github.com/ScoopInstaller/Extras
    2. 要添加除了 main 外的其他 Buckets 时,需要一个一个分别添加(多行),不能在同一行中用空格来分开 bucket。这点与 apps 的安装有点不同。
    3. 如果访问 GitHub 有点困难的话,可以尝试配置 Proxy
    4. 强烈建议先安装 aria2,有一些较大的软件我在下载时失败过好几次,在使用 aria2 后下载速度真的是超快
    5. 如果第一次运行脚本安装有失败的情况,别担心,再运行一次,还失败的话就再运行两次

    我的 Windows 工具清单

    在这次安装新机时,我也把自己在 Windows 上使用过的软件做了一个整理,并尝试制作了自己的 Scoop 脚本。

    # filename: my-windows-apps.ps1 (can be run via Powershell)
    # setup examples: https://github.com/ScoopInstaller/Scoop/wiki/Example-Setup-Scripts
    # search apps from https://scoop.sh/#/
    
    # check if scoop is installed
    if (-Not (Get-Command scoop -ErrorAction SilentlyContinue)) {
        Write-Host "Scoop is not installed, we're installing it."
    
        # install scoop
        Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
    
        # if you want to use a proxy that isn't already configured in Internet Options
        [net.webrequest]::defaultwebproxy = new-object net.webproxy "http://proxy.esri.com:8080"
        iex (new-object net.webclient).downloadstring('https://get.scoop.sh')
    
        # irm get.scoop.sh | iex
    } else {
        Write-Host "Scoop is installed already."
    }
    
    ## preparation of requirements
    scoop install main/git
    
    # a workaround for bucket add with proxy
    # scoop config proxy ':@proxy.example.org:8080'
    scoop bucket add extras
    scoop bucket add versions
    scoop bucket add sysinternals
    
    # change to use the proxy normally
    # scoop config proxy proxy.example.org:8080
    
    # utilize aria2 to use multi-connection downloads
    scoop install aria2
    scoop config aria2-warning-enabled false
    
    # utils
    scoop install 7zip curl sudo git openssh coreutils grep sed less wget
    
    # programming languages
    scoop install dotnet-sdk extras/windowsdesktop-runtime extras/vcredist2022
    scoop install python ruby go
    
    # install nodejs with nvm
    scoop install nvm
    nvm install lts
    nvm list
    
    # scoop config rm proxy
    
    # programming editors 
    scoop install neovim extras/vscode extras/sublime-text
    
    # programming tools
    scoop install extras/snoop extras/postman extras/linqpad extras/winscp extras/sqlitebrowser
    
    # my utils - first
    scoop install extras/irfanview extras/quicklook extras/listary sysinternals/zoomit
    
    # my utils - second
    scoop install extras/notion extras/logseq extras/obsidian
    scoop install extras/qtranslate extras/ditto
    
    # Failed in my last installation
    scoop install extras/googlechrome
    
    # my utils - third
    scoop install extras/spotify extras/discord
    scoop install extras/mypaint extras/gimp extras/inkscape
    scoop install vlc extras/obs-studio extras/handbrake extras/screentogif ffmpeg
    
    # console theme
    scoop install concfg pshazz
    concfg import solarized small
    
    # vim
    scoop install vim
    '
    set ff=unix
    
    set cindent
    set tabstop=4
    set shiftwidth=4
    set expandtab
    set backupdir=$TEMP
    ' | out-file ~/.vimrc -enc oem -append

    有一些软件并没有在 Scoop 提供的 buckets 中,目前我自己是手动安装的那些软件。完全可以自己做一个 app manifest 来完成那些不在 buckets 中的软件,这暂且作为我的一个作业吧。

    总结

    首先,很开心有了新电脑,也为自己能有这样的尝试感到高兴。在制作自己的脚本过程中,也用到了一点 Powershell 脚本相关的东西,而这也是我接下来要去学习的内容。

    我还要继续去研究 Scoop,尝试更深入的了解它,并推广开来。

subscribe via RSS