diff options
author | Roman Chvanikov <chvanikoff@pm.me> | 2020-07-17 23:29:26 +0300 |
---|---|---|
committer | Roman Chvanikov <chvanikoff@pm.me> | 2020-07-17 23:29:26 +0300 |
commit | 6ab7a40f92b1a715a45545db08bb5faccab3c974 (patch) | |
tree | 7a3f25460ca53f847f586ac16b7d7524b79ed9e9 | |
parent | 9f92ccee60a08da3f6ebaf81250da1459bc0c24f (diff) | |
download | pleroma-6ab7a40f92b1a715a45545db08bb5faccab3c974.tar.gz |
refactor frontend task
-rw-r--r-- | lib/mix/tasks/pleroma/frontend.ex | 223 | ||||
-rw-r--r-- | test/fixtures/frontends/admin/README.md | 1 | ||||
-rw-r--r-- | test/fixtures/frontends/admin/dist/index.html | 2 | ||||
-rw-r--r-- | test/fixtures/frontends/admin/package.json | 1 | ||||
-rw-r--r-- | test/fixtures/frontends/kenoma/README.md | 1 | ||||
-rw-r--r-- | test/fixtures/frontends/kenoma/build/index.html | 2 | ||||
-rw-r--r-- | test/fixtures/frontends/kenoma/package.json | 1 | ||||
-rw-r--r-- | test/fixtures/frontends/mastodon/README.md | 1 | ||||
-rw-r--r-- | test/fixtures/frontends/mastodon/package.json | 1 | ||||
-rw-r--r-- | test/fixtures/frontends/mastodon/public/assets/sw.js | 0 | ||||
-rw-r--r-- | test/fixtures/frontends/mastodon/public/packs/locales.js | 0 | ||||
-rw-r--r-- | test/fixtures/frontends/mastodon/public/unused_dir/unused_file2 | 0 | ||||
-rw-r--r-- | test/fixtures/frontends/mastodon/public/unused_file | 0 | ||||
-rw-r--r-- | test/fixtures/frontends/pleroma/README.md | 1 | ||||
-rw-r--r-- | test/fixtures/frontends/pleroma/dist/index.html | 2 | ||||
-rw-r--r-- | test/fixtures/frontends/pleroma/package.json | 1 | ||||
-rw-r--r-- | test/tasks/frontend_test.exs | 36 |
17 files changed, 108 insertions, 165 deletions
diff --git a/lib/mix/tasks/pleroma/frontend.ex b/lib/mix/tasks/pleroma/frontend.ex index 658b981b2..c7de5d217 100644 --- a/lib/mix/tasks/pleroma/frontend.ex +++ b/lib/mix/tasks/pleroma/frontend.ex @@ -3,6 +3,32 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Mix.Tasks.Pleroma.Frontend do + @doc """ + Scenario 1: + - clone repo to /frontends/fe/_src_tmp + - build fe + - move built files into /frontends/fe + - remove frontends/fe/_src_tmp + + Scenario 2: + - download bundle from CI to /frontends/fe/_src_tmp + - move build files + - remove tmp + + Scenario 3: + - move built files from _path to /frontends/fe + + Pleroma: + /dist + Kenoma: + /build + Fedi: + /dist + Admin: + /dist + Mastodon + /public + """ use Mix.Task import Mix.Pleroma @@ -12,17 +38,15 @@ defmodule Mix.Tasks.Pleroma.Frontend do @shortdoc "Manages bundled Pleroma frontends" @moduledoc File.read!("docs/administration/CLI_tasks/frontend.md") - @pleroma_gitlab_host "git.pleroma.social" @frontends %{ "admin" => %{"project" => "pleroma/admin-fe"}, "kenoma" => %{"project" => "lambadalambda/kenoma"}, "mastodon" => %{"project" => "pleroma/mastofe"}, - "pleroma" => %{"project" => "pleroma/pleroma-fe"} + "pleroma" => %{"project" => "pleroma/pleroma-fe"}, + "fedi" => %{"project" => "dockyard/fedi-fe"} } @known_frontends Map.keys(@frontends) - @ref_stable "__stable__" - @ref_develop "__develop__" @ref_local "__local__" def run(["install", "none" | _args]) do @@ -52,175 +76,74 @@ defmodule Mix.Tasks.Pleroma.Frontend do ] ) - ref = - cond do - options[:ref] -> - options[:ref] - - options[:develop] -> - @ref_develop - - options[:path] -> - @ref_local - - true -> - @ref_stable - end - - %{"name" => bundle_name, "url" => bundle_url} = - case options[:path] do - nil -> - get_bundle_meta(ref, @frontends[frontend]["project"]) - - path -> - version = - path - |> File.read!() - |> Jason.decode!() - |> Map.get("version") - - %{"name" => version, "url" => {:local, path}} - end - - shell_info("Installing frontend #{frontend}, version: #{bundle_name}") + path = options[:path] + ref = local_path_frontend_ref(path) dest = Path.join([ Pleroma.Config.get!([:instance, :static_dir]), "frontends", frontend, - bundle_name + ref ]) - with :ok <- download_bundle(bundle_url, dest), - :ok <- install_bundle(frontend, dest) do - shell_info("Installed!") - else - {:error, error} -> - shell_error("Error: #{inspect(error)}") - end - - Logger.configure(level: log_level) - end - - defp get_bundle_meta(@ref_develop, project) do - url = "#{gitlab_api_url(project)}/repository/branches" - - %{status: 200, body: json} = Tesla.get!(http_client(), url) - - %{"name" => name, "commit" => %{"short_id" => last_commit_ref}} = - Enum.find(json, & &1["default"]) - - %{ - "name" => name, - "url" => build_url(project, last_commit_ref) - } - end + shell_info("Installing frontend #{frontend} (#{ref}) from local path") - defp get_bundle_meta(@ref_stable, project) do - url = "#{gitlab_api_url(project)}/releases" - %{status: 200, body: json} = Tesla.get!(http_client(), url) - - [%{"commit" => %{"short_id" => commit_id}, "name" => name} | _] = - Enum.sort(json, fn r1, r2 -> - {:ok, date1, _offset} = DateTime.from_iso8601(r1["created_at"]) - {:ok, date2, _offset} = DateTime.from_iso8601(r2["created_at"]) - DateTime.compare(date1, date2) != :lt - end) - - %{ - "name" => name, - "url" => build_url(project, commit_id) - } - end + install_bundle(frontend, path, dest) + shell_info("Frontend #{frontend} (#{ref}) installed to #{dest}") - defp get_bundle_meta(ref, project) do - %{ - "name" => ref, - "url" => build_url(project, ref) - } + Logger.configure(level: log_level) end - defp download_bundle({:local, _path}, _dir), do: :ok - - defp download_bundle(bundle_url, dir) do - http_client = http_client() - - with {:ok, %{status: 200, body: zip_body}} <- Tesla.get(http_client, bundle_url), - {:ok, unzipped} <- :zip.unzip(zip_body, [:memory]), - filtered = - Enum.filter(unzipped, fn - {[?d, ?i, ?s, ?t, ?/ | _rest], _data} -> true - _ -> false - end), - true <- length(filtered) > 0 do - File.rm_rf!(dir) - - Enum.each(unzipped, fn {[?d, ?i, ?s, ?t, ?/ | path], data} -> - file_path = Path.join(dir, path) - - file_path - |> Path.dirname() - |> File.mkdir_p!() - - File.write!(file_path, data) - end) - else - {:ok, %{status: 404}} -> - {:error, "Bundle not found"} - - false -> - {:error, "Zip archive must contain \"dist\" folder"} - - error -> - {:error, error} + defp local_path_frontend_ref(path) do + path + |> Path.join("package.json") + |> File.read() + |> case do + {:ok, bin} -> + bin + |> Jason.decode!() + |> Map.get("version", @ref_local) + + _ -> + @ref_local end end - defp install_bundle("mastodon", base_path) do - File.ls!(base_path) |> IO.inspect() - required_paths = ["public/assets/sw.js", "public/packs"] - - with false <- Enum.all?(required_paths, &([base_path, &1] |> Path.join() |> File.exists?())) do - build_bundle!("mastodon", base_path) - end - - with :ok <- File.rename("#{base_path}/public/assets/sw.js", "#{base_path}/sw.js"), - :ok <- File.rename("#{base_path}/public/packs", "#{base_path}/packs"), - {:ok, _deleted_files} <- File.rm_rf("#{base_path}/public") do - :ok - else - error -> - {:error, error} - end - end + defp post_install("mastodon", path) do + File.rename!("#{path}/assets/sw.js", "#{path}/sw.js") - defp install_bundle(_fe_name, _base_path), do: :ok + {:ok, files} = File.ls(path) - defp build_bundle!("mastodon", base_path) do - Pleroma.Utils.command_required!("yarn") - {_out, 0} = System.cmd("yarn", ["install"], cd: base_path) - {_out, 0} = System.cmd("yarn", ["run", "build"], cd: base_path) + Enum.each(files, fn file -> + with false <- file in ~w(packs sw.js) do + [path, file] + |> Path.join() + |> File.rm_rf!() + end + end) end - defp build_bundle!(_frontend, base_path) do - Pleroma.Utils.command_required!("yarn") - {_out, 0} = System.cmd("yarn", [], cd: base_path) - {_out, 0} = System.cmd("npm", ["run", "build"], cd: base_path) + defp post_install(_frontend, _path) do + :ok end - defp gitlab_api_url(project), - do: "https://#{@pleroma_gitlab_host}/api/v4/projects/#{URI.encode_www_form(project)}" + defp install_bundle(frontend, source, dest) do + from = + case frontend do + "mastodon" -> + "public" - defp build_url(project, ref), - do: "https://#{@pleroma_gitlab_host}/#{project}/-/jobs/artifacts/#{ref}/download?job=build" + "kenoma" -> + "build" - defp http_client do - middleware = [ - Tesla.Middleware.FollowRedirects, - Tesla.Middleware.JSON - ] + _ -> + "dist" + end - Tesla.client(middleware) + File.mkdir_p!(dest) + File.cp_r!(Path.join([source, from]), dest) + post_install(frontend, dest) + :ok end end diff --git a/test/fixtures/frontends/admin/README.md b/test/fixtures/frontends/admin/README.md new file mode 100644 index 000000000..6953c067e --- /dev/null +++ b/test/fixtures/frontends/admin/README.md @@ -0,0 +1 @@ +# Fixture for Admin frontend
\ No newline at end of file diff --git a/test/fixtures/frontends/admin/dist/index.html b/test/fixtures/frontends/admin/dist/index.html new file mode 100644 index 000000000..00ccd713a --- /dev/null +++ b/test/fixtures/frontends/admin/dist/index.html @@ -0,0 +1,2 @@ +<h1>test Admin FE</h1> +<!--server-generated-meta-->
\ No newline at end of file diff --git a/test/fixtures/frontends/admin/package.json b/test/fixtures/frontends/admin/package.json new file mode 100644 index 000000000..eef793abd --- /dev/null +++ b/test/fixtures/frontends/admin/package.json @@ -0,0 +1 @@ +{"version": "42"} diff --git a/test/fixtures/frontends/kenoma/README.md b/test/fixtures/frontends/kenoma/README.md new file mode 100644 index 000000000..5043a37cf --- /dev/null +++ b/test/fixtures/frontends/kenoma/README.md @@ -0,0 +1 @@ +# Fixture for Kenoma frontend
\ No newline at end of file diff --git a/test/fixtures/frontends/kenoma/build/index.html b/test/fixtures/frontends/kenoma/build/index.html new file mode 100644 index 000000000..313c1e274 --- /dev/null +++ b/test/fixtures/frontends/kenoma/build/index.html @@ -0,0 +1,2 @@ +<h1>test Kenoma FE</h1> +<!--server-generated-meta-->
\ No newline at end of file diff --git a/test/fixtures/frontends/kenoma/package.json b/test/fixtures/frontends/kenoma/package.json new file mode 100644 index 000000000..eef793abd --- /dev/null +++ b/test/fixtures/frontends/kenoma/package.json @@ -0,0 +1 @@ +{"version": "42"} diff --git a/test/fixtures/frontends/mastodon/README.md b/test/fixtures/frontends/mastodon/README.md new file mode 100644 index 000000000..f5ec3e2d4 --- /dev/null +++ b/test/fixtures/frontends/mastodon/README.md @@ -0,0 +1 @@ +# Fixture for Mastodon frontend
\ No newline at end of file diff --git a/test/fixtures/frontends/mastodon/package.json b/test/fixtures/frontends/mastodon/package.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/test/fixtures/frontends/mastodon/package.json @@ -0,0 +1 @@ +{}
\ No newline at end of file diff --git a/test/fixtures/frontends/mastodon/public/assets/sw.js b/test/fixtures/frontends/mastodon/public/assets/sw.js new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/test/fixtures/frontends/mastodon/public/assets/sw.js diff --git a/test/fixtures/frontends/mastodon/public/packs/locales.js b/test/fixtures/frontends/mastodon/public/packs/locales.js new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/test/fixtures/frontends/mastodon/public/packs/locales.js diff --git a/test/fixtures/frontends/mastodon/public/unused_dir/unused_file2 b/test/fixtures/frontends/mastodon/public/unused_dir/unused_file2 new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/test/fixtures/frontends/mastodon/public/unused_dir/unused_file2 diff --git a/test/fixtures/frontends/mastodon/public/unused_file b/test/fixtures/frontends/mastodon/public/unused_file new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/test/fixtures/frontends/mastodon/public/unused_file diff --git a/test/fixtures/frontends/pleroma/README.md b/test/fixtures/frontends/pleroma/README.md new file mode 100644 index 000000000..63b74bedd --- /dev/null +++ b/test/fixtures/frontends/pleroma/README.md @@ -0,0 +1 @@ +# Fixture for Pleroma frontend
\ No newline at end of file diff --git a/test/fixtures/frontends/pleroma/dist/index.html b/test/fixtures/frontends/pleroma/dist/index.html new file mode 100644 index 000000000..38400b1ee --- /dev/null +++ b/test/fixtures/frontends/pleroma/dist/index.html @@ -0,0 +1,2 @@ +<h1>test Pleroma FE</h1> +<!--server-generated-meta-->
\ No newline at end of file diff --git a/test/fixtures/frontends/pleroma/package.json b/test/fixtures/frontends/pleroma/package.json new file mode 100644 index 000000000..eef793abd --- /dev/null +++ b/test/fixtures/frontends/pleroma/package.json @@ -0,0 +1 @@ +{"version": "42"} diff --git a/test/tasks/frontend_test.exs b/test/tasks/frontend_test.exs index 2f9458f89..fcd07150f 100644 --- a/test/tasks/frontend_test.exs +++ b/test/tasks/frontend_test.exs @@ -10,7 +10,8 @@ defmodule Mix.Tasks.Pleroma.FrontendTest do @bundle_zip_path Path.absname("test/fixtures/tesla_mock/fe-bundle.zip") - @dir "test/tmp/instance_static" + @tmp "test/tmp" + @dir "#{@tmp}/instance_static" setup_all do Mix.shell(Mix.Shell.Process) @@ -47,21 +48,26 @@ defmodule Mix.Tasks.Pleroma.FrontendTest do :ok end - test "installations" do - frontends = ~w(pleroma kenoma mastodon admin) - refs = ~w(develop stable 1.2.3) + describe "Installations from local path" do + test "Frontends with standard dist structure" do + ~w(pleroma kenoma admin) + |> Enum.each(fn frontend -> + path = "test/fixtures/frontends/#{frontend}" + Mix.Tasks.Pleroma.Frontend.run(~w(install #{frontend} --path #{path})) - Enum.each(frontends, fn frontend -> - Enum.each(refs, fn ref -> - Mix.Tasks.Pleroma.Frontend.run([ - "install", - frontend, - "--ref", - ref - ]) - - assert File.exists?(Path.join([@dir, "frontends/#{frontend}/#{ref}/index.html"])) + assert File.exists?("#{@dir}/frontends/#{frontend}/42/index.html") + refute File.exists?("#{@dir}/frontends/#{frontend}/42/package.json") end) - end) + end + + test "Mastodon" do + path = "test/fixtures/frontends/mastodon" + Mix.Tasks.Pleroma.Frontend.run(~w(install mastodon --path #{path})) + + assert File.exists?("#{@dir}/frontends/mastodon/__local__/sw.js") + assert File.exists?("#{@dir}/frontends/mastodon/__local__/packs/locales.js") + refute File.exists?("#{@dir}/frontends/mastodon/__local__/unused_file") + refute File.exists?("#{@dir}/frontends/mastodon/__local__/unused_dir") + end end end |