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

Update dependency com.ncorti.ktfmt.gradle to v0.20.1 #354

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ detekt = "1.23.6"
diffUtils = "4.12"
junit = "5.11.0"
kotlin = "2.0.20"
ktfmt-plugin = "0.20.0"
ktfmt-plugin = "0.20.1"
ktfmt = "0.52"
pluginPublish = "1.2.1"
truth = "1.4.4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal object KtfmtAndroidUtils {
topLevelFormat: TaskProvider<Task>,
topLevelCheck: TaskProvider<Task>,
ktfmtExtension: KtfmtExtension,
isKmpProject: Boolean = false
isKmpProject: Boolean = false,
) {
fun applyKtfmtForAndroid() {
project.extensions.configure(BaseExtension::class.java) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ abstract class KtfmtPlugin : Plugin<Project> {
attributes.apply {
attribute(
Usage.USAGE_ATTRIBUTE,
project.objects.named(Usage::class.java, Usage.JAVA_RUNTIME))
project.objects.named(Usage::class.java, Usage.JAVA_RUNTIME),
)
}
isVisible = false
isCanBeConsumed = false
Expand Down Expand Up @@ -106,7 +107,12 @@ abstract class KtfmtPlugin : Plugin<Project> {
extension.targets.all { kotlinTarget ->
if (kotlinTarget.platformType == KotlinPlatformType.androidJvm) {
applyKtfmtToAndroidProject(
project, topLevelFormat, topLevelCheck, ktfmtExtension, isKmpProject = true)
project,
topLevelFormat,
topLevelCheck,
ktfmtExtension,
isKmpProject = true,
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal object KtfmtPluginUtils {
srcSetDir: FileCollection,
ktfmtExtension: KtfmtExtension,
topLevelFormat: TaskProvider<Task>,
topLevelCheck: TaskProvider<Task>
topLevelCheck: TaskProvider<Task>,
) {
if (shouldCreateTasks(srcSetName).not()) {
return
Expand Down Expand Up @@ -121,7 +121,7 @@ internal object KtfmtPluginUtils {

private fun Project.getSelectedSrcSets(
srcDir: FileCollection,
ktfmtExtension: KtfmtExtension
ktfmtExtension: KtfmtExtension,
): Provider<List<File>> {
val excludedSourceSets = ktfmtExtension.srcSetPathExclusionPattern

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import org.gradle.workers.WorkerExecutor
abstract class KtfmtBaseTask
internal constructor(
private val workerExecutor: WorkerExecutor,
private val layout: ProjectLayout
private val layout: ProjectLayout,
) : SourceTask() {

init {
Expand All @@ -43,7 +43,8 @@ internal constructor(
option = "include-only",
description =
"A comma separate list of relative file paths to include exclusively. " +
"If set the task will run the processing only on such files.")
"If set the task will run the processing only on such files.",
)
@get:Input
abstract val includeOnly: Property<String>

Expand All @@ -64,15 +65,16 @@ internal constructor(

internal fun <T : KtfmtWorkAction> FileCollection.submitToQueue(
queue: WorkQueue,
action: Class<T>
action: Class<T>,
): List<Result> {
val workingDir = temporaryDir.resolve(UUID.randomUUID().toString())
workingDir.mkdirs()
try {
val includedFiles =
IncludedFilesParser.parse(includeOnly.get(), layout.projectDirectory.asFile)
logger.d(
"Preparing to format: includeOnly=${includeOnly.orNull}, includedFiles = $includedFiles")
"Preparing to format: includeOnly=${includeOnly.orNull}, includedFiles = $includedFiles"
)
forEach {
queue.submit(action) { parameters ->
parameters.sourceFile.set(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ internal constructor(workerExecutor: WorkerExecutor, layout: ProjectLayout) :
if (notFormattedFiles.isNotEmpty()) {
val fileList = notFormattedFiles.joinToString("\n") { it.relativePath }
error(
"[ktfmt] Found ${notFormattedFiles.size} files that are not properly formatted:\n$fileList")
"[ktfmt] Found ${notFormattedFiles.size} files that are not properly formatted:\n$fileList"
)
}

val message = "Successfully checked ${results.size} files with Ktfmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ internal object KtfmtFormatter {
val sourceFile: File,
val includedFiles: Set<File> = emptySet(),
val formattingOptions: FormattingOptionsBean = FormattingOptionsBean(),
val sourceRoot: File
val sourceRoot: File,
)

fun format(ctx: FormatContext): KtfmtResult {
logger.d("Formatting: $ctx")
if (ctx.includedFiles.isNotEmpty()) {
if (ctx.sourceFile.canonicalFile !in ctx.includedFiles) {
return KtfmtResult.KtfmtSkipped(
ctx.sourceFile, "Not included inside --include-only")
ctx.sourceFile,
"Not included inside --include-only",
)
}
}
@Suppress("TooGenericExceptionCaught")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ internal sealed class KtfmtResult(open val input: File) {
data class KtfmtSuccess(
override val input: File,
val isCorrectlyFormatted: Boolean,
@Language("kotlin") val formattedCode: String
@Language("kotlin") val formattedCode: String,
) : KtfmtResult(input)

data class KtfmtFailure(override val input: File, val message: String, val reason: Throwable) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal class PluginVersionCompatibilityTest {
private fun createTempFile(
@Language("kotlin") content: String,
fileName: String = "TestFile.kt",
path: String = "src/main/java"
path: String = "src/main/java",
) =
File(File(tempDir, path), fileName).apply {
createNewFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class KtfmtBaseTaskTest {
private fun createTempFile(
@Language("kotlin") content: String,
fileName: String = "TestFile.kt",
root: File = tempDir
root: File = tempDir,
): File =
File(root, fileName).apply {
parentFile.mkdirs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ internal class KtfmtCheckTaskIntegrationTest {
````
}
"""
.trimIndent())
.trimIndent()
)

val result =
GradleRunner.create()
Expand Down Expand Up @@ -166,7 +167,10 @@ internal class KtfmtCheckTaskIntegrationTest {
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments(
"ktfmtCheckMain", "--debug", "--include-only=${file2.relativeTo(tempDir)}")
"ktfmtCheckMain",
"--debug",
"--include-only=${file2.relativeTo(tempDir)}",
)
.build()

assertThat(result.task(":ktfmtCheckMain")?.outcome).isEqualTo(SUCCESS)
Expand Down Expand Up @@ -212,11 +216,20 @@ internal class KtfmtCheckTaskIntegrationTest {
@Test
fun `check task should be up-to-date when invoked twice with multiple different sized sourceSets`() {
createTempFile(
content = "val answer = 42\n", fileName = "SrcFile.kt", path = "src/main/java")
content = "val answer = 42\n",
fileName = "SrcFile.kt",
path = "src/main/java",
)
createTempFile(
content = "val answer = 42\n", fileName = "TestFile.kt", path = "src/test/java")
content = "val answer = 42\n",
fileName = "TestFile.kt",
path = "src/test/java",
)
createTempFile(
content = "val answer = 42\n", fileName = "TestFile2.kt", path = "src/test/java")
content = "val answer = 42\n",
fileName = "TestFile2.kt",
path = "src/test/java",
)

val firstRun: BuildResult =
GradleRunner.create()
Expand Down Expand Up @@ -269,7 +282,8 @@ internal class KtfmtCheckTaskIntegrationTest {
| source = fileTree("src/main/java")
|}
"""
.trimMargin())
.trimMargin()
)

GradleRunner.create()
.withProjectDir(tempDir)
Expand All @@ -291,7 +305,8 @@ internal class KtfmtCheckTaskIntegrationTest {
| }
|}
"""
.trimMargin())
.trimMargin()
)

createTempFile("val answer = 42\n", path = "src/someFolder")
createTempFile("val answer = 42\n", path = "test/someOtherFolder")
Expand All @@ -317,7 +332,8 @@ internal class KtfmtCheckTaskIntegrationTest {
| }
|}
"""
.trimMargin())
.trimMargin()
)

createTempFile(content = "val answer=42\n", path = "build/main")

Expand Down Expand Up @@ -346,7 +362,8 @@ internal class KtfmtCheckTaskIntegrationTest {
| srcSetPathExclusionPattern.set(Regex("customRules.*"))
|}
"""
.trimMargin())
.trimMargin()
)

createTempFile(content = "val answer=42\n", path = "build/generated/main")

Expand All @@ -369,7 +386,8 @@ internal class KtfmtCheckTaskIntegrationTest {
| srcSetPathExclusionPattern.set(Regex(".*[\\\\/]main[\\\\/].*"))
|}
"""
.trimMargin())
.trimMargin()
)

createTempFile(content = "val answer=42\n")

Expand All @@ -394,7 +412,7 @@ internal class KtfmtCheckTaskIntegrationTest {
private fun createTempFile(
@Language("kotlin") content: String,
fileName: String = "TestFile.kt",
path: String = "src/main/java"
path: String = "src/main/java",
): File =
tempDir.resolve(path).resolve(fileName).apply {
parentFile.mkdirs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ internal class KtfmtFormatTaskIntegrationTest {
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments(
"ktfmtFormatMain", "--info", "--include-only=${file2.relativeTo(tempDir)}")
"ktfmtFormatMain",
"--info",
"--include-only=${file2.relativeTo(tempDir)}",
)
.build()

assertThat(result.task(":ktfmtFormatMain")?.outcome).isEqualTo(SUCCESS)
Expand Down Expand Up @@ -271,7 +274,8 @@ internal class KtfmtFormatTaskIntegrationTest {
| source = fileTree("src/main/java")
|}
"""
.trimMargin())
.trimMargin()
)

GradleRunner.create()
.withProjectDir(tempDir)
Expand All @@ -291,7 +295,8 @@ internal class KtfmtFormatTaskIntegrationTest {
|println("HelloWorld2")
|}
"""
.trimMargin())
.trimMargin()
)

appendToBuildGradle("ktfmt { kotlinLangStyle() }")

Expand All @@ -308,7 +313,8 @@ internal class KtfmtFormatTaskIntegrationTest {
"fun someFun() {",
" println(\"Hello, World!\")",
" println(\"HelloWorld2\")",
"}")
"}",
)
}

@Test
Expand All @@ -322,7 +328,8 @@ internal class KtfmtFormatTaskIntegrationTest {
|println("HelloWorld2")
|}
"""
.trimMargin())
.trimMargin()
)

appendToBuildGradle("ktfmt { googleStyle() }")

Expand All @@ -340,7 +347,8 @@ internal class KtfmtFormatTaskIntegrationTest {
"fun someFun() {",
" println(\"Hello, World!\")",
" println(\"HelloWorld2\")",
"}")
"}",
)
}

@Test
Expand All @@ -356,7 +364,8 @@ internal class KtfmtFormatTaskIntegrationTest {
| }
|}
"""
.trimMargin())
.trimMargin()
)

val sourceFile = createTempFile("val answer = 42\n", path = "src/someFolder")
val testFile = createTempFile("val answer = 42\n", path = "test/someOtherFolder")
Expand Down Expand Up @@ -385,7 +394,8 @@ internal class KtfmtFormatTaskIntegrationTest {
| }
|}
"""
.trimMargin())
.trimMargin()
)

val file = createTempFile(content = "val answer=42\n", path = "build/main")

Expand Down Expand Up @@ -416,7 +426,8 @@ internal class KtfmtFormatTaskIntegrationTest {
| srcSetPathExclusionPattern.set(Regex("customRules.*"))
|}
"""
.trimMargin())
.trimMargin()
)

val file = createTempFile(content = "val answer=42\n", path = "build/generated/main")

Expand All @@ -439,7 +450,8 @@ internal class KtfmtFormatTaskIntegrationTest {
| srcSetPathExclusionPattern.set(Regex(".*[\\\\/]main[\\\\/].*"))
|}
"""
.trimMargin())
.trimMargin()
)

createTempFile(content = "val answer=42\n")

Expand All @@ -457,7 +469,7 @@ internal class KtfmtFormatTaskIntegrationTest {
private fun createTempFile(
@Language("kotlin") content: String,
fileName: String = "TestFile.kt",
path: String = "src/main/java"
path: String = "src/main/java",
): File =
tempDir.resolve(path).resolve(fileName).apply {
parentFile.mkdirs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ internal class KtfmtFormatterTest {
val newCtx =
formatCtx.copy(
sourceFile = input2,
includedFiles = setOf(formatCtx.sourceFile.relativeTo(tempDir)))
includedFiles = setOf(formatCtx.sourceFile.relativeTo(tempDir)),
)

val result = KtfmtFormatter.format(newCtx) as KtfmtResult.KtfmtSkipped

Expand All @@ -73,7 +74,7 @@ internal class KtfmtFormatterTest {
private fun createTempFile(
content: String,
fileName: String = "TestFile.kt",
root: File = tempDir
root: File = tempDir,
): File =
File(root, fileName).apply {
parentFile.mkdirs()
Expand All @@ -83,7 +84,7 @@ internal class KtfmtFormatterTest {

private fun createFormatContext(
content: String,
fileName: String = "TestFile.kt"
fileName: String = "TestFile.kt",
): KtfmtFormatter.FormatContext {
val input = createTempFile(content = content, fileName = fileName)
return KtfmtFormatter.FormatContext(sourceFile = input, sourceRoot = tempDir)
Expand Down
Loading
Loading