Credo.Plugin.append_task

You're seeing just the function append_task, go back to Credo.Plugin module for more information.
Link to this function

append_task(exec, group_name, task_mod)

View Source

Appends a Credo.Execution.Task module to Credo's main execution pipeline.

Credo's execution pipeline consists of several steps, each with a group of tasks, which you can hook into.

Appending tasks to these steps is easy:

# credo_demo_plugin.ex
defmodule CredoDemoPlugin do
  import Credo.Plugin

  def init(exec) do
    append_task(exec, :set_default_command, CredoDemoPlugin.SetDemoAsDefaultCommand)
  end
end

Note how Credo.Plugin.append_task/3 takes two arguments after the Credo.Execution struct: the name of the group to be modified and the module that should be executed.

The group names of Credo's main pipeline are:

  • :parse_cli_options
  • :initialize_plugins
  • :determine_command
  • :set_default_command
  • :initialize_command
  • :parse_cli_options_final
  • :validate_cli_options
  • :convert_cli_options_to_config
  • :resolve_config
  • :validate_config
  • :run_command
  • :halt_execution

The module appended to these groups should use Credo.Execution.Task:

# credo_demo_plugin/set_demo_as_default_command.ex
defmodule CredoDemoPlugin.SetDemoAsDefaultCommand do
  use Credo.Execution.Task

  alias Credo.CLI.Options

  def call(exec, _opts) do
    set_command(exec, exec.cli_options.command || "demo")
  end

  defp set_command(exec, command) do
    %Execution{exec | cli_options: %Options{exec.cli_options | command: command}}
  end
end

This example would have the effect that typing mix credo would no longer run the built-in Suggest command, but rather our plugin's Demo command.

Link to this function

append_task(exec, pipeline_key, group_name, task_mod)

View Source

Appends a Credo.Execution.Task module to the execution pipeline of an existing Command.

Credo's commands can also have an execution pipeline of their own, which is executed when the command is used and which you can hook into as well.

Appending tasks to these steps is easy:

# credo_demo_plugin.ex
defmodule CredoDemoPlugin do
  import Credo.Plugin

  def init(exec) do
    append_task(exec, Credo.CLI.Command.Suggest.SuggestCommand, :print_after_analysis, CredoDemoPlugin.WriteFile)
  end
end

Note how Credo.Plugin.append_task/4 takes three arguments after the Credo.Execution struct: the pipeline and the name of the group to be modified and the module that should be executed.

Here are the pipeline keys and group names:

  • Credo.CLI.Command.Suggest.SuggestCommand (run via mix credo suggest)

    • :load_and_validate_source_files
    • :prepare_analysis
    • :print_before_analysis
    • :run_analysis
    • :filter_issues
    • :print_after_analysis
  • Credo.CLI.Command.List.ListCommand (run via mix credo list)

    • :load_and_validate_source_files
    • :prepare_analysis
    • :print_before_analysis
    • :run_analysis
    • :filter_issues
    • :print_after_analysis
  • Credo.CLI.Command.Diff.DiffCommand (run via mix credo diff)

    • :load_and_validate_source_files
    • :prepare_analysis
    • :print_previous_analysis
    • :run_analysis
    • :filter_issues
    • :print_after_analysis
    • :filter_issues_for_exit_status
  • Credo.CLI.Command.Info.InfoCommand (run via mix credo info)

    • :load_and_validate_source_files
    • :prepare_analysis
    • :print_info

The module appended to these groups should use Credo.Execution.Task:

# credo_demo_plugin/write_file.ex
defmodule CredoDemoPlugin.WriteFile do
  use Credo.Execution.Task

  alias Credo.CLI.Options

  def call(exec, _opts) do
    issue_count = exec |> Execution.get_issues() |> Enum.count
    File.write!("demo.json", ~q({"issue_count": #{issue_count}}))

    exec
  end
end

This example would have the effect that running mix credo suggest would write the issue count in a JSON file.