diff --git a/lib/mix/tasks/site.build.ex b/lib/mix/tasks/site.build.ex index b70615c..f654a76 100644 --- a/lib/mix/tasks/site.build.ex +++ b/lib/mix/tasks/site.build.ex @@ -2,12 +2,13 @@ defmodule Mix.Tasks.Site.Build do @moduledoc "Build and output the site as HTML" @shortdoc "build the site" use Mix.Task + require Logger alias SloaneSH.Format @impl Mix.Task def run(_args) do {micro, :ok} = :timer.tc(&SloaneSH.build/0) - IO.puts("built site in #{Format.time(micro)}") + Logger.info("Built site in #{Format.time(micro)}") end end diff --git a/lib/mix/tasks/site.watch.ex b/lib/mix/tasks/site.watch.ex index 2fcc1da..d1d6dcf 100644 --- a/lib/mix/tasks/site.watch.ex +++ b/lib/mix/tasks/site.watch.ex @@ -2,10 +2,11 @@ defmodule Mix.Tasks.Site.Watch do @moduledoc "Build and output the site as HTML watching for changes" @shortdoc "build the site and watch for changes" use Mix.Task + require Logger @impl Mix.Task def run(_args) do - IO.puts("Starting site.watch...") + Logger.info("Starting site.watch...") {:ok, pid} = SloaneSH.watch() @@ -14,7 +15,7 @@ defmodule Mix.Tasks.Site.Watch do receive do {:DOWN, ^ref, _, _, _} -> - IO.puts("site.watch terminated") + Logger.info("site.watch terminated") :ok end end diff --git a/lib/sloane_sh.ex b/lib/sloane_sh.ex index 40e85e1..8834a17 100644 --- a/lib/sloane_sh.ex +++ b/lib/sloane_sh.ex @@ -8,8 +8,6 @@ defmodule SloaneSH do end def watch(_opts \\ []) do - Task.start_link(fn -> - Process.sleep(5_000) - end) + SloaneSH.Watch.start_link() end end diff --git a/lib/sloane_sh/watch.ex b/lib/sloane_sh/watch.ex new file mode 100644 index 0000000..152e8d9 --- /dev/null +++ b/lib/sloane_sh/watch.ex @@ -0,0 +1,30 @@ +defmodule SloaneSH.Watch do + use GenServer + require Logger + + def start_link(init_arg \\ [], opts \\ []) do + GenServer.start_link(__MODULE__, init_arg, opts) + end + + @impl GenServer + def init([]) do + dirs = [Path.join(:code.priv_dir(:sloane_sh), "site")] + Logger.info("Watching #{inspect(dirs)} for changes") + {:ok, pid} = FileSystem.start_link(dirs: dirs) + FileSystem.subscribe(pid) + + {:ok, pid} + end + + @impl GenServer + def handle_info({:file_event, pid, {path, events}}, pid) do + Logger.info("File event: #{inspect(path)} #{inspect(events)}") + {:noreply, pid} + end + + @impl GenServer + def handle_info({:file_event, pid, :stop}, pid) do + Logger.warning("File watcher stopped") + {:stop, :watcher_stopped, pid} + end +end