Skip to content

Commit c2883af

Browse files
authored
Reporting (#243)
* WIP - switching to laptop * WIP - switching to laptop * WIP - switching to laptop * fix unused name * WIP - switching to laptop * finish reporting * Apply KtLint format Signed-off-by: GitHub Actions <actions@github.com> * fixing Rule inheritance * fixing Rule inheritance * fixing Rule inheritance
1 parent af13022 commit c2883af

File tree

58 files changed

+1254
-427
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1254
-427
lines changed

dependabot-bridge/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies {
3434
dependencySync("javax.inject:javax.inject:1")
3535
dependencySync("org.antlr:antlr4:4.9.2")
3636
dependencySync("org.antlr:antlr4-runtime:4.9.2")
37+
dependencySync("org.unbescape:unbescape:1.1.6.RELEASE")
3738
dependencySync("net.swiftzer.semver:semver:1.1.1")
3839
dependencySync("org.codehaus.groovy:groovy-xml:3.0.9")
3940
dependencySync("org.codehaus.groovy:groovy:3.0.9")

detekt/detekt-config.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ complexity:
139139
TooManyFunctions:
140140
active: true
141141
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/jsTest/**', '**/iosTest/**']
142-
thresholdInFiles: 11
143-
thresholdInClasses: 11
144-
thresholdInInterfaces: 11
145-
thresholdInObjects: 11
146-
thresholdInEnums: 11
142+
thresholdInFiles: 13
143+
thresholdInClasses: 13
144+
thresholdInInterfaces: 13
145+
thresholdInObjects: 13
146+
thresholdInEnums: 13
147147
ignoreDeprecated: false
148148
ignorePrivate: false
149149
ignoreOverridden: false

gradle/libs.versions.toml

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ kotlinx-knit = { module = "org.jetbrains.kotlinx:kotlinx-knit", version.ref = "k
6363

6464
semVer = "net.swiftzer.semver:semver:1.1.1"
6565

66+
unbescape = "org.unbescape:unbescape:1.1.6.RELEASE"
67+
6668
[bundles]
6769
jUnit = ["junit-api", "junit-params", "junit-engine"]
6870
kotest = ["kotest-assertions", "kotest-properties", "kotest-runner"]

modulecheck-api/src/main/kotlin/modulecheck/api/Finding.kt

+14-9
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,30 @@ interface Finding {
2222

2323
val problemName: String
2424
val dependentPath: String
25+
val message: String
2526
val buildFile: File
2627

2728
val statementOrNull: ModuleDependencyDeclaration? get() = null
2829
val statementTextOrNull: String? get() = null
2930
val positionOrNull: Position?
3031

31-
fun toResult(fixed: Boolean): FindingResult
32+
fun toResult(fixed: Boolean): FindingResult {
33+
return FindingResult(
34+
dependentPath = dependentPath,
35+
problemName = problemName,
36+
sourceOrNull = null,
37+
dependencyPath = "",
38+
positionOrNull = positionOrNull,
39+
buildFile = buildFile,
40+
message = message,
41+
fixed = fixed
42+
)
43+
}
3244

3345
fun shouldSkip(): Boolean = statementOrNull?.suppressed
3446
?.contains(problemName)
3547
?: false
3648

37-
fun logString(): String {
38-
return "${buildFile.path}: ${positionString()} $problemName"
39-
}
40-
41-
fun positionString() = positionOrNull?.logString() ?: ""
42-
4349
data class Position(
4450
val row: Int,
4551
val column: Int
@@ -57,10 +63,9 @@ interface Finding {
5763
val dependencyPath: String,
5864
val positionOrNull: Position?,
5965
val buildFile: File,
66+
val message: String,
6067
val fixed: Boolean
6168
) {
6269
val filePathString: String = "${buildFile.path}: ${positionOrNull?.logString().orEmpty()}"
63-
64-
val message: String = "TO DO"
6570
}
6671
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2021 Rick Busarow
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package modulecheck.api
17+
18+
import modulecheck.api.Finding.FindingResult
19+
20+
fun interface FindingProcessor {
21+
22+
fun List<Finding>.toResults(
23+
autoCorrect: Boolean,
24+
deleteUnused: Boolean
25+
): List<Finding.FindingResult>
26+
}
27+
28+
class RealFindingProcessor : FindingProcessor {
29+
30+
override fun List<Finding>.toResults(
31+
autoCorrect: Boolean,
32+
deleteUnused: Boolean
33+
): List<FindingResult> {
34+
35+
return onEach { it.positionOrNull }
36+
.map { finding ->
37+
38+
val fixed = when {
39+
!autoCorrect -> false
40+
deleteUnused && finding is Deletable -> {
41+
finding.delete()
42+
}
43+
finding is Fixable -> finding.fix()
44+
else -> false
45+
}
46+
47+
finding.toResult(fixed)
48+
}
49+
}
50+
}

modulecheck-api/src/main/kotlin/modulecheck/api/FindingFixer.kt renamed to modulecheck-api/src/main/kotlin/modulecheck/api/FindingResultFactory.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ package modulecheck.api
1717

1818
import modulecheck.api.Finding.FindingResult
1919

20-
fun interface FindingFixer {
20+
fun interface FindingResultFactory {
2121

22-
fun toResults(
22+
fun create(
2323
findings: List<Finding>,
2424
autoCorrect: Boolean,
2525
deleteUnused: Boolean
2626
): List<Finding.FindingResult>
2727
}
2828

29-
class RealFindingFixer : FindingFixer {
29+
class RealFindingResultFactory : FindingResultFactory {
3030

31-
override fun toResults(
31+
override fun create(
3232
findings: List<Finding>,
3333
autoCorrect: Boolean,
3434
deleteUnused: Boolean

modulecheck-api/src/main/kotlin/modulecheck/api/Fixable.kt

+13-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,23 @@
1515

1616
package modulecheck.api
1717

18+
import modulecheck.api.Finding.FindingResult
19+
1820
interface Fixable : Finding {
1921

2022
val dependencyIdentifier: String
2123

22-
override fun logString(): String {
23-
return "${buildFile.path}: ${positionString()} $problemName: $dependencyIdentifier"
24+
override fun toResult(fixed: Boolean): FindingResult {
25+
return FindingResult(
26+
dependentPath = dependentPath,
27+
problemName = problemName,
28+
sourceOrNull = null,
29+
dependencyPath = dependencyIdentifier,
30+
positionOrNull = positionOrNull,
31+
buildFile = buildFile,
32+
message = message,
33+
fixed = fixed
34+
)
2435
}
2536

2637
fun fix(): Boolean = synchronized(buildFile) {

modulecheck-api/src/main/kotlin/modulecheck/api/Logger.kt

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
package modulecheck.api
1717

1818
interface Logger {
19+
20+
fun printReport(report: Report)
21+
1922
fun printHeader(message: String)
2023
fun printWarning(message: String)
2124
fun printWarningLine(message: String)

modulecheck-core/src/main/kotlin/modulecheck/core/rule/ModuleCheckRule.kt renamed to modulecheck-api/src/main/kotlin/modulecheck/api/ModuleCheckRule.kt

+14-7
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,27 @@
1313
* limitations under the License.
1414
*/
1515

16-
package modulecheck.core.rule
16+
package modulecheck.api
1717

18+
import modulecheck.api.settings.ChecksSettings
1819
import modulecheck.api.settings.ModuleCheckSettings
1920
import modulecheck.parsing.McProject
2021
import modulecheck.parsing.psi.internal.asKtsFileOrNull
2122
import org.jetbrains.kotlin.psi.KtFile
2223

23-
abstract class ModuleCheckRule<T> {
24+
interface ModuleCheckRule<T> {
2425

25-
abstract val settings: ModuleCheckSettings
26-
abstract val id: String
27-
abstract val description: String
26+
val settings: ModuleCheckSettings
27+
val id: String
28+
val description: String
2829

29-
abstract fun check(project: McProject): List<T>
30+
fun check(project: McProject): List<T>
31+
fun shouldApply(checksSettings: ChecksSettings): Boolean
3032

31-
protected fun McProject.kotlinBuildFileOrNull(): KtFile? = buildFile.asKtsFileOrNull()
33+
fun McProject.kotlinBuildFileOrNull(): KtFile? = buildFile.asKtsFileOrNull()
34+
}
35+
36+
fun interface RuleFactory {
37+
38+
fun create(settings: ModuleCheckSettings): List<ModuleCheckRule<out Finding>>
3239
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (C) 2021 Rick Busarow
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package modulecheck.api
17+
18+
class PrintLogger : Logger {
19+
override fun printReport(report: Report) {
20+
println(report.joinToString())
21+
}
22+
23+
override fun printHeader(message: String) = println(message)
24+
25+
override fun printWarning(message: String) = print(message)
26+
27+
override fun printWarningLine(message: String) = println(message)
28+
29+
override fun printInfo(message: String) = println(message)
30+
31+
override fun printFailure(message: String) = print(message)
32+
33+
override fun printFailureLine(message: String) = println(message)
34+
35+
override fun printFailureHeader(message: String) = println(message)
36+
37+
override fun printSuccess(message: String) = print(message)
38+
39+
override fun printSuccessLine(message: String) = println(message)
40+
41+
override fun printSuccessHeader(message: String) = println(message)
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (C) 2021 Rick Busarow
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package modulecheck.api
17+
18+
import modulecheck.api.Report.ReportEntry.*
19+
20+
data class Report(val entries: List<ReportEntry>) {
21+
22+
fun joinToString(): String = buildString {
23+
entries
24+
.forEach { reportEntry ->
25+
when (reportEntry) {
26+
is AppendNewLine -> appendLine(reportEntry.message.trimEnd())
27+
else -> append(reportEntry.message)
28+
}
29+
}
30+
}
31+
32+
sealed interface ReportEntry {
33+
val message: String
34+
35+
@JvmInline
36+
value class Header(override val message: String) : ReportEntry, AppendNewLine
37+
38+
@JvmInline
39+
value class Warning(override val message: String) : ReportEntry
40+
41+
@JvmInline
42+
value class WarningLine(override val message: String) : ReportEntry, AppendNewLine
43+
44+
@JvmInline
45+
value class Info(override val message: String) : ReportEntry, AppendNewLine
46+
47+
@JvmInline
48+
value class Failure(override val message: String) : ReportEntry
49+
50+
@JvmInline
51+
value class FailureLine(override val message: String) : ReportEntry, AppendNewLine
52+
53+
@JvmInline
54+
value class FailureHeader(override val message: String) : ReportEntry, AppendNewLine
55+
56+
@JvmInline
57+
value class Success(override val message: String) : ReportEntry
58+
59+
@JvmInline
60+
value class SuccessLine(override val message: String) : ReportEntry, AppendNewLine
61+
62+
@JvmInline
63+
value class SuccessHeader(override val message: String) : ReportEntry, AppendNewLine
64+
65+
interface AppendNewLine
66+
}
67+
68+
class ReportBuilderScope(
69+
private val entries: MutableList<ReportEntry> = mutableListOf()
70+
) {
71+
72+
fun header(message: String) {
73+
entries.add(Header(message.trimEnd()))
74+
}
75+
76+
fun warning(message: String) {
77+
entries.add(Warning(message))
78+
}
79+
80+
fun warningLine(message: String) {
81+
entries.add(WarningLine(message.trimEnd()))
82+
}
83+
84+
fun info(message: String) {
85+
entries.add(Info(message))
86+
}
87+
88+
fun failure(message: String) {
89+
entries.add(Failure(message))
90+
}
91+
92+
fun failureLine(message: String) {
93+
entries.add(FailureLine(message.trimEnd()))
94+
}
95+
96+
fun failureHeader(message: String) {
97+
entries.add(FailureHeader(message.trimEnd()))
98+
}
99+
100+
fun success(message: String) {
101+
entries.add(Success(message))
102+
}
103+
104+
fun successLine(message: String) {
105+
entries.add(SuccessLine(message.trimEnd()))
106+
}
107+
108+
fun successHeader(message: String) {
109+
entries.add(SuccessHeader(message.trimEnd()))
110+
}
111+
}
112+
113+
companion object {
114+
115+
fun build(buildAction: ReportBuilderScope.() -> Unit): Report {
116+
val entries = mutableListOf<ReportEntry>()
117+
118+
ReportBuilderScope(entries).buildAction()
119+
120+
return Report(entries)
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)