XiaO

Homebrew 多版本安装与切换

XiaO / 2022-05-31


如下以 Hugo 的安装为例,演示如何安装该软件的不同版本,并在不同版本之间切换使用。

安装最新版本的 Hugo:

brew install hugo

编辑 Hugo 的 formula

brew eitd hugo # 使用编辑器打开 hugo 的 <formula>

class Hugo < Formula
  desc "Configurable static site generator"
  homepage "https://gohugo.io/"
  url "https://github.com/gohugoio/hugo/archive/v0.89.4.tar.gz" # 修改版本号为自己需要的版本
  # sha256 "790c4f218e6380f31a0d5c10bacc7e1f7b1533ccba88ef526f764d413325cff1" # 关闭校验
  license "Apache-2.0"
  head "https://github.com/gohugoio/hugo.git", branch: "master"

  # bottle do # 关闭 bottle 选项
  #   sha256 cellar: :any_skip_relocation, arm64_monterey: "d622981916a5a892d1cce2646e09bc79c9efdaf534fc494575dca6083077bb67"
  #   sha256 cellar: :any_skip_relocation, arm64_big_sur:  "d10ddc86f10725713b95994f5ee676f9bd6e71f6d8c01c517e40e74118c95240"
  #   sha256 cellar: :any_skip_relocation, monterey:       "36f55c7b8bba0dd62236739eca383c896c94e23a957e9616e3c9cc165e8acaf5"
  #   sha256 cellar: :any_skip_relocation, big_sur:        "02bd1500ab2994a09d2abde41ae1382fbe4ff17d31909b6c41a8d6fd1a953ca2"
  #   sha256 cellar: :any_skip_relocation, catalina:       "66b1d3cb7a75c264f23813760fa004d096bcaada8f17bb3ef11cfd8c554a2ecf"
  #   sha256 cellar: :any_skip_relocation, x86_64_linux:   "6022f0632b00237810837842bcc147f0b82590e3dea08a6d62e9086ce00bcd39"
  # end

  depends_on "go" => :build

  def install
    system "go", "build", *std_go_args(ldflags: "-s -w"), "-tags", "extended"

    # Install bash completion
    output = Utils.safe_popen_read(bin/"hugo", "completion", "bash")
    (bash_completion/"hugo").write output

    # Install zsh completion
    output = Utils.safe_popen_read(bin/"hugo", "completion", "zsh")
    (zsh_completion/"_hugo").write output

    # Install fish completion
    output = Utils.safe_popen_read(bin/"hugo", "completion", "fish")
    (fish_completion/"hugo.fish").write output

    # Build man pages; target dir man/ is hardcoded :(
    (Pathname.pwd/"man").mkpath
    system bin/"hugo", "gen", "man"
    man1.install Dir["man/*.1"]
  end

  test do
    site = testpath/"hops-yeast-malt-water"
    system "#{bin}/hugo", "new", "site", site
    assert_predicate testpath/"#{site}/config.toml", :exist?
  end
end

源码编译安装该软件的旧版本:

brew unlink hugo # 解除 Hugo 的链接 
brew install --build-from-source hugo # 源码编译安装 Hugo

查看当前所安装的 Hugo 版本:

brew list --versions hugo 
hugo 0.100.0 0.89.4

切换不同版本的 Hugo

brew swith hugo 0.89.4

禁止该软件的更新:

brew pin hugo