Files
tribes-plugin-aether/lib/my_plugin/plugin.ex
2026-03-25 12:42:19 +01:00

82 lines
2.1 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
# In a release, manifest.json sits alongside ebin/ in the plugin directory.
# In dev mode (path dep), it's at the project root.
case :code.priv_dir(:my_plugin) do
{:error, :bad_name} ->
# Dev mode fallback: relative to project root
Path.join(__DIR__, "../../../manifest.json") |> Path.expand()
priv_dir ->
priv_dir |> to_string() |> Path.join("../manifest.json") |> Path.expand()
end
end
defp migrations_path(manifest) do
if manifest["migrations"] do
case :code.priv_dir(:my_plugin) do
{:error, :bad_name} -> nil
priv_dir -> priv_dir |> to_string() |> Path.join("repo/migrations")
end
end
end
end