2adbf8d72c9835cfbc01cf76aaabc306952742cc
[akkoma] / lib / mix / tasks / pleroma / frontend.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.Frontend do
6 use Mix.Task
7
8 import Mix.Pleroma
9
10 @shortdoc "Manages bundled Pleroma frontends"
11
12 @moduledoc File.read!("docs/administration/CLI_tasks/frontend.md")
13
14 def run(["install", "none" | _args]) do
15 shell_info("Skipping frontend installation because none was requested")
16 "none"
17 end
18
19 def run(["install", frontend | args]) do
20 log_level = Logger.level()
21 Logger.configure(level: :warn)
22 start_pleroma()
23
24 {options, [], []} =
25 OptionParser.parse(
26 args,
27 strict: [
28 ref: :string,
29 static_dir: :string,
30 build_url: :string,
31 build_dir: :string,
32 file: :string
33 ]
34 )
35
36 instance_static_dir =
37 with nil <- options[:static_dir] do
38 Pleroma.Config.get!([:instance, :static_dir])
39 end
40
41 cmd_frontend_info = %{
42 "name" => frontend,
43 "ref" => options[:ref],
44 "build_url" => options[:build_url],
45 "build_dir" => options[:build_dir]
46 }
47
48 config_frontend_info = Pleroma.Config.get([:frontends, :available, frontend], %{})
49
50 frontend_info =
51 Map.merge(config_frontend_info, cmd_frontend_info, fn _key, config, cmd ->
52 # This only overrides things that are actually set
53 cmd || config
54 end)
55
56 ref = frontend_info["ref"]
57
58 unless ref do
59 raise "No ref given or configured"
60 end
61
62 dest =
63 Path.join([
64 instance_static_dir,
65 "frontends",
66 frontend,
67 ref
68 ])
69
70 fe_label = "#{frontend} (#{ref})"
71
72 tmp_dir = Path.join(dest, "tmp")
73
74 with {_, :ok} <-
75 {:download_or_unzip, download_or_unzip(frontend_info, tmp_dir, options[:file])},
76 shell_info("Installing #{fe_label} to #{dest}"),
77 :ok <- install_frontend(frontend_info, tmp_dir, dest) do
78 File.rm_rf!(tmp_dir)
79 shell_info("Frontend #{fe_label} installed to #{dest}")
80
81 Logger.configure(level: log_level)
82 else
83 {:download_or_unzip, _} ->
84 shell_info("Could not download or unzip the frontend")
85
86 _e ->
87 shell_info("Could not install the frontend")
88 end
89 end
90
91 defp download_or_unzip(frontend_info, temp_dir, file) do
92 if file do
93 with {:ok, zip} <- File.read(Path.expand(file)) do
94 unzip(zip, temp_dir)
95 end
96 else
97 download_build(frontend_info, temp_dir)
98 end
99 end
100
101 def unzip(zip, dest) do
102 with {:ok, unzipped} <- :zip.unzip(zip, [:memory]) do
103 File.rm_rf!(dest)
104 File.mkdir_p!(dest)
105
106 Enum.each(unzipped, fn {filename, data} ->
107 path = filename
108
109 new_file_path = Path.join(dest, path)
110
111 new_file_path
112 |> Path.dirname()
113 |> File.mkdir_p!()
114
115 File.write!(new_file_path, data)
116 end)
117
118 :ok
119 end
120 end
121
122 defp download_build(frontend_info, dest) do
123 shell_info("Downloading pre-built bundle for #{frontend_info["name"]}")
124 url = String.replace(frontend_info["build_url"], "${ref}", frontend_info["ref"])
125
126 with {:ok, %{status: 200, body: zip_body}} <-
127 Pleroma.HTTP.get(url, [], timeout: 120_000, recv_timeout: 120_000) do
128 unzip(zip_body, dest)
129 else
130 e -> {:error, e}
131 end
132 end
133
134 defp install_frontend(frontend_info, source, dest) do
135 from = frontend_info["build_dir"] || "dist"
136 File.mkdir_p!(dest)
137 File.cp_r!(Path.join([source, from]), dest)
138 :ok
139 end
140 end