-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtableau.build.ex
130 lines (97 loc) · 3.25 KB
/
tableau.build.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
defmodule Mix.Tasks.Tableau.Build do
@shortdoc "Builds the site"
@moduledoc "Task to build the tableau site"
use Mix.Task
require Logger
@config :tableau |> Application.compile_env(:config, %{}) |> Map.new()
@impl Mix.Task
def run(argv) do
Application.ensure_all_started(:telemetry)
{:ok, config} = Tableau.Config.new(@config)
token = %{site: %{config: config}}
Mix.Task.run("app.start", ["--preload-modules"])
{opts, _argv} = OptionParser.parse!(argv, strict: [out: :string])
out = Keyword.get(opts, :out, "_site")
mods = :code.all_available()
token =
for module <- pre_build_extensions(mods), reduce: token do
token ->
config_mod = Module.concat([module, Config])
raw_config =
:tableau |> Application.get_env(module, %{}) |> Map.new()
if raw_config[:enabled] do
{:ok, config} =
config_mod.new(raw_config)
{:ok, key} = Tableau.Extension.key(module)
token = put_in(token[key], config)
case module.run(token) do
{:ok, token} ->
token
:error ->
Logger.error("#{inspect(module)} failed to run")
token
end
else
token
end
end
mods = :code.all_available()
graph = Tableau.Graph.new(mods)
File.mkdir_p!(out)
pages =
for mod <- Graph.vertices(graph), {:ok, :page} == Tableau.Graph.Node.type(mod) do
{mod, Map.new(mod.__tableau_opts__() || [])}
end
{page_mods, pages} = Enum.unzip(pages)
token = put_in(token.site[:pages], pages)
for mod <- page_mods do
content = Tableau.Document.render(graph, mod, token)
permalink = mod.__tableau_permalink__()
dir = Path.join(out, permalink)
File.mkdir_p!(dir)
File.write!(Path.join(dir, "index.html"), content)
end
if File.exists?(config.include_dir) do
File.cp_r!(config.include_dir, out)
end
for module <- post_write_extensions(mods), reduce: token do
token ->
config_mod = Module.concat([module, Config])
raw_config =
:tableau |> Application.get_env(module, %{}) |> Map.new()
if raw_config[:enabled] do
{:ok, config} =
config_mod.new(raw_config)
{:ok, key} = Tableau.Extension.key(module)
token = put_in(token[key], config)
case module.run(token) do
{:ok, token} ->
token
:error ->
Logger.error("#{inspect(module)} failed to run")
token
end
else
token
end
end
end
defp pre_build_extensions(modules) do
extensions =
for {mod, _, _} <- modules,
mod = Module.concat([to_string(mod)]),
match?({:ok, :pre_build}, Tableau.Extension.type(mod)) do
mod
end
Enum.sort_by(extensions, & &1.__tableau_extension_priority__())
end
defp post_write_extensions(modules) do
extensions =
for {mod, _, _} <- modules,
mod = Module.concat([to_string(mod)]),
match?({:ok, :post_write}, Tableau.Extension.type(mod)) do
mod
end
Enum.sort_by(extensions, & &1.__tableau_extension_priority__())
end
end