[英]In elixir, How do i install packages globally?
Can I use mix to install some packages globally?我可以使用 mix 在全球范围内安装一些软件包吗? I'd like a behaviour like npm
's global option or gem
's install - it could be useful for packages I use everywhere like csv
or yaml
.我想要像npm
的 global 选项或gem
的 install 这样的行为——它可能对我在任何地方使用的包很有用,比如csv
或yaml
。
There is no such thing in Elixir, you always use dependencies in the context of a project. Elixir 中没有这样的东西,你总是在项目的上下文中使用依赖项。 Solutions like archives or escripts are meant to solve specific problems, they do not allow package sharing between projects.档案或 escripts 等解决方案旨在解决特定问题,它们不允许项目之间的包共享。
However, there is no need to worry about sharing frequently used packages.但是,无需担心共享常用包。 Hex, the package manager, already cache those and it will take care of handling it for you. Hex,包管理器,已经缓存了这些,它会为你处理它。
Certain packages will provide an archive file that you can install globally.某些软件包将提供一个可以全局安装的存档文件。
http://elixir-lang.org/docs/v1.1/mix/Mix.Tasks.Archive.Install.html http://elixir-lang.org/docs/v1.1/mix/Mix.Tasks.Archive.Install.html
For example Phoenix:例如凤凰:
mix archive.install https://github.com/phoenixframework/phoenix/releases/download/v1.0.3/phoenix_new-1.0.3.ez
This allows access to the mix phoenix.new
task globally.这允许全局访问mix phoenix.new
任务。 There is not anything specific for allowing the installation of libraries that are available in all your mix projects though.但是,没有任何特定的允许安装所有混音项目中可用的库。
For Elixir scripts, you can virtually install global packages by using erun
.对于 Elixir 脚本,您可以使用erun
虚拟安装全局包。 By compiling erun
with your dependencies, you can run通过使用您的依赖项编译erun
,您可以运行
$ erun foo.exs
( erun
is just an escript.) ( erun
只是一个脚本。)
https://github.com/s417-lama/erun https://github.com/s417-lama/erun
Not global, but if you just want to use a dependency in a script outside of a Mix project, you can use Mix.install/2
:不是全局的,但如果您只想在 Mix 项目之外的脚本中使用依赖项,您可以使用Mix.install/2
:
For example, this script:例如,这个脚本:
Mix.install [:jason]
%{hello: "world!"}
|> Jason.encode!()
|> IO.puts()
Produces on the first run:在第一次运行时产生:
$ elixir script.exs
Resolving Hex dependencies...
Dependency resolution completed:
New:
jason 1.3.0
* Getting jason (Hex package)
==> jason
Compiling 10 files (.ex)
Generated jason app
{"hello":"world!"}
Subsequent runs:后续运行:
$ elixir script.exs
{"hello":"world!"}
The dependencies installed with Mix.install/2
are not global, they are cached, and each script specifies the dependencies (and the versions of which) it needs.使用Mix.install/2
安装的依赖项不是全局的,它们是缓存的,每个脚本都指定了它需要的依赖项(及其版本)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.