feat: more completemetadata_diff
This commit is contained in:
@@ -5,7 +5,9 @@ defmodule BDS.Maintenance do
|
||||
|
||||
alias BDS.Frontmatter
|
||||
alias BDS.Media.Media
|
||||
alias BDS.Media.Translation, as: MediaTranslation
|
||||
alias BDS.Posts.Post
|
||||
alias BDS.Posts.Translation, as: PostTranslation
|
||||
alias BDS.Projects
|
||||
alias BDS.Repo
|
||||
alias BDS.Scripts.Script
|
||||
@@ -27,7 +29,9 @@ defmodule BDS.Maintenance do
|
||||
|
||||
diff_reports =
|
||||
post_diff_reports(project_id, project) ++
|
||||
post_translation_diff_reports(project_id, project) ++
|
||||
media_diff_reports(project_id, project) ++
|
||||
media_translation_diff_reports(project_id, project) ++
|
||||
script_diff_reports(project_id, project) ++
|
||||
template_diff_reports(project_id, project)
|
||||
|
||||
@@ -110,6 +114,65 @@ defmodule BDS.Maintenance do
|
||||
end)
|
||||
end
|
||||
|
||||
defp post_translation_diff_reports(project_id, project) do
|
||||
Repo.all(
|
||||
from translation in PostTranslation,
|
||||
where: translation.project_id == ^project_id and not is_nil(translation.file_path) and translation.file_path != ""
|
||||
)
|
||||
|> Enum.flat_map(fn translation ->
|
||||
case read_frontmatter_document(project, translation.file_path) do
|
||||
{:ok, %{fields: fields}} ->
|
||||
differences =
|
||||
[
|
||||
diff_field("title", translation.title, Map.get(fields, "title")),
|
||||
diff_field("excerpt", translation.excerpt, Map.get(fields, "excerpt")),
|
||||
diff_field("language", translation.language, Map.get(fields, "language")),
|
||||
diff_field("status", translation.status, Map.get(fields, "status")),
|
||||
diff_field("translation_for", translation.translation_for, Map.get(fields, "translation_for"))
|
||||
]
|
||||
|> Enum.reject(&is_nil/1)
|
||||
|
||||
if differences == [] do
|
||||
[]
|
||||
else
|
||||
[%{entity_type: "post_translation", entity_id: translation.id, differences: differences}]
|
||||
end
|
||||
|
||||
{:error, _reason} ->
|
||||
[]
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp media_translation_diff_reports(project_id, project) do
|
||||
Repo.all(from translation in MediaTranslation, where: translation.project_id == ^project_id)
|
||||
|> Enum.flat_map(fn translation ->
|
||||
sidecar_path = media_translation_sidecar_path(project_id, translation)
|
||||
|
||||
case sidecar_path && read_sidecar_document(project, sidecar_path) do
|
||||
{:ok, fields} ->
|
||||
differences =
|
||||
[
|
||||
diff_field("title", translation.title, Map.get(fields, "title")),
|
||||
diff_field("alt", translation.alt, Map.get(fields, "alt")),
|
||||
diff_field("caption", translation.caption, Map.get(fields, "caption")),
|
||||
diff_field("language", translation.language, Map.get(fields, "language")),
|
||||
diff_field("translation_for", translation.translation_for, Map.get(fields, "translation_for"))
|
||||
]
|
||||
|> Enum.reject(&is_nil/1)
|
||||
|
||||
if differences == [] do
|
||||
[]
|
||||
else
|
||||
[%{entity_type: "media_translation", entity_id: translation.id, differences: differences}]
|
||||
end
|
||||
|
||||
_ ->
|
||||
[]
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp script_diff_reports(project_id, project) do
|
||||
Repo.all(
|
||||
from script in Script,
|
||||
@@ -168,6 +231,8 @@ defmodule BDS.Maintenance do
|
||||
defp orphan_reports(project_id, project) do
|
||||
post_paths = MapSet.new(Repo.all(from post in Post, where: post.project_id == ^project_id, select: post.file_path))
|
||||
media_paths = MapSet.new(Repo.all(from media in Media, where: media.project_id == ^project_id, select: media.sidecar_path))
|
||||
post_translation_paths = MapSet.new(Repo.all(from translation in PostTranslation, where: translation.project_id == ^project_id, select: translation.file_path))
|
||||
media_translation_paths = MapSet.new(media_translation_sidecar_paths(project_id))
|
||||
script_paths = MapSet.new(Repo.all(from script in Script, where: script.project_id == ^project_id, select: script.file_path))
|
||||
template_paths = MapSet.new(Repo.all(from template in Template, where: template.project_id == ^project_id, select: template.file_path))
|
||||
|
||||
@@ -175,8 +240,16 @@ defmodule BDS.Maintenance do
|
||||
project
|
||||
|> list_project_files("posts/**/*.md")
|
||||
|> Enum.map(&Path.relative_to(&1, Projects.project_data_dir(project)))
|
||||
|> Enum.reject(&translation_post_file?/1)
|
||||
|> Enum.reject(&MapSet.member?(post_paths, &1))
|
||||
|
||||
post_translation_orphans =
|
||||
project
|
||||
|> list_project_files("posts/**/*.md")
|
||||
|> Enum.map(&Path.relative_to(&1, Projects.project_data_dir(project)))
|
||||
|> Enum.filter(&translation_post_file?/1)
|
||||
|> Enum.reject(&MapSet.member?(post_translation_paths, &1))
|
||||
|
||||
media_orphans =
|
||||
project
|
||||
|> list_project_files("media/**/*.meta")
|
||||
@@ -184,6 +257,13 @@ defmodule BDS.Maintenance do
|
||||
|> Enum.filter(&canonical_media_sidecar?/1)
|
||||
|> Enum.reject(&MapSet.member?(media_paths, &1))
|
||||
|
||||
media_translation_orphans =
|
||||
project
|
||||
|> list_project_files("media/**/*.meta")
|
||||
|> Enum.map(&Path.relative_to(&1, Projects.project_data_dir(project)))
|
||||
|> Enum.filter(&translation_media_sidecar?/1)
|
||||
|> Enum.reject(&MapSet.member?(media_translation_paths, &1))
|
||||
|
||||
script_orphans =
|
||||
project
|
||||
|> list_project_files("scripts/**/*.lua")
|
||||
@@ -196,7 +276,7 @@ defmodule BDS.Maintenance do
|
||||
|> Enum.map(&Path.relative_to(&1, Projects.project_data_dir(project)))
|
||||
|> Enum.reject(&MapSet.member?(template_paths, &1))
|
||||
|
||||
(post_orphans ++ media_orphans ++ script_orphans ++ template_orphans)
|
||||
(post_orphans ++ post_translation_orphans ++ media_orphans ++ media_translation_orphans ++ script_orphans ++ template_orphans)
|
||||
|> Enum.sort()
|
||||
|> Enum.map(&%{file_path: &1})
|
||||
end
|
||||
@@ -249,4 +329,25 @@ defmodule BDS.Maintenance do
|
||||
defp canonical_media_sidecar?(relative_path) do
|
||||
not Regex.match?(~r/\.[a-z]{2}\.meta$/i, relative_path)
|
||||
end
|
||||
|
||||
defp translation_post_file?(relative_path) do
|
||||
Regex.match?(~r/\.[a-z]{2}\.md$/i, relative_path)
|
||||
end
|
||||
|
||||
defp translation_media_sidecar?(relative_path) do
|
||||
Regex.match?(~r/\.[a-z]{2}\.meta$/i, relative_path)
|
||||
end
|
||||
|
||||
defp media_translation_sidecar_paths(project_id) do
|
||||
Repo.all(from translation in MediaTranslation, where: translation.project_id == ^project_id)
|
||||
|> Enum.map(&media_translation_sidecar_path(project_id, &1))
|
||||
|> Enum.reject(&is_nil/1)
|
||||
end
|
||||
|
||||
defp media_translation_sidecar_path(project_id, translation) do
|
||||
case Repo.one(from media in Media, where: media.project_id == ^project_id and media.id == ^translation.translation_for, select: media.file_path) do
|
||||
nil -> nil
|
||||
file_path -> "#{file_path}.#{translation.language}.meta"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user