Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify exact files to process in ktlint run #4201

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions kotlinlib/src/mill/kotlinlib/ktlint/KtlintModule.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package mill.kotlinlib.ktlint

import mainargs.arg
import mill._
import mill.api.{Loose, PathRef}
import mill.define.{Discover, ExternalModule}
import mill.javalib.JavaModule
import mill.kotlinlib.DepSyntax
import mill.main.Tasks
import mill.util.Jvm

/**
Expand All @@ -16,7 +18,13 @@ trait KtlintModule extends JavaModule {
* Runs [[https://pinterest.github.io/ktlint/latest/install/integrations/ Ktlint]]
*/
def ktlint(@mainargs.arg ktlintArgs: KtlintArgs): Command[Unit] = Task.Command {
KtlintModule.ktlintAction(ktlintArgs, ktlintConfig(), ktlintOptions(), ktlintClasspath())
KtlintModule.ktlintAction(
ktlintArgs,
sources(),
ktlintConfig(),
ktlintOptions(),
ktlintClasspath()
)
}

/**
Expand Down Expand Up @@ -57,15 +65,14 @@ object KtlintModule extends ExternalModule with KtlintModule with TaskModule {

/**
* Reformats Kotlin source files.
*
* @param check if an exception should be raised when formatting errors are found
* - when set, files are not formatted
*/
def reformatAll(
check: mainargs.Flag = mainargs.Flag(value = false)
@arg(positional = true) sources: Tasks[Seq[PathRef]] =
Tasks.resolveMainDefault("__.sources")
): Command[Unit] = Task.Command {
ktlintAction(
KtlintArgs(format = true, check = check.value),
KtlintArgs(format = true, check = true),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a guess, but isn't this a typo? Shouldn't this be

KtlintArgs(format = true, check = false),

@0xnm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, because check flag controls exit code handling (see

if (ktlintArgs.check) {
throw new RuntimeException(s"ktlint exited abnormally with exit code = $exitCode")
} else {
ctx.log.error(s"ktlint exited abnormally with exit code = $exitCode")
}
). So if format succeeds, exit code will be 0, but we still want to throw exception if something went wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, cool, thank :)

T.sequence(sources.value)().flatten,
ktlintConfig(),
ktlintOptions(),
ktlintClasspath()
Expand All @@ -74,15 +81,14 @@ object KtlintModule extends ExternalModule with KtlintModule with TaskModule {

/**
* Checks the Kotlin source files formatting without reformatting the files.
*
* @param check if an exception should be raised when formatting errors are found
* - when set, files are not formatted
*/
def checkFormatAll(
check: mainargs.Flag = mainargs.Flag(value = false)
@arg(positional = true) sources: Tasks[Seq[PathRef]] =
Tasks.resolveMainDefault("__.sources")
): Command[Unit] = Task.Command {
ktlintAction(
KtlintArgs(format = false, check = check.value),
KtlintArgs(format = false, check = false),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a guess, but isn't this a typo? Shouldn't this be

KtlintArgs(format = false, check = true),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, it is a copy-paste typo here. #4247

T.sequence(sources.value)().flatten,
ktlintConfig(),
ktlintOptions(),
ktlintClasspath()
Expand All @@ -91,6 +97,7 @@ object KtlintModule extends ExternalModule with KtlintModule with TaskModule {

private def ktlintAction(
ktlintArgs: KtlintArgs,
filesToFormat: Seq[PathRef],
config: Option[PathRef],
options: Seq[String],
classPath: Loose.Agg[PathRef]
Expand All @@ -111,12 +118,15 @@ object KtlintModule extends ExternalModule with KtlintModule with TaskModule {
args ++= options
args ++= configArgument
args ++= formatArgument
args ++= filesToFormat.map(_.path)
.filter(f => os.exists(f) && (f.ext == "kt" || f.ext == "kts"))
.map(_.toString())

val exitCode = Jvm.callSubprocess(
mainClass = "com.pinterest.ktlint.Main",
classPath = classPath.map(_.path),
mainArgs = args.result(),
workingDir = millSourcePath, // allow passing relative paths for sources like src/a/b
workingDir = millSourcePath,
streamOut = true,
check = false
).exitCode
Expand Down
Loading