~mika/www.m1kadev.nl

ref: 7f2b3874a542bb3f395709779309224470cc230b www.m1kadev.nl/kethel/lib/statics.ex -rw-r--r-- 1.1 KiB
7f2b3874 — m1ka new thought: cargo a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
defmodule Statics do
  @moduledoc """
    Statics is an exception, because we only copy the files. Therefore, the performance gain from reading files into memory for processing isn't worth it. The compile fuction gets called at load-time for all other asset types.
  """

  @spec ensure_file_structure(binary()) :: :ok
  defp ensure_file_structure(project_root) do
    File.mkdir_p!("#{project_root}/build/static/")
  end

  defp trim_abs_path(path, project_root) do
    # ROOT + static
    begin = byte_size(project_root) + byte_size("static/")
    # remove extension
    binary_part(path, begin, byte_size(path) - begin)
  end

  # we dont async_stream here because copying is (relatively speaking) inexpensive, and we want to keep more threads open for other (more expensive) compilation processes
  def compile(project_root) do
    ensure_file_structure(project_root)

    for path <- Path.wildcard("#{project_root}/static/**/*") do
      rel_path = trim_abs_path(path, project_root)
      output_path = "#{project_root}/build/static#{rel_path}"

      File.copy!(path, output_path)

      IO.puts("[ COPIED ] #{path} -> #{output_path}")
    end
  end
end