fix: better progress reporting on tasks
This commit is contained in:
@@ -133,15 +133,27 @@ defmodule BDS.Templates do
|
||||
end
|
||||
end
|
||||
|
||||
def rebuild_templates_from_files(project_id) do
|
||||
def rebuild_templates_from_files(project_id, opts \\ []) do
|
||||
project = Projects.get_project!(project_id)
|
||||
|
||||
templates =
|
||||
template_paths =
|
||||
project
|
||||
|> Projects.project_data_dir()
|
||||
|> Path.join("templates")
|
||||
|> list_matching_files("*.liquid")
|
||||
|> Enum.map(&upsert_template_from_file(project_id, project, &1))
|
||||
|
||||
total_files = length(template_paths)
|
||||
on_progress = progress_callback(opts)
|
||||
:ok = report_rebuild_started(on_progress, total_files, "template files")
|
||||
|
||||
templates =
|
||||
template_paths
|
||||
|> Enum.with_index(1)
|
||||
|> Enum.map(fn {path, index} ->
|
||||
template = upsert_template_from_file(project_id, project, path)
|
||||
:ok = report_rebuild_progress(on_progress, index, total_files, "template files")
|
||||
template
|
||||
end)
|
||||
|
||||
{:ok, templates}
|
||||
end
|
||||
@@ -410,4 +422,31 @@ defmodule BDS.Templates do
|
||||
true -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp progress_callback(opts) do
|
||||
case Keyword.get(opts, :on_progress) do
|
||||
callback when is_function(callback, 2) -> callback
|
||||
_other -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp report_rebuild_started(nil, _total, _label), do: :ok
|
||||
|
||||
defp report_rebuild_started(callback, 0, label) do
|
||||
callback.(1.0, "No #{label} found")
|
||||
:ok
|
||||
end
|
||||
|
||||
defp report_rebuild_started(callback, total, label) do
|
||||
callback.(0.05, "Rebuilding #{label} (0/#{total})")
|
||||
:ok
|
||||
end
|
||||
|
||||
defp report_rebuild_progress(nil, _current, _total, _label), do: :ok
|
||||
defp report_rebuild_progress(_callback, _current, 0, _label), do: :ok
|
||||
|
||||
defp report_rebuild_progress(callback, current, total, label) do
|
||||
callback.(0.05 + 0.95 * (current / total), "Rebuilding #{label} (#{current}/#{total})")
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user