mirror of
https://github.com/ash-project/igniter.git
synced 2024-09-19 21:12:54 +12:00
improvement: support "notices" (#65)
This commit is contained in:
parent
f53e4e97ac
commit
fa5f849928
1 changed files with 36 additions and 11 deletions
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue