Skip to content

Commit

Permalink
Require Phoenix to be run as adminstrator only once on Windows (phoen…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengxiaoyao0716 authored and LostKobrakai committed Sep 14, 2018
1 parent 7d87cac commit 4ffb9ec
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions lib/phoenix/code_reloader/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ defmodule Phoenix.CodeReloader.Server do

def handle_call(:check_symlinks, _from, checked?) do
if not checked? and Code.ensure_loaded?(Mix.Project) do
build_path = Mix.Project.build_path()
symlink = Path.join(Path.dirname(build_path), "__phoenix__")
priv_path = "#{Mix.Project.app_path}/priv"

case :file.read_link(priv_path) do
{:ok, _} ->
:ok

case File.ln_s(build_path, symlink) do
:ok ->
File.rm(symlink)
{:error, :eexist} ->
File.rm(symlink)
{:error, _} ->
Logger.warn "Phoenix is unable to create symlinks. Phoenix' code reloader will run " <>
"considerably faster if symlinks are allowed." <> os_symlink(:os.type)
if can_symlink?() do
File.rm_rf(priv_path)
Mix.Project.build_structure
else
Logger.warn "Phoenix is unable to create symlinks. Phoenix' code reloader will run " <>
"considerably faster if symlinks are allowed." <> os_symlink(:os.type)
end
end
end

Expand Down Expand Up @@ -78,10 +81,30 @@ defmodule Phoenix.CodeReloader.Server do
end

defp os_symlink({:win32, _}),
do: " On Windows, such can be done by starting the shell with \"Run as Administrator\"."
do: " On Windows, the lack of symlinks may even cause empty assets to be served. " <>
"Luckily, you can address this issue by starting your Windows terminal at least " <>
"once with \"Run as Administrator\" and then running your Phoenix application."
defp os_symlink(_),
do: ""

defp can_symlink?() do
build_path = Mix.Project.build_path()
symlink = Path.join(Path.dirname(build_path), "__phoenix__")

case File.ln_s(build_path, symlink) do
:ok ->
File.rm_rf(symlink)
true

{:error, :eexist} ->
File.rm_rf(symlink)
true

{:error, _} ->
false
end
end

defp load_backup(mod) do
mod
|> :code.which()
Expand Down

0 comments on commit 4ffb9ec

Please sign in to comment.