From 2a1d93718921812790d34dbde6b511af5ede8a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Sat, 30 Apr 2022 17:14:10 +0200 Subject: [PATCH] Update wording around dependencies and packages (#1151) --- assets/js/hooks/session.js | 8 ++-- lib/livebook.ex | 8 ++-- lib/livebook/runtime.ex | 13 +++---- lib/livebook/runtime/attached.ex | 2 +- lib/livebook/runtime/dependencies.ex | 32 ++++++++-------- lib/livebook/runtime/elixir_standalone.ex | 4 +- lib/livebook/runtime/embedded.ex | 10 ++--- lib/livebook/runtime/mix_standalone.ex | 2 +- lib/livebook_web/live/session_live.ex | 8 ++-- .../live/session_live/cell_component.ex | 12 +++--- ..._search_live.ex => package_search_live.ex} | 38 +++++++++---------- .../live/session_live/shortcuts_component.ex | 2 +- lib/livebook_web/router.ex | 2 +- test/livebook/runtime/dependencies_test.exs | 2 +- test/livebook_web/live/session_live_test.exs | 4 +- test/support/noop_runtime.ex | 2 +- test/test_helper.exs | 6 +-- 17 files changed, 77 insertions(+), 78 deletions(-) rename lib/livebook_web/live/session_live/{dependency_search_live.ex => package_search_live.ex} (77%) diff --git a/assets/js/hooks/session.js b/assets/js/hooks/session.js index 5b9dda398..098239863 100644 --- a/assets/js/hooks/session.js +++ b/assets/js/hooks/session.js @@ -339,8 +339,8 @@ const Session = { this.toggleRuntimeInfo(); } else if (keyBuffer.tryMatch(["s", "b"])) { this.showBin(); - } else if (keyBuffer.tryMatch(["s", "d"])) { - this.showDependencySearch(); + } else if (keyBuffer.tryMatch(["s", "p"])) { + this.showPackageSearch(); } else if (keyBuffer.tryMatch(["e", "x"])) { this.cancelFocusedCellEvaluation(); } else if (keyBuffer.tryMatch(["0", "0"])) { @@ -683,10 +683,10 @@ const Session = { actionEl && actionEl.click(); }, - showDependencySearch() { + showPackageSearch() { this.setFocusedEl("setup"); - const actionEl = this.el.querySelector(`[data-btn-dependency-search]`); + const actionEl = this.el.querySelector(`[data-btn-package-search]`); actionEl && actionEl.click(); }, diff --git a/lib/livebook.ex b/lib/livebook.ex index 588d87161..21fbee27a 100644 --- a/lib/livebook.ex +++ b/lib/livebook.ex @@ -19,12 +19,12 @@ defmodule Livebook do ### Embedded runtime dependencies - In case you use the Embedded runtime and support installing dependencies - with `Mix.install/2`, you can make those discoverable in the - dependency search, by configuring a loader function: + In case you use the Embedded runtime and support installing + dependencies with `Mix.install/2`, you can make those discoverable + in the package search, by configuring a loader function: config :livebook, Livebook.Runtime.Embedded, - load_dependency_entries: {Loader, :dependency_entries, []} + load_packages: {Loader, :packages, []} The function should return a list of entries like this: diff --git a/lib/livebook/runtime.ex b/lib/livebook/runtime.ex index 34ff3ac8d..ca0ad9812 100644 --- a/lib/livebook/runtime.ex +++ b/lib/livebook/runtime.ex @@ -208,10 +208,9 @@ defprotocol Livebook.Runtime do @type dependency :: term() - @type search_dependencies_response :: - {:ok, list(search_dependencies_entry())} | {:error, String.t()} + @type search_packages_response :: {:ok, list(package())} | {:error, String.t()} - @type search_dependencies_entry :: %{ + @type package :: %{ name: String.t(), version: String.t(), description: String.t() | nil, @@ -443,7 +442,7 @@ defprotocol Livebook.Runtime do dependencies, the dependencies are not considered fixed. When dependencies are fixed, the following functions are allowed to - raise an implementation error: `add_dependencies/3`, `search_dependencies/3`. + raise an implementation error: `add_dependencies/3`, `search_packages/3`. """ @spec fixed_dependencies?(t()) :: boolean() def fixed_dependencies?(runtime) @@ -460,8 +459,8 @@ defprotocol Livebook.Runtime do The response is sent to the `send_to` process as - * `{:runtime_search_dependencies_response, ref, response}`. + * `{:runtime_search_packages_response, ref, response}`. """ - @spec search_dependencies(t(), pid(), String.t()) :: reference() - def search_dependencies(runtime, send_to, search) + @spec search_packages(t(), pid(), String.t()) :: reference() + def search_packages(runtime, send_to, search) end diff --git a/lib/livebook/runtime/attached.ex b/lib/livebook/runtime/attached.ex index 46d164ea8..6aead097a 100644 --- a/lib/livebook/runtime/attached.ex +++ b/lib/livebook/runtime/attached.ex @@ -120,7 +120,7 @@ defimpl Livebook.Runtime, for: Livebook.Runtime.Attached do raise "not supported" end - def search_dependencies(_runtime, _send_to, _search) do + def search_packages(_runtime, _send_to, _search) do raise "not supported" end end diff --git a/lib/livebook/runtime/dependencies.ex b/lib/livebook/runtime/dependencies.ex index 5d85de0a0..5bb8f4efc 100644 --- a/lib/livebook/runtime/dependencies.ex +++ b/lib/livebook/runtime/dependencies.ex @@ -157,45 +157,45 @@ defmodule Livebook.Runtime.Dependencies do defp unescape_term(_node), do: :error @doc """ - Implements `Livebook.Runtime.search_dependencies/3` on top of + Implements `Livebook.Runtime.search_packages/3` on top of `search_hex/2`. """ - @spec search_dependencies_on_hex(pid(), String.t()) :: reference() - def search_dependencies_on_hex(send_to, search) do + @spec search_packages_on_hex(pid(), String.t()) :: reference() + def search_packages_on_hex(send_to, search) do ref = make_ref() Task.Supervisor.start_child(Livebook.TaskSupervisor, fn -> response = search_hex(search) - send(send_to, {:runtime_search_dependencies_response, ref, response}) + send(send_to, {:runtime_search_packages_response, ref, response}) end) ref end @doc """ - Implements `Livebook.Runtime.search_dependencies/3` by searching - through the given list of entries. + Implements `Livebook.Runtime.search_packages/3` by searching + through the given list of packages. """ - @spec search_dependencies_in_entries( - list(Livebook.Runtime.search_dependencies_entry()), + @spec search_packages_in_list( + list(Livebook.Runtime.package()), pid(), String.t() ) :: reference() - def search_dependencies_in_entries(entries, send_to, search) do + def search_packages_in_list(packages, send_to, search) do ref = make_ref() - entries = Enum.filter(entries, &String.starts_with?(&1.name, search)) - send(send_to, {:runtime_search_dependencies_response, ref, {:ok, entries}}) + packages = Enum.filter(packages, &String.starts_with?(&1.name, search)) + send(send_to, {:runtime_search_packages_response, ref, {:ok, packages}}) ref end @doc """ - Searches for packages on Hex and returns them as dependency entries. + Searches for packages on Hex. ## Options * `:api_url` - the base URL for Hex API requests. Optional """ - @spec search_hex(String.t(), keyword()) :: Livebook.Runtime.search_dependencies_response() + @spec search_hex(String.t(), keyword()) :: Livebook.Runtime.search_packages_response() def search_hex(search, opts \\ []) def search_hex("", _opts), do: {:ok, []} @@ -209,8 +209,8 @@ defmodule Livebook.Runtime.Dependencies do case Livebook.Utils.HTTP.request(:get, url) do {:ok, status, _headers, body} -> with 200 <- status, {:ok, packages} <- Jason.decode(body) do - entries = Enum.map(packages, &package_to_entry/1) - {:ok, entries} + packages = Enum.map(packages, &parse_package/1) + {:ok, packages} else _ -> {:error, "unexpected response"} end @@ -220,7 +220,7 @@ defmodule Livebook.Runtime.Dependencies do end end - defp package_to_entry(package) do + defp parse_package(package) do {:ok, dependency} = parse_term(package["configs"]["mix.exs"]) %{ diff --git a/lib/livebook/runtime/elixir_standalone.ex b/lib/livebook/runtime/elixir_standalone.ex index 3665f46e4..5da5962c7 100644 --- a/lib/livebook/runtime/elixir_standalone.ex +++ b/lib/livebook/runtime/elixir_standalone.ex @@ -192,7 +192,7 @@ defimpl Livebook.Runtime, for: Livebook.Runtime.ElixirStandalone do Livebook.Runtime.Dependencies.add_mix_deps(code, dependencies) end - def search_dependencies(_runtime, send_to, search) do - Livebook.Runtime.Dependencies.search_dependencies_on_hex(send_to, search) + def search_packages(_runtime, send_to, search) do + Livebook.Runtime.Dependencies.search_packages_on_hex(send_to, search) end end diff --git a/lib/livebook/runtime/embedded.ex b/lib/livebook/runtime/embedded.ex index 141b2d47a..bef481e2b 100644 --- a/lib/livebook/runtime/embedded.ex +++ b/lib/livebook/runtime/embedded.ex @@ -109,17 +109,17 @@ defimpl Livebook.Runtime, for: Livebook.Runtime.Embedded do end def fixed_dependencies?(_runtime) do - not Keyword.has_key?(config(), :load_dependency_entries) + not Keyword.has_key?(config(), :load_packages) end def add_dependencies(_runtime, code, dependencies) do Livebook.Runtime.Dependencies.add_mix_deps(code, dependencies) end - def search_dependencies(_runtime, send_to, search) do - {mod, fun, args} = config()[:load_dependency_entries] - entries = apply(mod, fun, args) - Livebook.Runtime.Dependencies.search_dependencies_in_entries(entries, send_to, search) + def search_packages(_runtime, send_to, search) do + {mod, fun, args} = config()[:load_packages] + packages = apply(mod, fun, args) + Livebook.Runtime.Dependencies.search_packages_in_list(packages, send_to, search) end defp config() do diff --git a/lib/livebook/runtime/mix_standalone.ex b/lib/livebook/runtime/mix_standalone.ex index e43b6539f..01b9fcdb1 100644 --- a/lib/livebook/runtime/mix_standalone.ex +++ b/lib/livebook/runtime/mix_standalone.ex @@ -209,7 +209,7 @@ defimpl Livebook.Runtime, for: Livebook.Runtime.MixStandalone do raise "not supported" end - def search_dependencies(_runtime, _send_to, _search) do + def search_packages(_runtime, _send_to, _search) do raise "not supported" end end diff --git a/lib/livebook_web/live/session_live.ex b/lib/livebook_web/live/session_live.ex index a0c8a4473..4700e8bfd 100644 --- a/lib/livebook_web/live/session_live.ex +++ b/lib/livebook_web/live/session_live.ex @@ -317,10 +317,10 @@ defmodule LivebookWeb.SessionLive do <% end %> - <%= if @live_action == :dependency_search do %> - <.modal id="dependency-search-modal" show class="w-full max-w-xl" patch={@self_path}> - <%= live_render @socket, LivebookWeb.SessionLive.DependencySearchLive, - id: "dependency-search", + <%= if @live_action == :package_search do %> + <.modal id="package-search-modal" show class="w-full max-w-xl" patch={@self_path}> + <%= live_render @socket, LivebookWeb.SessionLive.PackageSearchLive, + id: "package-search", session: %{ "session" => @session, "runtime" => @data_view.runtime, diff --git a/lib/livebook_web/live/session_live/cell_component.ex b/lib/livebook_web/live/session_live/cell_component.ex index 0a0fba62b..8406402fa 100644 --- a/lib/livebook_web/live/session_live/cell_component.ex +++ b/lib/livebook_web/live/session_live/cell_component.ex @@ -104,7 +104,7 @@ defmodule LivebookWeb.SessionLive.CellComponent do <:secondary> <.enable_insert_mode_button /> - <.dependency_search_button session_id={@session_id} runtime={@runtime} socket={@socket} /> + <.package_search_button session_id={@session_id} runtime={@runtime} socket={@socket} /> <.cell_link_button cell_id={@cell_view.id} /> <.setup_cell_info /> @@ -362,7 +362,7 @@ defmodule LivebookWeb.SessionLive.CellComponent do <% else %> - - <%= live_patch to: Routes.session_path(@socket, :dependency_search, @session_id), + + <%= live_patch to: Routes.session_path(@socket, :package_search, @session_id), class: "icon-button", aria_label: "add dependency", role: "button", - data_btn_dependency_search: true do %> + data_btn_package_search: true do %> <.remix_icon icon="play-list-add-line" class="text-xl" /> <% end %> diff --git a/lib/livebook_web/live/session_live/dependency_search_live.ex b/lib/livebook_web/live/session_live/package_search_live.ex similarity index 77% rename from lib/livebook_web/live/session_live/dependency_search_live.ex rename to lib/livebook_web/live/session_live/package_search_live.ex index e383c7064..744f332c2 100644 --- a/lib/livebook_web/live/session_live/dependency_search_live.ex +++ b/lib/livebook_web/live/session_live/package_search_live.ex @@ -1,4 +1,4 @@ -defmodule LivebookWeb.SessionLive.DependencySearchLive do +defmodule LivebookWeb.SessionLive.PackageSearchLive do use LivebookWeb, :live_view @impl true @@ -14,7 +14,7 @@ defmodule LivebookWeb.SessionLive.DependencySearchLive do return_to: return_to, search: "", search_ref: nil, - entries: [], + packages: [], error_message: nil ) @@ -28,7 +28,7 @@ defmodule LivebookWeb.SessionLive.DependencySearchLive do ~H"""

- Dependency search + Search packages

Find external packages for your notebook @@ -50,15 +50,15 @@ defmodule LivebookWeb.SessionLive.DependencySearchLive do <%= @error_message %>

- <% @entries == [] -> %> + <% @packages == [] -> %>
<.remix_icon icon="windy-line" class="text-xl" />
No results
<% true -> %> - <%= for {entry, idx} <- Enum.with_index(@entries) do %> - <.dependency_entry entry={entry} idx={idx} /> + <%= for {package, idx} <- Enum.with_index(@packages) do %> + <.package package={package} idx={idx} /> <% end %> <% end %> @@ -66,20 +66,20 @@ defmodule LivebookWeb.SessionLive.DependencySearchLive do """ end - defp dependency_entry(assigns) do + defp package(assigns) do ~H"""
- <%= if @entry[:url] do %> - <%= @entry.name %> + <%= if @package[:url] do %> + <%= @package.name %> <% else %> - <%= @entry.name %> + <%= @package.name %> <% end %> - <%= @entry.version %> + <%= @package.version %>
- <%= @entry.description %> + <%= @package.description %>
@@ -102,7 +102,7 @@ defmodule LivebookWeb.SessionLive.DependencySearchLive do @impl true def handle_event("submit", %{}, socket) do socket = - case socket.assigns.entries do + case socket.assigns.packages do [] -> socket [first | _] -> add_dependency(socket, first.dependency) end @@ -112,20 +112,20 @@ defmodule LivebookWeb.SessionLive.DependencySearchLive do @impl true def handle_event("add", %{"idx" => idx}, socket) do - entry = Enum.fetch!(socket.assigns.entries, idx) - socket = add_dependency(socket, entry.dependency) + package = Enum.fetch!(socket.assigns.packages, idx) + socket = add_dependency(socket, package.dependency) {:noreply, socket} end @impl true def handle_info( - {:runtime_search_dependencies_response, ref, response}, + {:runtime_search_packages_response, ref, response}, %{assigns: %{search_ref: ref}} = socket ) do socket = case response do - {:ok, entries} -> - assign(socket, entries: entries, error_message: nil) + {:ok, packages} -> + assign(socket, packages: packages, error_message: nil) {:error, message} -> assign(socket, error_message: Livebook.Utils.upcase_first(message)) @@ -137,7 +137,7 @@ defmodule LivebookWeb.SessionLive.DependencySearchLive do def handle_info(_message, socket), do: {:noreply, socket} defp do_search(socket, search) do - search_ref = Livebook.Runtime.search_dependencies(socket.assigns.runtime, self(), search) + search_ref = Livebook.Runtime.search_packages(socket.assigns.runtime, self(), search) assign(socket, search_ref: search_ref, search: search) end diff --git a/lib/livebook_web/live/session_live/shortcuts_component.ex b/lib/livebook_web/live/session_live/shortcuts_component.ex index ad9276a12..42c38dc44 100644 --- a/lib/livebook_web/live/session_live/shortcuts_component.ex +++ b/lib/livebook_web/live/session_live/shortcuts_component.ex @@ -101,7 +101,7 @@ defmodule LivebookWeb.SessionLive.ShortcutsComponent do %{seq: ["s", "u"], desc: "Toggle users panel"}, %{seq: ["s", "r"], desc: "Show runtime panel"}, %{seq: ["s", "b"], desc: "Show bin"}, - %{seq: ["s", "d"], desc: "Show dependency search"}, + %{seq: ["s", "p"], desc: "Show package search"}, %{seq: ["0", "0"], desc: "Reconnect current runtime"} ], universal: [ diff --git a/lib/livebook_web/router.ex b/lib/livebook_web/router.ex index 070e4ac7e..eac9b9b47 100644 --- a/lib/livebook_web/router.ex +++ b/lib/livebook_web/router.ex @@ -65,7 +65,7 @@ defmodule LivebookWeb.Router do live "/sessions/:id/cell-settings/:cell_id", SessionLive, :cell_settings live "/sessions/:id/cell-upload/:cell_id", SessionLive, :cell_upload live "/sessions/:id/delete-section/:section_id", SessionLive, :delete_section - live "/sessions/:id/dependency-search", SessionLive, :dependency_search + live "/sessions/:id/package-search", SessionLive, :package_search get "/sessions/:id/images/:image", SessionController, :show_image live "/sessions/:id/*path_parts", SessionLive, :catch_all end diff --git a/test/livebook/runtime/dependencies_test.exs b/test/livebook/runtime/dependencies_test.exs index 830d7fc73..666fd9b48 100644 --- a/test/livebook/runtime/dependencies_test.exs +++ b/test/livebook/runtime/dependencies_test.exs @@ -196,7 +196,7 @@ defmodule Livebook.Runtime.DependenciesTest do {:ok, bypass: bypass} end - test "parses the response into dependency entries", %{bypass: bypass} do + test "parses the response into a list of packages", %{bypass: bypass} do Bypass.expect_once(bypass, "GET", "/api/packages", fn conn -> conn |> Plug.Conn.put_resp_content_type("application/json") diff --git a/test/livebook_web/live/session_live_test.exs b/test/livebook_web/live/session_live_test.exs index c562b0ed2..4614e7877 100644 --- a/test/livebook_web/live/session_live_test.exs +++ b/test/livebook_web/live/session_live_test.exs @@ -896,9 +896,9 @@ defmodule LivebookWeb.SessionLiveTest do end end - describe "dependency search" do + describe "package search" do test "lists search entries", %{conn: conn, session: session} do - {:ok, view, _} = live(conn, "/sessions/#{session.id}/dependency-search") + {:ok, view, _} = live(conn, "/sessions/#{session.id}/package-search") [search_view] = live_children(view) diff --git a/test/support/noop_runtime.ex b/test/support/noop_runtime.ex index c95d1c295..9bc3e72b7 100644 --- a/test/support/noop_runtime.ex +++ b/test/support/noop_runtime.ex @@ -37,6 +37,6 @@ defmodule Livebook.Runtime.NoopRuntime do Livebook.Runtime.Dependencies.add_mix_deps(code, dependencies) end - def search_dependencies(_, _, _), do: make_ref() + def search_packages(_, _, _), do: make_ref() end end diff --git a/test/test_helper.exs b/test/test_helper.exs index 2f5718cd4..b821318e6 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -18,8 +18,8 @@ Application.put_env(:livebook, :runtime_modules, [ Livebook.Runtime.Embedded ]) -defmodule Livebook.Runtime.Embedded.Dependencies do - def entries() do +defmodule Livebook.Runtime.Embedded.Packages do + def list() do [ %{ dependency: {:jason, "~> 1.3.0"}, @@ -34,7 +34,7 @@ end # Enable dependency saerch for the embedded runtime Application.put_env(:livebook, Livebook.Runtime.Embedded, - load_dependency_entries: {Livebook.Runtime.Embedded.Dependencies, :entries, []} + load_packages: {Livebook.Runtime.Embedded.Packages, :list, []} ) # Disable autosaving