如何在简单的Elixir脚本中加载Elixir库? [英] How do I load an elixir library in a simple elixir script?

查看:95
本文介绍了如何在简单的Elixir脚本中加载Elixir库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ruby脚本中,我可以简单地做:

In ruby scripts, I could simply do:

require 'some-gem'

SomeGem.do_something!

我如何在长生不老药 exs 脚本而未创建全新的混合项目?到目前为止,我已经在google上搜索了实现此目的的方法,并阅读了几篇博客文章(这样的),但找不到正确的方法(读简单)

How can I do something similar in elixir exs scripts without creating a brand new mix project? So far I've searched google on ways to do this, and read a few blog posts (such as this), but can't figure out a proper (read 'simple') way to do this.

具体地说,我想使用 HTTPoison 在我的药剂脚本中。

推荐答案

Elixir中没有像Ruby中那样安装全局软件包。虽然在技术上可以将依赖项编译为 .beam 文件并将其添加到脚本的加载路径(如链接到的文章),但是如果需要的话行为与Ruby有点类似,我建议您使用 mix run 来运行加载了所有项目依赖项的任意脚本。

There are no global package installs in Elixir like there are in Ruby. While it might be technically possible to compile the dependencies into .beam files and add them to the load path of your scripts (like the article you linked to does), if you want behavior somewhat similar to Ruby, I would recommend you to make use of mix run to run arbitrary scripts with all the project dependencies loaded.

使用您要在 mix.exs 中指定的所有依赖项创建一个全局 mix 项目, .exs 文件中的代码(不必位于同一文件夹中),并通过以下方式执行它:

Create one global mix project with all the dependencies you want specified in mix.exs, write your code in any .exs file (doesn't have to be in the same folder), and execute it by

cd /path/to/mix/project && mix run /path/to/.exs

您甚至可以创建包装器外壳脚本来自动执行只需调用 my-elixir script.exs 即可。

You could even create a wrapper shell script to automatically do the above by just invoking my-elixir script.exs.

(我经常在测试代码以获取答案的同时这样做在StackOverflow上,它使用一些常见的依赖项,例如HTTPoison和/或Poison。)

(I regularly do this while testing code for answers here on StackOverflow which use some common dependencies like HTTPoison and/or Poison.)

这篇关于如何在简单的Elixir脚本中加载Elixir库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