didier1

didier1

Mix project for a simple script

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)

Marked As Solved

IloSophiep

IloSophiep

My solution for this was to define a test that runs my “entry point function” and use mix test to execute it whenever I wanted. There is also a section about running mix test with a filesystem watcher if you want to have even less manual work to do. You can also only run stale tests so you can use your one mix project for all the days and just run the test case(/-s) related to your current day.

Not exactly an answer to your question, sorry, but might be helpful either way… :sweat_smile:

Also Liked

didier1

didier1

actually, this is pretty good. It might not be perfect, but it does the job for sure.

thanks a lot IloSophiep

Last Post!

didier1

didier1

D4no0: I haven’t had a chance to try lovebook yet. Although I keep hearing about it. I was trying to see if a simpler local solution would work or try to figure if I was completely misusing mix. Thx.

Where Next?

Popular in Questions Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New