2022-11-03 20:47:05 +13:00
|
|
|
defmodule Mix.Tasks.Ash.ReplaceDocLinks do
|
|
|
|
@moduledoc """
|
|
|
|
Replaces any documentation links with text appropriate for hex docs.
|
|
|
|
"""
|
|
|
|
use Mix.Task
|
|
|
|
|
2023-01-18 18:34:20 +13:00
|
|
|
@shortdoc "Replaces any ash specific doc links with text appropriate for hex docs."
|
2022-11-03 20:47:05 +13:00
|
|
|
def run(_argv) do
|
2023-01-18 18:34:20 +13:00
|
|
|
mix_project = Mix.Project.get!()
|
|
|
|
module_prefix = mix_project |> Module.split() |> Enum.at(0)
|
|
|
|
|
2022-11-03 20:47:05 +13:00
|
|
|
"doc/**/*.html"
|
|
|
|
|> Path.wildcard()
|
|
|
|
|> Enum.each(fn file ->
|
|
|
|
new_contents =
|
|
|
|
file
|
|
|
|
|> File.read!()
|
2023-01-18 18:34:20 +13:00
|
|
|
|> String.replace(~r/\>d\:.*\</, fn ">d:" <> contents ->
|
|
|
|
contents =
|
|
|
|
contents
|
|
|
|
|> String.trim_trailing("<")
|
|
|
|
|> String.replace("|", ".")
|
|
|
|
|
|
|
|
module_name =
|
|
|
|
contents
|
|
|
|
|> String.split(".")
|
|
|
|
|> Enum.take_while(&capitalized?/1)
|
|
|
|
|> Enum.join(".")
|
|
|
|
|
|
|
|
if String.starts_with?(module_name, module_prefix) do
|
|
|
|
case Code.ensure_compiled(Module.concat([module_name])) do
|
|
|
|
{:module, _} ->
|
2022-11-22 12:27:35 +13:00
|
|
|
:ok
|
2022-11-16 15:26:25 +13:00
|
|
|
|
|
|
|
{:error, error} ->
|
2023-01-18 18:34:20 +13:00
|
|
|
raise "Expected #{module_name} to be compiled, but it was not: #{inspect(error)}"
|
2022-11-16 15:26:25 +13:00
|
|
|
end
|
2023-01-18 18:34:20 +13:00
|
|
|
end
|
2022-11-16 15:26:25 +13:00
|
|
|
|
2023-01-18 18:34:20 +13:00
|
|
|
">#{contents}<"
|
2022-11-03 20:47:05 +13:00
|
|
|
end)
|
2023-01-18 18:34:20 +13:00
|
|
|
|> String.replace(~r</documentation/.*/.*.md>, fn "/documentation/" <> type_and_name ->
|
|
|
|
[_, filename] = String.split(type_and_name, "/")
|
2022-11-03 20:47:05 +13:00
|
|
|
|
2023-01-18 18:34:20 +13:00
|
|
|
filename |> String.trim_trailing(".md") |> Kernel.<>(".html")
|
2022-12-08 17:35:00 +13:00
|
|
|
end)
|
|
|
|
|
|
|
|
File.write!(file, new_contents)
|
|
|
|
end)
|
2022-11-03 20:47:05 +13:00
|
|
|
end
|
|
|
|
|
2023-01-18 18:34:20 +13:00
|
|
|
defp capitalized?(string) do
|
|
|
|
first =
|
|
|
|
string
|
|
|
|
|> String.graphemes()
|
|
|
|
|> Enum.at(0)
|
2022-12-08 16:38:47 +13:00
|
|
|
|
2023-01-18 18:34:20 +13:00
|
|
|
String.downcase(first) != first
|
2022-12-08 16:38:47 +13:00
|
|
|
end
|
2022-11-03 20:47:05 +13:00
|
|
|
end
|