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. 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 alias Sourceror.Zipper
@ -12,6 +12,7 @@ defmodule Igniter do
issues: [String.t()], issues: [String.t()],
tasks: [{String.t() | list(String.t())}], tasks: [{String.t() | list(String.t())}],
warnings: [String.t()], warnings: [String.t()],
notices: [String.t()],
assigns: map(), assigns: map(),
moves: %{optional(String.t()) => String.t()} moves: %{optional(String.t()) => String.t()}
} }
@ -157,6 +158,16 @@ defmodule Igniter do
%{igniter | warnings: List.wrap(warning) ++ igniter.warnings} %{igniter | warnings: List.wrap(warning) ++ igniter.warnings}
end 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" @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 def add_task(igniter, task, argv \\ []) when is_binary(task) do
%{igniter | tasks: igniter.tasks ++ [{task, argv}]} %{igniter | tasks: igniter.tasks ++ [{task, argv}]}
@ -590,6 +601,8 @@ defmodule Igniter do
Mix.shell().cmd("mix #{task} #{Enum.join(args, " ")}") Mix.shell().cmd("mix #{task} #{Enum.join(args, " ")}")
end) end)
display_notices(igniter)
:changes_made :changes_made
{:error, error, rewrite} -> {:error, error, rewrite} ->
@ -1059,17 +1072,29 @@ defmodule Igniter do
defp display_warnings(%{warnings: []}, _title), do: :ok defp display_warnings(%{warnings: []}, _title), do: :ok
defp display_warnings(%{warnings: warnings}, title) do 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 warnings =
|> Enum.map_join("\n --- \n", fn error -> warnings
if is_binary(error) do |> Enum.map_join("\n\n", fn error ->
"* #{IO.ANSI.yellow()}#{error}#{IO.ANSI.reset()}" if is_binary(error) do
else "* #{IO.ANSI.yellow()}#{error}#{IO.ANSI.reset()}"
"* #{IO.ANSI.yellow()}#{Exception.format(:error, error)}#{IO.ANSI.reset()}" else
end "* #{IO.ANSI.yellow()}#{Exception.format(:error, error)}#{IO.ANSI.reset()}"
end) end
|> Mix.shell().info() 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 end
defp display_moves(%{moves: moves}) when moves == %{}, do: :ok defp display_moves(%{moves: moves}) when moves == %{}, do: :ok