improvement: support "notices" (#65)

This commit is contained in:
Igor Barakaiev 2024-08-04 00:52:09 +03:00 committed by GitHub
parent f53e4e97ac
commit fa5f849928
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,7 +3,7 @@ defmodule Igniter do
Tools for generating and patching code into an Elixir project.
"""
defstruct [:rewrite, issues: [], tasks: [], warnings: [], assigns: %{}, moves: %{}]
defstruct [:rewrite, issues: [], tasks: [], warnings: [], notices: [], assigns: %{}, moves: %{}]
alias Sourceror.Zipper
@ -12,6 +12,7 @@ defmodule Igniter do
issues: [String.t()],
tasks: [{String.t() | list(String.t())}],
warnings: [String.t()],
notices: [String.t()],
assigns: map(),
moves: %{optional(String.t()) => String.t()}
}
@ -157,6 +158,16 @@ defmodule Igniter do
%{igniter | warnings: List.wrap(warning) ++ igniter.warnings}
end
@doc "Adds a notice to the notices list. Notices are displayed to the user once the igniter finishes running."
@spec add_notice(t, String.t()) :: t()
def add_notice(igniter, notice) do
if notice in igniter.notices do
igniter
else
%{igniter | notices: [notice] ++ igniter.notices}
end
end
@doc "Adds a task to the tasks list. Tasks will be run after all changes have been commited"
def add_task(igniter, task, argv \\ []) when is_binary(task) do
%{igniter | tasks: igniter.tasks ++ [{task, argv}]}
@ -590,6 +601,8 @@ defmodule Igniter do
Mix.shell().cmd("mix #{task} #{Enum.join(args, " ")}")
end)
display_notices(igniter)
:changes_made
{:error, error, rewrite} ->
@ -1059,17 +1072,29 @@ defmodule Igniter do
defp display_warnings(%{warnings: []}, _title), do: :ok
defp display_warnings(%{warnings: warnings}, title) do
Mix.shell().info("\n#{title} - #{IO.ANSI.yellow()}Notices:#{IO.ANSI.reset()}\n")
Mix.shell().info("\n#{title} - #{IO.ANSI.yellow()}Warnings:#{IO.ANSI.reset()}\n")
warnings
|> Enum.map_join("\n --- \n", fn error ->
if is_binary(error) do
"* #{IO.ANSI.yellow()}#{error}#{IO.ANSI.reset()}"
else
"* #{IO.ANSI.yellow()}#{Exception.format(:error, error)}#{IO.ANSI.reset()}"
end
end)
|> Mix.shell().info()
warnings =
warnings
|> Enum.map_join("\n\n", fn error ->
if is_binary(error) do
"* #{IO.ANSI.yellow()}#{error}#{IO.ANSI.reset()}"
else
"* #{IO.ANSI.yellow()}#{Exception.format(:error, error)}#{IO.ANSI.reset()}"
end
end)
Mix.shell().info(warnings <> "\n\n")
end
defp display_notices(igniter) do
notices =
igniter.notices
|> Enum.map_join("\n\n", fn notice ->
"#{IO.ANSI.green()}#{notice}#{IO.ANSI.reset()}"
end)
Mix.shell().info("\n" <> notices)
end
defp display_moves(%{moves: moves}) when moves == %{}, do: :ok