Skip to content

Commit

Permalink
Add support to libs declaration in different files
Browse files Browse the repository at this point in the history
  • Loading branch information
jeziellago committed May 28, 2020
1 parent 72c3d6a commit ea3766e
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 64 deletions.
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,45 @@ 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"
}
```

## `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}"
}
```
Original file line number Diff line number Diff line change
@@ -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}"
Expand All @@ -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}"
}
}
4 changes: 4 additions & 0 deletions buildSrc/src/main/java/TestLibs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object TestLibs {

val junit = "junit:junit:${Versions.junitTest}"
}
14 changes: 14 additions & 0 deletions buildSrc/src/main/java/Versions.kt
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 1 addition & 1 deletion buildSrc/versionlint.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lintversioncheck/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String>()
private var buildSrcKotlinFiles = mutableMapOf<String, File>()

override fun checkDslPropertyAssignment(
context: GradleContext,
Expand Down Expand Up @@ -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<String>,
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()
}
Expand All @@ -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)) {
Expand All @@ -138,11 +140,17 @@ class VersionCheckerGradleLint : Detector(), Detector.GradleScanner {
return extractedLibrary
}

private fun getDependenciesFileLines(dependenciesFile: File): List<String> {
if (dependenciesFileLines.isEmpty()) {
dependenciesFileLines = dependenciesFile.readLines()
private fun getMapOfKotlinFilesFromBuildSrc(
buildSrcDir: File,
librariesFileSuffix: String,
versionFileName: String
): Map<String, File> {
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 {
Expand All @@ -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(
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -226,7 +233,7 @@ class VersionCheckerGradleLint : Detector(), Detector.GradleScanner {
7,
Severity.WARNING,
IMPLEMENTATION
).setEnabledByDefault(false)
).setEnabledByDefault(true)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -22,3 +21,18 @@ internal fun File.findBuildSrcFromProjectDir(buildSrcModuleName: String = BUILD_
}
return null
}

internal fun File.findKotlinFilesWithSuffix(suffix: List<String>): Map<String, File> {
val kotlinFilesMap = mutableMapOf<String, File>()
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
Empty file.
Empty file.

0 comments on commit ea3766e

Please # to comment.