require Mix
require EEx
Mix.install([
{:tzdata, "~> 1.1"}
])
Calendar.put_time_zone_database(Tzdata.TimeZoneDatabase)
defmodule Templates do
binfo_template = """
commit=<%= commit %>lightningcss=<%= lightningcss %>html_minifier_next=<%= html_minifier_next %>uglifyjs=<%= uglifyjs %>build_time=<%= build_time %>
"""
pastelink_template = """
<%= name %> | <%= date %>
"""
EEx.function_from_string(
:def,
:build_info,
binfo_template,
[
:commit,
:lightningcss,
:html_minifier_next,
:uglifyjs,
:build_time
]
)
EEx.function_from_string(
:def,
:pastelink,
pastelink_template,
[
:location,
:name,
:date
]
)
end
defmodule Builders do
def html(input_path) do
output_path = String.replace_prefix(input_path, "src", "build")
{_, 0} =
System.cmd("html-minifier-next", [
"--preset",
"conservative",
"-o",
output_path,
input_path
])
end
def css(input_path) do
output_path = String.replace_prefix(input_path, "src", "build")
{_, 0} =
System.cmd("lightningcss", [
"-m",
"-o",
output_path,
input_path
])
end
def js(input_path) do
output_path = String.replace_prefix(input_path, "src", "build")
{_, 0} =
System.cmd("uglifyjs", [
"-c",
"-o",
output_path,
input_path
])
end
def fxg(input_path, template) do
own_template = input_path |> String.replace_suffix("fxg", "html")
case File.read(own_template) do
{:ok, ctemplate} -> _fxg(input_path, ctemplate)
_ -> _fxg(input_path, template)
end
end
def paste(input_path, template) do
{:ok, rcode} = File.read(input_path)
output = "build/" <> input_path <> ".html"
code =
rcode
|> String.replace("&", "&")
|> String.replace("<", "<")
|> String.replace(">", ">")
lang = Path.extname(input_path) |> String.slice(1..-1//1)
file = Path.basename(input_path)
{:ok, %File.Stat{mtime: mtime}} = File.stat(input_path, time: :posix)
result =
template
|> String.replace("$LANG", lang)
|> String.replace("$CODE", code)
|> String.replace("$FILENAME", input_path)
|> String.replace("$FILEMETA", "Created on " <> Integer.to_string(mtime))
:ok = File.write(output, result)
end
defp _fxg(input_path, template) do
output_path =
String.replace_prefix(input_path, "src", "build")
|> String.replace_suffix("fxg", "html")
{output, 0} = System.cmd("fxg", [input_path])
output = String.replace(template, "
")
pastes_header = """
random code snippets i found useful or wanted to save :))
" <> paste_data <> "
") File.write("build/pastes/index.html", paste_index) {:ok, date} = DateTime.now("Europe/Amsterdam") build_time = Calendar.strftime(date, "%H:%M:%S %d/%m/%y") File.write( "build/info.txt", Templates.build_info( commit, lightningcss |> String.split_at(13) |> elem(1), html_minifier_next, uglifyjs |> String.split_at(10) |> elem(1), build_time ) )