Skip to content

Add --open flag #1493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions lib/mix/tasks/docs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ defmodule Mix.Tasks.Docs do
* `--output`, `-o` - Output directory for the generated
docs, default: `"doc"`

* `--open` - open browser window pointed to the documentation

The command line options have higher precedence than the options
specified in your `mix.exs` file below.

Expand All @@ -32,17 +34,21 @@ defmodule Mix.Tasks.Docs do
from ExDoc, for example:

def project do
[app: :my_app,
version: "0.1.0-dev",
deps: deps(),

# Docs
name: "My App",
source_url: "https://github.com/USER/PROJECT",
homepage_url: "http://YOUR_PROJECT_HOMEPAGE",
docs: [main: "MyApp", # The main page in the docs
logo: "path/to/logo.png",
extras: ["README.md"]]]
[
app: :my_app,
version: "0.1.0-dev",
deps: deps(),

# Docs
name: "My App",
source_url: "https://github.com/USER/PROJECT",
homepage_url: "http://YOUR_PROJECT_HOMEPAGE",
docs: [
main: "MyApp", # The main page in the docs
logo: "path/to/logo.png",
extras: ["README.md"]
]
]
end

ExDoc also allows configuration specific to the documentation to
Expand Down Expand Up @@ -301,7 +307,8 @@ defmodule Mix.Tasks.Docs do
canonical: :string,
formatter: :keep,
language: :string,
output: :string
output: :string,
open: :boolean
]

@aliases [n: :canonical, f: :formatter, o: :output]
Expand Down Expand Up @@ -351,6 +358,11 @@ defmodule Mix.Tasks.Docs do
for formatter <- get_formatters(options) do
index = generator.(project, version, Keyword.put(options, :formatter, formatter))
Mix.shell().info([:green, "View #{inspect(formatter)} docs at #{inspect(index)}"])

if cli_opts[:open] do
browser_open(index)
end

index
end
end
Expand Down Expand Up @@ -465,4 +477,15 @@ defmodule Mix.Tasks.Docs do
options
end
end

defp browser_open(url) do
{cmd, args} =
case :os.type() do
{:win32, _} -> {"cmd", ["/c", "start", url]}
{:unix, :darwin} -> {"open", [url]}
{:unix, _} -> {"xdg-open", [url]}
end

System.cmd(cmd, args)
end
end