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

Allow to override generated reports base directory. #346

Merged
merged 3 commits into from
Jul 17, 2020
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]
### Added
- ?
- Allow to specify reporters output dir [#321](https://github.com/JLLeitschuh/ktlint-gradle/issues/321)
### Changed
- Check pre-commit hook will not add partially committed files to git commit [#330](https://github.com/JLLeitschuh/ktlint-gradle/issues/330)
- Update Gradle to `6.2.1` version
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The assumption being that you would not want to lint code you weren't compiling.
- [Simple setup](#idea-plugin-simple-setup)
- [Using new plugin API](#idea-plugin-setup-using-new-plugin-api)
- [Plugin configuration](#configuration)
- [Setting reports output directory](#setting-reports-output-directory)
- [Customer reporters](#custom-reporters)
- [Samples](#samples)
- [Task details](#tasks-added)
Expand Down Expand Up @@ -282,6 +283,32 @@ dependencies {
```
</details>

#### Setting reports output directory

It is possible also to define different from default output directory for generated reports
(by default it is "build/reports/ktlint"):
<details>
<summary>Groovy</summary>

```groovy
tasks.withType(org.jlleitschuh.gradle.ktlint.KtlintCheckTask) {
reporterOutputDir = project.layout.buildDirectory.dir("other/location")
}
```
</details>

<details open>
<summary>Kotlin script</summary>

```kotlin
tasks.withType<org.jlleitschuh.gradle.ktlint.KtlintCheckTask>() {
reporterOutputDir.set(
project.layout.buildDirectory.dir("other/location")
)
}
```
</details>

#### Custom reporters

**Note**: If Ktlint custom reporter creates report output file internally, for example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.util.concurrent.Callable
import net.swiftzer.semver.SemVer
import org.gradle.api.GradleException
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTree
import org.gradle.api.file.ProjectLayout
Expand All @@ -20,6 +21,7 @@ import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.OutputFiles
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
Expand All @@ -41,6 +43,7 @@ abstract class BaseKtlintCheckTask(
@get:Internal
internal val additionalEditorconfigFile: RegularFileProperty = objectFactory.fileProperty()

@Suppress("unused")
@get:PathSensitive(PathSensitivity.RELATIVE)
@get:InputFiles
internal val editorConfigFiles: FileCollection by lazy(LazyThreadSafetyMode.NONE) {
Expand Down Expand Up @@ -256,21 +259,30 @@ abstract class BaseKtlintCheckTask(
private fun ReporterType.isAvailable() =
SemVer.parse(ktlintVersion.get()) >= availableSinceVersion

private fun ReporterType.getOutputFile() =
projectLayout.buildDirectory.file(project.provider {
"reports/ktlint/${this@BaseKtlintCheckTask.name}.$fileExtension"
})
private fun ReporterType.getOutputFile() = reporterOutputDir.map {
it.file("${this@BaseKtlintCheckTask.name}.$fileExtension")
}

private fun CustomReporter.getOutputFile() =
projectLayout.buildDirectory.file(project.provider {
"reports/ktlint/${this@BaseKtlintCheckTask.name}.$fileExtension"
})
private fun CustomReporter.getOutputFile() = reporterOutputDir.map {
it.file("${this@BaseKtlintCheckTask.name}.$fileExtension")
}

private fun File.toRelativeFile(): File = relativeTo(projectLayout.projectDirectory.asFile)

/**
* Base location of ktlint generated reports.
*
* Default is "${project.buildDir}/reports/ktlint".
*/
@get:OutputDirectory
val reporterOutputDir: DirectoryProperty = objectFactory.directoryProperty().convention(
project.layout.buildDirectory.dir("reports/ktlint")
)

/**
* Provides all reports outputs map: reporter id to reporter output file.
*/
@Suppress("unused")
@get:OutputFiles
val allReportsOutputFiles: Map<String, RegularFileProperty>
get() = allReports.associateTo(mutableMapOf()) { it.reporterId to it.outputFile }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,50 @@ abstract class ReportersTest : AbstractPluginTest() {
}
}

private fun assertReportCreated(reportFileExtension: String) {
assertThat(reportLocation(reportFileExtension).isFile).isTrue()
@Test
internal fun `Should create reports in modified reports output dir`() {
projectRoot.withFailingSources()
val newLocation = "other/location"

projectRoot.buildFile().appendText("""

ktlint.reporters {
reporter "checkstyle"
reporter "json"
}

tasks.withType(org.jlleitschuh.gradle.ktlint.KtlintCheckTask.class) {
reporterOutputDir = project.layout.buildDirectory.dir("$newLocation")
}
""".trimIndent())

buildAndFail("ktlintCheck").apply {
assertThat(task(":ktlintMainSourceSetCheck")?.outcome).isEqualTo(TaskOutcome.FAILED)

assertReportNotCreated(ReporterType.CHECKSTYLE.fileExtension)
assertReportCreated(ReporterType.CHECKSTYLE.fileExtension, "build/$newLocation")

assertReportNotCreated(ReporterType.JSON.fileExtension)
assertReportCreated(ReporterType.JSON.fileExtension, "build/$newLocation")
}
}

private fun assertReportCreated(
reportFileExtension: String,
reportsLocation: String = "build/reports/ktlint"
) {
assertThat(reportLocation(reportsLocation, reportFileExtension).isFile).isTrue()
}

private fun assertReportNotCreated(reportFileExtension: String) {
assertThat(reportLocation(reportFileExtension).isFile).isFalse()
private fun assertReportNotCreated(
reportFileExtension: String,
reportsLocation: String = "build/reports/ktlint"
) {
assertThat(reportLocation(reportsLocation, reportFileExtension).isFile).isFalse()
}

private fun reportLocation(reportFileExtension: String) =
projectRoot.resolve("build/reports/ktlint/ktlintMainSourceSetCheck.$reportFileExtension")
private fun reportLocation(
reportsLocation: String,
reportFileExtension: String
) = projectRoot.resolve("$reportsLocation/ktlintMainSourceSetCheck.$reportFileExtension")
}