From af23cf56b7787f87a1a3d25fa4ecddf710d4e144 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Tue, 4 Jul 2023 19:08:33 +0530 Subject: [PATCH 1/4] Detecting deprecated code via detekt and fixed detekt detection for the issues in project, and refactored deprecated rules of detekt --- .github/workflows/pull_request.yml | 2 +- .../src/main/kotlin/plugin/AllProjectConfigurer.kt | 8 ++++++++ config/detekt/detekt.yml | 13 +++++++------ team-props/git-hooks/pre-commit.sh | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 53b6b48722..266ad43184 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -72,7 +72,7 @@ jobs: ${{ runner.os }}-gradle- - name: Static Analysis - run: ./gradlew detekt + run: ./gradlew detektDebug detektCustomExampleDebug - name: Upload Lint Reports uses: actions/upload-artifact@v3 if: ${{ always() }} diff --git a/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt b/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt index 468574d82a..9e74061e52 100644 --- a/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt +++ b/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt @@ -22,6 +22,8 @@ import Config import Libs import com.android.build.api.dsl.CommonExtension import com.android.build.gradle.BaseExtension +import io.gitlab.arturbosch.detekt.Detekt +import io.gitlab.arturbosch.detekt.DetektCreateBaselineTask import io.gitlab.arturbosch.detekt.extensions.DetektExtension import org.gradle.api.Project import org.gradle.kotlin.dsl.apply @@ -67,6 +69,12 @@ class AllProjectConfigurer { target.tasks.withType(KotlinCompile::class.java) { kotlinOptions.jvmTarget = "1.8" } + target.tasks.withType(Detekt::class.java).configureEach { + jvmTarget = "1.8" + } + target.tasks.withType(DetektCreateBaselineTask::class.java).configureEach { + jvmTarget = "1.8" + } buildFeatures.viewBinding = true testOptions { diff --git a/config/detekt/detekt.yml b/config/detekt/detekt.yml index 45ba1cf73c..ec68df6451 100644 --- a/config/detekt/detekt.yml +++ b/config/detekt/detekt.yml @@ -79,7 +79,8 @@ complexity: threshold: 60 LongParameterList: active: true - threshold: 6 + functionThreshold: 6 + constructorThreshold: 6 ignoreDefaultParameters: false MethodOverloading: active: false @@ -316,7 +317,7 @@ potential-bugs: LateinitUsage: active: false excludes: "**/test/**,**/androidTest/**,**/*.Test.kt,**/*.Spec.kt,**/*.Spek.kt" - excludeAnnotatedProperties: "" + ignoreAnnotated: [] ignoreOnClassesPattern: "" MapGetWithNotNullAssertionOperator: active: false @@ -379,7 +380,7 @@ style: active: true ignoreOverridableFunction: true excludedFunctions: 'describeContents' - excludeAnnotatedFunction: "dagger.Provides" + ignoreAnnotated: ["dagger.Provides"] LibraryCodeMustSpecifyReturnType: active: true LoopWithTooManyJumpStatements: @@ -450,10 +451,10 @@ style: active: false UnderscoresInNumericLiterals: active: false - acceptableDecimalLength: 5 + acceptableLength: 5 UnnecessaryAbstractClass: active: true - excludeAnnotatedClasses: "dagger.Module" + ignoreAnnotated: ["dagger.Module"] UnnecessaryAnnotationUseSiteTarget: active: false UnnecessaryApply: @@ -479,7 +480,7 @@ style: active: false UseDataClass: active: false - excludeAnnotatedClasses: "" + ignoreAnnotated: [] allowVars: false UseIfInsteadOfWhen: active: false diff --git a/team-props/git-hooks/pre-commit.sh b/team-props/git-hooks/pre-commit.sh index 9c66d7b785..4f8eb37f12 100644 --- a/team-props/git-hooks/pre-commit.sh +++ b/team-props/git-hooks/pre-commit.sh @@ -2,7 +2,7 @@ echo "Running lint..." -./gradlew ktlintCheck detekt app:lintDebug custom:lintCustomexampleDebug --daemon +./gradlew ktlintCheck detektDebug detektCustomExampleDebug app:lintDebug custom:lintCustomexampleDebug --daemon status=$? From 8e6c74abe83e594dcd98acb488db77043472bbd0 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Wed, 5 Jul 2023 15:52:24 +0530 Subject: [PATCH 2/4] Introduced custom rule for detekt to exclude the deprecated imports --- buildSrc/build.gradle.kts | 2 ++ .../main/kotlin/plugin/CustomRuleProvider.kt | 34 +++++++++++++++++++ .../plugin/DeprecationExclusionRules.kt | 31 +++++++++++++++++ config/detekt/detekt.yml | 7 +++- 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 buildSrc/src/main/kotlin/plugin/CustomRuleProvider.kt create mode 100644 buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 33a40c49bd..2443ee8098 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -22,6 +22,8 @@ dependencies { exclude(group = "com.google.guava", module = "guava") } implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.20.0") + // For defining custom rules in detekt + implementation("io.gitlab.arturbosch.detekt:detekt-api:1.20.0") implementation("com.googlecode.json-simple:json-simple:1.1") implementation(gradleApi()) diff --git a/buildSrc/src/main/kotlin/plugin/CustomRuleProvider.kt b/buildSrc/src/main/kotlin/plugin/CustomRuleProvider.kt new file mode 100644 index 0000000000..6027d791c0 --- /dev/null +++ b/buildSrc/src/main/kotlin/plugin/CustomRuleProvider.kt @@ -0,0 +1,34 @@ +/* + * Kiwix Android + * Copyright (c) 2023 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package plugin + +import io.gitlab.arturbosch.detekt.api.Config +import io.gitlab.arturbosch.detekt.api.RuleSet +import io.gitlab.arturbosch.detekt.api.RuleSetProvider + +class CustomRulesProvider : RuleSetProvider { + override val ruleSetId: String = "deprecation-rules" + + override fun instance(config: Config): RuleSet = RuleSet( + ruleSetId, + listOf( + DeprecationExclusionRule() + ) + ) +} diff --git a/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt b/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt new file mode 100644 index 0000000000..da12cbbe27 --- /dev/null +++ b/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt @@ -0,0 +1,31 @@ +package plugin + +import io.gitlab.arturbosch.detekt.api.CodeSmell +import io.gitlab.arturbosch.detekt.api.Debt +import io.gitlab.arturbosch.detekt.api.Entity +import io.gitlab.arturbosch.detekt.api.Issue +import io.gitlab.arturbosch.detekt.api.Rule +import io.gitlab.arturbosch.detekt.api.Severity +import org.jetbrains.kotlin.psi.KtImportDirective + +class DeprecationExclusionRule : Rule() { + override val issue = Issue( + id = "DeprecationExclusion", + severity = Severity.CodeSmell, + description = "Deprecated imports should be excluded", + debt = Debt.FIVE_MINS + ) + + override fun visitImportDirective(importDirective: KtImportDirective) { + val importText = importDirective.text + if (importText.contains("@Deprecated")) { + report( + CodeSmell( + issue, + Entity.from(importDirective), + "Deprecated import found: $importText" + ) + ) + } + } +} diff --git a/config/detekt/detekt.yml b/config/detekt/detekt.yml index ec68df6451..be95c17dac 100644 --- a/config/detekt/detekt.yml +++ b/config/detekt/detekt.yml @@ -31,6 +31,11 @@ console-reports: # - 'FindingsReport' - 'FileBasedFindingsReport' +deprecation-rules: + active: true + DeprecationExclusion: + active: true + comments: active: true excludes: "**/test/**,**/androidTest/**,**/*.Test.kt,**/*.Spec.kt,**/*.Spek.kt" @@ -295,7 +300,7 @@ performance: potential-bugs: active: true Deprecation: - active: false + active: true DuplicateCaseInWhenExpression: active: true EqualsAlwaysReturnsTrueOrFalse: From ae5b3a5074fcf224db7e599f04065cfa75e3ae8b Mon Sep 17 00:00:00 2001 From: MohitMali Date: Wed, 5 Jul 2023 19:34:59 +0530 Subject: [PATCH 3/4] Added RuleSetProvider file to define our custom rules --- .../plugin/DeprecationExclusionRules.kt | 2 +- ...tlab.arturbosch.detekt.api.RuleSetProvider | 19 +++++++++++++++++++ config/detekt/detekt.yml | 10 +++++----- 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 buildSrc/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider diff --git a/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt b/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt index da12cbbe27..5009cd1611 100644 --- a/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt +++ b/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt @@ -10,7 +10,7 @@ import org.jetbrains.kotlin.psi.KtImportDirective class DeprecationExclusionRule : Rule() { override val issue = Issue( - id = "DeprecationExclusion", + id = javaClass.simpleName, severity = Severity.CodeSmell, description = "Deprecated imports should be excluded", debt = Debt.FIVE_MINS diff --git a/buildSrc/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider b/buildSrc/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider new file mode 100644 index 0000000000..9c9c11ad78 --- /dev/null +++ b/buildSrc/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider @@ -0,0 +1,19 @@ +# +# Kiwix Android +# Copyright (c) 2023 Kiwix +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# + +plugin.CustomRulesProvider diff --git a/config/detekt/detekt.yml b/config/detekt/detekt.yml index be95c17dac..120dd3d549 100644 --- a/config/detekt/detekt.yml +++ b/config/detekt/detekt.yml @@ -12,6 +12,11 @@ config: # when writing own rules with new properties, exclude the property path e.g.: "my_rule_set,.*>.*>[my_property]" excludes: "" +CustomRulesProvider: + active: true + DeprecationExclusionRule: + active: true + processors: active: true exclude: @@ -31,11 +36,6 @@ console-reports: # - 'FindingsReport' - 'FileBasedFindingsReport' -deprecation-rules: - active: true - DeprecationExclusion: - active: true - comments: active: true excludes: "**/test/**,**/androidTest/**,**/*.Test.kt,**/*.Spec.kt,**/*.Spek.kt" From 9d49bbbf31276c0fd08a53f46391617eb9512505 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Thu, 6 Jul 2023 17:54:25 +0530 Subject: [PATCH 4/4] Removed unnecessary classes from the custom rule and suppressed the Compat21 file because we are handling the deprecated code within it. Therefore, we don't want Detekt to check this file for deprecations. --- buildSrc/build.gradle.kts | 2 -- .../kotlin/plugin/AllProjectConfigurer.kt | 8 ----- .../main/kotlin/plugin/CustomRuleProvider.kt | 34 ------------------- .../plugin/DeprecationExclusionRules.kt | 31 ----------------- ...tlab.arturbosch.detekt.api.RuleSetProvider | 19 ----------- config/detekt/detekt.yml | 5 --- .../kiwixmobile/core/compat/CompatV21.kt | 2 +- .../core/data/remote/KiwixService.kt | 2 ++ 8 files changed, 3 insertions(+), 100 deletions(-) delete mode 100644 buildSrc/src/main/kotlin/plugin/CustomRuleProvider.kt delete mode 100644 buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt delete mode 100644 buildSrc/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 2443ee8098..33a40c49bd 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -22,8 +22,6 @@ dependencies { exclude(group = "com.google.guava", module = "guava") } implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.20.0") - // For defining custom rules in detekt - implementation("io.gitlab.arturbosch.detekt:detekt-api:1.20.0") implementation("com.googlecode.json-simple:json-simple:1.1") implementation(gradleApi()) diff --git a/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt b/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt index 9e74061e52..468574d82a 100644 --- a/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt +++ b/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt @@ -22,8 +22,6 @@ import Config import Libs import com.android.build.api.dsl.CommonExtension import com.android.build.gradle.BaseExtension -import io.gitlab.arturbosch.detekt.Detekt -import io.gitlab.arturbosch.detekt.DetektCreateBaselineTask import io.gitlab.arturbosch.detekt.extensions.DetektExtension import org.gradle.api.Project import org.gradle.kotlin.dsl.apply @@ -69,12 +67,6 @@ class AllProjectConfigurer { target.tasks.withType(KotlinCompile::class.java) { kotlinOptions.jvmTarget = "1.8" } - target.tasks.withType(Detekt::class.java).configureEach { - jvmTarget = "1.8" - } - target.tasks.withType(DetektCreateBaselineTask::class.java).configureEach { - jvmTarget = "1.8" - } buildFeatures.viewBinding = true testOptions { diff --git a/buildSrc/src/main/kotlin/plugin/CustomRuleProvider.kt b/buildSrc/src/main/kotlin/plugin/CustomRuleProvider.kt deleted file mode 100644 index 6027d791c0..0000000000 --- a/buildSrc/src/main/kotlin/plugin/CustomRuleProvider.kt +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2023 Kiwix - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package plugin - -import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.api.RuleSet -import io.gitlab.arturbosch.detekt.api.RuleSetProvider - -class CustomRulesProvider : RuleSetProvider { - override val ruleSetId: String = "deprecation-rules" - - override fun instance(config: Config): RuleSet = RuleSet( - ruleSetId, - listOf( - DeprecationExclusionRule() - ) - ) -} diff --git a/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt b/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt deleted file mode 100644 index 5009cd1611..0000000000 --- a/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt +++ /dev/null @@ -1,31 +0,0 @@ -package plugin - -import io.gitlab.arturbosch.detekt.api.CodeSmell -import io.gitlab.arturbosch.detekt.api.Debt -import io.gitlab.arturbosch.detekt.api.Entity -import io.gitlab.arturbosch.detekt.api.Issue -import io.gitlab.arturbosch.detekt.api.Rule -import io.gitlab.arturbosch.detekt.api.Severity -import org.jetbrains.kotlin.psi.KtImportDirective - -class DeprecationExclusionRule : Rule() { - override val issue = Issue( - id = javaClass.simpleName, - severity = Severity.CodeSmell, - description = "Deprecated imports should be excluded", - debt = Debt.FIVE_MINS - ) - - override fun visitImportDirective(importDirective: KtImportDirective) { - val importText = importDirective.text - if (importText.contains("@Deprecated")) { - report( - CodeSmell( - issue, - Entity.from(importDirective), - "Deprecated import found: $importText" - ) - ) - } - } -} diff --git a/buildSrc/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider b/buildSrc/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider deleted file mode 100644 index 9c9c11ad78..0000000000 --- a/buildSrc/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider +++ /dev/null @@ -1,19 +0,0 @@ -# -# Kiwix Android -# Copyright (c) 2023 Kiwix -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -# - -plugin.CustomRulesProvider diff --git a/config/detekt/detekt.yml b/config/detekt/detekt.yml index 120dd3d549..10e5a48511 100644 --- a/config/detekt/detekt.yml +++ b/config/detekt/detekt.yml @@ -12,11 +12,6 @@ config: # when writing own rules with new properties, exclude the property path e.g.: "my_rule_set,.*>.*>[my_property]" excludes: "" -CustomRulesProvider: - active: true - DeprecationExclusionRule: - active: true - processors: active: true exclude: diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/compat/CompatV21.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/compat/CompatV21.kt index 78f9e8f2c0..180b5f6610 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/compat/CompatV21.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/compat/CompatV21.kt @@ -15,6 +15,7 @@ * along with this program. If not, see . * */ +@file:Suppress("DEPRECATION") package org.kiwix.kiwixmobile.core.compat @@ -26,7 +27,6 @@ import android.net.ConnectivityManager import android.net.ConnectivityManager.TYPE_WIFI import android.net.NetworkInfo.State.CONNECTED -@Suppress("Deprecation") open class CompatV21 : Compat { override fun queryIntentActivities( packageManager: PackageManager, diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/remote/KiwixService.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/remote/KiwixService.kt index a9a8c0fee7..b948da3d47 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/remote/KiwixService.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/remote/KiwixService.kt @@ -15,6 +15,8 @@ * along with this program. If not, see . * */ +@file:Suppress("DEPRECATION") + package org.kiwix.kiwixmobile.core.data.remote import io.reactivex.Observable