1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Frontend do
10 @shortdoc "Manages bundled Pleroma frontends"
12 @moduledoc File.read!("docs/administration/CLI_tasks/frontend.md")
14 def run(["install", "none" | _args]) do
15 shell_info("Skipping frontend installation because none was requested")
19 def run(["install", frontend | args]) do
20 log_level = Logger.level()
21 Logger.configure(level: :warn)
37 with nil <- options[:static_dir] do
38 Pleroma.Config.get!([:instance, :static_dir])
41 cmd_frontend_info = %{
43 "ref" => options[:ref],
44 "build_url" => options[:build_url],
45 "build_dir" => options[:build_dir]
48 config_frontend_info = Pleroma.Config.get([:frontends, :available, frontend], %{})
51 Map.merge(config_frontend_info, cmd_frontend_info, fn _key, config, cmd ->
52 # This only overrides things that are actually set
56 ref = frontend_info["ref"]
59 raise "No ref given or configured"
70 fe_label = "#{frontend} (#{ref})"
72 tmp_dir = Path.join([instance_static_dir, "frontends", "tmp"])
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
79 shell_info("Frontend #{fe_label} installed to #{dest}")
81 Logger.configure(level: log_level)
83 {:download_or_unzip, _} ->
84 shell_info("Could not download or unzip the frontend")
87 shell_info("Could not install the frontend")
91 defp download_or_unzip(frontend_info, temp_dir, file) do
93 with {:ok, zip} <- File.read(Path.expand(file)) do
97 download_build(frontend_info, temp_dir)
101 def unzip(zip, dest) do
102 with {:ok, unzipped} <- :zip.unzip(zip, [:memory]) do
106 Enum.each(unzipped, fn {filename, data} ->
109 new_file_path = Path.join(dest, path)
115 File.write!(new_file_path, data)
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"])
126 with {:ok, %{status: 200, body: zip_body}} <-
127 Pleroma.HTTP.get(url, [],
128 adapter: [pool: :media, timeout: 120_000, recv_timeout: 120_000]
130 unzip(zip_body, dest)
136 defp install_frontend(frontend_info, source, dest) do
137 from = frontend_info["build_dir"] || "dist"
140 File.cp_r!(Path.join([source, from]), dest)