Skip to content

Commit 505c312

Browse files
authoredMay 3, 2022
Add relative option similar to CLI's --relative (#573)
1 parent 601e1a7 commit 505c312

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed
 

‎CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
9+
### Added
10+
- `relative` option to generate reports with paths relative to the root project ([#573](https://github.com/JLLeitschuh/ktlint-gradle/pull/573))
11+
912
### Fixed
1013
- Fix install hook action when git `hooks` folder doesn't exist [issue: #557](https://github.com/JLLeitschuh/ktlint-gradle/issues/557), [#563](https://github.com/JLLeitschuh/ktlint-gradle/pull/563)
1114
- Fix pre-commit hook command not found error [issue: #562](https://github.com/JLLeitschuh/ktlint-gradle/issues/562), [#564](https://github.com/JLLeitschuh/ktlint-gradle/pull/564)

‎plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintExtension.kt

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ internal constructor(
3737
*/
3838
val version: Property<String> = objectFactory.property { set("0.42.1") }
3939

40+
/**
41+
* Enable relative paths in reports
42+
*/
43+
val relative: Property<Boolean> = objectFactory.property { set(false) }
44+
4045
/**
4146
* Enable verbose mode.
4247
*/

‎plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/TaskCreation.kt

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ private fun <T : BaseKtLintCheckTask> GenerateReportsTask.commonConfiguration(
184184
ignoreFailures.set(pluginHolder.extension.ignoreFailures)
185185
verbose.set(pluginHolder.extension.verbose)
186186
ktLintVersion.set(pluginHolder.extension.version)
187+
relative.set(pluginHolder.extension.relative)
187188
@Suppress("UnstableApiUsage")
188189
baseline.set(
189190
pluginHolder.extension.baseline

‎plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/tasks/GenerateReportsTask.kt

+6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ abstract class GenerateReportsTask @Inject constructor(
8282
@get:Input
8383
internal abstract val ktLintVersion: Property<String>
8484

85+
@get:Input
86+
internal abstract val relative: Property<Boolean>
87+
8588
@get:PathSensitive(PathSensitivity.RELATIVE)
8689
@get:InputFile
8790
@get:Optional
@@ -138,6 +141,9 @@ abstract class GenerateReportsTask @Inject constructor(
138141
param.ktLintVersion.set(ktLintVersion)
139142
param.baseline.set(baseline)
140143
param.projectDirectory.set(projectLayout.projectDirectory)
144+
if (relative.get()) {
145+
param.filePathsRelativeTo.set(project.rootDir)
146+
}
141147
}
142148
}
143149

‎plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/worker/GenerateReportsWorkAction.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal abstract class GenerateReportsWorkAction : WorkAction<GenerateReportsWo
4545

4646
reporter.beforeAll()
4747
discoveredErrors.forEach { lintErrorResult ->
48-
val filePath = lintErrorResult.lintedFile.absolutePath
48+
val filePath = filePathForReport(lintErrorResult.lintedFile)
4949
val baselineLintErrors = baselineRules?.get(
5050
lintErrorResult.lintedFile.toRelativeString(projectDir).replace(File.separatorChar, '/')
5151
)
@@ -61,6 +61,15 @@ internal abstract class GenerateReportsWorkAction : WorkAction<GenerateReportsWo
6161
}
6262
}
6363

64+
private fun filePathForReport(file: File): String {
65+
val rootDir = parameters.filePathsRelativeTo.orNull
66+
if (rootDir != null) {
67+
return file.toRelativeString(rootDir)
68+
}
69+
70+
return file.absolutePath
71+
}
72+
6473
internal interface GenerateReportsParameters : WorkParameters {
6574
val discoveredErrorsFile: RegularFileProperty
6675
val loadedReporterProviders: RegularFileProperty
@@ -70,5 +79,6 @@ internal abstract class GenerateReportsWorkAction : WorkAction<GenerateReportsWo
7079
val ktLintVersion: Property<String>
7180
val baseline: RegularFileProperty
7281
val projectDirectory: DirectoryProperty
82+
val filePathsRelativeTo: Property<File>
7383
}
7484
}

0 commit comments

Comments
 (0)