From 81c48d330d79993859c37eb19c01497f266552e3 Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Thu, 29 Mar 2018 21:04:44 +0200 Subject: [PATCH 1/3] Compute elixir_lib_dir even when running as escript --- lib/ex_doc/cli.ex | 2 ++ lib/ex_doc/formatter/html/autolink.ex | 34 +++++++++++++------- test/ex_doc/cli_test.exs | 4 +++ test/ex_doc/formatter/html/autolink_test.exs | 1 + 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/ex_doc/cli.ex b/lib/ex_doc/cli.ex index 17ba53ba5..2654e31b0 100644 --- a/lib/ex_doc/cli.ex +++ b/lib/ex_doc/cli.ex @@ -5,6 +5,8 @@ defmodule ExDoc.CLI do Handles the command line parsing for the escript. """ def main(args, generator \\ &ExDoc.generate_docs/3) do + Application.put_env(:ex_doc, :running_as_escript, true) + {opts, args, _invalid} = OptionParser.parse(args, aliases: [ diff --git a/lib/ex_doc/formatter/html/autolink.ex b/lib/ex_doc/formatter/html/autolink.ex index 5fad0c2df..74a2518ba 100644 --- a/lib/ex_doc/formatter/html/autolink.ex +++ b/lib/ex_doc/formatter/html/autolink.ex @@ -542,27 +542,37 @@ defmodule ExDoc.Formatter.HTML.Autolink do case Application.fetch_env(:ex_doc, :elixir_lib_dirs) do {:ok, lib_dirs} -> lib_dirs - :error -> - lib_dir = - case :code.where_is_file('Elixir.Kernel.beam') do - :non_existing -> - [0] - path -> - path - |> Path.dirname() - |> Path.dirname() - |> Path.dirname() - end + :error -> lib_dirs = for app <- ~w(elixir eex iex logger mix ex_unit) do - {lib_dir <> "/" <> app <> "/ebin", @elixir_docs <> app <> "/"} + {elixir_lib_dir(app), @elixir_docs <> app <> "/"} end + Application.put_env(:ex_doc, :elixir_lib_dirs, lib_dirs) lib_dirs end end + defp elixir_lib_dir(app) do + path = + case :code.where_is_file('Elixir.Kernel.beam') do + :non_existing -> raise "foo" + path -> List.to_string(path) + end + + if Application.get_env(:ex_doc, :running_as_escript) do + Path.dirname(path) + else + path = + path + |> Path.dirname() + |> Path.dirname() + |> Path.dirname() + path <> "/" <> app <> "/ebin" + end + end + defp erlang_lib_dirs do case Application.fetch_env(:ex_doc, :erlang_lib_dirs) do {:ok, lib_dirs} -> diff --git a/test/ex_doc/cli_test.exs b/test/ex_doc/cli_test.exs index 2f009edfc..b5f499534 100644 --- a/test/ex_doc/cli_test.exs +++ b/test/ex_doc/cli_test.exs @@ -7,6 +7,10 @@ defmodule ExDoc.CLITest do ExDoc.CLI.main(args, &{&1, &2, &3}) end + setup_all do + on_exit(fn -> Application.put_env(:ex_doc, :running_as_escript, false) end) + end + test "minimum command-line options" do assert {"ExDoc", "1.2.3", [extras: [], source_beam: "/"]} == run(["ExDoc", "1.2.3", "/"]) end diff --git a/test/ex_doc/formatter/html/autolink_test.exs b/test/ex_doc/formatter/html/autolink_test.exs index a48ee3cc3..c36fb9f3d 100644 --- a/test/ex_doc/formatter/html/autolink_test.exs +++ b/test/ex_doc/formatter/html/autolink_test.exs @@ -34,6 +34,7 @@ defmodule ExDoc.Formatter.HTML.AutolinkTest do # links to types without arity don't work assert Autolink.local_doc("`t:my_type`", ["t:my_type/0"]) == "`t:my_type`" end + test "autolink to basic and built-in types" do assert Autolink.local_doc("`t:atom/0`", []) == "[`atom/0`](#{@elixir_docs}elixir/typespecs.html#basic-types)" From a1374ca1417d49df2d01ee7874d9ce9a0640a779 Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Thu, 29 Mar 2018 21:11:49 +0200 Subject: [PATCH 2/3] Remove debug cruft --- lib/ex_doc/formatter/html/autolink.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ex_doc/formatter/html/autolink.ex b/lib/ex_doc/formatter/html/autolink.ex index 74a2518ba..3d9fb1a57 100644 --- a/lib/ex_doc/formatter/html/autolink.ex +++ b/lib/ex_doc/formatter/html/autolink.ex @@ -557,7 +557,7 @@ defmodule ExDoc.Formatter.HTML.Autolink do defp elixir_lib_dir(app) do path = case :code.where_is_file('Elixir.Kernel.beam') do - :non_existing -> raise "foo" + :non_existing -> "" path -> List.to_string(path) end From 19065744495e5eeb39e93c0c69678a3832170354 Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Thu, 29 Mar 2018 21:20:16 +0200 Subject: [PATCH 3/3] Revert unnecessary escript check --- lib/ex_doc/cli.ex | 2 -- lib/ex_doc/formatter/html/autolink.ex | 7 ++++--- test/ex_doc/cli_test.exs | 4 ---- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/ex_doc/cli.ex b/lib/ex_doc/cli.ex index 2654e31b0..17ba53ba5 100644 --- a/lib/ex_doc/cli.ex +++ b/lib/ex_doc/cli.ex @@ -5,8 +5,6 @@ defmodule ExDoc.CLI do Handles the command line parsing for the escript. """ def main(args, generator \\ &ExDoc.generate_docs/3) do - Application.put_env(:ex_doc, :running_as_escript, true) - {opts, args, _invalid} = OptionParser.parse(args, aliases: [ diff --git a/lib/ex_doc/formatter/html/autolink.ex b/lib/ex_doc/formatter/html/autolink.ex index 3d9fb1a57..7d6bded0d 100644 --- a/lib/ex_doc/formatter/html/autolink.ex +++ b/lib/ex_doc/formatter/html/autolink.ex @@ -561,15 +561,16 @@ defmodule ExDoc.Formatter.HTML.Autolink do path -> List.to_string(path) end - if Application.get_env(:ex_doc, :running_as_escript) do - Path.dirname(path) - else + if File.exists?(path) do path = path |> Path.dirname() |> Path.dirname() |> Path.dirname() path <> "/" <> app <> "/ebin" + else + # if beam file doesn't exists it's likely an escript + Path.dirname(path) end end diff --git a/test/ex_doc/cli_test.exs b/test/ex_doc/cli_test.exs index b5f499534..2f009edfc 100644 --- a/test/ex_doc/cli_test.exs +++ b/test/ex_doc/cli_test.exs @@ -7,10 +7,6 @@ defmodule ExDoc.CLITest do ExDoc.CLI.main(args, &{&1, &2, &3}) end - setup_all do - on_exit(fn -> Application.put_env(:ex_doc, :running_as_escript, false) end) - end - test "minimum command-line options" do assert {"ExDoc", "1.2.3", [extras: [], source_beam: "/"]} == run(["ExDoc", "1.2.3", "/"]) end