From ee02c6b5eac77d7e91b42da3893a61f9b8749089 Mon Sep 17 00:00:00 2001 From: sloane <1699281+sloanelybutsurely@users.noreply.github.com> Date: Fri, 16 Feb 2024 17:05:28 -0500 Subject: [PATCH] outline basic build and watch tasks --- lib/mix/tasks/site.build.ex | 13 +++++++++++++ lib/mix/tasks/site.watch.ex | 26 ++++++++++++++++++++++++++ lib/sloane_sh.ex | 10 ++++++++++ lib/sloane_sh/format.ex | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 lib/mix/tasks/site.build.ex create mode 100644 lib/mix/tasks/site.watch.ex create mode 100644 lib/sloane_sh/format.ex diff --git a/lib/mix/tasks/site.build.ex b/lib/mix/tasks/site.build.ex new file mode 100644 index 0000000..b70615c --- /dev/null +++ b/lib/mix/tasks/site.build.ex @@ -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 diff --git a/lib/mix/tasks/site.watch.ex b/lib/mix/tasks/site.watch.ex new file mode 100644 index 0000000..2fcc1da --- /dev/null +++ b/lib/mix/tasks/site.watch.ex @@ -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 diff --git a/lib/sloane_sh.ex b/lib/sloane_sh.ex index 6dcdae8..40e85e1 100644 --- a/lib/sloane_sh.ex +++ b/lib/sloane_sh.ex @@ -2,4 +2,14 @@ defmodule SloaneSH do @moduledoc """ 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 diff --git a/lib/sloane_sh/format.ex b/lib/sloane_sh/format.ex new file mode 100644 index 0000000..58a7056 --- /dev/null +++ b/lib/sloane_sh/format.ex @@ -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