You've already forked tribes-plugin-new
21f8f861c4
Default generated plugins to TribeOne.TribesPlugin.* entry modules and remove the old Tribes.Plugins bridge module pattern from templates and tests.
106 lines
2.5 KiB
Elixir
106 lines
2.5 KiB
Elixir
defmodule TribesPlugin.Naming do
|
|
@moduledoc false
|
|
|
|
@app_regex ~r/\A[a-z][a-z0-9_]*\z/
|
|
@module_regex ~r/\A[A-Z][A-Za-z0-9]*(\.[A-Z][A-Za-z0-9]*)*\z/
|
|
|
|
defstruct app: nil,
|
|
module: nil,
|
|
path: nil,
|
|
title: nil,
|
|
css_class: nil,
|
|
web_module: nil,
|
|
entry_module: nil
|
|
|
|
def resolve(path, opts \\ []) do
|
|
app = opts |> Keyword.get(:app) |> blank_to_nil() || derive_app(path)
|
|
module = opts |> Keyword.get(:module) |> blank_to_nil() |> normalize_module(app)
|
|
|
|
with :ok <- validate_app(app),
|
|
:ok <- validate_module(module) do
|
|
{:ok,
|
|
%__MODULE__{
|
|
app: app,
|
|
module: module,
|
|
path: "/" <> app,
|
|
title: titleize(app),
|
|
css_class: String.replace(app, "_", "-"),
|
|
web_module: module <> "Web",
|
|
entry_module: module <> ".Plugin"
|
|
}}
|
|
end
|
|
end
|
|
|
|
def resolve!(path, opts \\ []) do
|
|
case resolve(path, opts) do
|
|
{:ok, naming} -> naming
|
|
{:error, message} -> raise ArgumentError, message
|
|
end
|
|
end
|
|
|
|
def page_module(name) when is_binary(name) do
|
|
name
|
|
|> String.trim()
|
|
|> String.trim_trailing("Live")
|
|
|> Macro.camelize()
|
|
|> Kernel.<>("Live")
|
|
end
|
|
|
|
def page_path(name) when is_binary(name) do
|
|
name
|
|
|> String.trim()
|
|
|> Macro.underscore()
|
|
|> String.trim_trailing("_live")
|
|
|> String.replace("_", "-")
|
|
end
|
|
|
|
defp derive_app(path) do
|
|
path
|
|
|> Path.basename()
|
|
|> String.replace("-", "_")
|
|
|> Macro.underscore()
|
|
|> String.replace_prefix("tribes_plugin_", "")
|
|
end
|
|
|
|
defp titleize(app) do
|
|
app
|
|
|> String.split("_")
|
|
|> Enum.map_join(" ", &String.capitalize/1)
|
|
end
|
|
|
|
defp validate_app(app) do
|
|
if Regex.match?(@app_regex, app) do
|
|
:ok
|
|
else
|
|
{:error, "app must match [a-z][a-z0-9_]*, got: #{inspect(app)}"}
|
|
end
|
|
end
|
|
|
|
defp validate_module(module) do
|
|
if Regex.match?(@module_regex, module) do
|
|
:ok
|
|
else
|
|
{:error, "module must be a valid Elixir alias, got: #{inspect(module)}"}
|
|
end
|
|
end
|
|
|
|
defp normalize_module(nil, app), do: "TribeOne.TribesPlugin." <> Macro.camelize(app)
|
|
|
|
defp normalize_module(module, _app) when is_binary(module) do
|
|
if String.contains?(module, ".") do
|
|
module
|
|
else
|
|
"TribeOne.TribesPlugin." <> module
|
|
end
|
|
end
|
|
|
|
defp blank_to_nil(nil), do: nil
|
|
|
|
defp blank_to_nil(value) when is_binary(value) do
|
|
case String.trim(value) do
|
|
"" -> nil
|
|
trimmed -> trimmed
|
|
end
|
|
end
|
|
end
|