dialyzer.ex (3349B)
1 defmodule Dialyxir.Dialyzer do 2 import Dialyxir.Output, only: [color: 2, info: 1] 3 alias String.Chars 4 alias Dialyxir.Formatter 5 alias Dialyxir.Project 6 alias Dialyxir.FilterMap 7 8 defmodule Runner do 9 @dialyxir_args [ 10 :raw, 11 :format, 12 :list_unused_filters, 13 :ignore_exit_status 14 ] 15 16 def run(args, filterer) do 17 try do 18 {split, args} = Keyword.split(args, @dialyxir_args) 19 20 formatter = 21 cond do 22 split[:format] == "dialyzer" -> 23 Dialyxir.Formatter.Dialyzer 24 25 split[:format] == "dialyxir" -> 26 Dialyxir.Formatter.Dialyxir 27 28 split[:format] == "github" -> 29 Dialyxir.Formatter.Github 30 31 split[:format] == "ignore_file" -> 32 Dialyxir.Formatter.IgnoreFile 33 34 split[:format] == "raw" -> 35 Dialyxir.Formatter.Raw 36 37 split[:format] == "short" -> 38 Dialyxir.Formatter.Short 39 40 split[:raw] -> 41 Dialyxir.Formatter.Raw 42 43 true -> 44 Dialyxir.Formatter.Dialyxir 45 end 46 47 info("Starting Dialyzer") 48 49 args 50 |> inspect(label: "dialyzer args", pretty: true, limit: 8) 51 |> info 52 53 {duration_us, result} = :timer.tc(&:dialyzer.run/1, [args]) 54 55 formatted_time_elapsed = Formatter.formatted_time(duration_us) 56 57 filter_map_args = FilterMap.to_args(split) 58 59 case Formatter.format_and_filter(result, filterer, filter_map_args, formatter) do 60 {:ok, formatted_warnings, :no_unused_filters} -> 61 {:ok, {formatted_time_elapsed, formatted_warnings, ""}} 62 63 {:warn, formatted_warnings, {:unused_filters_present, formatted_unnecessary_skips}} -> 64 {:ok, {formatted_time_elapsed, formatted_warnings, formatted_unnecessary_skips}} 65 66 {:error, _formatted_warnings, {:unused_filters_present, formatted_unnecessary_skips}} -> 67 {:error, {"unused filters present", formatted_unnecessary_skips}} 68 end 69 catch 70 {:dialyzer_error, msg} -> 71 {:error, ":dialyzer.run error: " <> Chars.to_string(msg)} 72 end 73 end 74 end 75 76 @success_return_code 0 77 @warning_return_code 2 78 @error_return_code 1 79 80 def dialyze(args, runner \\ Runner, filterer \\ Project) do 81 case runner.run(args, filterer) do 82 {:ok, {time, [], formatted_unnecessary_skips}} -> 83 {:ok, @success_return_code, [time, formatted_unnecessary_skips, success_msg()]} 84 85 {:ok, {time, result, formatted_unnecessary_skips}} -> 86 warnings = Enum.map(result, &color(&1, :red)) 87 88 {:warn, @warning_return_code, 89 [time] ++ warnings ++ [formatted_unnecessary_skips, warnings_msg()]} 90 91 {:warn, {time, result, formatted_unnecessary_skips}} -> 92 warnings = Enum.map(result, &color(&1, :red)) 93 94 {:warn, @warning_return_code, 95 [time] ++ warnings ++ [formatted_unnecessary_skips, warnings_msg()]} 96 97 {:error, {msg, formatted_unnecessary_skips}} -> 98 {:error, @error_return_code, [color(formatted_unnecessary_skips, :red), color(msg, :red)]} 99 100 {:error, msg} -> 101 {:error, @error_return_code, [color(msg, :red)]} 102 end 103 end 104 105 defp success_msg, do: color("done (passed successfully)", :green) 106 107 defp warnings_msg, do: color("done (warnings were emitted)", :yellow) 108 end