mirror of
https://github.com/apple/pkl.git
synced 2026-07-08 05:55:13 +02:00
Make format command write relative paths (#1290)
This commit is contained in:
@@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.pkl.cli
|
package org.pkl.cli
|
||||||
|
|
||||||
import java.io.File
|
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.io.Writer
|
import java.io.Writer
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
@@ -28,9 +27,8 @@ import kotlin.io.path.writeText
|
|||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
import org.pkl.commons.cli.CliBaseOptions
|
import org.pkl.commons.cli.CliBaseOptions
|
||||||
import org.pkl.commons.cli.CliCommand
|
import org.pkl.commons.cli.CliCommand
|
||||||
|
import org.pkl.commons.cli.CliException
|
||||||
import org.pkl.commons.cli.CliTestException
|
import org.pkl.commons.cli.CliTestException
|
||||||
import org.pkl.core.ModuleSource
|
|
||||||
import org.pkl.core.runtime.VmUtils
|
|
||||||
import org.pkl.core.util.IoUtils
|
import org.pkl.core.util.IoUtils
|
||||||
import org.pkl.formatter.Formatter
|
import org.pkl.formatter.Formatter
|
||||||
import org.pkl.formatter.GrammarVersion
|
import org.pkl.formatter.GrammarVersion
|
||||||
@@ -47,7 +45,6 @@ constructor(
|
|||||||
private val consoleWriter: Writer = System.out.writer(),
|
private val consoleWriter: Writer = System.out.writer(),
|
||||||
private val errWriter: Writer = System.err.writer(),
|
private val errWriter: Writer = System.err.writer(),
|
||||||
) : CliCommand(CliBaseOptions()) {
|
) : CliCommand(CliBaseOptions()) {
|
||||||
|
|
||||||
private fun format(contents: String): String {
|
private fun format(contents: String): String {
|
||||||
return Formatter().format(contents, grammarVersion)
|
return Formatter().format(contents, grammarVersion)
|
||||||
}
|
}
|
||||||
@@ -65,15 +62,15 @@ constructor(
|
|||||||
consoleWriter.flush()
|
consoleWriter.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun allSources(): Stream<ModuleSource> {
|
private fun allPaths(): Stream<Path> {
|
||||||
return paths.distinct().stream().flatMap { path ->
|
return paths.distinct().stream().flatMap { path ->
|
||||||
when {
|
when {
|
||||||
path.toString() == "-" -> Stream.of(ModuleSource.text(IoUtils.readString(System.`in`)))
|
path.toString() == "-" -> Stream.of(path)
|
||||||
path.isDirectory() ->
|
path.isDirectory() ->
|
||||||
Files.walk(path)
|
Files.walk(path)
|
||||||
.filter { it.extension == "pkl" || it.name == "PklProject" }
|
.filter { it.extension == "pkl" || it.name == "PklProject" }
|
||||||
.map { ModuleSource.path(it.toAbsolutePath().normalize()) }
|
.map { it.normalize() }
|
||||||
else -> Stream.of(ModuleSource.path(path.toAbsolutePath().normalize()))
|
else -> Stream.of(path.normalize())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,7 +78,7 @@ constructor(
|
|||||||
override fun doRun() {
|
override fun doRun() {
|
||||||
val status = Status(SUCCESS)
|
val status = Status(SUCCESS)
|
||||||
|
|
||||||
handleSources(status)
|
handlePaths(status)
|
||||||
|
|
||||||
when (status.status) {
|
when (status.status) {
|
||||||
FORMATTING_VIOLATION -> {
|
FORMATTING_VIOLATION -> {
|
||||||
@@ -98,27 +95,25 @@ constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleSources(status: Status) {
|
private fun handlePaths(status: Status) {
|
||||||
for (source in allSources()) {
|
for (path in allPaths()) {
|
||||||
val path = if (source.uri == VmUtils.REPL_TEXT_URI) Path.of("-") else Path.of(source.uri)
|
val pathStr = path.toString()
|
||||||
try {
|
try {
|
||||||
val contents =
|
val contents =
|
||||||
if (source.contents != null) {
|
when {
|
||||||
if (overwrite) {
|
pathStr == "-" -> IoUtils.readString(System.`in`)
|
||||||
writeErr("Cannot write to stdin.")
|
else -> Files.readString(path)
|
||||||
throw CliTestException("", ERROR)
|
|
||||||
}
|
|
||||||
source.contents!!
|
|
||||||
} else {
|
|
||||||
File(source.uri).readText()
|
|
||||||
}
|
}
|
||||||
|
if (pathStr == "-" && overwrite) {
|
||||||
|
throw CliException("Cannot write to stdin", ERROR)
|
||||||
|
}
|
||||||
|
|
||||||
val formatted = format(contents)
|
val formatted = format(contents)
|
||||||
if (contents != formatted) {
|
if (contents != formatted) {
|
||||||
status.update(FORMATTING_VIOLATION)
|
status.update(FORMATTING_VIOLATION)
|
||||||
if (diffNameOnly || overwrite) {
|
if (diffNameOnly || overwrite) {
|
||||||
// if `--diff-name-only` or `-w` is specified, only write file names
|
// if `--diff-name-only` or `-w` is specified, only write file names
|
||||||
write(path.toAbsolutePath().toString())
|
write(pathStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (overwrite) {
|
if (overwrite) {
|
||||||
@@ -130,10 +125,10 @@ constructor(
|
|||||||
write(formatted)
|
write(formatted)
|
||||||
}
|
}
|
||||||
} catch (pe: GenericParserError) {
|
} catch (pe: GenericParserError) {
|
||||||
writeErr("Could not format `$path`: $pe")
|
writeErr("Could not format `$pathStr`: $pe")
|
||||||
status.update(ERROR)
|
status.update(ERROR)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
writeErr("IO error while reading `$path`: ${e.message}")
|
writeErr("IO error while reading `$pathStr`: ${e.message}")
|
||||||
status.update(ERROR)
|
status.update(ERROR)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user