Files
tribes-plugin-template/lib/my_plugin/plugin.ex

102 lines
2.5 KiB
Elixir

defmodule MyPlugin.Plugin do
@moduledoc """
Tribes plugin entry point.
Implements the `Tribes.Plugin` behaviour. This module is referenced by
`entry_module` in manifest.json and is called by the host's PluginManager
during startup.
"""
# Once Tribes.Plugin.Base is available, replace the manual implementation
# with:
#
# use Tribes.Plugin.Base, otp_app: :my_plugin
#
# and override only register/1.
# @behaviour Tribes.Plugin
def register(_context) do
manifest = read_manifest()
%{
name: manifest["name"],
version: manifest["version"],
provides: manifest["provides"] || [],
requires: manifest["requires"] || [],
enhances_with: manifest["enhances_with"] || [],
nav_items: [
%{
label: "My Plugin",
path: "/plugins/my_plugin",
icon: nil,
requires: [],
order: 50
}
],
pages: [
%{
path: "/plugins/my_plugin",
live_view: MyPluginWeb.HomeLive,
layout: nil
}
],
api_routes: [],
plugs: [],
children: [],
global_js: get_in(manifest, ["assets", "global_js"]) || [],
global_css: get_in(manifest, ["assets", "global_css"]) || [],
migrations_path: migrations_path(manifest),
hooks: %{}
}
end
defp read_manifest do
manifest_path()
|> File.read!()
|> Jason.decode!()
end
defp manifest_path do
project_manifest = Path.join(__DIR__, "../../manifest.json") |> Path.expand()
candidates =
case :code.priv_dir(:my_plugin) do
{:error, :bad_name} ->
[project_manifest]
priv_dir ->
priv_dir = to_string(priv_dir)
[
Path.join(priv_dir, "../manifest.json") |> Path.expand(),
project_manifest
]
end
first_existing_path(candidates) || project_manifest
end
defp migrations_path(manifest) do
if manifest["migrations"] do
candidates =
case :code.priv_dir(:my_plugin) do
{:error, :bad_name} ->
[Path.join(__DIR__, "../../priv/repo/migrations") |> Path.expand()]
priv_dir ->
[
Path.join(to_string(priv_dir), "repo/migrations") |> Path.expand(),
Path.join(__DIR__, "../../priv/repo/migrations") |> Path.expand()
]
end
first_existing_path(candidates)
end
end
defp first_existing_path(paths) do
Enum.find(paths, &File.exists?/1)
end
end