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)
35 with nil <- options[:static_dir] do
36 Pleroma.Config.get!([:instance, :static_dir])
39 cmd_frontend_info = %{
41 "ref" => options[:ref],
42 "build_url" => options[:build_url]
45 config_frontend_info = Pleroma.Config.get([:frontends, :available, frontend], %{})
48 Map.merge(config_frontend_info, cmd_frontend_info, fn _key, config, cmd ->
49 # This only overrides things that are actually set
53 ref = frontend_info["ref"]
56 raise "No ref given or configured"
67 fe_label = "#{frontend} (#{ref})"
69 shell_info("Downloading pre-built bundle for #{fe_label}")
70 tmp_dir = Path.join(dest, "tmp")
72 with {_, :ok} <- {:download, download_build(frontend_info, tmp_dir)},
73 shell_info("Installing #{fe_label} to #{dest}"),
74 :ok <- install_frontend(frontend_info, tmp_dir, dest) do
76 shell_info("Frontend #{fe_label} installed to #{dest}")
78 Logger.configure(level: log_level)
81 shell_info("Could not download the frontend")
84 shell_info("Could not install the frontend")
88 defp download_build(frontend_info, dest) do
89 url = String.replace(frontend_info["build_url"], "${ref}", frontend_info["ref"])
91 with {:ok, %{status: 200, body: zip_body}} <-
92 Pleroma.HTTP.get(url, [], timeout: 120_000, recv_timeout: 120_000),
93 {:ok, unzipped} <- :zip.unzip(zip_body, [:memory]) do
97 Enum.each(unzipped, fn {filename, data} ->
100 new_file_path = Path.join(dest, path)
106 File.write!(new_file_path, data)
115 defp install_frontend(frontend_info, source, dest) do
116 from = frontend_info["build_dir"] || "dist"
118 File.cp_r!(Path.join([source, from]), dest)