Compare commits

..

1 Commits

Author SHA1 Message Date
Simon Rüegg 8367ac92b2 Only write changed files 2025-09-19 13:18:39 +02:00
4 changed files with 17 additions and 29 deletions
@@ -22,11 +22,8 @@ import kotlin.io.path.writeText
import org.pkl.commons.cli.CliBaseOptions
import org.pkl.commons.cli.CliException
class CliFormatterApply(
cliBaseOptions: CliBaseOptions,
paths: List<Path>,
private val silent: Boolean,
) : CliFormatterCommand(cliBaseOptions, paths) {
class CliFormatterApply(cliBaseOptions: CliBaseOptions, path: Path, private val silent: Boolean) :
CliFormatterCommand(cliBaseOptions, path) {
override fun doRun() {
var status = 0
@@ -35,8 +32,8 @@ class CliFormatterApply(
val contents = Files.readString(path)
val (formatted, stat) = format(path, contents)
status = if (status == 0) stat else status
if (stat != 0) continue
if (!silent && contents != formatted) {
if (stat != 0 || contents == formatted) continue
if (!silent) {
consoleWriter.write(path.toAbsolutePath().toString())
consoleWriter.appendLine()
consoleWriter.flush()
@@ -20,8 +20,8 @@ import java.nio.file.Path
import org.pkl.commons.cli.CliBaseOptions
import org.pkl.commons.cli.CliException
class CliFormatterCheck(cliBaseOptions: CliBaseOptions, paths: List<Path>) :
CliFormatterCommand(cliBaseOptions, paths) {
class CliFormatterCheck(cliBaseOptions: CliBaseOptions, path: Path) :
CliFormatterCommand(cliBaseOptions, path) {
override fun doRun() {
var status = 0
@@ -31,7 +31,7 @@ abstract class CliFormatterCommand
@JvmOverloads
constructor(
options: CliBaseOptions,
protected val paths: List<Path>,
protected val path: Path,
protected val consoleWriter: Writer = System.out.writer(),
) : CliCommand(options) {
protected fun format(file: Path, contents: String): Pair<String, Int> {
@@ -46,15 +46,9 @@ constructor(
}
@OptIn(ExperimentalPathApi::class)
protected fun paths(): Set<Path> {
val allPaths = mutableSetOf<Path>()
for (path in paths) {
if (path.isDirectory()) {
allPaths.addAll(path.walk().filter { it.extension == "pkl" || it.name == "PklProject" })
} else {
allPaths.add(path)
}
}
return allPaths
protected fun paths(): Sequence<Path> {
return if (path.isDirectory()) {
path.walk().filter { it.extension == "pkl" || it.name == "PklProject" }
} else sequenceOf(path)
}
}
@@ -19,7 +19,6 @@ import com.github.ajalt.clikt.core.Context
import com.github.ajalt.clikt.core.NoOpCliktCommand
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.multiple
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.path
@@ -42,13 +41,12 @@ class FormatterCheckCommand : BaseCommand(name = "check", helpLink = helpLink) {
override val helpString: String =
"Check if the given files are properly formatted, printing the file name to stdout in case they are not. Returns non-zero in case of failure."
val paths: List<Path> by
argument(name = "paths", help = "Files or directory to check.")
val path: Path by
argument(name = "path", help = "File or directory to check.")
.path(mustExist = true, canBeDir = true)
.multiple()
override fun run() {
CliFormatterCheck(baseOptions.baseOptions(emptyList()), paths).run()
CliFormatterCheck(baseOptions.baseOptions(emptyList()), path).run()
}
}
@@ -56,10 +54,9 @@ class FormatterApplyCommand : BaseCommand(name = "apply", helpLink = helpLink) {
override val helpString: String =
"Overwrite all the files in place with the formatted version. Returns non-zero in case of failure."
val paths: List<Path> by
argument(name = "paths", help = "Files or directory to format.")
val path: Path by
argument(name = "path", help = "File or directory to format.")
.path(mustExist = true, canBeDir = true)
.multiple()
val silent: Boolean by
option(
@@ -69,6 +66,6 @@ class FormatterApplyCommand : BaseCommand(name = "apply", helpLink = helpLink) {
.flag()
override fun run() {
CliFormatterApply(baseOptions.baseOptions(emptyList()), paths, silent).run()
CliFormatterApply(baseOptions.baseOptions(emptyList()), path, silent).run()
}
}