2024-02-16 17:27:46 -05:00
|
|
|
defmodule SloaneSH.Watch do
|
|
|
|
use GenServer
|
2024-02-16 22:28:13 -05:00
|
|
|
use TypedStruct
|
2024-02-16 17:27:46 -05:00
|
|
|
require Logger
|
|
|
|
|
2024-02-16 22:28:13 -05:00
|
|
|
alias SloaneSH.Build
|
|
|
|
alias SloaneSH.Context
|
2024-02-23 22:15:36 -05:00
|
|
|
alias SloaneSH.Layouts
|
2024-02-16 22:28:13 -05:00
|
|
|
|
|
|
|
typedstruct do
|
|
|
|
field :ctx, Context.t(), enforce: true
|
|
|
|
field :watcher_pid, pid(), enforce: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_link(%Context{} = ctx, opts \\ []) do
|
|
|
|
GenServer.start_link(__MODULE__, ctx, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl GenServer
|
|
|
|
def init(%Context{} = ctx) do
|
|
|
|
{:ok, watcher_pid} =
|
|
|
|
FileSystem.start_link(
|
2024-02-23 22:15:36 -05:00
|
|
|
dirs:
|
|
|
|
[
|
|
|
|
# ctx.config.layouts_dir,
|
|
|
|
"priv/site/layouts",
|
|
|
|
ctx.config.pages_dir,
|
|
|
|
ctx.config.posts_dir
|
|
|
|
]
|
|
|
|
|> dbg()
|
2024-02-16 22:28:13 -05:00
|
|
|
)
|
|
|
|
|
2024-02-23 22:15:36 -05:00
|
|
|
:ok = FileSystem.subscribe(watcher_pid)
|
2024-02-16 22:28:13 -05:00
|
|
|
|
2024-02-23 22:15:36 -05:00
|
|
|
Task.start_link(fn ->
|
|
|
|
Tailwind.install_and_run(:default, ~w[--watch])
|
|
|
|
end)
|
2024-02-21 09:17:58 -05:00
|
|
|
|
2024-02-16 22:28:13 -05:00
|
|
|
state = %__MODULE__{ctx: ctx, watcher_pid: watcher_pid}
|
|
|
|
|
|
|
|
{:ok, state, {:continue, :build}}
|
2024-02-16 17:27:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl GenServer
|
2024-02-16 22:28:13 -05:00
|
|
|
def handle_continue(:build, %{ctx: ctx} = state) do
|
|
|
|
Build.run(ctx)
|
2024-02-16 17:27:46 -05:00
|
|
|
|
2024-02-16 22:28:13 -05:00
|
|
|
{:noreply, state}
|
2024-02-16 17:27:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl GenServer
|
2024-02-16 22:28:13 -05:00
|
|
|
def handle_info({:file_event, pid, {path, events}}, %{ctx: ctx, watcher_pid: pid} = state) do
|
2024-02-23 22:15:36 -05:00
|
|
|
dbg(path)
|
|
|
|
|
2024-02-17 08:30:17 -05:00
|
|
|
if String.match?(path, ~r/layouts/) do
|
2024-02-23 22:15:36 -05:00
|
|
|
recompile_layouts()
|
2024-02-17 08:30:17 -05:00
|
|
|
Build.run(ctx)
|
|
|
|
{:noreply, state}
|
|
|
|
else
|
|
|
|
ctx = Context.maybe_add(ctx, path)
|
2024-02-16 22:28:13 -05:00
|
|
|
|
2024-02-17 08:30:17 -05:00
|
|
|
if Context.in_context?(ctx, path) do
|
|
|
|
path = Path.relative_to(path, ctx.config.pages_dir)
|
|
|
|
Logger.info("File changed: #{path}")
|
|
|
|
Build.build_page(ctx, path)
|
|
|
|
end
|
2024-02-16 22:28:13 -05:00
|
|
|
|
2024-02-17 08:30:17 -05:00
|
|
|
%{state | ctx: ctx}
|
2024-02-16 22:28:13 -05:00
|
|
|
|
2024-02-17 08:30:17 -05:00
|
|
|
{:noreply, state}
|
|
|
|
end
|
2024-02-16 17:27:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl GenServer
|
2024-02-16 22:28:13 -05:00
|
|
|
def handle_info({:file_event, pid, :stop}, %{watcher_pid: pid}) do
|
2024-02-16 17:27:46 -05:00
|
|
|
Logger.warning("File watcher stopped")
|
|
|
|
{:stop, :watcher_stopped, pid}
|
|
|
|
end
|
2024-02-17 08:30:17 -05:00
|
|
|
|
2024-02-23 22:15:36 -05:00
|
|
|
defp recompile_layouts do
|
|
|
|
layouts_source = Layouts.module_info(:compile)[:source] |> List.to_string()
|
|
|
|
{:ok, _, _} = Kernel.ParallelCompiler.compile([layouts_source])
|
2024-02-17 08:30:17 -05:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
2024-02-16 17:27:46 -05:00
|
|
|
end
|