11

I'm writing a quick Elixir script, and I'd like to use a csv library dependency. It seems a bit overkill to create a new mix project just to add in dependency management for this one library. What would you recommend? Would you go the mix project route for a simple script with a dependency?

EDIT

Note: I'm not asking how to install and access dependencies globally. The question is, "would you go the mix project route..." What is the suggested approach?

Elliot Larson
  • 10,669
  • 5
  • 38
  • 57
  • +1 because I'd like to know if there's an easy way to do it that I haven't seen. However, cloning and/or downloading the source for the library seems like as much or more work than creating a mix project and letting it do the work of retrieving the library for you. – CoderDennis Jan 06 '16 at 22:47
  • One workaround (that I've considered but not done myself yet) is to keep a generated mix project around for these one-offs, and just add dependencies to it as you need them. After a while it will include the ones you keep using. – Henrik N Jan 06 '16 at 23:05
  • @CoderDennis: Yeah, I agree. It's not hard to create a new mix project. But, I'm wondering if that's the suggested approach for simple scripts. Coming from a Ruby background, I write a lot of little utility scripts that are just a single file. It seems like overkill to have a project for each. – Elliot Larson Jan 06 '16 at 23:06
  • @HenrikN: That's a good suggestion. All my utility scripts could be a part of a single project. It still sort of lacks the portability of quick, single file utility scripts in Ruby. That might just be an emotional response, though. :) Maybe that's not that big of a deal. – Elliot Larson Jan 06 '16 at 23:09
  • I also miss Ruby conveniences (like this one) in Elixir – it can be frustrating but I guess it also saves us from some pains. Still undecided if we're better off on balance ;) Pretty sure I've seen Eric (the Hex guy) discuss this (whether to allow using dependencies without a full project) somewhere, but I can't find it now, and I don't remember what he said, I'm afraid… – Henrik N Jan 06 '16 at 23:31
  • Possible duplicate of [In elixir, How do i install packages globally?](http://stackoverflow.com/questions/33548079/in-elixir-how-do-i-install-packages-globally) – Patrick Oscity Jan 07 '16 at 00:39
  • Patrick, I'm asking a different question. See edits. – Elliot Larson Jan 07 '16 at 01:36
  • As I now read both questions again, I realize that they seemed very similar to me since having global packages is kind of a prerequisite for this. Your question really isn't technically a duplicate though. Retracting my close vote, sorry for that. – Patrick Oscity Jan 07 '16 at 08:08

3 Answers3

4

Go the mix project route. Quick and dirty scripts have a way of growing into bigger projects.

Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
2

Introduced as an experimental feature in Elixir 1.12, Mix.install/2 now allows you to specify a dependency directly in an Elixir script, giving you an alternative to a full-blown mix project.

Usage is quite straightforward: list your dependencies at the top of your script (in the same format that is returned from the regular deps function in a mix project), and it will take care of downloading, compiling, and caching the dependencies for you:

Mix.install([
  {:httpoison, "~> 1.8"},
  :jason
])
HTTPoison.start
IO.puts(Jason.encode!(%{hello: :world}))
...
Gar
  • 1,141
  • 9
  • 12
0

It is possible to do this by using using Code.require_file/2

E.g. consider this:

# foo.ex
defmodule Foo do
  def bar, do: "Barrrr"
end

and then, in the same directory, put your script:

# main.exs
Code.require_file("foo.ex", __DIR__)

Foo.bar()
|> IO.puts()

You can tweak that to include some other 3rd party file that you manually put in your directory, but it isn't very idiomatic! You're unlikely to see something like the above ever (in fact, I just made it up). As previously noted, you're usually better off sticking to a Mix project (perhaps make a custom mix task).

Remember that Elixir is first and foremost a virtual machine (the Erlang VM), so it always brings along that "baggage". For quick one-off scripts where portability is the top priority, Elixir may not be the best choice. I might reach for Python for simple scripts (because rather than carrying its own baggage, it assumes your computer has all this stuff lying around), or Go (because it would compile any complexities into a single executable).

Everett
  • 8,746
  • 5
  • 35
  • 49