Run the rule from source code
Running a rule from source code is the simplest way to run a custom rule. However, rules that are compiled from source have the following limitations:
- Limited code re-use, rule must be implemented in a single source file
- No dependencies, rules can only use the Scalafix public API
- Slow, rule is re-compiled on every invocation so it's not great for interactive usage.
- No tab completion in the sbt shell, users need to manually type the path to the source file to discover the rule
To run a custom rule from source, start by installing the sbt plugin as
instructed in here. The SemanticDB compiler
plugin must be enabled to run semantic rules like
Next, open an sbt shell
You have different options to run the rule from soruce:
The easiest way to run a custom rule from source code is to use the
Another way to run a rule from source is to publish it as a gist and share the raw URL
Another way to run custom rules from source is to use the
sbt > scalafix github:olafurpg/named-literal-arguments ...
The expansion rules for
github:org/repo is the following
|(at commit |
Publish your rule to Maven Central
The most robust way to share a custom rule is to publish it as a library to
Maven Central. The diff in
step-6 shows the necessary changes to build.sbt to
populate publishing information. The
sbt-ci-release readme documents
the further steps to configure gpg and Sonatype.
Once you have your rule, users can depend on it in the sbt plugin by updating
// build.sbt scalafixDependencies in ThisBuild += "com.geirsson" %% "named-literal-arguments" % "VERSION" // sbt shell > scalafix NamedLiteralArguments
Users of the Scalafix command-line interface can use the
scalafix \ --tool-classpath $(coursier fetch com.geirsson:named-literal-arguments_2.12:VERSION) \ -r NamedLiteralArguments \ --classpath MY_PROJECT_CLASSPATH \ my-project/src/main/scala
Don't be intimidating by publishing to Maven Central, it gets easier once you've done it the first time. The benefits of publishing a rule to Maven Central are many.
- Dependencies, you can use custom library dependency to implement your rule
- Fast to run, no need to re-compile the rule on every Scalafix invocation
- Tab completion in sbt, users can tab-complete your rule when using sbt plugin j