Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extension priority #16

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mix/tasks/tableau.build.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ defmodule Mix.Tasks.Tableau.Build do
match?({:ok, :pre_build}, Tableau.Extension.type(mod)) do
mod
end
|> Enum.sort_by(& &1.__tableau_extension_priority__())
end
end
11 changes: 9 additions & 2 deletions lib/tableau/extension.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ defmodule Tableau.Extension do

An extension can be used to generate other kinds of content.

## Types

There are currently the following extension types:

- `:pre_build` - executed before tableau builds your site and writes anything to disk.

## Priority

Extensions can be assigned a numeric priority for used with sorting.

```elixir
defmodule MySite.PostsExtension do
use Tableau.Extension, type: :pre_build
use Tableau.Extension, type: :pre_build, priority: 300

def run(_site) do
posts = Path.wildcard("_posts/**/*.md")
Expand All @@ -37,11 +43,12 @@ defmodule Tableau.Extension do
@callback run(map()) :: :ok | :error

defmacro __using__(opts) do
opts = Keyword.validate!(opts, [:type])
opts = Keyword.validate!(opts, [:type, :priority])

prelude =
quote do
def __tableau_extension_type__, do: unquote(opts)[:type]
def __tableau_extension_priority__, do: unquote(opts)[:priority] || 0
end

postlude =
Expand Down
35 changes: 27 additions & 8 deletions test/mix/tasks/tableau.build_test.exs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
defmodule Mix.Tasks.Tableau.LogExtension do
use Tableau.Extension, type: :pre_build
use Tableau.Extension, type: :pre_build, priority: 200

def run(_site) do
IO.puts("hi!")
IO.inspect(System.monotonic_time(), label: "second")
:ok
end
end

defmodule Mix.Tasks.Tableau.FailExtension do
use Tableau.Extension, type: :pre_build
use Tableau.Extension, type: :pre_build, priority: 100

def run(_site) do
IO.inspect(System.monotonic_time(), label: "first")
:error
end
end
Expand Down Expand Up @@ -109,11 +110,29 @@ defmodule Mix.Tasks.Tableau.BuildTest do

@tag :tmp_dir
test "renders all pages", %{tmp_dir: out} do
assert capture_io(fn ->
assert capture_log(fn ->
_documents = Build.run(["--out", out])
end) =~ "FailExtension failed to run"
end) =~ "hi!"
{log, io} =
with_io(fn ->
{_, log} =
with_log(fn ->
_documents = Build.run(["--out", out])
end)

log
end)

assert [{"first", first}, {"second", second}] =
io
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
[order, time] =
Regex.run(~r/^(first|second): (.*)$/, line, capture: :all_but_first)

{order, String.to_integer(time)}
end)

assert first < second

assert log =~ "FailExtension failed to run"

assert File.exists?(Path.join(out, "/index.html"))
assert File.exists?(Path.join(out, "/about/index.html"))
Expand Down