outline basic build and watch tasks

This commit is contained in:
sloane 2024-02-16 17:05:28 -05:00
parent d4e3f73881
commit ee02c6b5ea
4 changed files with 83 additions and 0 deletions

View file

@ -0,0 +1,13 @@
defmodule Mix.Tasks.Site.Build do
@moduledoc "Build and output the site as HTML"
@shortdoc "build the site"
use Mix.Task
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)}")
end
end

View file

@ -0,0 +1,26 @@
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
@impl Mix.Task
def run(_args) do
IO.puts("Starting site.watch...")
{:ok, pid} = SloaneSH.watch()
unless iex_running?() do
ref = Process.monitor(pid)
receive do
{:DOWN, ^ref, _, _, _} ->
IO.puts("site.watch terminated")
:ok
end
end
end
defp iex_running? do
Code.ensure_loaded?(IEx) and IEx.started?()
end
end

View file

@ -2,4 +2,14 @@ defmodule SloaneSH do
@moduledoc """ @moduledoc """
Sloane's personal static site generator powering [sloane.sh](https://sloane.sh). Sloane's personal static site generator powering [sloane.sh](https://sloane.sh).
""" """
def build(_opts \\ []) do
:ok
end
def watch(_opts \\ []) do
Task.start_link(fn ->
Process.sleep(5_000)
end)
end
end end

34
lib/sloane_sh/format.ex Normal file
View file

@ -0,0 +1,34 @@
defmodule SloaneSH.Format do
@moduledoc """
Functions to format various literals into human readable forms.
"""
@millisecond 1000
@second @millisecond ** 2
@minute 60 * @second
@hour 60 * @minute
@max_depth 2
def time(micro, depth \\ 0)
def time(_micro, depth) when depth >= @max_depth, do: []
time_units = [
{@hour, "h"},
{@minute, "m"},
{@second, "s"},
{@millisecond, "ms"}
]
for {division, unit} <- time_units do
def time(micro, depth) when micro >= unquote(division) do
count = "#{div(micro, unquote(division))}#{unquote(unit)}"
rem = rem(micro, unquote(division))
[count, time(rem, depth + 1)]
end
end
def time(micro, _depth) do
"#{micro}μs"
end
end