use FileSystem to get fs events
This commit is contained in:
parent
ee02c6b5ea
commit
aa01ee6967
4 changed files with 36 additions and 6 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
30
lib/sloane_sh/watch.ex
Normal file
30
lib/sloane_sh/watch.ex
Normal file
|
@ -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
|
Loading…
Reference in a new issue