Skip to content

Commit

Permalink
refactor a bit, less obscure
Browse files Browse the repository at this point in the history
  • Loading branch information
pdgonzalez872 committed Nov 1, 2024
1 parent c290651 commit 2462eb0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
Binary file removed docs/images/favicon.ico
Binary file not shown.
45 changes: 19 additions & 26 deletions lib/mix/tasks/build_static.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,43 @@ defmodule Mix.Tasks.BuildStatic do
@shortdoc "Builds the static content"

def run(_args) do
IO.puts("Booting app")
Mix.Task.run("app.start")
IO.puts("Building static site...")

IO.puts("Start building /docs static")

IO.puts("Create directory")
build_dir = "build"
File.rm_rf!(build_dir)
File.mkdir_p!(build_dir)

IO.puts("Create static pages")
generate_page("/", "index.html", build_dir)
generate_page("/resume_technical", "resume_technical.html", build_dir)
generate_posts(build_dir)
copy_assets(build_dir)

IO.puts("Static site generated in #{build_dir}/")
end

defp generate_page(path, filename, build_dir) do
conn = build_conn(path)
content = conn.resp_body
File.write!(Path.join(build_dir, filename), content)
IO.puts("Generated #{filename}")
end

defp generate_posts(build_dir) do
posts = PgBlog.Blog.all_posts()

Enum.each(posts, fn post ->
IO.puts("Create posts - add them in priv/posts/")
PgBlog.Blog.all_posts()
|> Enum.each(fn post ->
path = "/posts/#{post.id}"
filename = "#{post.id}.html"

generate_page(path, filename, build_dir)
end)

IO.puts("Copy assets")
File.cp_r!("priv/static/.", build_dir)

IO.puts("Finished building /docs")
end

defp build_conn(path) do
defp generate_page(path, filename, build_dir) do
conn = Plug.Test.conn(:get, path)
PgBlogWeb.Endpoint.call(conn, [])
end
%{resp_body: body} = PgBlogWeb.Endpoint.call(conn, [])

defp copy_assets(build_dir) do
# Instead of all assets, maybe just copy what we need, which isn't a lot.
# TODO: HERE
File.cp_r!("priv/static/.", build_dir)
build_dir
|> Path.join(filename)
|> File.write!(body)

File.cp("priv/static/favicon.ico", Path.join([build_dir, "images", "favicon.ico"]))
|> IO.inspect(label: "cp")
IO.puts("Created #{filename}")
end
end

0 comments on commit 2462eb0

Please sign in to comment.