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

Use SourceSet in task names and reports files names. #180

Merged
merged 1 commit into from
Dec 6, 2018
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- ?
### Changed
- Update Kotlin to `1.3.10` version
- Breaking: check/format tasks for specific source sets
Copy link
Contributor

@tasomaniac tasomaniac Dec 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to have an example for Android. Something like below?

Example: In an Android project with foo flavor, ktlintFooDebugSourceSetCheck task will check the foo sourceSet (not main). ktlintFooDebugCheck meta task will check all the sourceSets for fooDebug build variant.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add this explanation to the next PR that adds such variant meta tasks, as it, imho, belongs there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed 👍

and according reports outputs now include `SourceSet` in their name(#170)
### Deleted
- ?
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ open class KtlintPlugin : Plugin<Project> {
val ktLintConfig = createConfiguration(target, extension)

fun createTasks(
fullVariantName: String,
sourceSetName: String,
sources: FileCollection
) {
val checkTask = createCheckTask(
target,
extension,
fullVariantName,
sourceSetName,
ktLintConfig,
sources
)
Expand All @@ -141,7 +141,7 @@ open class KtlintPlugin : Plugin<Project> {
val ktlintSourceSetFormatTask = createFormatTask(
target,
extension,
fullVariantName,
sourceSetName,
ktLintConfig,
sources
)
Expand Down Expand Up @@ -273,7 +273,7 @@ open class KtlintPlugin : Plugin<Project> {
ktLintConfig: Configuration,
kotlinSourceDirectories: Iterable<*>
): Task {
return target.taskHelper<KtlintFormatTask>("ktlint${sourceSetName.capitalize()}Format") {
return target.taskHelper<KtlintFormatTask>(sourceSetName.sourceSetFormatTaskName()) {
description = "Runs a check against all .kt files to ensure that they are formatted according to ktlint."
configurePluginTask(target, extension, ktLintConfig, kotlinSourceDirectories)
}
Expand All @@ -286,7 +286,7 @@ open class KtlintPlugin : Plugin<Project> {
ktLintConfig: Configuration,
kotlinSourceDirectories: Iterable<*>
): Task {
return target.taskHelper<KtlintCheckTask>("ktlint${sourceSetName.capitalize()}Check") {
return target.taskHelper<KtlintCheckTask>(sourceSetName.sourceSetCheckTaskName()) {
description = "Runs a check against all .kt files to ensure that they are formatted according to ktlint."
configurePluginTask(target, extension, ktLintConfig, kotlinSourceDirectories)
}
Expand Down
10 changes: 10 additions & 0 deletions plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/PluginUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,13 @@ internal inline fun <reified T> ObjectFactory.setProperty(
internal inline fun <reified T> ObjectFactory.listProperty(
configuration: ListProperty<T>.() -> Unit = {}
) = listProperty(T::class.java).apply(configuration)

/**
* Create check task name from source set name.
*/
internal fun String.sourceSetCheckTaskName() = "ktlint${capitalize()}SourceSetCheck"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is so nice with Kotlin to be able to do this.


/**
* Create format task name from source set name.
*/
internal fun String.sourceSetFormatTaskName() = "ktlint${capitalize()}SourceSetFormat"
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() {
projectRoot.withCleanSources()

buildAndFail("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")?.outcome, equalTo(TaskOutcome.FAILED))
assertThat(task(":ktlintMainSourceSetCheck")?.outcome, equalTo(TaskOutcome.FAILED))
assertThat(output, containsString("Ktlint versions less than 0.10.0 are not supported. Detected Ktlint version: 0.9.0."))
}
}
Expand All @@ -68,7 +68,7 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() {
projectRoot.withFailingSources()

buildAndFail("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.FAILED))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.FAILED))
assertThat(output, containsString("Unnecessary space(s)"))
assertReportCreated(ReporterType.PLAIN)
assertReportCreated(ReporterType.CHECKSTYLE)
Expand All @@ -86,7 +86,7 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() {
""".trimIndent())

buildAndFail("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.FAILED))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.FAILED))
assertThat(output, containsString("Unnecessary space(s)"))
assertReportCreated(ReporterType.PLAIN_GROUP_BY_FILE)
assertReportCreated(ReporterType.CHECKSTYLE)
Expand All @@ -104,13 +104,13 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() {
""".trimIndent())

build("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertReportCreated(ReporterType.PLAIN)
assertReportCreated(ReporterType.JSON)
}

build("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.UP_TO_DATE))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.UP_TO_DATE))
assertReportCreated(ReporterType.PLAIN)
assertReportCreated(ReporterType.JSON)
assertReportNotCreated(ReporterType.CHECKSTYLE)
Expand All @@ -122,7 +122,7 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() {
""".trimIndent())

build("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertReportCreated(ReporterType.PLAIN_GROUP_BY_FILE)
assertReportCreated(ReporterType.JSON)
}
Expand All @@ -133,7 +133,7 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() {
""".trimIndent())

build("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertReportCreated(ReporterType.JSON)
assertReportCreated(ReporterType.CHECKSTYLE)
// TODO: Stale reports are not cleaned up
Expand All @@ -147,10 +147,10 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() {
projectRoot.createEditorconfigFile()

build("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
}
build("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.UP_TO_DATE))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.UP_TO_DATE))
}
}

Expand All @@ -160,12 +160,12 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() {
projectRoot.createEditorconfigFile()

build("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
}

projectRoot.modifyEditorconfigFile(100)
build("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
}
}

Expand Down Expand Up @@ -207,15 +207,15 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() {
.withArguments("ktlintCheck", "--build-cache")
.forwardOutput()
.build().apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
}

GradleRunner.create()
.withProjectDir(relocatedLocation)
.withArguments("ktlintCheck", "--build-cache")
.forwardOutput()
.build().apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.FROM_CACHE))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.FROM_CACHE))
}
}

Expand All @@ -230,15 +230,15 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() {
}

private fun reportLocation(reportType: ReporterType) =
projectRoot.resolve("build/reports/ktlint/ktlintMainCheck.${reportType.fileExtension}")
projectRoot.resolve("build/reports/ktlint/ktlintMainSourceSetCheck.${reportType.fileExtension}")

@Test
fun `should succeed check on clean sources`() {

projectRoot.withCleanSources()

build("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
}
}

Expand Down Expand Up @@ -327,7 +327,7 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() {
""".trimIndent())

build(":ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
}
}

Expand All @@ -345,7 +345,7 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() {
""".trimIndent())

buildAndFail(":ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.FAILED))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.FAILED))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ class KtlintPluginVersionTest : AbstractPluginTest() {
fun `with ktlint version equal to 0_20`() {
projectRoot.buildScriptUsingKtlintVersion("0.20.0")
build("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.SUCCESS))
}
}

@Test
fun `with ktlint version less than 0_20`() {
projectRoot.buildScriptUsingKtlintVersion("0.19.0")
buildAndFail("ktlintCheck").apply {
assertThat(task(":ktlintMainCheck")!!.outcome, equalTo(TaskOutcome.FAILED))
assertThat(task(":ktlintMainSourceSetCheck")!!.outcome, equalTo(TaskOutcome.FAILED))
}
}
}
4 changes: 2 additions & 2 deletions samples/kotlin-rulesets-using/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ configure<KtlintExtension> {
reporters.set(setOf(ReporterType.CHECKSTYLE, ReporterType.JSON))
}

tasks.findByName("ktlintMainCheck")?.dependsOn(":samples:kotlin-rulesets-creating:build")
tasks.findByName("ktlintTestCheck")?.dependsOn(":samples:kotlin-rulesets-creating:build")
tasks.findByName("ktlintMainSourceSetCheck")?.dependsOn(":samples:kotlin-rulesets-creating:build")
tasks.findByName("ktlintTestSourceSetCheck")?.dependsOn(":samples:kotlin-rulesets-creating:build")