So, I am using elixir for the advent of code daily challenges. Initially I just had a simple .exs
file and I would just invoke it with elixir <file-name>
(or with #!/usr/bin/env elixir
as the first line). That worked fine.
Ok, fast forward to more complex scripts and now I need to use some external packages (say, the Heap package).
I naturally switched to mix so I can cleanly define my dependencies in my mix.exs file. I created a simple app without supervisor (mix new day17
)
I am running into some issues running my script.
Now imagine my script is a simple IO.puts(:hello)
(in the default lib/p.exs, outside of a defmodule).
If I leave it as is, mix will execute this during its compile phase, so calling mix run
will indeed execute that IO.puts. Unfortunately calling mix run
a second won’t (because it’s already compiled).
So if I want mix run
to always run my code, I guess I have to define a proper app in my mix.exs
. Easy. I defined a start/2 function in the P module file (and moved my IO.puts in there) and added mod: {P, []}
to the def application
section in mix.exs.
This works (and it will properly execute my IO.puts whenever I invoke mix run
), except that it fails at the end because it expect my start function to return the pid of a forked process, etc… the whole supervisor tree thing, which is definitely not what I want for my simple script.
Am I missing something obvious here ? (yes, I could also use iex -S mix
and then invoke something like P.start
manually, and then c("lib/p.ex")
whenever I change anything in my file, but that seems quite tedious)