2021-03-04 05:56:28 +08:00
|
|
|
defmodule LivebookWeb.SessionLive.MixStandaloneLive do
|
|
|
|
use LivebookWeb, :live_view
|
2021-02-27 03:53:29 +08:00
|
|
|
|
2021-03-04 05:56:28 +08:00
|
|
|
alias Livebook.{Session, Runtime, Utils}
|
2021-02-27 03:53:29 +08:00
|
|
|
|
|
|
|
@type status :: :initial | :initializing | :finished
|
|
|
|
|
|
|
|
@impl true
|
2021-03-20 21:10:15 +08:00
|
|
|
def mount(_params, %{"session_id" => session_id, "current_runtime" => current_runtime}, socket) do
|
2021-03-24 02:41:03 +08:00
|
|
|
if connected?(socket) do
|
|
|
|
Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions:#{session_id}")
|
|
|
|
end
|
|
|
|
|
2021-02-27 03:53:29 +08:00
|
|
|
{:ok,
|
|
|
|
assign(socket,
|
|
|
|
session_id: session_id,
|
|
|
|
status: :initial,
|
2021-03-24 02:41:03 +08:00
|
|
|
current_runtime: current_runtime,
|
|
|
|
path: initial_path(current_runtime),
|
2021-02-27 03:53:29 +08:00
|
|
|
outputs: [],
|
|
|
|
emitter: nil
|
|
|
|
), temporary_assigns: [outputs: []]}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
~L"""
|
2021-04-04 18:42:46 +08:00
|
|
|
<div class="flex-col space-y-5">
|
2021-03-20 21:10:15 +08:00
|
|
|
<p class="text-gray-700">
|
2021-02-27 03:53:29 +08:00
|
|
|
Start a new local node in the context of a Mix project.
|
|
|
|
This way all your code and dependencies will be available
|
|
|
|
within the notebook.
|
|
|
|
</p>
|
2021-06-09 22:51:53 +08:00
|
|
|
<p class="text-gray-700">
|
|
|
|
<span class="font-semibold">Warning:</span>
|
|
|
|
Notebooks that use <code>Mix.install/1</code> do not work
|
|
|
|
inside a Mix project because the dependencies of the project
|
|
|
|
itself have been installed instead.
|
|
|
|
</p>
|
2021-02-27 03:53:29 +08:00
|
|
|
<%= if @status == :initial do %>
|
2021-03-20 21:10:15 +08:00
|
|
|
<div class="h-full h-52">
|
2021-06-03 19:53:03 +08:00
|
|
|
<%= live_component LivebookWeb.PathSelectComponent,
|
2021-03-20 21:10:15 +08:00
|
|
|
id: "path_select",
|
|
|
|
path: @path,
|
|
|
|
extnames: [],
|
|
|
|
running_paths: [],
|
2021-04-13 04:59:48 +08:00
|
|
|
phx_target: nil,
|
|
|
|
phx_submit: if(disabled?(@path), do: nil, else: "init") %>
|
2021-03-20 21:10:15 +08:00
|
|
|
</div>
|
2021-03-24 02:41:03 +08:00
|
|
|
<%= content_tag :button, if(matching_runtime?(@current_runtime, @path), do: "Reconnect", else: "Connect"),
|
2021-03-26 00:39:18 +08:00
|
|
|
class: "button button-blue",
|
2021-03-24 02:41:03 +08:00
|
|
|
phx_click: "init",
|
2021-04-13 04:59:48 +08:00
|
|
|
disabled: disabled?(@path) %>
|
2021-02-27 03:53:29 +08:00
|
|
|
<% end %>
|
|
|
|
<%= if @status != :initial do %>
|
|
|
|
<div class="markdown">
|
|
|
|
<pre><code class="max-h-40 overflow-y-auto tiny-scrollbar"
|
|
|
|
id="mix-standalone-init-output"
|
|
|
|
phx-update="append"
|
|
|
|
phx-hook="ScrollOnUpdate"
|
|
|
|
><%= for {output, i} <- @outputs do %><span id="output-<%= i %>"><%= ansi_string_to_html(output) %></span><% end %></code></pre>
|
|
|
|
</div>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("set_path", %{"path" => path}, socket) do
|
|
|
|
{:noreply, assign(socket, path: path)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("init", _params, socket) do
|
|
|
|
emitter = Utils.Emitter.new(self())
|
|
|
|
Runtime.MixStandalone.init_async(socket.assigns.path, emitter)
|
|
|
|
{:noreply, assign(socket, status: :initializing, emitter: emitter)}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info({:emitter, ref, message}, %{assigns: %{emitter: %{ref: ref}}} = socket) do
|
|
|
|
case message do
|
|
|
|
{:output, output} ->
|
|
|
|
{:noreply, add_output(socket, output)}
|
|
|
|
|
|
|
|
{:ok, runtime} ->
|
|
|
|
Session.connect_runtime(socket.assigns.session_id, runtime)
|
|
|
|
{:noreply, socket |> assign(status: :finished) |> add_output("Connected successfully")}
|
|
|
|
|
|
|
|
{:error, error} ->
|
|
|
|
{:noreply, socket |> assign(status: :finished) |> add_output("Error: #{error}")}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-24 02:41:03 +08:00
|
|
|
def handle_info({:operation, {:set_runtime, _pid, runtime}}, socket) do
|
|
|
|
{:noreply, assign(socket, current_runtime: runtime)}
|
|
|
|
end
|
|
|
|
|
2021-02-27 03:53:29 +08:00
|
|
|
def handle_info(_, socket), do: {:noreply, socket}
|
|
|
|
|
|
|
|
defp add_output(socket, output) do
|
|
|
|
assign(socket, outputs: socket.assigns.outputs ++ [{output, Utils.random_id()}])
|
|
|
|
end
|
|
|
|
|
2021-03-24 02:41:03 +08:00
|
|
|
defp initial_path(%Runtime.MixStandalone{} = current_runtime) do
|
|
|
|
current_runtime.project_path
|
|
|
|
end
|
|
|
|
|
|
|
|
defp initial_path(_runtime), do: File.cwd!() <> "/"
|
2021-02-27 03:53:29 +08:00
|
|
|
|
|
|
|
defp mix_project_root?(path) do
|
|
|
|
File.dir?(path) and File.exists?(Path.join(path, "mix.exs"))
|
|
|
|
end
|
2021-04-13 04:59:48 +08:00
|
|
|
|
|
|
|
defp matching_runtime?(%Runtime.MixStandalone{} = runtime, path) do
|
|
|
|
Path.expand(runtime.project_path) == Path.expand(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp matching_runtime?(_runtime, _path), do: false
|
|
|
|
|
|
|
|
defp disabled?(path) do
|
|
|
|
not mix_project_root?(path)
|
|
|
|
end
|
2021-02-27 03:53:29 +08:00
|
|
|
end
|