feat: persisting elements in filesystem

This commit is contained in:
2026-04-23 14:26:16 +02:00
parent 6f57728a6d
commit 5374ffe613
8 changed files with 811 additions and 53 deletions

29
lib/bds/frontmatter.ex Normal file
View File

@@ -0,0 +1,29 @@
defmodule BDS.Frontmatter do
@moduledoc false
def serialize_document(fields, body) when is_list(fields) do
frontmatter =
fields
|> Enum.flat_map(&serialize_field/1)
|> Enum.join("\n")
["---", frontmatter, "---", body || "", ""]
|> Enum.join("\n")
end
defp serialize_field({_key, nil}), do: []
defp serialize_field({_key, ""}), do: []
defp serialize_field({_key, false}), do: []
defp serialize_field({key, true}) do
["#{key}: true"]
end
defp serialize_field({key, values}) when is_list(values) do
["#{key}:" | Enum.map(values, &" - #{&1}")]
end
defp serialize_field({key, value}) do
["#{key}: #{value}"]
end
end

View File

@@ -3,6 +3,7 @@ defmodule BDS.Posts do
import Ecto.Query
alias BDS.Frontmatter
alias BDS.Posts.Post
alias BDS.Projects
alias BDS.Repo
@@ -76,9 +77,10 @@ defmodule BDS.Posts do
published_at = post.published_at || System.system_time(:second)
relative_path = build_post_relative_path(post.slug, post.created_at)
full_path = Path.join(Projects.project_data_dir(project), relative_path)
updated_at = System.system_time(:second)
:ok = File.mkdir_p(Path.dirname(full_path))
:ok = File.write(full_path, serialize_post_file(post, published_at))
:ok = File.write(full_path, serialize_post_file(%{post | updated_at: updated_at}, published_at))
post
|> Post.changeset(%{
@@ -86,12 +88,28 @@ defmodule BDS.Posts do
published_at: published_at,
file_path: relative_path,
content: nil,
updated_at: System.system_time(:second)
updated_at: updated_at
})
|> Repo.update()
end
end
def get_post!(post_id), do: Repo.get!(Post, post_id)
def rewrite_published_post(post_id) do
post = Repo.get!(Post, post_id)
if post.status == :published and post.file_path not in [nil, ""] do
project = Projects.get_project!(post.project_id)
full_path = Path.join(Projects.project_data_dir(project), post.file_path)
body = published_post_body(post, full_path)
:ok = File.mkdir_p(Path.dirname(full_path))
:ok = File.write(full_path, serialize_post_file(%{post | content: body}, post.published_at || System.system_time(:second)))
end
:ok
end
defp normalize_updates(attrs, _post) do
%{}
|> maybe_put(:title, normalize_optional_title(attr(attrs, :title), attrs))
@@ -197,41 +215,42 @@ defmodule BDS.Posts do
end
defp serialize_post_file(post, published_at) do
frontmatter_lines = [
"id: #{post.id}",
"title: #{post.title}",
"slug: #{post.slug}",
maybe_line("excerpt", post.excerpt),
"status: published",
maybe_line("author", post.author),
maybe_line("language", post.language),
maybe_boolean_line("do_not_translate", post.do_not_translate),
maybe_line("template_slug", post.template_slug),
"created_at: #{post.created_at}",
"updated_at: #{post.updated_at}",
"published_at: #{published_at}",
list_lines("tags", post.tags),
list_lines("categories", post.categories)
]
|> List.flatten()
|> Enum.reject(&is_nil/1)
|> Enum.join("\n")
["---", frontmatter_lines, "---", post.content || "", ""]
|> Enum.join("\n")
Frontmatter.serialize_document(
[
{:id, post.id},
{:title, post.title},
{:slug, post.slug},
{:excerpt, post.excerpt},
{:status, :published},
{:author, post.author},
{:language, post.language},
{:do_not_translate, post.do_not_translate},
{:template_slug, post.template_slug},
{:created_at, post.created_at},
{:updated_at, post.updated_at},
{:published_at, published_at},
{:tags, post.tags || []},
{:categories, post.categories || []}
],
post.content || ""
)
end
defp list_lines(label, items) do
["#{label}:" | Enum.map(items || [], &" - #{&1}")]
defp published_post_body(%Post{content: content}, _full_path) when is_binary(content), do: content
defp published_post_body(_post, full_path) do
case File.read(full_path) do
{:ok, contents} ->
case String.split(contents, "\n---\n", parts: 2) do
[_frontmatter, body] -> String.trim_trailing(body, "\n")
_parts -> ""
end
{:error, _reason} ->
""
end
end
defp maybe_line(_label, nil), do: nil
defp maybe_line(_label, ""), do: nil
defp maybe_line(label, value), do: "#{label}: #{value}"
defp maybe_boolean_line(_label, false), do: nil
defp maybe_boolean_line(label, true), do: "#{label}: true"
defp has_attr?(attrs, key) do
Map.has_key?(attrs, key) or Map.has_key?(attrs, Atom.to_string(key))
end

