|
1 | 1 | # Creating Plugins
|
2 | 2 |
|
3 |
| -A plugin is basically just a module that provides an `init/1` callback, taking a `Credo.Execution` struct as its only parameter. |
| 3 | +## Basics |
| 4 | + |
| 5 | +A plugin is basically just a module that provides an `init/1` callback, taking a `Credo.Execution` struct as its only parameter and returning a `Credo.Execution`. That's basically it. |
4 | 6 |
|
5 | 7 | ```elixir
|
6 | 8 | defmodule CredoDemoPlugin do
|
@@ -40,7 +42,43 @@ You can find more information on `Credo.Plugin` and the functions imported:
|
40 | 42 | - `Credo.Plugin.prepend_task/3`
|
41 | 43 | - `Credo.Plugin.prepend_task/4`
|
42 | 44 |
|
| 45 | +## Development |
| 46 | + |
| 47 | +Plugins are generally developed by putting them in a Hex package, referencing that package in `mix.exs` and then configuring the plugin in `.credo.exs`. |
| 48 | + |
| 49 | +However, for local development it can be beneficial to develop a plugin inside a project. |
| 50 | +But referencing modules from the current Mix project in `.credo.exs` does not work out of the box, because the project is not loaded for every `mix` task. |
| 51 | + |
| 52 | +To be able to use a module from the current project in `.credo.exs`, run `mix app.config` first: |
| 53 | + |
| 54 | +```bash |
| 55 | +mix do app.config + credo |
| 56 | +``` |
| 57 | + |
| 58 | +This way, local plugins can be referenced in `.credo.exs`. |
| 59 | + |
| 60 | +Another, even more pragmatic way to do it is to run `app.config` from `.credo.exs` directly (since it is just a script file): |
| 61 | + |
| 62 | +```elixir |
| 63 | +Mix.Task.run("app.config", []) |
| 64 | + |
| 65 | +%{ |
| 66 | + configs: [ |
| 67 | + %{ |
| 68 | + name: "default", |
| 69 | + plugins: [ |
| 70 | + {MyProject.CredoPlugin, []} |
| 71 | + ] |
| 72 | + } |
| 73 | + ] |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +This should naturally taken with a grain of salt, e.g. taking steps that this is only active during development. |
| 78 | + |
| 79 | +## Further reading |
| 80 | + |
43 | 81 | The demo plugin used in the docs can be found on GitHub and Hex:
|
44 | 82 |
|
45 | 83 | - https://github.com/rrrene/credo_demo_plugin
|
46 |
| -- https://hex.pm/packages/credo_demo_plugin |
| 84 | +- https://hex.pm/packages/credo_demo_plugin |
0 commit comments