Created
November 11, 2023 00:36
-
-
Save hauleth/8f7e25223de73eb6b7373c6adae43fb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Mix.Tasks.Man do | |
@moduledoc """ | |
Print documentation for given Elixir term. | |
""" | |
@shortdoc "Show manual for term" | |
use Mix.Task | |
require IEx.Helpers | |
@impl Mix.Task | |
def run([]), do: Mix.shell().error("What manual you want?") | |
def run([string]) do | |
function = | |
if string =~ ~r/^~[A-Za-z]$/ do | |
"~" <> rest = string | |
{Kernel, :"sigil_#{rest}"} | |
else | |
{:ok, code} = Code.string_to_quoted(string) | |
ast = IEx.Introspection.decompose(code, __ENV__) | |
{data, _} = Code.eval_quoted(ast) | |
data | |
end | |
IEx.Introspection.h(function) | |
end | |
def run(["mix", command]) do | |
Mix.Task.run("help", [command]) | |
end | |
def run(args) do | |
Mix.shell().error("Unknown term: #{join_args(args)}") | |
end | |
defp join_args(args) do | |
Enum.map_join(args, " ", fn arg -> | |
if arg =~ ~r/\s/ do | |
inspect(arg) | |
else | |
arg | |
end | |
end) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment