Make elixir app recompile and reload on source code change
Asked Answered
T

5

30

How to automatically recompile and reload my iex + mix application every time I modify the source code?

If there's no way for iex + mix combination to do that, what's the easiest alternative? I've checked phoenix's way to do reload, and it doesn't seems easy to implement for my small test project.

I also know about José's .iex.exs :

defmodule R do
  def reload! do
    Mix.Task.reenable "compile.elixir"
    Application.stop(Mix.Project.config[:app])
    Mix.Task.run "compile.elixir"
    Application.start(Mix.Project.config[:app], :permanent)
  end
end

And I'm not extremely happy since it's not reloading automatically on code change, I still need to type R.reload! on iex.

And.. please help me create "iex" tag on stackoverflow, my reputation is not sufficient :)

Trixie answered 12/9, 2015 at 15:47 Comment(2)
I think we had some discussions on elixir-lang-talk mailing list about this. I think even some open source projects came out of it... or was it for mix test.watch?Birkner
ahh... yes there a few projects on github, I'll test and comeback whether they are working or not.Trixie
T
13

Currently iex has an internal function to do this:

invoke IEx.Helpers.recompile inside iex console.

Trixie answered 10/4, 2017 at 18:50 Comment(2)
You don't need to type the fully qualified command IEx.Helpers.recompile, it's enough to just run recompileAmain
you can just do iex> r My.ModuleNofretete
C
15

I think this is what you are looking for: https://github.com/AgilionApps/remix

Circumvallate answered 2/2, 2016 at 21:7 Comment(0)
T
13

Currently iex has an internal function to do this:

invoke IEx.Helpers.recompile inside iex console.

Trixie answered 10/4, 2017 at 18:50 Comment(2)
You don't need to type the fully qualified command IEx.Helpers.recompile, it's enough to just run recompileAmain
you can just do iex> r My.ModuleNofretete
L
3

Closest thing i know of is in Alchemist for emacs by @tonini. In Alchemist, there's an option to enable

(setq alchemist-hooks-test-on-save t)

which makes it compile and runs tests when you save.

https://github.com/tonini/alchemist.el#testing

Lues answered 29/9, 2015 at 17:56 Comment(0)
A
1

For Erlang, I was using "relx -d" together with https://github.com/rustyio/sync The "-d" means that source files are linked using symlinks.

The same works nicely in Elixir. In mix.exs I added :sync in the following two places

  defp deps do
        [{:sync, git: "https://github.com/rustyio/sync.git", tag: "master"}, ....


  def application do
    [applications: [:logger, :sync],

Then, the following command compiles it into a release (before it works, you will have to install hex and exrm).

mix release -dev

Now, when you change a .ex file, and save, it will be recompiled and loaded directly.

19:33:46.474 [info]  ... /apps/testapp1/lib/kv/bucket.ex:0: Recompiled.

You only want sync in your development environment!

Acton answered 15/3, 2016 at 18:41 Comment(3)
I do not think this could be working right due to: github.com/rustyio/sync/issues/56Allerus
@Michael Terry yes, same problem here.Kurtzman
This doesn't work for elixir consistently. Infinite loop mania.Lotta
C
1

https://github.com/falood/exsync/ is another library that will automatically recompile your code when the source code changes.

Cozza answered 10/6, 2023 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.