Skip to content

Commit

Permalink
fix: refresh graph after running extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Jun 26, 2023
1 parent 34c9b09 commit fbb2621
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
13 changes: 8 additions & 5 deletions lib/mix/tasks/tableau.build.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ defmodule Mix.Tasks.Tableau.Build do
{opts, _argv} = OptionParser.parse!(argv, strict: [out: :string])

out = Keyword.get(opts, :out, "_site")

mods = :code.all_available()
graph = Tableau.Graph.new(mods)
File.mkdir_p!(out)

for module <- pre_write_extensions(mods) do
with :error <- module.run(graph, %{site: %{}}) do
for module <- pre_build_extensions(mods) do
with :error <- module.run(%{site: %{}}) do
Logger.error("#{inspect(module)} failed to run")
end
end

mods = :code.all_available()
graph = Tableau.Graph.new(mods)
File.mkdir_p!(out)

for mod <- Graph.vertices(graph), {:ok, :page} == Tableau.Graph.Node.type(mod) do
content = Tableau.Document.render(graph, mod, %{site: %{}})
permalink = mod.__tableau_permalink__()
Expand All @@ -40,7 +43,7 @@ defmodule Mix.Tasks.Tableau.Build do
end
end

defp pre_write_extensions(modules) do
defp pre_build_extensions(modules) do
for {mod, _, _} <- modules,
mod = Module.concat([to_string(mod)]),
match?({:ok, :pre_build}, Tableau.Extension.type(mod)) do
Expand Down
1 change: 1 addition & 0 deletions lib/mix/tasks/tableau.server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Mix.Tasks.Tableau.Server do
@impl Mix.Task
def run(_args) do
Application.put_env(:tableau, :server, true)
Code.put_compiler_option(:ignore_module_conflict, true)

Logger.debug("server started on http://localhost:4999")

Expand Down
8 changes: 3 additions & 5 deletions lib/tableau/extension.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Tableau.Extension do
defmodule MySite.PostsExtension do
use Tableau.Extension, type: :pre_build
def run(_graph, _site) do
def run(_site) do
posts = Path.wildcard("_posts/**/*.md")
for post <- post do
Expand All @@ -32,11 +32,9 @@ defmodule Tableau.Extension do
@doc """
The extension entry point.
The function is passed the layout graph and a set of default assigns.
The graph is an instance of a [Graph.t](https://hexdocs.pm/libgraph/0.16.0/Graph.html#t:t/0) from the library `libgraph`.
The function is passed the a set of default assigns.
"""
@callback run(Graph.t(), map()) :: :ok | :error
@callback run(map()) :: :ok | :error

defmacro __using__(opts) do
opts = Keyword.validate!(opts, [:type])
Expand Down
4 changes: 2 additions & 2 deletions test/mix/tasks/tableau.build_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Mix.Tasks.Tableau.LogExtension do
use Tableau.Extension, type: :pre_build

def run(_graph, _site) do
def run(_site) do
IO.puts("hi!")
:ok
end
Expand All @@ -10,7 +10,7 @@ end
defmodule Mix.Tasks.Tableau.FailExtension do
use Tableau.Extension, type: :pre_build

def run(_graph, _site) do
def run(_site) do
:error
end
end
Expand Down

0 comments on commit fbb2621

Please sign in to comment.