Skip to content

Commit

Permalink
Pluralize script task names
Browse files Browse the repository at this point in the history
  • Loading branch information
Roggstars committed Nov 29, 2024
1 parent 0391b98 commit 4bd1fa6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ Those two tasks will invoke `ktfmt` on the **whole module**. More specific tasks

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

- `ktfmtCheckScript` that will check if the top-level script files in your module is ktfmt-compliant
- `ktfmtFormatScript` that will reformat top-level script files with ktfmt
- `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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +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.createScriptTasks
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 @@ -63,7 +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 {
createScriptTasks(project, project.projectDir, topLevelFormat, topLevelCheck)
createScriptsTasks(project, project.projectDir, topLevelFormat, topLevelCheck)
applyKtfmtToAndroidProject(project, topLevelFormat, topLevelCheck, ktfmtExtension)
}
}
Expand All @@ -75,7 +75,7 @@ abstract class KtfmtPlugin : Plugin<Project> {

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

private fun applyKtfmtToMultiplatformProject(project: Project, ktfmtExtension: KtfmtExtension) {
val extension = project.extensions.getByType(KotlinMultiplatformExtension::class.java)
createScriptTasks(project, project.projectDir, topLevelFormat, topLevelCheck)
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,14 +63,14 @@ internal object KtfmtPluginUtils {
}
}

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

val scriptFiles =
project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class KtfmtPluginTest {
assertThat(project["ktfmtCheckTest"]).isInstanceOf(KtfmtCheckTask::class.java)
assertThat(project["ktfmtFormatTest"]).isInstanceOf(KtfmtFormatTask::class.java)

assertThat(project["ktfmtCheckScript"]).isInstanceOf(KtfmtCheckTask::class.java)
assertThat(project["ktfmtFormatScript"]).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", "ktfmtCheckScript")
.containsExactly("ktfmtCheckMain", "ktfmtCheckTest", "ktfmtCheckScripts")
assertThat(project["ktfmtFormat"].dependencies)
.containsExactly("ktfmtFormatMain", "ktfmtFormatTest", "ktfmtFormatScript")
.containsExactly("ktfmtFormatMain", "ktfmtFormatTest", "ktfmtFormatScripts")
}

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

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

@Test
Expand All @@ -77,13 +77,13 @@ class KtfmtPluginTest {
.containsExactly(
"ktfmtCheckKmpCommonMain",
"ktfmtCheckKmpCommonTest",
"ktfmtCheckScript",
"ktfmtCheckScripts",
)
assertThat(project["ktfmtFormat"].dependencies)
.containsExactly(
"ktfmtFormatKmpCommonMain",
"ktfmtFormatKmpCommonTest",
"ktfmtFormatScript",
"ktfmtFormatScripts",
)
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,22 +385,22 @@ internal class KtfmtCheckTaskIntegrationTest {
}

@Test
fun `check script task should validate top-level script file`() {
fun `check scripts task should validate top-level script file`() {
createTempFile(content = "val answer=42\n", fileName = "TestFile.kts", path = "")

val result =
GradleRunner.create()
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments("ktfmtCheckScript")
.withArguments("ktfmtCheckScripts")
.buildAndFail()

assertThat(result.task(":ktfmtCheckScript")?.outcome).isEqualTo(FAILED)
assertThat(result.task(":ktfmtCheckScripts")?.outcome).isEqualTo(FAILED)
assertThat(result.output).containsMatch("Invalid formatting for: .*TestFile.kts")
}

@Test
fun `check script task should ignore non top-level script files`() {
fun `check scripts task should ignore non top-level script files`() {
createTempFile(
content = "val answer=42\n",
fileName = "TestFile.kts",
Expand All @@ -411,11 +411,11 @@ internal class KtfmtCheckTaskIntegrationTest {
GradleRunner.create()
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments("ktfmtCheckScript")
.withArguments("ktfmtCheckScripts")
.forwardOutput()
.build()

assertThat(result.task(":ktfmtCheckScript")?.outcome).isEqualTo(SUCCESS)
assertThat(result.task(":ktfmtCheckScripts")?.outcome).isEqualTo(SUCCESS)
}

private fun appendToBuildGradle(content: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,54 +448,54 @@ internal class KtfmtFormatTaskIntegrationTest {
}

@Test
fun `format script task should fail if top-level script file could not be parsed`() {
fun `format scripts task should fail if top-level script file could not be parsed`() {
val scriptFile = createTempFile(content = "val answer=\n", fileName = "my.kts", path = "")

val result =
GradleRunner.create()
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments("ktfmtFormatScript")
.withArguments("ktfmtFormatScripts")
.forwardOutput()
.buildAndFail()

val actual = scriptFile.readText()
assertThat(actual).isEqualTo("val answer=\n")
assertThat(result.task(":ktfmtFormatScript")?.outcome).isEqualTo(FAILED)
assertThat(result.task(":ktfmtFormatScripts")?.outcome).isEqualTo(FAILED)
}

@Test
fun `format script task should format top-level script file`() {
fun `format scripts task should format top-level script file`() {
val scriptFile = createTempFile(content = "val answer=42\n", fileName = "my.kts", path = "")

val result =
GradleRunner.create()
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments("ktfmtFormatScript")
.withArguments("ktfmtFormatScripts")
.forwardOutput()
.build()

val actual = scriptFile.readText()
assertThat(actual).isEqualTo("val answer = 42\n")
assertThat(result.task(":ktfmtFormatScript")?.outcome).isEqualTo(SUCCESS)
assertThat(result.task(":ktfmtFormatScripts")?.outcome).isEqualTo(SUCCESS)
}

@Test
fun `format script task should not format non top-level script file`() {
fun `format scripts task should not format non top-level script file`() {
val scriptFile = createTempFile(content = "val answer=42\n", fileName = "my.kts")

val result =
GradleRunner.create()
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments("ktfmtFormatScript")
.withArguments("ktfmtFormatScripts")
.forwardOutput()
.build()

val actual = scriptFile.readText()
assertThat(actual).isEqualTo("val answer=42\n")
assertThat(result.task(":ktfmtFormatScript")?.outcome).isEqualTo(SUCCESS)
assertThat(result.task(":ktfmtFormatScripts")?.outcome).isEqualTo(SUCCESS)
}

private fun createTempFile(
Expand Down

0 comments on commit 4bd1fa6

Please sign in to comment.