Created
January 11, 2017 21:10
-
-
Save loxs/5823b9e9121fc8aee442b207a93238bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule CodeReloaderWorker do | |
use GenServer | |
require Logger | |
def start_link do | |
GenServer.start_link __MODULE__, [], name: __MODULE__ | |
end | |
def init([]) do | |
Logger.debug "#{__MODULE__} starting" | |
if Mix.env == :dev do | |
Logger.debug "DEV detected. Subscribing to .beam and .ex files for recompilation" | |
:fs.subscribe | |
end | |
{:ok, :state} | |
end | |
def handle_info({_pid, {:fs, :file_event}, {fullpath, _events}}, state) do | |
basename = Path.basename fullpath | |
#rootname = Path.rootname basename | |
extname = Path.extname basename | |
case extname do | |
".ex" -> | |
# don't recompile emacs .# backup files | |
unless binary_part(basename, 0, 2) == ".#" do | |
Logger.debug "Recompiling #{fullpath}" | |
IEx.Helpers.recompile | |
end | |
#".beam" -> | |
# Logger.debug "Reloading #{rootname} #{extname}" | |
# IEx.Helpers.r :"#{rootname}" | |
_ -> | |
:pass | |
end | |
{:noreply, state} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment