From ea3766ede7472ecb5a1c6c7d58c7530987e78f79 Mon Sep 17 00:00:00 2001 From: Jeziel Lago Date: Fri, 22 May 2020 14:12:30 -0300 Subject: [PATCH] Add support to libs declaration in different files --- README.md | 29 +++++---- .../main/java/{Dependencies.kt => Libs.kt} | 22 +------ buildSrc/src/main/java/TestLibs.kt | 4 ++ buildSrc/src/main/java/Versions.kt | 14 +++++ buildSrc/versionlint.properties | 2 +- gradle.properties | 2 +- lintversioncheck/build.gradle.kts | 4 +- .../versioncheck/VersionCheckerGradleLint.kt | 61 +++++++++++-------- .../versioncheck/extensions/FileExtensions.kt | 16 ++++- .../extensions/FileExtensionsTest.kt | 21 +++++++ .../test/resources/kotlin-files/lib/MyLibs.kt | 0 .../kotlin-files/version/Versions.kt | 0 12 files changed, 111 insertions(+), 64 deletions(-) rename buildSrc/src/main/java/{Dependencies.kt => Libs.kt} (55%) create mode 100644 buildSrc/src/main/java/TestLibs.kt create mode 100644 buildSrc/src/main/java/Versions.kt create mode 100644 lintversioncheck/src/test/resources/kotlin-files/lib/MyLibs.kt create mode 100644 lintversioncheck/src/test/resources/kotlin-files/version/Versions.kt diff --git a/README.md b/README.md index eff5a52..62e4c71 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,10 @@ Warning on new versions available even when using Kotlin-DSL plugin. ![](example.png) ## How to use? -- Add lint dependency -```groovy -dependencies { - lintChecks "com.picpay.gradlelint:version-checker:VERSION" -} -``` -- Enable lint with `lintOptions` +- Add lint dependency: +> copy `version-checker.jar` to `HOME/.android/lint/` + +- Enable/Disable lint with `lintOptions` (default: `enabled`) ```groovy lintOptions { enable "VersionCheckerGradleLint" @@ -18,24 +15,34 @@ lintOptions { ``` ## `buildSrc` module with kotlin-dsl plugin -- `buildSrc/src/main/java/Dependencies.kt` -```kotlin -// file: Dependencies.kt +### Create `version` file +```kotlin object Versions { val kotlinVersion = "1.3.70" val junit4Version = "4.12" } - +``` +### Create lib files +```kotlin object Libs { val kotlin = "org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlinVersion}" } +``` +```kotlin object TestLibs { val junit4 = "junit:junit:${Versions.junit4Version}" } +``` + +```kotlin +object OtherLibs { + + val myLib = "mylib:mylib:${Versions.myLib}" +} ``` \ No newline at end of file diff --git a/buildSrc/src/main/java/Dependencies.kt b/buildSrc/src/main/java/Libs.kt similarity index 55% rename from buildSrc/src/main/java/Dependencies.kt rename to buildSrc/src/main/java/Libs.kt index 7bbaed3..5c5b612 100644 --- a/buildSrc/src/main/java/Dependencies.kt +++ b/buildSrc/src/main/java/Libs.kt @@ -1,18 +1,3 @@ -object Versions { - // Libs - const val appCompatVersion = "1.0.0" - const val retrofitVersion = "2.6.4" - - const val playServiceLocation = "16.0.0" - - const val koinCoreVersion = "2.0.1" - - const val mpchart = "v3.1.0-alpha" - - // Test Libs - const val junitTest = "4.12" -} - object Libs { val koinCore = "org.koin:koin-core:${Versions.koinCoreVersion}" @@ -22,9 +7,4 @@ object Libs { "com.squareup.retrofit2:retrofit:${Versions.retrofitVersion}" val playServiceLocation = "com.google.android.gms:play-services-location:${Versions.playServiceLocation}" -} - -object TestLibs { - - val junit = "junit:junit:${Versions.junitTest}" -} +} \ No newline at end of file diff --git a/buildSrc/src/main/java/TestLibs.kt b/buildSrc/src/main/java/TestLibs.kt new file mode 100644 index 0000000..45eb085 --- /dev/null +++ b/buildSrc/src/main/java/TestLibs.kt @@ -0,0 +1,4 @@ +object TestLibs { + + val junit = "junit:junit:${Versions.junitTest}" +} \ No newline at end of file diff --git a/buildSrc/src/main/java/Versions.kt b/buildSrc/src/main/java/Versions.kt new file mode 100644 index 0000000..9c14f94 --- /dev/null +++ b/buildSrc/src/main/java/Versions.kt @@ -0,0 +1,14 @@ +object Versions { + // Libs + const val appCompatVersion = "1.0.0" + const val retrofitVersion = "2.6.4" + + const val playServiceLocation = "16.0.0" + + const val koinCoreVersion = "2.0.1" + + const val mpchart = "v3.1.0-alpha" + + // Test Libs + const val junitTest = "4.12" +} \ No newline at end of file diff --git a/buildSrc/versionlint.properties b/buildSrc/versionlint.properties index 57f5927..33e0449 100644 --- a/buildSrc/versionlint.properties +++ b/buildSrc/versionlint.properties @@ -2,5 +2,5 @@ #Thu May 14 13:30:17 BRT 2020 versionlint.cache.time.minutes=60 versionlint.prerelease.enable=true -versionlint.dependencies.file=Dependencies +versionlint.versions.file=Versions versionlint.dependencies.suffix=Libs diff --git a/gradle.properties b/gradle.properties index 975a57a..ba93746 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,7 +21,7 @@ android.enableJetifier=true kotlin.code.style=official # Info to Maven Publish -library.version=0.1.1 +library.version=0.1.2 library.groupId=com.picpay.gradlelint library.artifactId=version-checker library.repository=version-checker-gradle-lint diff --git a/lintversioncheck/build.gradle.kts b/lintversioncheck/build.gradle.kts index 853c3e7..a40f871 100644 --- a/lintversioncheck/build.gradle.kts +++ b/lintversioncheck/build.gradle.kts @@ -14,8 +14,8 @@ repositories { dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72") - api("com.android.tools.lint:lint-api:26.6.3") - api("com.android.tools.lint:lint-checks:26.6.3") + implementation("com.android.tools.lint:lint-api:26.6.3") + implementation("com.android.tools.lint:lint-checks:26.6.3") testImplementation("junit:junit:4.13") } diff --git a/lintversioncheck/src/main/java/com/picpay/gradlelint/versioncheck/VersionCheckerGradleLint.kt b/lintversioncheck/src/main/java/com/picpay/gradlelint/versioncheck/VersionCheckerGradleLint.kt index 572a052..8c311f6 100644 --- a/lintversioncheck/src/main/java/com/picpay/gradlelint/versioncheck/VersionCheckerGradleLint.kt +++ b/lintversioncheck/src/main/java/com/picpay/gradlelint/versioncheck/VersionCheckerGradleLint.kt @@ -11,6 +11,7 @@ import com.picpay.gradlelint.versioncheck.api.Api import com.picpay.gradlelint.versioncheck.cache.RepositoryCache import com.picpay.gradlelint.versioncheck.extensions.containsVersionNumber import com.picpay.gradlelint.versioncheck.extensions.findBuildSrcFromProjectDir +import com.picpay.gradlelint.versioncheck.extensions.findKotlinFilesWithSuffix import com.picpay.gradlelint.versioncheck.extensions.getVarNameInVersionDeclaration import com.picpay.gradlelint.versioncheck.extensions.getVarValueFromVersionsFileLines import com.picpay.gradlelint.versioncheck.extensions.isVersionNumber @@ -30,7 +31,7 @@ class VersionCheckerGradleLint : Detector(), Detector.GradleScanner { private var repositoryHandler: MavenRemoteRepositoryHandler? = null private var buildSrcDir: File? = null private var versionsProperties: Properties? = null - private var dependenciesFileLines = emptyList() + private var buildSrcKotlinFiles = mutableMapOf() override fun checkDslPropertyAssignment( context: GradleContext, @@ -73,49 +74,48 @@ class VersionCheckerGradleLint : Detector(), Detector.GradleScanner { ): Library? { val properties = readVersionCheckerProperties(context.project.dir) - val dependenciesFileName = properties.getProperty(LINT_DEPENDENCIES_PROPERTY) - val versionsFile = properties.getProperty(LINT_VERSIONS_PROPERTY) + val versionsFileName = properties.getProperty(LINT_VERSIONS_PROPERTY) + val suffix = properties.getProperty(LINT_SUFFIX_PROPERTY) val enableCheckForPreReleases: Boolean = properties .getProperty(LINT_ENABLE_CHECK_PRE_RELEASES) ?.toBoolean() ?: false - if (versionsFile != dependenciesFileName) { - //TODO: fazer a busca em arquivos diferentes - } - - val linesFromDependencyFile = getDependenciesFileLines( - dependenciesFile = File( - getBuildSrcDir(context.project.dir).absolutePath, - "src${File.separator}" + - "main${File.separator}" + - "java${File.separator}" + - "$dependenciesFileName.kt" - ) + val librariesKotlinFiles = getMapOfKotlinFilesFromBuildSrc( + buildSrcDir = getBuildSrcDir(context.project.dir), + librariesFileSuffix = suffix, + versionFileName = versionsFileName ) + val nameToFindLibDeclaration = value.split(".").first() + val libraryDeclarationFile: File = librariesKotlinFiles[nameToFindLibDeclaration] ?: return null + val versionDeclarationFile: File = librariesKotlinFiles[versionsFileName] ?: return null + return extractLibraryFromFileLines( valueInGradle = value, - fileLines = linesFromDependencyFile, + libraryDeclarationFile = libraryDeclarationFile, + versionDeclarationFile = versionDeclarationFile, enableCheckForPreReleases = enableCheckForPreReleases ) } private fun extractLibraryFromFileLines( valueInGradle: String, - fileLines: List, + libraryDeclarationFile: File, + versionDeclarationFile: File, enableCheckForPreReleases: Boolean ): Library? { var extractedLibrary: Library? = null + val libraryDeclarationFileLines = libraryDeclarationFile.readLines() - fileLines.forEachIndexed { index, line -> + libraryDeclarationFileLines.forEachIndexed { index, line -> val dependencyVarName = valueInGradle.split(".")[1] if (line.tokenize().contains(dependencyVarName) && !line.containsVersionNumber()) { val dependency = if (!line.contains("$")) { - dependenciesFileLines[index + 1].removeComments() + libraryDeclarationFileLines[index + 1].removeComments() } else { line.split("=")[1].removeComments() } @@ -126,7 +126,9 @@ class VersionCheckerGradleLint : Detector(), Detector.GradleScanner { .trim() val versionVarName = dependencyCleaned[1].getVarNameInVersionDeclaration() - val versionNumber = fileLines.getVarValueFromVersionsFileLines(versionVarName) + val versionNumber = versionDeclarationFile.readLines() + .getVarValueFromVersionsFileLines(versionVarName) + val version = versionNumber.replace("\"", "") if (version.isVersionNumber(enableCheckForPreReleases)) { @@ -138,11 +140,17 @@ class VersionCheckerGradleLint : Detector(), Detector.GradleScanner { return extractedLibrary } - private fun getDependenciesFileLines(dependenciesFile: File): List { - if (dependenciesFileLines.isEmpty()) { - dependenciesFileLines = dependenciesFile.readLines() + private fun getMapOfKotlinFilesFromBuildSrc( + buildSrcDir: File, + librariesFileSuffix: String, + versionFileName: String + ): Map { + if (buildSrcKotlinFiles.isEmpty()) { + val whatToFind = listOf(librariesFileSuffix, versionFileName) + buildSrcDir.findKotlinFilesWithSuffix(whatToFind) + .also { fileMap -> buildSrcKotlinFiles.putAll(fileMap) } } - return dependenciesFileLines + return buildSrcKotlinFiles } private fun readVersionCheckerProperties(projectDir: File): Properties { @@ -153,8 +161,8 @@ class VersionCheckerGradleLint : Detector(), Detector.GradleScanner { ) Properties().apply { if (!versionLintPropertiesFile.exists()) { - put(LINT_DEPENDENCIES_PROPERTY, "Dependencies") put(LINT_SUFFIX_PROPERTY, "Libs") + put(LINT_VERSIONS_PROPERTY, "Versions") put(LINT_ENABLE_CHECK_PRE_RELEASES, "false") put(LINT_CACHE_LIFETIME, "60") store( @@ -198,7 +206,6 @@ class VersionCheckerGradleLint : Detector(), Detector.GradleScanner { companion object { private const val LINT_PROPERTIES = "versionlint.properties" - private const val LINT_DEPENDENCIES_PROPERTY = "versionlint.dependencies.file" private const val LINT_SUFFIX_PROPERTY = "versionlint.dependencies.suffix" private const val LINT_VERSIONS_PROPERTY = "versionlint.versions.file" private const val LINT_ENABLE_CHECK_PRE_RELEASES = "versionlint.prerelease.enable" @@ -226,7 +233,7 @@ class VersionCheckerGradleLint : Detector(), Detector.GradleScanner { 7, Severity.WARNING, IMPLEMENTATION - ).setEnabledByDefault(false) + ).setEnabledByDefault(true) } } \ No newline at end of file diff --git a/lintversioncheck/src/main/java/com/picpay/gradlelint/versioncheck/extensions/FileExtensions.kt b/lintversioncheck/src/main/java/com/picpay/gradlelint/versioncheck/extensions/FileExtensions.kt index 6bb585d..71c4c4d 100644 --- a/lintversioncheck/src/main/java/com/picpay/gradlelint/versioncheck/extensions/FileExtensions.kt +++ b/lintversioncheck/src/main/java/com/picpay/gradlelint/versioncheck/extensions/FileExtensions.kt @@ -10,7 +10,6 @@ internal fun File.findBuildSrcFromProjectDir(buildSrcModuleName: String = BUILD_ val currentDir = File(dir) val containsBuildSrc = currentDir.listFiles() - ?.asList() ?.any { it.name == buildSrcModuleName } ?: false @@ -22,3 +21,18 @@ internal fun File.findBuildSrcFromProjectDir(buildSrcModuleName: String = BUILD_ } return null } + +internal fun File.findKotlinFilesWithSuffix(suffix: List): Map { + val kotlinFilesMap = mutableMapOf() + return if (!this.isDirectory) emptyMap() + else { + listFiles()?.forEach { file -> + if (file.isDirectory) { + kotlinFilesMap.putAll(file.findKotlinFilesWithSuffix(suffix)) + } else if (file.isFile && suffix.any { file.name.endsWith("$it.kt") }) { + kotlinFilesMap[file.nameWithoutExtension] = file + } + } + kotlinFilesMap + } +} diff --git a/lintversioncheck/src/test/java/com/picpay/gradlelint/versioncheck/extensions/FileExtensionsTest.kt b/lintversioncheck/src/test/java/com/picpay/gradlelint/versioncheck/extensions/FileExtensionsTest.kt index 71282bf..8669499 100644 --- a/lintversioncheck/src/test/java/com/picpay/gradlelint/versioncheck/extensions/FileExtensionsTest.kt +++ b/lintversioncheck/src/test/java/com/picpay/gradlelint/versioncheck/extensions/FileExtensionsTest.kt @@ -20,4 +20,25 @@ internal class FileExtensionsTest { assertEquals(expected, resultFile!!.absolutePath) } + + @Test + fun `findKotlinFilesWithSuffix with parent dir as File Should get kotlin files`() { + val parentDir = File("").absolutePath + val projectDir = File(parentDir, "src/test/resources") + + val pathVersionsFile = "${projectDir.absolutePath}/kotlin-files/version/Versions.kt" + val pathLibFile = "${projectDir.absolutePath}/kotlin-files/lib/MyLibs.kt" + + val fileMap = projectDir.findKotlinFilesWithSuffix(listOf("Libs", "Versions")) + + assertEquals( + "Check Versions File", + pathVersionsFile, + (fileMap["Versions"] ?: error("Versions not found")).absolutePath + ) + assertEquals( + "Check Lib File", + pathLibFile, (fileMap["MyLibs"] ?: error("MyLibs not found")).absolutePath + ) + } } \ No newline at end of file diff --git a/lintversioncheck/src/test/resources/kotlin-files/lib/MyLibs.kt b/lintversioncheck/src/test/resources/kotlin-files/lib/MyLibs.kt new file mode 100644 index 0000000..e69de29 diff --git a/lintversioncheck/src/test/resources/kotlin-files/version/Versions.kt b/lintversioncheck/src/test/resources/kotlin-files/version/Versions.kt new file mode 100644 index 0000000..e69de29