Skip to content

Instantly share code, notes, and snippets.

@loxs
Created January 11, 2017 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loxs/5823b9e9121fc8aee442b207a93238bd to your computer and use it in GitHub Desktop.
Save loxs/5823b9e9121fc8aee442b207a93238bd to your computer and use it in GitHub Desktop.
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