GitHub: https://github.com/SafiBank/SaFiMono/tree/main/common/ktlint/rulesets
This library is used to add additional custom rules to ktlint and thus enforce additional code policies in projects within standard gradle build.
We are using version 0.42.1
of Ktlint.
Creating new custom rule
For new rule create class which inherits from
com.pinterest.ktlint.core.Rule
Add that class into our RuleSet provider https://github.com/SafiBank/SaFiMono/blob/main/common/ktlint/rulesets/src/main/kotlin/ph/safibank/ktlintruleset/SaFiRuleSetProvider.kt
Write rule itself. For inspiration check this blogs:
https://medium.com/@vanniktech/writing-your-first-ktlint-rule-5a1707f4ca5b
https://www.glureau.com/2020/05/26/Ktlint-Moshi-Introduction/ (this blog includes intro to auto-fix)
Create PR which will trigger common-new-ktlint-rule.yml. It checks if all gradle projects of SaFiMono, which use our ktlint-ruleset, are compliant with new rule. If there are any violations, fix them before merge!
Usage
In the project where you want to use the rules, add following repository (if not present) and dependency to build.gradle.kts:
repositories { maven { url = uri("https://asia-southeast1-maven.pkg.dev/safi-repos/safi-maven") } } dependencies { ktlintRuleset("ph.safibank.common:ktlint-rules:1.+") }
Use dynamic version in order to have always the newest rules.