|
| 1 | +# lint_roller - A plugin specification for linters |
| 2 | + |
| 3 | +`lint_roller` is an itty-bitty plugin API for code analysis tools like linters |
| 4 | +and formatters. It provides plugins for those tools to load extensions and |
| 5 | +specify custom rulesets. |
| 6 | + |
| 7 | +(As of this release, only [Standard |
| 8 | +Ruby](https://github.com/standardrb/standard) supports `lint_roller` plugins, |
| 9 | +but we think [RuboCop](https://github.com/rubocop/rubocop) could add support and |
| 10 | +most plugins would be compatible with both `rubocop` and `standardrb`. |
| 11 | +Additionally, there's nothing that would prevent other tools like |
| 12 | +[rufo](https://github.com/ruby-formatter/rufo) from adopting it.) |
| 13 | + |
| 14 | +## How to make a plugin |
| 15 | + |
| 16 | +If you want to make a plugin, the first thing you should do is extend |
| 17 | +[LintRoller::Plugin](/lib/lint_roller/plugin.rb) with a custom class. Let's take |
| 18 | +this example plugin for banana-related static analysis: |
| 19 | + |
| 20 | +```ruby |
| 21 | +module BananaRoller |
| 22 | + class Plugin |
| 23 | + # `config' is a Hash of options passed to the plugin by the user |
| 24 | + def initialize(config = {}) |
| 25 | + @alternative = config["alternative"] ||= "chocolate" |
| 26 | + end |
| 27 | + |
| 28 | + def about |
| 29 | + LintRoller::About.new( |
| 30 | + name: "banana_roller", |
| 31 | + version: "1.0", # or, in a gem, probably BananaRoller::VERSION |
| 32 | + homepage: "https://github.com/example/banana_roller", |
| 33 | + description: "Configuration of banana-related code" |
| 34 | + ) |
| 35 | + end |
| 36 | + |
| 37 | + # `context' is an instance of LintRoller::Context provided by the runner |
| 38 | + def supported?(context) |
| 39 | + if context.engine == :rubocop |
| 40 | + true |
| 41 | + else |
| 42 | + false |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + # `context' is an instance of LintRoller::Context provided by the runner |
| 47 | + def rules(context) |
| 48 | + LintRoller::Rules.new( |
| 49 | + type: :path, |
| 50 | + config_format: :rubocop, |
| 51 | + value: Pathname.new(__dir__).join("../../config/default.yml") |
| 52 | + ) |
| 53 | + end |
| 54 | + end |
| 55 | +end |
| 56 | +``` |
| 57 | + |
| 58 | +And that's pretty much it. Just a declarative way to identify your plugin, |
| 59 | +detect whether it supports the given context (e.g. the current `runner` and |
| 60 | +`engine` and their respective `_version`s), and then provides your custom |
| 61 | +configuration as a [LintRoller::Rules](lib/lint_roller/rules.rb) object. |
| 62 | + |
| 63 | +## Packaging a plugin in a gem |
| 64 | + |
| 65 | +In order for a formatter to use your plugin, it needs to know where to what path |
| 66 | +to require and the name of your plugin class to instantiate and invoke. |
| 67 | + |
| 68 | +To make this work seamlessly for your users without additional configuration of |
| 69 | +their own, all you need to do is specify a metadata attribute called |
| 70 | +`default_lint_roller_plugin` in your gemspec. |
| 71 | + |
| 72 | +Taking [standard-custom](https://github.com/standardrb/standard-custom) as an |
| 73 | +example, its gemspec contains: |
| 74 | + |
| 75 | +```ruby |
| 76 | +Gem::Specification.new do |spec| |
| 77 | + # … |
| 78 | + spec.metadata["default_lint_roller_plugin"] = "Standard::Custom::Plugin" |
| 79 | + # … |
| 80 | +end |
| 81 | +``` |
| 82 | + |
| 83 | +Because gem specs are loaded by RubyGems and Bundler very early, remember to |
| 84 | +specify the plugin as a string representation of the constant, as load order |
| 85 | +usually matters, and most tools will need to be loaded before any custom |
| 86 | +extensions. Hence, `"Standard::Custom::Plugin"` instead of |
| 87 | +`Standard::Custom::Plugin`. |
| 88 | + |
| 89 | +## Using your plugin |
| 90 | + |
| 91 | +Once you've made your plugin, here's how it's configured from a Standard Ruby |
| 92 | +`.standard.yml` file. |
| 93 | + |
| 94 | +### If your plugin is packaged as a gem |
| 95 | + |
| 96 | +Packaging your plugin in a gem is the golden path, both because distributing |
| 97 | +code via [RubyGems.org](https://rubygems.org) is very neat, but also because it |
| 98 | +makes the least work for your users. |
| 99 | + |
| 100 | +If your gem name is `banana_roller` and you've set |
| 101 | +`spec.metadata["default_lint_roller_plugin"]` to `"BananaRoller::Plugin"`, then |
| 102 | +your users could just add this to their `.standard.yml` file: |
| 103 | + |
| 104 | +```yaml |
| 105 | +plugins: |
| 106 | + - banana_roller |
| 107 | +``` |
| 108 | +
|
| 109 | +And that's it! During initialization, `standardrb` will `require |
| 110 | +"banana_roller"` and know to call `BananaRoller::Plugin.new(config)` to |
| 111 | +instantiate it. |
| 112 | + |
| 113 | +### If your plugin ISN'T in a gem |
| 114 | + |
| 115 | +If you're developing a plugin for internal use or in conjunction with a single |
| 116 | +project, you may want it to live in the same repo as opposed to packaging it in |
| 117 | +a gem. |
| 118 | + |
| 119 | +To do this, then—in lieu of a gem name—provide the path you want to be required |
| 120 | +as its name, and (since there is no `spec.metadata` to learn of your plugin's |
| 121 | +class name), specify it as an option on the plugin: |
| 122 | + |
| 123 | +```yaml |
| 124 | +plugins: |
| 125 | + - lib/banana_roller/plugin: |
| 126 | + plugin_class_name: BananaRoller::Plugin |
| 127 | +``` |
| 128 | + |
| 129 | +(Be careful with the indentation here! Any configuration under a plugin must be |
| 130 | +indented in order for it to be parsed as a hash under the |
| 131 | +`"lib/banana_roller/plugin"` key.) |
| 132 | + |
| 133 | +Additionally, if you want the plugin's name to make more sense, you can give |
| 134 | +it whatever name you like in the configuration and specify the `require_path` |
| 135 | +explicitly: |
| 136 | + |
| 137 | +```yaml |
| 138 | +plugins: |
| 139 | + - banana_roller: |
| 140 | + require_path: lib/banana_roller/plugin |
| 141 | + plugin_class_name: BananaRoller::Plugin |
| 142 | +``` |
| 143 | + |
| 144 | +### Passing user configuration to the plugin |
| 145 | + |
| 146 | +When a `LintRoller::Plugin` is instantiated, users can pass a configuration hash |
| 147 | +that tells your plugin how to behave. |
| 148 | + |
| 149 | +To illustrate how this works in Standard Ruby, anything passed as a hash beneath |
| 150 | +a plugin will be passed to the class's `initialize` method: |
| 151 | + |
| 152 | +```yaml |
| 153 | +plugins: |
| 154 | + - apple_roller |
| 155 | + - banana_roller: |
| 156 | + require_path: lib/banana_roller/plugin |
| 157 | + plugin_class_name: BananaRoller::Plugin |
| 158 | + alternative: "apples" |
| 159 | + - orange_roller: |
| 160 | + rind: false |
| 161 | +``` |
| 162 | + |
| 163 | +In the above case, `apple_roller`'s plugin class will be instantiated with |
| 164 | +`new({})`, `banana_roller` will get all 3 of those parameters passed |
| 165 | +`BananaRoller::Plugin.new({require_path…})`, and `orange_roller`'s class will be |
| 166 | +called with `new({rind: false})`. |
| 167 | + |
| 168 | +## Code of Conduct |
| 169 | + |
| 170 | +This project follows Test Double's [code of |
| 171 | +conduct](https://testdouble.com/code-of-conduct) for all community interactions, |
| 172 | +including (but not limited to) one-on-one communications, public posts/comments, |
| 173 | +code reviews, pull requests, and GitHub issues. If violations occur, Test Double |
| 174 | +will take any action they deem appropriate for the infraction, up to and |
| 175 | +including blocking a user from the organization's repositories. |
| 176 | + |
0 commit comments