author: Bobby Grayson author_link: https://github.com/notactuallypagemcconnell categories: general date: 2018-10-01 layout: post title: Agnostic Version Management With asdf excerpt: >

Take a dive into flexible version management of Elixir, Erlang, and OTP with asdf!

使用 asdf 进行不可知版本管理

它是啥?

很多时候我们需要使用多个版本的工具。

很多社区都有自己的东西来做这个事情。

在 Ruby 中,我们有 chrubyrbenvrvm 等等,NodeJS 有 nvm

这些工具可以让我们轻松快速的在某个项目或环境中切换我们使用的工具。

今天将要讨论我最喜欢的版本管理器 asdf,为什么是我最喜欢的呢?因为它可以让您仅用一种工具就可以管理多种语言,因为它与您使用其管理的版本无关。

在我看来,asdf 有然而其他工具都没有的一个很大的优点,它让我如此轻松地做到:控制我的 Elixir 是用哪个版本的 OTP 编译的,并把它和 Elixir + OTP 的几个版本一起管理。

让我们来看看它吧!

安装

安装 asdf 轻而易举。

首先,将其克隆:

  1. git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.5.1

现在是时候安装了

在 macOS 上:

  1. echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.bash_profile
  2. echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.bash_profile

在 linux 上(用一个标准的 bash shell):

  1. echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.bashrc
  2. echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc

使用 ZSH:

  1. echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.zshrc
  2. echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.zshrc

使用 Fish:

  1. echo 'source ~/.asdf/asdf.fish' >> ~/.config/fish/config.fish
  2. mkdir -p ~/.config/fish/completions; and cp ~/.asdf/completions/asdf.fish ~/.config/fish/completions

Now restart your shell, and type asdf and we get our first introduction to the tool.

现在重启你的 shell,然后输入 asdf,我们首先得到一个工具介绍。

  1. asdf
  2. MANAGE PLUGINS
  3. asdf plugin-add <name> [<git-url>] Add a plugin from the plugin repo OR, add a Git repo
  4. as a plugin by specifying the name and repo url
  5. asdf plugin-list List installed plugins
  6. [...]
  7. MANAGE PACKAGES
  8. asdf install <name> <version> Install a specific version of a package or,
  9. with no arguments, install all the package
  10. versions listed in the .tool-versions file
  11. asdf uninstall <name> <version> Remove a specific version of a package
  12. asdf current Display current version set or being used for all packages
  13. asdf current <name> Display current version set or being used for package
  14. [...]
  15. UTILS
  16. asdf reshim <name> <version> Recreate shims for version of a package
  17. asdf update Update asdf to the latest stable release
  18. asdf update --head Update asdf to the latest on the master branch

搭配 Elixir 使用

要让 asdf 与 Elixir 一起工作,我们首先需要 Erlang。

根据我们的系统不同,需要有一些简单的步骤。

在 OSX 上:

  1. brew install autoconf wxmac
  2. asdf plugin-add erlang https://github.com/asdf-vm/asdf-erlang.git
  3. asdf install erlang 21.1

在 Ubuntu 上:

  1. apt-get -y install build-essential autoconf m4 libncurses5-dev libwxgtk3.0-dev libgl1-mesa-dev libglu1-mesa-dev libpng3 libssh-dev
  2. asdf plugin-add erlang https://github.com/asdf-vm/asdf-erlang.git
  3. asdf install erlang 21.1

对于 Erlang 的部分,我们可以使用 git 中的任何 ref,或者传递一个主要的 OTP 版本。

asdf install erlang ref:master 会让我们从 git 获得最新的 master 版本。

既然我们也可以使用 Elixir 做到这一点,您可以想象它使从特定分支或版本进行构建来调试对 Elixir 本身的贡献(包括多个版本)变得多么容易!

现在,让我们把 Elixir 设置好。

自从我们使用 Erlang 完成管道以来,所有系统上的情况都是相同的。

  1. asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git
  2. asdf install elixir 1.7

现在,如果我们碰巧知道我们需要在 OTP 20 而不是 OTP 21 上进行编译并在该环境中运行,该怎么办?

  1. asdf install erlang 20.3
  2. asdf install elixir 1.7-otp-20

现在,我们可以在给定项目(本地环境,每个目录)中设置要使用的版本,如下所示:

  1. asdf local erlang 20.3
  2. asdf local elixir 1.7.0-otp-20

或者,我们也可以选择设置全局配置(我们的整个系统):

  1. asdf global erlang 20.3
  2. asdf global elixir 1.7.0-otp-20

要了解有关 asdf 如何管理这些内容并进一步进行自定义的更多信息,请查看文档

如您所见,这使得能够在服务之下有些复杂的工具集之间无缝切换。

我发现 asdf 是管理日常生活中这种复杂性的好工具。

骇客入侵!