diff options
author | Roman Chvanikov <chvanikoff@pm.me> | 2020-07-28 17:49:30 +0300 |
---|---|---|
committer | Roman Chvanikov <chvanikoff@pm.me> | 2020-07-28 17:49:30 +0300 |
commit | cc7b7296a2d4aa0f57616f6befdbb2bed58a7e5a (patch) | |
tree | bc1452209c4f338fd2e1bf076daa1c1776e62d94 /lib | |
parent | 90b237c2736a891bf6c6491da7e444d5f6cbe585 (diff) | |
download | pleroma-refactor/fe-bundles.tar.gz |
Try to download pre-built bundles and fallback to building from sourcerefactor/fe-bundles
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mix/tasks/pleroma/frontend.ex | 97 | ||||
-rw-r--r-- | lib/mix/tasks/pleroma/instance.ex | 37 |
2 files changed, 84 insertions, 50 deletions
diff --git a/lib/mix/tasks/pleroma/frontend.ex b/lib/mix/tasks/pleroma/frontend.ex index bc048014e..b3b3caa9b 100644 --- a/lib/mix/tasks/pleroma/frontend.ex +++ b/lib/mix/tasks/pleroma/frontend.ex @@ -30,24 +30,24 @@ defmodule Mix.Tasks.Pleroma.Frontend do "none" end - def run(["install", "all"]) do + def run(["install", "all" | options]) do start_pleroma() configs = Pleroma.Config.get(:frontends, %{}) with config when not is_nil(config) <- configs[:primary], ref when ref != "none" <- config["ref"] do - run(["install", config["name"], "--ref", ref]) + run(["install", config["name"], "--ref", ref | options]) end with config when not is_nil(config) <- configs[:mastodon], ref when ref != "none" <- config["ref"] do - run(["install", "mastodon", "--ref", ref]) + run(["install", "mastodon", "--ref", ref | options]) end with config when not is_nil(config) <- configs[:admin], ref when ref != "none" <- config["ref"] do - run(["install", "admin", "--ref", ref]) + run(["install", "admin", "--ref", ref | options]) end end @@ -106,32 +106,67 @@ defmodule Mix.Tasks.Pleroma.Frontend do fe_label = "#{frontend} (#{ref})" - from = + {from, proceed?} = with nil <- options[:path] do - Pleroma.Utils.command_required!("yarn") + tmp_dir = Path.join(dest, "tmp/src") - url = archive_url(frontend, ref) + shell_info("Downloading pre-built bundle for #{fe_label}") - tmp_dir = Path.join(dest, "tmp/src") + proceed? = + with {:error, error} <- download_frontend(frontend, ref, tmp_dir, :build) do + shell_info("Could not download pre-built bundle: #{inspect(error)}.") + shell_info("Falling back to building locally from source") + + download_and_build = fn callback -> + case Pleroma.Utils.command_available?("yarn") do + false -> + message = + "To build frontend #{fe_label} from sources, `yarn` command is required. Please install it before continue. ([C]ontinue/[A]bort)" + + case String.downcase(shell_prompt(message, "C")) do + abort when abort in ["a", "abort"] -> + false + + _continue -> + callback.(callback) + end + + _ -> + shell_info("Downloading #{fe_label} sources to #{tmp_dir}") + :ok = download_frontend(frontend, ref, tmp_dir, :source) + + shell_info("Building #{fe_label} (this will take some time)") + :ok = build_frontend(frontend, tmp_dir) + end + end - shell_info("Downloading #{fe_label} to #{tmp_dir}") - :ok = download_frontend(url, tmp_dir) + download_and_build.(download_and_build) + else + _ -> + true + end - shell_info("Building #{fe_label} (this will take some time)") - :ok = build_frontend(frontend, tmp_dir) - tmp_dir + {tmp_dir, proceed?} + else + path -> + {path, true} end - shell_info("Installing #{fe_label} to #{dest}") + if proceed? do + shell_info("Installing #{fe_label} to #{dest}") + + :ok = install_frontend(frontend, from, dest) - :ok = install_frontend(frontend, from, dest) + shell_info("Frontend #{fe_label} installed to #{dest}") + end - shell_info("Frontend #{fe_label} installed to #{dest}") Logger.configure(level: log_level) ref end - defp download_frontend(url, dest) do + defp download_frontend(frontend, ref, dest, kind) do + url = frontend_url(frontend, ref, kind) + with {:ok, %{status: 200, body: zip_body}} <- Pleroma.HTTP.get(url, [], timeout: 120_000, recv_timeout: 120_000), {:ok, unzipped} <- :zip.unzip(zip_body, [:memory]) do @@ -139,8 +174,18 @@ defmodule Mix.Tasks.Pleroma.Frontend do File.mkdir_p!(dest) Enum.each(unzipped, fn {filename, data} -> - [_root | paths] = Path.split(filename) - path = Enum.join(paths, "/") + path = + case kind do + :source -> + filename + |> Path.split() + |> Enum.drop(1) + |> Enum.join("/") + + :build -> + filename + end + new_file_path = Path.join(dest, path) new_file_path @@ -151,10 +196,7 @@ defmodule Mix.Tasks.Pleroma.Frontend do end) else {:ok, %{status: 404}} -> - {:error, "Bundle not found"} - - false -> - {:error, "Zip archive must contain \"dist\" folder"} + {:error, "Zip archive with frontend #{kind} not found at #{url}"} error -> {:error, error} @@ -208,9 +250,16 @@ defmodule Mix.Tasks.Pleroma.Frontend do URI.encode_www_form(@frontends[frontend]["project"]) }" - defp archive_url(frontend, ref), + defp source_url(frontend, ref), do: "https://#{@pleroma_gitlab_host}/#{@frontends[frontend]["project"]}/-/archive/#{ref}.zip" + defp build_url(frontend, ref), + do: + "https://#{@pleroma_gitlab_host}/#{@frontends[frontend]["project"]}/-/jobs/artifacts/#{ref}/download?job=build" + + defp frontend_url(frontend, ref, :source), do: source_url(frontend, ref) + defp frontend_url(frontend, ref, :build), do: build_url(frontend, ref) + defp local_path_frontend_ref(path) do path |> Path.join("package.json") diff --git a/lib/mix/tasks/pleroma/instance.ex b/lib/mix/tasks/pleroma/instance.ex index f57053a4c..0355b8881 100644 --- a/lib/mix/tasks/pleroma/instance.ex +++ b/lib/mix/tasks/pleroma/instance.ex @@ -165,31 +165,16 @@ defmodule Mix.Tasks.Pleroma.Instance do install_fe = case Mix.env() do :test -> - fn _, _ -> "42" end + fn _ -> "42" end _ -> - fn frontend, callback -> - case Pleroma.Utils.command_available?("yarn") do - false when frontend != "none" -> - message = - "To install #{frontend} frontend, `yarn` command is required. Please install it before continue. ([C]ontinue/[A]bort)" - - case String.downcase(shell_prompt(message, "C")) do - abort when abort in ["a", "abort"] -> - "none" - - _continue -> - callback.(frontend, callback) - end - - _ -> - Mix.Tasks.Pleroma.Frontend.run([ - "install", - frontend, - "--static-dir", - static_dir - ]) - end + fn frontend -> + Mix.Tasks.Pleroma.Frontend.run([ + "install", + frontend, + "--static-dir", + static_dir + ]) end end @@ -201,7 +186,7 @@ defmodule Mix.Tasks.Pleroma.Instance do "pleroma" ) - fe_primary_ref = install_fe.(fe_primary, install_fe) + fe_primary_ref = install_fe.(fe_primary) enable_static_fe? = get_option( @@ -222,7 +207,7 @@ defmodule Mix.Tasks.Pleroma.Instance do fe_mastodon_ref = case install_mastodon_fe? do true -> - install_fe.("mastodon", install_fe) + install_fe.("mastodon") false -> "none" @@ -238,7 +223,7 @@ defmodule Mix.Tasks.Pleroma.Instance do fe_admin_ref = case install_admin_fe? do - true -> install_fe.("admin", install_fe) + true -> install_fe.("admin") false -> "none" end |