Skip to content

Commit

Permalink
Support umbrellas and deps
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord committed Feb 27, 2024
1 parent 45c0f03 commit 759a2fc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
14 changes: 14 additions & 0 deletions lib/phoenix_live_reload/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ defmodule Phoenix.LiveReloader.Channel do
WebConsoleLogger.subscribe(@logs)
end

deps_paths =
for {app, path} <- Mix.Project.deps_paths(), into: %{}, do: {to_string(app), path}

config = socket.endpoint.config(:live_reload)

socket =
socket
|> assign(:patterns, config[:patterns] || [])
|> assign(:debounce, config[:debounce] || 0)
|> assign(:notify_patterns, config[:notify] || [])
|> assign(:deps_paths, deps_paths)

{:ok, join_info(), socket}
else
Expand Down Expand Up @@ -74,6 +78,16 @@ defmodule Phoenix.LiveReloader.Channel do
{:noreply, socket}
end

def handle_in("full_path", %{"rel_path" => rel_path, "app" => app}, socket) do
case socket.assigns.deps_paths do
%{^app => dep_path} ->
{:reply, {:ok, %{full_path: Path.join(dep_path, rel_path)}}, socket}

%{} ->
{:reply, {:ok, %{full_path: Path.join(File.cwd!(), rel_path)}}, socket}
end
end

defp debounce(0, _exts, _patterns), do: []

defp debounce(time, exts, patterns) when is_integer(time) and time > 0 do
Expand Down
44 changes: 25 additions & 19 deletions priv/static/phoenix_live_reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class LiveReloader {
this.logsEnabled = false
this.enabledOnce = false
this.editorURL = null
this.relativePath = null
}
enable(){
this.socket.onOpen(() => {
Expand All @@ -72,9 +71,8 @@ class LiveReloader {
setTimeout(() => reloadStrategy(this.channel), interval)
})
this.channel.on("log", ({msg, level}) => this.logsEnabled && this.log(level, msg))
this.channel.join().receive("ok", ({editor_url, relative_path}) => {
this.channel.join().receive("ok", ({editor_url}) => {
this.editorURL = editor_url
this.relativePath = relative_path
})
this.socket.connect()
}
Expand All @@ -92,12 +90,9 @@ class LiveReloader {
return console.error("phoenix_live_reload cannot openEditorAtCaller without configured PLUG_EDITOR")
}

let fileLine = this.closestCallerFileLine(targetNode)
if(fileLine){
let [file, line] = fileLine.split(":")
let fullPath = [this.relativePath, file].join("/")
let url = this.editorURL.replace("__FILE__", fullPath).replace("__LINE__", line)
window.open(url, "_self")
let fileLineApp = this.closestCallerFileLine(targetNode)
if(fileLineApp){
this.openFullPath(...fileLineApp)
}
}

Expand All @@ -106,16 +101,25 @@ class LiveReloader {
return console.error("phoenix_live_reload cannot openEditorAtDef without configured PLUG_EDITOR")
}

let fileLine = this.closestDefFileLine(targetNode)
if(fileLine){
let [file, line] = fileLine.split(":")
let fullPath = [this.relativePath, file].join("/")
let url = this.editorURL.replace("__FILE__", fullPath).replace("__LINE__", line)
window.open(url, "_self")
let fileLineApp = this.closestDefFileLine(targetNode)
if(fileLineApp){
this.openFullPath(...fileLineApp)
}
}

// private

openFullPath(file, line, app){
console.log("opening full path", file, line, app)
this.channel.push("full_path", {rel_path: file, app: app})
.receive("ok", ({full_path}) => {
console.log("full path", full_path)
let url = this.editorURL.replace("__FILE__", full_path).replace("__LINE__", line)
window.open(url, "_self")
})
.receive("error", reason => console.error("failed to resolve full path", reason))
}

dispatchConnected(){
parent.dispatchEvent(new CustomEvent("phx:live_reload:connected", {detail: this}))
}
Expand All @@ -133,10 +137,10 @@ class LiveReloader {
let callerComment = node.previousSibling
let callerMatch = callerComment &&
callerComment.nodeType === Node.COMMENT_NODE &&
callerComment.nodeValue.match(/\s@caller\s+(.+):(\d+)\s/i)
callerComment.nodeValue.match(/\s@caller\s+(.+):(\d+)\s\((.*)\)\s/i)

if(callerMatch){
return `${callerMatch[1]}:${callerMatch[2]}`
return [callerMatch[1], callerMatch[2], callerMatch[3]]
}
}
}
Expand All @@ -147,8 +151,10 @@ class LiveReloader {
while(node.previousSibling){
node = node.previousSibling
if(node.nodeType === Node.COMMENT_NODE){
let fcMatch = node.nodeValue.match(/.*>\s([\w\/]+.*ex:\d+)/i)
if(fcMatch){ return fcMatch[1] }
let fcMatch = node.nodeValue.match(/.*>\s([\w\/]+.*ex):(\d+)\s\((.*)\)\s/i)
if(fcMatch){
return [fcMatch[1], fcMatch[2], fcMatch[3]]
}
}
}
if(node.parentNode){ return this.closestDefFileLine(node.parentNode) }
Expand Down

0 comments on commit 759a2fc

Please sign in to comment.