Skip to content

Commit

Permalink
Use docs output if set in project's config
Browse files Browse the repository at this point in the history
Closes #1025
  • Loading branch information
pnezis committed May 15, 2024
1 parent 39ebd09 commit d51162e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/mix/tasks/hex.publish.ex
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,12 @@ defmodule Mix.Tasks.Hex.Publish do
end

defp docs_dir do
docs_output = docs_output_from_config(Mix.Project.config()[:docs])

cond do
docs_output != nil ->
docs_output

File.exists?("doc") ->
"doc"

Expand All @@ -483,6 +488,12 @@ defmodule Mix.Tasks.Hex.Publish do
end
end

defp docs_output_from_config(nil), do: nil
defp docs_output_from_config(config) when is_list(config), do: config[:output]

defp docs_output_from_config(config) when is_function(config, 0),
do: Keyword.get(config.(), :output)

defp create_release(build, organization, auth, opts) do
meta = build.meta
%{tarball: tarball, outer_checksum: checksum} = Hex.Tar.create!(meta, meta.files, :memory)
Expand Down
64 changes: 64 additions & 0 deletions test/mix/tasks/hex.publish_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,40 @@ defmodule Mix.Tasks.Hex.PublishTest do
end
end

defmodule DocsOutputConfigured.MixProject do
def project do
[
app: :ex_doc,
version: "0.1.0",
aliases: [docs: [&docs/1]],
docs: [output: "my_docs"]
]
end

defp docs(_) do
File.mkdir_p!("my_docs")
File.write!("my_docs/index.html", "the index")
end
end

defmodule DocsOutputConfiguredFunction.MixProject do
def project do
[
app: :ex_doc,
version: "0.1.0",
aliases: [docs: [&docs/1]],
docs: &docs_config/0
]
end

defp docs(_) do
File.mkdir_p!("my_docs")
File.write!("my_docs/index.html", "the index")
end

defp docs_config, do: [output: "my_docs"]
end

test "ensure user exists" do
Process.put(:hex_test_app_name, :publish_ensure_user_exists)
Mix.Project.push(ReleaseSimple.MixProject)
Expand Down Expand Up @@ -211,6 +245,36 @@ defmodule Mix.Tasks.Hex.PublishTest do
end)
end

test "publishes docs with different output configured" do
Mix.Project.push(DocsOutputConfigured.MixProject)

in_tmp(fn ->
set_home_tmp()
setup_auth("user", "hunter42")

send(self(), {:mix_shell_input, :prompt, "hunter42"})
Mix.Tasks.Hex.Publish.run(["docs", "--no-progress", "--replace"])

assert_received {:mix_shell, :info,
["Docs published to http://localhost:4043/docs/ex_doc-0.1.0.tar.gz"]}
end)
end

test "publishes docs when docs config is a function" do
Mix.Project.push(DocsOutputConfiguredFunction.MixProject)

in_tmp(fn ->
set_home_tmp()
setup_auth("user", "hunter42")

send(self(), {:mix_shell_input, :prompt, "hunter42"})
Mix.Tasks.Hex.Publish.run(["docs", "--no-progress", "--replace"])

assert_received {:mix_shell, :info,
["Docs published to http://localhost:4043/docs/ex_doc-0.1.0.tar.gz"]}
end)
end

test "docs when package is not published yet" do
Mix.Project.push(DocsError.MixProject)

Expand Down

0 comments on commit d51162e

Please sign in to comment.