Frontend mix task: Support installation from local file.

This commit is contained in:
lain 2020-07-31 17:58:50 +02:00
parent 99bfdffb1d
commit 7e01339ddd

View file

@ -27,7 +27,9 @@ def run(["install", frontend | args]) do
strict: [ strict: [
ref: :string, ref: :string,
static_dir: :string, static_dir: :string,
build_url: :string build_url: :string,
build_dir: :string,
file: :string
] ]
) )
@ -39,7 +41,8 @@ def run(["install", frontend | args]) do
cmd_frontend_info = %{ cmd_frontend_info = %{
"name" => frontend, "name" => frontend,
"ref" => options[:ref], "ref" => options[:ref],
"build_url" => options[:build_url] "build_url" => options[:build_url],
"build_dir" => options[:build_dir]
} }
config_frontend_info = Pleroma.Config.get([:frontends, :available, frontend], %{}) config_frontend_info = Pleroma.Config.get([:frontends, :available, frontend], %{})
@ -66,10 +69,10 @@ def run(["install", frontend | args]) do
fe_label = "#{frontend} (#{ref})" fe_label = "#{frontend} (#{ref})"
shell_info("Downloading pre-built bundle for #{fe_label}")
tmp_dir = Path.join(dest, "tmp") tmp_dir = Path.join(dest, "tmp")
with {_, :ok} <- {:download, download_build(frontend_info, tmp_dir)}, with {_, :ok} <-
{:download_or_unzip, download_or_unzip(frontend_info, tmp_dir, options[:file])},
shell_info("Installing #{fe_label} to #{dest}"), shell_info("Installing #{fe_label} to #{dest}"),
:ok <- install_frontend(frontend_info, tmp_dir, dest) do :ok <- install_frontend(frontend_info, tmp_dir, dest) do
File.rm_rf!(tmp_dir) File.rm_rf!(tmp_dir)
@ -77,20 +80,26 @@ def run(["install", frontend | args]) do
Logger.configure(level: log_level) Logger.configure(level: log_level)
else else
{:download, _} -> {:download_or_unzip, _} ->
shell_info("Could not download the frontend") shell_info("Could not download or unzip the frontend")
_e -> _e ->
shell_info("Could not install the frontend") shell_info("Could not install the frontend")
end end
end end
defp download_build(frontend_info, dest) do defp download_or_unzip(frontend_info, temp_dir, file) do
url = String.replace(frontend_info["build_url"], "${ref}", frontend_info["ref"]) if file do
with {:ok, zip} <- File.read(Path.expand(file)) do
unzip(zip, temp_dir)
end
else
download_build(frontend_info, temp_dir)
end
end
with {:ok, %{status: 200, body: zip_body}} <- def unzip(zip, dest) do
Pleroma.HTTP.get(url, [], timeout: 120_000, recv_timeout: 120_000), with {:ok, unzipped} <- :zip.unzip(zip, [:memory]) do
{:ok, unzipped} <- :zip.unzip(zip_body, [:memory]) do
File.rm_rf!(dest) File.rm_rf!(dest)
File.mkdir_p!(dest) File.mkdir_p!(dest)
@ -107,6 +116,16 @@ defp download_build(frontend_info, dest) do
end) end)
:ok :ok
end
end
defp download_build(frontend_info, dest) do
shell_info("Downloading pre-built bundle for #{frontend_info["name"]}")
url = String.replace(frontend_info["build_url"], "${ref}", frontend_info["ref"])
with {:ok, %{status: 200, body: zip_body}} <-
Pleroma.HTTP.get(url, [], timeout: 120_000, recv_timeout: 120_000) do
unzip(zip_body, dest)
else else
e -> {:error, e} e -> {:error, e}
end end