View File

@@ -3,6 +3,8 @@ defmodule BDS.Scripts do
import Ecto.Query
alias BDS.Frontmatter
alias BDS.Projects
alias BDS.Repo
alias BDS.Scripts.Script
alias BDS.Slug
@@ -32,31 +34,152 @@ defmodule BDS.Scripts do
|> Repo.insert()
end
def publish_script(script_id) do
case Repo.get(Script, script_id) do
nil ->
{:error, :not_found}
script ->
file_path = script_file_path(script.slug)
full_path = full_file_path(script.project_id, file_path)
updated_at = System.system_time(:second)
content = script.content || ""
:ok = File.mkdir_p(Path.dirname(full_path))
:ok =
File.write(
full_path,
serialize_script_file(%{script | status: :published, file_path: file_path, updated_at: updated_at}, content)
)
script
|> Script.changeset(%{status: :published, file_path: file_path, content: nil, updated_at: updated_at})
|> Repo.update()
end
end
def update_script(script_id, attrs) do
case Repo.get(Script, script_id) do
nil ->
{:error, :not_found}
script ->
next_slug =
if has_attr?(attrs, :slug) do
unique_slug(script.project_id, Slug.slugify(attr(attrs, :slug)), "script", script.id)
else
script.slug
end
content_changed? = has_attr?(attrs, :content) and attr(attrs, :content) != script.content
now = System.system_time(:second)
updates = %{}
|> maybe_put(:title, attr(attrs, :title))
|> maybe_put(:kind, attr(attrs, :kind))
|> maybe_put(:entrypoint, attr(attrs, :entrypoint))
|> maybe_put(:enabled, attr(attrs, :enabled))
|> maybe_put(:content, attr(attrs, :content))
|> Map.put(:slug, next_slug)
|> Map.put(:version, script.version + 1)
|> Map.put(:updated_at, now)
|> maybe_put(:status, if(script.status == :published and content_changed?, do: :draft, else: nil))
script
|> Script.changeset(updates)
|> Repo.update()
end
end
def delete_script(script_id) do
case Repo.get(Script, script_id) do
nil ->
{:error, :not_found}
script ->
delete_file_if_present(script.project_id, script.file_path)
Repo.delete!(script)
{:ok, :deleted}
end
end
defp default_entrypoint(:macro), do: "render"
defp default_entrypoint(_kind), do: "main"
defp unique_slug(project_id, base_slug, fallback) do
defp unique_slug(project_id, base_slug, fallback, exclude_id \\ nil) do
normalized = if base_slug in [nil, ""], do: fallback, else: base_slug
if slug_available?(project_id, normalized) do
if slug_available?(project_id, normalized, exclude_id) do
normalized
else
find_unique_slug(project_id, normalized, 2)
find_unique_slug(project_id, normalized, 2, exclude_id)
end
end
defp find_unique_slug(project_id, base_slug, suffix) do
defp find_unique_slug(project_id, base_slug, suffix, exclude_id) do
candidate = "#{base_slug}-#{suffix}"
if slug_available?(project_id, candidate) do
if slug_available?(project_id, candidate, exclude_id) do
candidate
else
find_unique_slug(project_id, base_slug, suffix + 1)
find_unique_slug(project_id, base_slug, suffix + 1, exclude_id)
end
end
defp slug_available?(project_id, slug) do
not Repo.exists?(from script in Script, where: script.project_id == ^project_id and script.slug == ^slug)
defp slug_available?(project_id, slug, exclude_id) do
query = from script in Script, where: script.project_id == ^project_id and script.slug == ^slug
scoped_query =
case exclude_id do
nil -> query
_id -> from script in query, where: script.id != ^exclude_id
end
not Repo.exists?(scoped_query)
end
defp script_file_path(slug), do: Path.join(["scripts", "#{slug}.lua"])
defp full_file_path(project_id, relative_path) do
project = Projects.get_project!(project_id)
Path.join(Projects.project_data_dir(project), relative_path)
end
defp serialize_script_file(script, content) do
Frontmatter.serialize_document(
[
{:id, script.id},
{:slug, script.slug},
{:title, script.title},
{:kind, script.kind},
{:entrypoint, script.entrypoint},
{:enabled, script.enabled},
{:version, script.version},
{:created_at, script.created_at},
{:updated_at, script.updated_at}
],
content
)
end
defp delete_file_if_present(_project_id, file_path) when file_path in [nil, ""], do: :ok
defp delete_file_if_present(project_id, file_path) do
full_path = full_file_path(project_id, file_path)
case File.rm(full_path) do
:ok -> :ok
{:error, :enoent} -> :ok
{:error, reason} -> {:error, reason}
end
end
defp maybe_put(map, _key, nil), do: map
defp maybe_put(map, key, value), do: Map.put(map, key, value)
defp has_attr?(attrs, key) do
Map.has_key?(attrs, key) or Map.has_key?(attrs, Atom.to_string(key))
end
defp attr(attrs, key) do

