Share an Elixir or dev-env tip a day

I love using mix hex.docs and often need them offline. So I set up a helper to fetch project docs for any dependencies as follows:

in ~/elixir_utils/docs_loader.exs I put this little code block in:

defmodule OfflineSetupUtils do
  def run_mix_task(args, opts \\ []) do
    IO.puts("Running Mix Command: mix #{Enum.join(args, " ")}")
    {output, exit_status} = System.cmd("mix", args, opts)
    IO.puts(output)
    IO.puts("Finished mix task with exit code #{exit_status}\n\n")
  end
end

Mix.Project.config()
|> Keyword.get(:deps)
|> Enum.map(fn dep ->
  Tuple.to_list(dep)
  |> List.first()
  |> Atom.to_string()
end)
|> Enum.each(fn name ->
  OfflineSetupUtils.run_mix_task(["hex.docs", "fetch", name])
end)

then when I’m in a mix project directory, run iex -S mix to get an iex session scoped to the mix project, then run Code.eval_file(System.user_home!() <> “/elixir_utils/docs_loader.exs”), and watch it pull all the docs at the correct versions for me.

7 Likes