Skip to content

Commit

Permalink
Add example/kotlinlib/linting
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnm committed Sep 15, 2024
1 parent cf3af72 commit 9d83320
Show file tree
Hide file tree
Showing 7 changed files with 964 additions and 0 deletions.
76 changes: 76 additions & 0 deletions example/kotlinlib/linting/1-detekt/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package build

import mill._, kotlinlib._
import mill.util.Jvm
import mill.api.Loose

object `package` extends RootModule with KotlinModule {

def kotlinVersion = "1.9.24"

// TODO extract this as Detekt module, similar to CheckstyleModule
/**
* Runs [[https://detekt.dev/docs/gettingstarted/cli Detekt]]
*/
def detekt(): Command[Unit] = T.command {
val exitCode = detekt0()()

detektHandleErrors(exitCode)
}

private def detekt0() = T.task {

val args = Seq("-i", PathRef(T.workspace).path.toString()) ++
Seq("-c", PathRef(T.workspace / "detekt-config.yml").path.toString())

T.log.info("running detekt ...")
T.log.debug(s"with $args")

Jvm.callSubprocess(
mainClass = "io.gitlab.arturbosch.detekt.cli.Main",
classPath = detektClasspath().map(_.path),
mainArgs = args,
workingDir = millSourcePath, // allow passing relative paths for sources like src/a/b
streamOut = true,
check = false
).exitCode
}

private def detektHandleErrors(exitCode: Int)(implicit ctx: mill.api.Ctx) = {

if (exitCode == 0) {} // do nothing
else if (exitCode == 2) {
throw new RuntimeException("detekt: Max issues was reached ")
} else if (exitCode == 3) {
throw new RuntimeException("detekt: Invalid configuration file detected")
}
else {
throw new RuntimeException(s"detekt exited abnormally with exit code = $exitCode")
}
}

/**
* Classpath for running Dekekt.
*/
private def detektClasspath: T[Loose.Agg[PathRef]] = T {
defaultResolver().resolveDeps(
Agg(ivy"io.gitlab.arturbosch.detekt:detekt-cli:${detektVersion()}")
)
}

/**
* Detekt version.
*/
private def detektVersion: T[String] = T {
"1.23.7"
}
}

/** See Also: src/example/Foo.kt */

/** Usage

> ./mill detekt
error: ...Foo.kt:5:5: Function main is nested too deeply. [NestedBlockDepth]

*/
Loading

0 comments on commit 9d83320

Please sign in to comment.