Skip to content

Commit f20816c

Browse files
authored
Specify exact files to process in ktlint run (#4201)
This PR improves `ktlint` support: it explicitly specifies the files to process. Without that current `mill.kotlinlib.ktlint.KtlintModule/checkFormatAll` run, for example, will go through the files under the `out` folder, which is useless and pollutes logs. Also this PR removes redundant flags from `reformatAll` and `checkFormatAll`: former will always be with `format=true` by definition, the latter will always be with `format=false`. Co-authored-by: 0xnm <[email protected]>
1 parent 361f726 commit f20816c

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

kotlinlib/src/mill/kotlinlib/ktlint/KtlintModule.scala

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package mill.kotlinlib.ktlint
22

3+
import mainargs.arg
34
import mill._
45
import mill.api.{Loose, PathRef}
56
import mill.define.{Discover, ExternalModule}
67
import mill.javalib.JavaModule
78
import mill.kotlinlib.DepSyntax
9+
import mill.main.Tasks
810
import mill.util.Jvm
911

1012
/**
@@ -16,7 +18,13 @@ trait KtlintModule extends JavaModule {
1618
* Runs [[https://pinterest.github.io/ktlint/latest/install/integrations/ Ktlint]]
1719
*/
1820
def ktlint(@mainargs.arg ktlintArgs: KtlintArgs): Command[Unit] = Task.Command {
19-
KtlintModule.ktlintAction(ktlintArgs, ktlintConfig(), ktlintOptions(), ktlintClasspath())
21+
KtlintModule.ktlintAction(
22+
ktlintArgs,
23+
sources(),
24+
ktlintConfig(),
25+
ktlintOptions(),
26+
ktlintClasspath()
27+
)
2028
}
2129

2230
/**
@@ -57,15 +65,14 @@ object KtlintModule extends ExternalModule with KtlintModule with TaskModule {
5765

5866
/**
5967
* Reformats Kotlin source files.
60-
*
61-
* @param check if an exception should be raised when formatting errors are found
62-
* - when set, files are not formatted
6368
*/
6469
def reformatAll(
65-
check: mainargs.Flag = mainargs.Flag(value = false)
70+
@arg(positional = true) sources: Tasks[Seq[PathRef]] =
71+
Tasks.resolveMainDefault("__.sources")
6672
): Command[Unit] = Task.Command {
6773
ktlintAction(
68-
KtlintArgs(format = true, check = check.value),
74+
KtlintArgs(format = true, check = true),
75+
T.sequence(sources.value)().flatten,
6976
ktlintConfig(),
7077
ktlintOptions(),
7178
ktlintClasspath()
@@ -74,15 +81,14 @@ object KtlintModule extends ExternalModule with KtlintModule with TaskModule {
7481

7582
/**
7683
* Checks the Kotlin source files formatting without reformatting the files.
77-
*
78-
* @param check if an exception should be raised when formatting errors are found
79-
* - when set, files are not formatted
8084
*/
8185
def checkFormatAll(
82-
check: mainargs.Flag = mainargs.Flag(value = false)
86+
@arg(positional = true) sources: Tasks[Seq[PathRef]] =
87+
Tasks.resolveMainDefault("__.sources")
8388
): Command[Unit] = Task.Command {
8489
ktlintAction(
85-
KtlintArgs(format = false, check = check.value),
90+
KtlintArgs(format = false, check = false),
91+
T.sequence(sources.value)().flatten,
8692
ktlintConfig(),
8793
ktlintOptions(),
8894
ktlintClasspath()
@@ -91,6 +97,7 @@ object KtlintModule extends ExternalModule with KtlintModule with TaskModule {
9197

9298
private def ktlintAction(
9399
ktlintArgs: KtlintArgs,
100+
filesToFormat: Seq[PathRef],
94101
config: Option[PathRef],
95102
options: Seq[String],
96103
classPath: Loose.Agg[PathRef]
@@ -111,12 +118,15 @@ object KtlintModule extends ExternalModule with KtlintModule with TaskModule {
111118
args ++= options
112119
args ++= configArgument
113120
args ++= formatArgument
121+
args ++= filesToFormat.map(_.path)
122+
.filter(f => os.exists(f) && (f.ext == "kt" || f.ext == "kts"))
123+
.map(_.toString())
114124

115125
val exitCode = Jvm.callSubprocess(
116126
mainClass = "com.pinterest.ktlint.Main",
117127
classPath = classPath.map(_.path),
118128
mainArgs = args.result(),
119-
workingDir = millSourcePath, // allow passing relative paths for sources like src/a/b
129+
workingDir = millSourcePath,
120130
streamOut = true,
121131
check = false
122132
).exitCode

0 commit comments

Comments
 (0)