WartRemover Gradle Plugin
Index
Purpose
The WartRemover Gradle Plugin is a plugin for Gradle whose goal is to simplify the configuration of the code linter WartRemover in Scala 3 projects.
The WartRemover Gradle Plugin relies on the WartRemover Compiler Plugin for the Scala 3 compiler, which is used to search the user’s code for warts during compilation.
This plugin offers two basic services:
-
It automatically applies the WartRemover Compiler Plugin to the Scala 3 compiler used by Gradle.
Note: this service requires the user to apply the Scala Gradle Plugin to his Gradle project, so that this plugin can configure the Scala 3 compiler. Moreover, it requires the user to provide a Scala 3 dependency supported by the WartRemover Compiler Plugin (e.g.
org.scala-lang:scala3-library_3:3.2.2
). -
It can convert proper HOCON configuration files into compiler options for the WartRemover Compiler Plugin.
Implementation
The implementation of this plugin is described by the following UML Class Diagram, which will be explained in the following sections.
WartRemoverPlugin
The WartRemoverPlugin
is a org.gradle.api.Plugin
, meaning that it can be applied to a Gradle project as any other Gradle plugin.
When applied, the WartRemoverPlugin
does the following:
- Provide an extension called
wartremover
, whose purpose is to configure this plugin; - After the project evaluation:
-
Add the WartRemover Compiler Plugin to the project dependencies, applying it to the Scala 3 compiler.
The version of the WartRemover Compiler Plugin is computed from the version of the Scala 3 dependency provided by the user;
- Configure the WartRemover Compiler Plugin using the HOCON configuration file provided by the user;
- Provide a task called
wartRemoverCompilerOptions
, whose purpose is to print to the console the compiler options provided by the user.
-
WartRemoverExtension
The wartremover
extension is modelled by the class WartRemoverExtension
, which provides the following methods:
configFile
: set the path to the HOCON configuration file used to compute the compiler options for the WartRemover Compiler Plugin. By default, this is set to the.wartremover.conf
file within the root directory of the user’s project.compilerOptions
: parse the HOCON configuration file provided by the user, producing the compiler options for the WartRemover Compiler Plugin. If no configuration file is provided, it produces the default compiler options.
These methods can be accessed also by the user as follows:
// build.gradle.kts
wartremover {
configFile(".custom-wartremover.conf")
val options = compilerOptions()
}
The compiler options for the WartRemover Compiler Plugin are modelled by the class WartRemoverCompilerConfiguration
, which is a type of CompilerConfiguration
.
A CompilerConfiguration
is a list of compiler options that can be applied to a certain compiler. In particular, a WartRemoverCompilerConfiguration
is a set of compiler options to be applied to the Scala 3 compiler for configuring the WartRemover Compiler Plugin.
A WartRemoverCompilerConfiguration
can be defined as a map from wart identifiers to TraverserType
s, where a TraverserType
defines the threat level assigned to a wart when it is found during compilation.
When calling its method toCompilerOptions
, the WartRemoverCompilerConfiguration
produces a list of compiler options for configuring the WartRemover Compiler Plugin, computed from its internal map.
These compiler options are finally applied by the WartRemoverPlugin
to the Scala 3 compiler, after the evaluation of the project.
Tasks
-
wartRemoverCompilerOptions
The task
wartRemoverCompilerOptions
is a type ofPrintTask
, where aPrintTask
is a task which takes a text as input to be printed to the console when called by the user.In particular, the task
wartRemoverCompilerOptions
is defined by theWartRemoverPlugin
after the project evaluation, in order to print the compiler options computed for configuring the WartRemover Compiler Plugin.
Testing
The plugin has been tested using the Gradle TestKit on a build file defined specifically for testing purposes.
The goal of the provided tests was mainly to verify that this plugin could be applied to a Gradle project and that the compiler options for the WartRemover Compiler Plugin were computed correctly from the HOCON configuration file provided by the user.
The plugin has also been manually tested against several Scala 3 projects.
Note: the test suite should be extended to verify that all warts are identified correctly within some sample Scala 3 projects.