diff --git a/lib/ash_phoenix/gen/live.ex b/lib/ash_phoenix/gen/live.ex index 1a59741..5b1cdbe 100644 --- a/lib/ash_phoenix/gen/live.ex +++ b/lib/ash_phoenix/gen/live.ex @@ -125,10 +125,12 @@ if Code.ensure_loaded?(Igniter) do {"old", igniter} end - template_folder = template_folder(path) + user_template_files = user_template_files() + + default_template_folder = default_template_folder(path) action? = assigns[:update_action] || assigns[:create_action] - template_folder + default_template_folder |> File.ls!() |> Enum.reject(&String.ends_with?(&1, ".license")) |> Enum.reduce(igniter, fn @@ -142,7 +144,7 @@ if Code.ensure_loaded?(Igniter) do {formatter_function, _options} = Mix.Tasks.Format.formatter_for_file(destination_path) - path = Path.join(template_folder, file) + path = Map.get(user_template_files, file, Path.join(default_template_folder, file)) contents = path |> EEx.eval_file(assigns: assigns) |> formatter_function.() Igniter.create_new_file(igniter, destination_path, contents, generate_opts) end) @@ -322,12 +324,29 @@ if Code.ensure_loaded?(Igniter) do Igniter.Libs.Phoenix.web_module(igniter) end - defp template_folder(path) do + def default_template_folder(path) do :code.priv_dir(:ash_phoenix) |> Path.join("templates/ash_phoenix.gen.live") |> Path.join(path) end + defp user_template_files() do + user_template_folder = + File.cwd!() + |> Path.join("priv/templates/ash_phoenix.gen.live") + + case File.exists?(user_template_folder) do + true -> + File.ls!(user_template_folder) + |> Enum.reduce(%{}, fn file_handle, file_map -> + Map.put(file_map, file_handle, Path.join(user_template_folder, file_handle)) + end) + + false -> + %{} + end + end + def inputs(resource, action) do Enum.map( action.arguments ++ Enum.map(action.accept, &Ash.Resource.Info.attribute(resource, &1)), diff --git a/lib/mix/tasks/ash_phoenix.gen.live.ex b/lib/mix/tasks/ash_phoenix.gen.live.ex index e08fec0..3ae7ae3 100644 --- a/lib/mix/tasks/ash_phoenix.gen.live.ex +++ b/lib/mix/tasks/ash_phoenix.gen.live.ex @@ -28,6 +28,16 @@ if Code.ensure_loaded?(Igniter) do * `--resource-plural` - Pluralized version resource name for the route paths and templates * `--resource-plural-for-routes` - Override the plural name used in route paths (e.g. "random-things"). Useful when the route prefix contains dashes. * `--phx-version` - Phoenix version 1.7 (old) or 1.8 (new). Defaults to 1.8 + + ## Custom Templates + + If you wish to use custom generator templates, they must be placed in your `priv/templates/ash_phoenix.gen.live` directory. Any custom templates located will replace the standard AshPhoenix template file for that file name. + + AshPhoenix will allow use of the following files for templates: + * `form.ex.eex` + * `index.ex.eex` + * `show.ex.eex` + """ def info(_argv, _composing_task) do