Skip to content

Commit

Permalink
Add --open flag (#1493)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach authored Jan 18, 2022
1 parent b65a1a6 commit ca25e85
Showing 1 changed file with 35 additions and 12 deletions.
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

0 comments on commit ca25e85

Please sign in to comment.