LoginSignup
4
0

More than 5 years have passed since last update.

mixで作成しているアプリを自動コンパイルする

Last updated at Posted at 2017-01-01

Phoenixのような自動コンパイル機能が素のElixirプロジェクトでも欲しくて、最低限必要な部分のみ抽出してみました。

kawakami-o3/reloader

サンプルとして作ってみたものの中身は

  1. Phoenix.LiveReloaderと同様にsynrc/fsで変更を監視して、
  2. .exファイルで変更があったときにmix compileする。

のみで、コードにしてもたったこれだけです。

defmodule Reloader do
  def main([]) do

    path = "/path/to/this/project"
    :fs.start_link(:my_watcher, Path.absname(path))
    :fs.subscribe(:my_watcher)

    loop()
  end

  defp loop() do
    IO.puts("watching...")
    receive do
      {_watcher_process, {:fs, :file_event}, {changedFile, _type}} ->
        if match?(changedFile) do
          mix_compile()
        end
    end

    loop()
  end

  defp match?(path) do
    path = to_string(path)
    String.match?(path, ~r{\.ex$}) and !String.match?(path, ~r{(^|/)_build/})
  end

  defp mix_compile() do
    Mix.Task.reenable("compile.elixir")    
    Mix.Project.build_structure
    Mix.Task.run("compile.elixir")
  end

end

監視対象や実行するタスクなど適宜変えてみると使いでがあるかもしれません。

参考
  1. https://medium.com/@kansi/elixir-plug-unveiled-bf354e364641#.m5mahil5u
  2. http://hostiledeveloper.com/2016/02/24/hey-watch-it-or-how-to-monitor-files-in-elixir.html
  3. https://github.com/phoenixframework/phoenix_live_reload
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0