-
Notifications
You must be signed in to change notification settings - Fork 413
/
format_delegator.ex
70 lines (56 loc) · 1.65 KB
/
format_delegator.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
defmodule Credo.CLI.Output.FormatDelegator do
@moduledoc """
This module can be used to easily delegate print-statements for different
formats to different modules.
Example:
use Credo.CLI.Output.FormatDelegator,
default: Credo.CLI.Command.Suggest.Output.Default,
flycheck: Credo.CLI.Command.Suggest.Output.FlyCheck,
oneline: Credo.CLI.Command.Suggest.Output.Oneline,
json: Credo.CLI.Command.Suggest.Output.Json
"""
@doc false
defmacro __using__(format_list) do
format_mods =
Enum.map(format_list, fn {format, output_mod} ->
case format do
:default ->
quote do
end
_ ->
quote do
defp format_mod(%Execution{format: unquote(to_string(format))}) do
unquote(output_mod)
end
end
end
end)
default_format_mod =
Enum.map(format_list, fn {format, output_mod} ->
case format do
:default ->
quote do
defp format_mod(_) do
unquote(output_mod)
end
end
_ ->
quote do
end
end
end)
quote do
alias Credo.Execution
def print_before_info(source_files, exec) do
format_mod = format_mod(exec)
format_mod.print_before_info(source_files, exec)
end
def print_after_info(source_files, exec, time_load, time_run) do
format_mod = format_mod(exec)
format_mod.print_after_info(source_files, exec, time_load, time_run)
end
unquote(format_mods)
unquote(default_format_mod)
end
end
end