Skip to content

Commit

Permalink
Revert "Safety check before running File.rm_rf! in doc gen (#1707)"
Browse files Browse the repository at this point in the history
Unfortunately this is generating ExDoc files with mixed entries.

This reverts commit 9766ed7.
  • Loading branch information
josevalim committed Oct 20, 2023
1 parent be88652 commit 4ece4a7
Show file tree
Hide file tree
Showing 8 changed files with 7 additions and 155 deletions.
19 changes: 2 additions & 17 deletions lib/ex_doc/formatter/epub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ defmodule ExDoc.Formatter.EPUB do
"""
@spec run([ExDoc.ModuleNode.t()], [ExDoc.ModuleNode.t()], ExDoc.Config.t()) :: String.t()
def run(project_nodes, filtered_modules, config) when is_map(config) do
parent = config.output
config = normalize_config(config)

HTML.setup_output(
parent,
&cleanup_output_dir(&1, config),
&create_output_dir(&1, config)
)
File.rm_rf!(config.output)
File.mkdir_p!(Path.join(config.output, "OEBPS"))

project_nodes =
HTML.render_all(project_nodes, filtered_modules, ".xhtml", config, highlight_tag: "samp")
Expand Down Expand Up @@ -50,16 +45,6 @@ defmodule ExDoc.Formatter.EPUB do
Path.relative_to_cwd(epub)
end

defp create_output_dir(root, config) do
File.mkdir_p!(Path.join(config.output, "OEBPS"))
File.touch!(Path.join(root, ".build"))
end

defp cleanup_output_dir(docs_root, config) do
File.rm_rf!(config.output)
create_output_dir(docs_root, config)
end

defp normalize_config(config) do
output =
config.output
Expand Down
45 changes: 5 additions & 40 deletions lib/ex_doc/formatter/html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule ExDoc.Formatter.HTML do
config = %{config | output: Path.expand(config.output)}

build = Path.join(config.output, ".build")
setup_output(config.output, &cleanup_output_dir(&1, config), &create_output_dir(&1, config))
output_setup(build, config)

project_nodes = render_all(project_nodes, filtered_modules, ".html", config, [])
extras = build_extras(config, ".html")
Expand Down Expand Up @@ -148,53 +148,18 @@ defmodule ExDoc.Formatter.HTML do
|> ExDoc.DocAST.highlight(language, opts)
end

def setup_output(root, cleanup, create) do
safety_path = Path.join(root, ".build")

cond do
File.exists?(safety_path) and File.exists?(root) ->
cleanup.(root)

not File.exists?(root) ->
create.(root)

File.ls!(root) == [] ->
add_safety_file(root)
:ok

true ->
IO.warn(
"ExDoc is outputting to an existing directory. " <>
"Beware documentation output may be mixed with other entries"
)

create.(root)
end
end

defp create_output_dir(root, _config) do
File.mkdir_p!(root)
add_safety_file(root)
end

defp add_safety_file(root) do
File.touch!(Path.join(root, ".build"))
end

defp cleanup_output_dir(docs_root, config) do
build = Path.join(docs_root, ".build")

defp output_setup(build, config) do
if File.exists?(build) do
build
|> File.read!()
|> String.split("\n", trim: true)
|> Enum.map(&Path.join(docs_root, &1))
|> Enum.map(&Path.join(config.output, &1))
|> Enum.each(&File.rm/1)

File.rm(build)
else
File.rm_rf!(docs_root)
create_output_dir(docs_root, config)
File.rm_rf!(config.output)
File.mkdir_p!(config.output)
end
end

Expand Down
38 changes: 0 additions & 38 deletions test/ex_doc/formatter/epub_io_test.exs

This file was deleted.

6 changes: 0 additions & 6 deletions test/ex_doc/formatter/html/erlang_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ defmodule ExDoc.Formatter.HTML.ErlangTest do
@moduletag :otp_eep48
@moduletag :tmp_dir

setup %{tmp_dir: tmp_dir} do
output = tmp_dir <> "/doc"
File.mkdir!(output)
File.touch!("#{output}/.build")
end

test "smoke test", c do
erlc(c, :foo, ~S"""
%% @doc
Expand Down
6 changes: 0 additions & 6 deletions test/ex_doc/formatter/html/search_data_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ defmodule ExDoc.Formatter.HTML.SearchDataTest do

@moduletag :tmp_dir

setup %{tmp_dir: tmp_dir} do
output = tmp_dir <> "/doc"
File.mkdir!(output)
File.touch!("#{output}/.build")
end

test "Elixir module", c do
modules =
elixirc(c, ~S'''
Expand Down
38 changes: 0 additions & 38 deletions test/ex_doc/formatter/html_io_test.exs

This file was deleted.

6 changes: 0 additions & 6 deletions test/ex_doc/formatter/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ defmodule ExDoc.Formatter.HTMLTest do

@moduletag :tmp_dir

setup %{tmp_dir: tmp_dir} do
output = tmp_dir <> "/html"
File.mkdir_p!(output)
File.touch!(output <> "/.build")
end

defp read_wildcard!(path) do
[file] = Path.wildcard(path)
File.read!(file)
Expand Down
4 changes: 0 additions & 4 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ defmodule TestHelper do
def elixirc(context, filename \\ "nofile", code) do
dir = context.tmp_dir

output_dir = context.tmp_dir <> "/html"
File.mkdir_p!(output_dir)
File.write!(output_dir <> "/.build", "")

src_path = Path.join([dir, filename])
src_path |> Path.dirname() |> File.mkdir_p!()
File.write!(src_path, code)
Expand Down

0 comments on commit 4ece4a7

Please sign in to comment.