Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions lib/ash_phoenix/gen/live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)),
Expand Down
10 changes: 10 additions & 0 deletions lib/mix/tasks/ash_phoenix.gen.live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down