Skip to content

Commit

Permalink
Add support for creating tasks to format script files (#337) (#382)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicola Corti <[email protected]>
  • Loading branch information
Roggstars and cortinico authored Dec 7, 2024
1 parent 658b397 commit 65004a7
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 165 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Please add your entries according to this format.

## Unreleased

- Add tasks `ktfmtCheckScripts` and `ktfmtFormatScripts` to check and format the `.kts` files in the project folder.

## Version 0.21.0 _(2024-11-01)_
- KtFmt to 0.53
- AGP to 8.7.1
Expand Down Expand Up @@ -187,4 +189,4 @@ Please add your entries according to this format.

## Version 0.1.0 _(2020-12-22)_

That's the first version of `ktfmt-gradle`
That's the first version of `ktfmt-gradle`
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ By default, `ktfmt-gradle` will add two Gradle tasks to your build:

Those two tasks will invoke `ktfmt` on the **whole module**. More specific tasks are avialable based on the module type.

Additionally, the plugin will create check/format tasks for top-level Kotlin script files:

- `ktfmtCheckScripts` that will check if the top-level script files in your module is ktfmt-compliant
- `ktfmtFormatScripts` that will reformat top-level script files with ktfmt

#### Jvm/Js Modules

For Jvm/Js modules, the plugin will create a check/format task for **every source set**. For example, jvm modules will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.ncorti.ktfmt.gradle.KtfmtAndroidUtils.applyKtfmtToAndroidProject
import com.ncorti.ktfmt.gradle.KtfmtPluginUtils.EXTENSION_NAME
import com.ncorti.ktfmt.gradle.KtfmtPluginUtils.TASK_NAME_CHECK
import com.ncorti.ktfmt.gradle.KtfmtPluginUtils.TASK_NAME_FORMAT
import com.ncorti.ktfmt.gradle.KtfmtPluginUtils.createScriptsTasks
import com.ncorti.ktfmt.gradle.KtfmtPluginUtils.createTasksForSourceSet
import com.ncorti.ktfmt.gradle.tasks.KtfmtBaseTask
import com.ncorti.ktfmt.gradle.util.i
Expand Down Expand Up @@ -62,6 +63,7 @@ abstract class KtfmtPlugin : Plugin<Project> {
if (project.plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")) {
project.logger.i("Skipping Android task creation, as KMP is applied")
} else {
createScriptsTasks(project, project.projectDir, topLevelFormat, topLevelCheck)
applyKtfmtToAndroidProject(project, topLevelFormat, topLevelCheck, ktfmtExtension)
}
}
Expand All @@ -73,6 +75,7 @@ abstract class KtfmtPlugin : Plugin<Project> {

private fun applyKtfmt(project: Project, ktfmtExtension: KtfmtExtension) {
val extension = project.extensions.getByType(KotlinProjectExtension::class.java)
createScriptsTasks(project, project.projectDir, topLevelFormat, topLevelCheck)
extension.sourceSets.all {
createTasksForSourceSet(
project,
Expand All @@ -87,6 +90,7 @@ abstract class KtfmtPlugin : Plugin<Project> {

private fun applyKtfmtToMultiplatformProject(project: Project, ktfmtExtension: KtfmtExtension) {
val extension = project.extensions.getByType(KotlinMultiplatformExtension::class.java)
createScriptsTasks(project, project.projectDir, topLevelFormat, topLevelCheck)
extension.sourceSets.all {
val name = "kmp ${it.name}"
if (it.name.startsWith("android")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,49 @@ internal object KtfmtPluginUtils {
}
}

internal fun createScriptsTasks(
project: Project,
projectDir: File,
topLevelFormat: TaskProvider<Task>,
topLevelCheck: TaskProvider<Task>,
) {
val checkTaskName = "${TASK_NAME_CHECK}Scripts"
val formatTaskName = "${TASK_NAME_FORMAT}Scripts"

val scriptFiles =
project
.fileTree(projectDir)
.filter { it.extension == "kts" }
.filter { it.parentFile == projectDir }

val scriptCheckTask =
project.tasks.register(checkTaskName, KtfmtCheckTask::class.java) {
it.description =
"Run Ktfmt formatter validation for script files on project '${project.name}'"
it.setSource(scriptFiles)
it.setIncludes(KtfmtPlugin.defaultIncludes)
}
val scriptFormatTask =
project.tasks.register(formatTaskName, KtfmtFormatTask::class.java) {
it.description = "Run Ktfmt formatter for script files on project '${project.name}'"
it.setSource(scriptFiles)
it.setIncludes(KtfmtPlugin.defaultIncludes)
}

project.tasks.withType(KotlinCompile::class.java).all {
it.mustRunAfter(scriptCheckTask, scriptFormatTask)
}

topLevelFormat.configure { task -> task.dependsOn(scriptFormatTask) }
topLevelCheck.configure { task -> task.dependsOn(scriptCheckTask) }

project.plugins.withType(LifecycleBasePlugin::class.java) {
project.tasks.named(LifecycleBasePlugin.CHECK_TASK_NAME).configure { task ->
task.dependsOn(scriptCheckTask)
}
}
}

private fun createCheckTask(
project: Project,
name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ class KtfmtPluginTest {
assertThat(project["ktfmtCheckTest"]).isInstanceOf(KtfmtCheckTask::class.java)
assertThat(project["ktfmtFormatTest"]).isInstanceOf(KtfmtFormatTask::class.java)

assertThat(project["ktfmtCheckScripts"]).isInstanceOf(KtfmtCheckTask::class.java)
assertThat(project["ktfmtFormatScripts"]).isInstanceOf(KtfmtFormatTask::class.java)

assertThat(project["ktfmtCheck"].dependencies)
.containsExactly("ktfmtCheckMain", "ktfmtCheckTest")
.containsExactly("ktfmtCheckMain", "ktfmtCheckTest", "ktfmtCheckScripts")
assertThat(project["ktfmtFormat"].dependencies)
.containsExactly("ktfmtFormatMain", "ktfmtFormatTest")
.containsExactly("ktfmtFormatMain", "ktfmtFormatTest", "ktfmtFormatScripts")
}

@Test
Expand All @@ -50,9 +53,9 @@ class KtfmtPluginTest {
assertThat(project["ktfmtFormatTest"]).isInstanceOf(KtfmtFormatTask::class.java)

assertThat(project["ktfmtCheck"].dependencies)
.containsExactly("ktfmtCheckMain", "ktfmtCheckTest")
.containsExactly("ktfmtCheckMain", "ktfmtCheckTest", "ktfmtCheckScripts")
assertThat(project["ktfmtFormat"].dependencies)
.containsExactly("ktfmtFormatMain", "ktfmtFormatTest")
.containsExactly("ktfmtFormatMain", "ktfmtFormatTest", "ktfmtFormatScripts")
}

@Test
Expand All @@ -71,9 +74,17 @@ class KtfmtPluginTest {
.isInstanceOf(KtfmtFormatTask::class.java)

assertThat(project["ktfmtCheck"].dependencies)
.containsExactly("ktfmtCheckKmpCommonMain", "ktfmtCheckKmpCommonTest")
.containsExactly(
"ktfmtCheckKmpCommonMain",
"ktfmtCheckKmpCommonTest",
"ktfmtCheckScripts",
)
assertThat(project["ktfmtFormat"].dependencies)
.containsExactly("ktfmtFormatKmpCommonMain", "ktfmtFormatKmpCommonTest")
.containsExactly(
"ktfmtFormatKmpCommonMain",
"ktfmtFormatKmpCommonTest",
"ktfmtFormatScripts",
)
}

@Suppress("LongMethod")
Expand Down Expand Up @@ -158,6 +169,7 @@ class KtfmtPluginTest {
"ktfmtCheckTestFixtures",
"ktfmtCheckTestFixturesRelease",
"ktfmtCheckTestFixturesDebug",
"ktfmtCheckScripts",
)
assertThat(project["ktfmtFormat"].dependencies)
.containsExactly(
Expand All @@ -173,6 +185,7 @@ class KtfmtPluginTest {
"ktfmtFormatTestFixtures",
"ktfmtFormatTestFixturesRelease",
"ktfmtFormatTestFixturesDebug",
"ktfmtFormatScripts",
)
}

Expand Down
Loading

0 comments on commit 65004a7

Please sign in to comment.