View File

@@ -3,6 +3,8 @@ defmodule BDS.Tags do
import Ecto.Query
alias BDS.Posts
alias BDS.Posts.Post
alias BDS.Projects
alias BDS.Repo
alias BDS.Tags.Tag
@@ -40,6 +42,100 @@ defmodule BDS.Tags do
Repo.all(from tag in Tag, where: tag.project_id == ^project_id, order_by: [asc: tag.name])
end
def sync_tags_json(project_id) do
write_tags_json(project_id)
:ok
end
def update_tag(tag_id, attrs) do
case Repo.get(Tag, tag_id) do
nil ->
{:error, :not_found}
tag ->
updates = %{
color: attr(attrs, :color),
post_template_slug: attr(attrs, :post_template_slug),
updated_at: System.system_time(:second)
}
tag
|> Tag.changeset(updates)
|> Repo.update()
|> case do
{:ok, updated_tag} ->
write_tags_json(updated_tag.project_id)
{:ok, updated_tag}
error ->
error
end
end
end
def rename_tag(tag_id, new_name) do
case Repo.get(Tag, tag_id) do
nil ->
{:error, :not_found}
tag ->
normalized_name = String.trim(new_name)
with :ok <- validate_rename_target(tag.project_id, tag.id, normalized_name) do
old_name = tag.name
Repo.transaction(fn ->
affected_posts = posts_with_tag(tag.project_id, old_name)
Enum.each(affected_posts, fn post ->
updated_tags = replace_tag(post.tags || [], old_name, normalized_name)
update_post_tags(post, updated_tags)
end)
updated_tag =
tag
|> Tag.changeset(%{name: normalized_name, updated_at: System.system_time(:second)})
|> Repo.update!()
write_tags_json(tag.project_id)
updated_tag
end)
|> case do
{:ok, updated_tag} -> {:ok, updated_tag}
{:error, reason} -> {:error, reason}
end
end
end
end
def merge_tags(source_tag_ids, target_tag_id) do
case Repo.get(Tag, target_tag_id) do
nil ->
{:error, :not_found}
target_tag ->
source_tags =
Repo.all(from tag in Tag, where: tag.id in ^source_tag_ids and tag.project_id == ^target_tag.project_id)
Repo.transaction(fn ->
source_names = Enum.map(source_tags, & &1.name)
posts_with_any_tag(target_tag.project_id, source_names)
|> Enum.each(fn post ->
updated_tags = merge_post_tags(post.tags || [], source_names, target_tag.name)
update_post_tags(post, updated_tags)
end)
Enum.each(source_tags, &Repo.delete!/1)
write_tags_json(target_tag.project_id)
end)
|> case do
{:ok, _} -> {:ok, :merged}
{:error, reason} -> {:error, reason}
end
end
end
defp write_tags_json(project_id) do
project = Projects.get_project!(project_id)
path = Path.join([Projects.project_data_dir(project), "meta", "tags.json"])
@@ -71,6 +167,51 @@ defmodule BDS.Tags do
end
end
defp validate_rename_target(project_id, tag_id, name) do
if Repo.exists?(from tag in Tag, where: tag.project_id == ^project_id and tag.id != ^tag_id and fragment("lower(?)", tag.name) == ^String.downcase(name)) do
{:error,
%Tag{}
|> Tag.changeset(%{project_id: project_id, name: name, id: tag_id, created_at: 0, updated_at: 0})
|> Ecto.Changeset.add_error(:name, "has already been taken")}
else
:ok
end
end
defp posts_with_tag(project_id, tag_name) do
Repo.all(from post in Post, where: post.project_id == ^project_id)
|> Enum.filter(fn post -> tag_name in (post.tags || []) end)
end
defp posts_with_any_tag(project_id, tag_names) do
Repo.all(from post in Post, where: post.project_id == ^project_id)
|> Enum.filter(fn post -> Enum.any?(post.tags || [], &(&1 in tag_names)) end)
end
defp replace_tag(tags, old_name, new_name) do
tags
|> Enum.map(fn tag -> if tag == old_name, do: new_name, else: tag end)
|> Enum.uniq()
end
defp merge_post_tags(tags, source_names, target_name) do
retained_tags = Enum.reject(tags, &(&1 in source_names))
if target_name in retained_tags do
retained_tags
else
retained_tags ++ [target_name]
end
end
defp update_post_tags(post, updated_tags) do
post
|> Post.changeset(%{tags: updated_tags, updated_at: System.system_time(:second)})
|> Repo.update!()
Posts.rewrite_published_post(post.id)
end
defp maybe_put(map, _key, nil), do: map
defp maybe_put(map, _key, ""), do: map
defp maybe_put(map, key, value), do: Map.put(map, key, value)

