~mika/www.m1kadev.nl

ref: 4651d7b67838db738cd487552f89f8b77dfdca56 www.m1kadev.nl/build.exs -rw-r--r-- 2.2 KiB
4651d7b6 — m1kadev update build script; change colours 7 months 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
defmodule Builders do
  require EEx

  binfo_template = """
  commit=<%= commit %>
  lightningcss=<%= lightningcss %>
  html_minifer_next=<%= html_minifier_next %>
  uglifyjs=<%= uglifyjs %>
  build_time=<%= build_time %>
  """

  def html(input_path) do
    output_path = String.replace_prefix(input_path, "src", "build")

    System.cmd("html-minifier-next", [
      "--preset",
      "comprehensive",
      "-o",
      output_path,
      input_path
    ])
  end

  def css(input_path) do
    output_path = String.replace_prefix(input_path, "src", "build")

    System.cmd("lightningcss", [
      "-m",
      "-o",
      output_path,
      input_path
    ])
  end

  def js(input_path) do
    output_path = String.replace_prefix(input_path, "src", "build")

    System.cmd("uglifyjs", [
      "-c",
      "-o",
      output_path,
      input_path
    ])
  end

  # TODO: js support :)

  EEx.function_from_string(
    :def,
    :build_info,
    binfo_template,
    [
      :commit,
      :lightningcss,
      :html_minifier_next,
      :uglifyjs,
      :build_time
    ]
  )
end

commit =
  System.cmd("git", ["rev-parse", "HEAD"])
  |> elem(0)
  |> String.trim()

lightningcss =
  System.cmd("lightningcss", ["-V"])
  |> elem(0)
  |> String.trim()

html_minifier_next =
  System.cmd("html-minifier-next", ["-V"])
  |> elem(0)
  |> String.trim()

uglifyjs =
  System.cmd("uglifyjs", ["-V"])
  |> elem(0)
  |> String.trim()

File.rm_rf("build")

case File.mkdir_p("build/static") do
  :ok -> {}
  _ -> raise "Couldn't create the build file structure (/build/static)"
end

Path.wildcard("src/**/*.html")
|> Task.async_stream(fn file -> Builders.html(file) end)
|> Enum.to_list()

Path.wildcard("src/**/*.css")
|> Task.async_stream(fn file -> Builders.css(file) end)
|> Enum.to_list()

Path.wildcard("src/**/*.js")
|> Task.async_stream(fn file -> Builders.js(file) end)
|> Enum.to_list()

Path.wildcard("static/**/*")
|> Task.async_stream(fn file -> File.cp(file, "build/" <> file) end)
|> Enum.to_list()

build_time =
  DateTime.utc_now()
  |> Calendar.strftime("%H:%M:%S %d/%m/%y")

File.write(
  "build/info.txt",
  Builders.build_info(commit, lightningcss, html_minifier_next, uglifyjs, build_time)
)