#How/where to define an entry point for a Mix application?

15 messages · Page 1 of 1 (latest)

blissful siren
#

I just created a new project with mix new whatever, and when I do mix run it compiles without any issues. However, I'm not sure where to start writing code that would be considered the entry point of my application (equivalent to a main function in other languages). Would that be in lib/whatever.esx? In which case, do I just put the code after MixProject module?

willow ferry
#

You need to configure it in application/0 callback function in mix.exs

blissful siren
#

perfect, thank you

sick hornet
#

just know that elixir is not really the same as other languages that have main functions; elixir is more suited to building long-running server type applications, so there is no entry point that just executes some code like you might be used to. The entry point is supposed to start a process, like a supervisor that spawns other child processes

#

it's more like an operating system

slender jungle
#

All that is true, but elixir also supports little scripts that you can play with to learn. Just doing elixir main.exs works, as well as mix run main.exs and it will compile and execute that script every time. You can put other modules in lib/my_module.ex and call them from your main script.

#
echo 'IO.puts("Hello, world")' > main.exs
mix run main.exs
blissful siren
#

Thanks, that all makes sense. So if my intention is to make a command line tool, is application/0 sitll what I'm after? Assuming it is, I read the article, and if I'm understanding it right, I want an Application Resource File?

slender jungle
#

Depends mostly on where you want to run the script. If you just want to create scripts for yourself, on a machine with elixir and erlang installed, you just have to create something like "script.exs":

#!/usr/bin/env elixir

IO.puts("Hello, world")

and then chmod +x script.exs and ./script.exs to execute.

There's also escripts, which don't require elixir to be installed, but do require erlang to be installed.

But I think the best, most cross compatible and distributive method is burrito: https://github.com/burrito-elixir/burrito

GitHub

Wrap your application in a BEAM Burrito! Contribute to burrito-elixir/burrito development by creating an account on GitHub.

#

It depends on what you want to do, or if you want to distribute it. That said, Elixir isn't generally the best language for system scripts, since it still has to start up the whole VM just to do a thing, then shut it down. Similar to Java.

blissful siren
#

Im familiar with all of that, although this isn't something I'd consider a system script. It's a tool I'll be using in a static site generator, so the extra start up cost from the VM won't be a problem

#

I'll check out burrito though, thanks

slender jungle
#

Ah yeah, if it's just for you to use, I would probably do escript then