View File

@@ -3,6 +3,8 @@ defmodule BDS.Templates do
import Ecto.Query
alias BDS.Frontmatter
alias BDS.Projects
alias BDS.Repo
alias BDS.Slug
alias BDS.Templates.Template
@@ -30,28 +32,194 @@ defmodule BDS.Templates do
|> Repo.insert()
end
defp unique_slug(project_id, base_slug, fallback) do
def publish_template(template_id) do
case Repo.get(Template, template_id) do
nil ->
{:error, :not_found}
template ->
file_path = template_file_path(template.slug)
full_path = full_file_path(template.project_id, file_path)
updated_at = System.system_time(:second)
content = template.content || ""
:ok = File.mkdir_p(Path.dirname(full_path))
:ok =
File.write(
full_path,
serialize_template_file(%{template | status: :published, file_path: file_path, updated_at: updated_at}, content)
)
template
|> Template.changeset(%{status: :published, file_path: file_path, content: nil, updated_at: updated_at})
|> Repo.update()
end
end
def update_template(template_id, attrs) do
case Repo.get(Template, template_id) do
nil ->
{:error, :not_found}
template ->
next_slug =
if has_attr?(attrs, :slug) do
unique_slug(template.project_id, Slug.slugify(attr(attrs, :slug)), "template", template.id)
else
template.slug
end
content_changed? = has_attr?(attrs, :content) and attr(attrs, :content) != template.content
now = System.system_time(:second)
updates = %{}
|> maybe_put(:title, attr(attrs, :title))
|> maybe_put(:kind, attr(attrs, :kind))
|> maybe_put(:enabled, attr(attrs, :enabled))
|> maybe_put(:content, attr(attrs, :content))
|> Map.put(:slug, next_slug)
|> Map.put(:version, template.version + 1)
|> Map.put(:updated_at, now)
|> maybe_put(:status, if(template.status == :published and content_changed?, do: :draft, else: nil))
template
|> Template.changeset(updates)
|> Repo.update()
end
end
def delete_template(template_id, opts \\ []) do
case Repo.get(Template, template_id) do
nil ->
{:error, :not_found}
template ->
post_count = count_referencing_posts(template)
tag_count = count_referencing_tags(template)
force? = Keyword.get(opts, :force, false)
cond do
not force? and (post_count > 0 or tag_count > 0) ->
{:error, {:has_references, %{posts: post_count, tags: tag_count}}}
true ->
if force? do
clear_template_references(template)
end
delete_file_if_present(template.project_id, template.file_path)
Repo.delete!(template)
{:ok, :deleted}
end
end
end
defp unique_slug(project_id, base_slug, fallback, exclude_id \\ nil) do
normalized = if base_slug in [nil, ""], do: fallback, else: base_slug
if slug_available?(project_id, normalized) do
if slug_available?(project_id, normalized, exclude_id) do
normalized
else
find_unique_slug(project_id, normalized, 2)
find_unique_slug(project_id, normalized, 2, exclude_id)
end
end
defp find_unique_slug(project_id, base_slug, suffix) do
defp find_unique_slug(project_id, base_slug, suffix, exclude_id) do
candidate = "#{base_slug}-#{suffix}"
if slug_available?(project_id, candidate) do
if slug_available?(project_id, candidate, exclude_id) do
candidate
else
find_unique_slug(project_id, base_slug, suffix + 1)
find_unique_slug(project_id, base_slug, suffix + 1, exclude_id)
end
end
defp slug_available?(project_id, slug) do
not Repo.exists?(from template in Template, where: template.project_id == ^project_id and template.slug == ^slug)
defp slug_available?(project_id, slug, exclude_id) do
query = from template in Template, where: template.project_id == ^project_id and template.slug == ^slug
scoped_query =
case exclude_id do
nil -> query
_id -> from template in query, where: template.id != ^exclude_id
end
not Repo.exists?(scoped_query)
end
defp template_file_path(slug), do: Path.join(["templates", "#{slug}.liquid"])
defp full_file_path(project_id, relative_path) do
project = Projects.get_project!(project_id)
Path.join(Projects.project_data_dir(project), relative_path)
end
defp serialize_template_file(template, content) do
Frontmatter.serialize_document(
[
{:id, template.id},
{:slug, template.slug},
{:title, template.title},
{:kind, template.kind},
{:enabled, template.enabled},
{:version, template.version},
{:created_at, template.created_at},
{:updated_at, template.updated_at}
],
content
)
end
defp count_referencing_posts(template) do
Repo.aggregate(from(post in BDS.Posts.Post, where: post.project_id == ^template.project_id and post.template_slug == ^template.slug), :count, :id)
end
defp count_referencing_tags(template) do
Repo.aggregate(from(tag in BDS.Tags.Tag, where: tag.project_id == ^template.project_id and tag.post_template_slug == ^template.slug), :count, :id)
end
defp clear_template_references(template) do
now = System.system_time(:second)
affected_posts =
Repo.all(
from(post in BDS.Posts.Post,
where: post.project_id == ^template.project_id and post.template_slug == ^template.slug
)
)
from(post in BDS.Posts.Post, where: post.project_id == ^template.project_id and post.template_slug == ^template.slug)
|> Repo.update_all(set: [template_slug: nil, updated_at: now])
from(tag in BDS.Tags.Tag, where: tag.project_id == ^template.project_id and tag.post_template_slug == ^template.slug)
|> Repo.update_all(set: [post_template_slug: nil, updated_at: now])
Enum.each(affected_posts, fn post ->
BDS.Posts.rewrite_published_post(post.id)
end)
BDS.Tags.sync_tags_json(template.project_id)
:ok
end
defp delete_file_if_present(_project_id, file_path) when file_path in [nil, ""], do: :ok
defp delete_file_if_present(project_id, file_path) do
full_path = full_file_path(project_id, file_path)
case File.rm(full_path) do
:ok -> :ok
{:error, :enoent} -> :ok
{:error, reason} -> {:error, reason}
end
end
defp maybe_put(map, _key, nil), do: map
defp maybe_put(map, key, value), do: Map.put(map, key, value)
defp has_attr?(attrs, key) do
Map.has_key?(attrs, key) or Map.has_key?(attrs, Atom.to_string(key))
end
defp attr(attrs, key) do