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

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement