template: make rename portable and add explicit otp_app dev flow

This commit is contained in:
2026-04-04 19:45:32 +02:00
parent 647d5537ff
commit 54d9bdd99c
5 changed files with 70 additions and 23 deletions

View File

@@ -58,24 +58,44 @@ defmodule MyPlugin.Plugin do
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()
project_manifest = Path.join(__DIR__, "../../manifest.json") |> Path.expand()
priv_dir ->
priv_dir |> to_string() |> Path.join("../manifest.json") |> Path.expand()
end
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
case :code.priv_dir(:my_plugin) do
{:error, :bad_name} -> nil
priv_dir -> priv_dir |> to_string() |> Path.join("repo/migrations")
end
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