Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ buildscript {
}

plugins {
id("com.autonomousapps.dependency-analysis") version "0.78.0"
id("com.github.ben-manes.versions") version "0.39.0"
id("io.gitlab.arturbosch.detekt") version "1.18.1"
id("org.jetbrains.dokka") version "1.5.31"
Expand Down
2 changes: 2 additions & 0 deletions dependabot-bridge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ dependencies {
dependencySync("io.kotest:kotest-property-jvm:4.6.3")
dependencySync("io.kotest:kotest-runner-junit5-jvm:4.6.3")
dependencySync("javax.inject:javax.inject:1")
dependencySync("org.antlr:antlr4:4.9.2")
dependencySync("org.antlr:antlr4-runtime:4.9.2")
dependencySync("net.swiftzer.semver:semver:1.1.1")
dependencySync("org.codehaus.groovy:groovy-xml:3.0.9")
dependencySync("org.codehaus.groovy:groovy:3.0.9")
Expand Down
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
androidTools = "7.0.2"
antlr = "4.9.2"
anvil = "2.3.4"
benManes = "0.36.0"
changeTracker = "0.7.4"
Expand All @@ -23,6 +24,9 @@ taskTree = "1.5"
[libraries]
agp = { module = "com.android.tools.build:gradle", version.ref = "androidTools" }

antlr-core = { module = "org.antlr:antlr4", version.ref = "antlr" }
antlr-runtime = { module = "org.antlr:antlr4-runtime", version.ref = "antlr" }

anvil = { module = "com.squareup.anvil:gradle-plugin", version.ref = "anvil" }

groovy = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface Fixable : Finding {
}

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

val text = buildFile.readText()

val statementText = statementTextOrNull ?: return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ fun Collection<File>.jvmFiles(
bindingContext: BindingContext
) = flatMap { it.jvmFiles(bindingContext) }

fun FileTreeWalk.dirs(): Sequence<File> = asSequence().filter { it.isDirectory }
fun FileTreeWalk.files(): Sequence<File> = asSequence().filter { it.isFile }

fun File.existsOrNull(): File? = if (exists()) this else null
2 changes: 2 additions & 0 deletions modulecheck-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dependencies {
api(libs.kotlin.compiler)

api(projects.modulecheckApi)
api(projects.modulecheckParsing.api)
api(projects.modulecheckParsing.groovyAntlr)
api(projects.modulecheckParsing.psi)

implementation(libs.agp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import modulecheck.parsing.DependenciesBlock
import modulecheck.parsing.DependencyBlockParser
import modulecheck.parsing.PluginBlockParser
import modulecheck.parsing.PluginsBlock
import modulecheck.parsing.groovy.antlr.GroovyDependencyBlockParser
import modulecheck.parsing.groovy.antlr.GroovyPluginsBlockParser
import modulecheck.parsing.psi.KotlinDependencyBlockParser
import modulecheck.parsing.psi.KotlinPluginsBlockParser
import modulecheck.parsing.psi.internal.asKtFile
Expand All @@ -28,6 +30,7 @@ import java.io.File
fun DependencyBlockParser.Companion.parse(file: File): List<DependenciesBlock> {
return when {
file.isKotlinFile(listOf("kts")) -> KotlinDependencyBlockParser().parse(file.asKtFile())
file.extension == "gradle" -> GroovyDependencyBlockParser().parse(file.readText())
else -> throw IllegalArgumentException(
"The file argument must be either a `*.gradle.kts` file or `*.gradle`. " +
"The supplied argument was `${file.name}`"
Expand All @@ -38,6 +41,7 @@ fun DependencyBlockParser.Companion.parse(file: File): List<DependenciesBlock> {
fun PluginBlockParser.Companion.parse(file: File): PluginsBlock? {
return when {
file.isKotlinFile(listOf("kts")) -> KotlinPluginsBlockParser().parse(file.asKtFile())
file.extension == "gradle" -> GroovyPluginsBlockParser().parse(file.readText())
else -> throw IllegalArgumentException(
"The file argument must be either a `*.gradle.kts` file or `*.gradle`. " +
"The supplied argument was `${file.name}`"
Expand Down
44 changes: 44 additions & 0 deletions modulecheck-parsing/groovy-antlr/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2021 Rick Busarow
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
javaLibrary
id("com.vanniktech.maven.publish")
groovy
}

dependencies {

compileOnly(gradleApi())

implementation(libs.kotlin.compiler)

api(projects.modulecheckParsing.api)

implementation(libs.antlr.core)
implementation(libs.antlr.runtime)

implementation(libs.agp)
implementation(libs.javaParser)
implementation(libs.kotlin.reflect)
implementation(libs.groovy)

testImplementation(libs.bundles.hermit)
testImplementation(libs.bundles.jUnit)
testImplementation(libs.bundles.kotest)

testImplementation(projects.modulecheckInternalTesting)
testImplementation(projects.modulecheckSpecs)
}
18 changes: 18 additions & 0 deletions modulecheck-parsing/groovy-antlr/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (C) 2021 Rick Busarow
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

POM_NAME=modulecheck-parsing-groovy-antlr
POM_ARTIFACT_ID=modulecheck-parsing-groovy-antlr
POM_PACKAGING=jar
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2021 Rick Busarow
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package modulecheck.parsing.groovy.antlr

import modulecheck.parsing.DependenciesBlock

class GroovyDependenciesBlock(
contentString: String
) : DependenciesBlock(contentString) {

override fun findOriginalStringIndex(parsedString: String) = originalLines
.indexOfFirst { originalLine ->
originalLine.collapseBlockComments()
.trimEachLineStart()
.trimLinesLikeAntlr()
.lines()
.any { it.startsWith(parsedString) }
}

override fun toString(): String {
return "GroovyDependenciesBlock(\n" +
"\tallModuleDeclarations=${
allModuleDeclarations.toList()
.joinToString(",\n\t\t", "\n\t\t")
},\n" +
")"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright (C) 2021 Rick Busarow
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:Suppress("RegExpRedundantEscape")

package modulecheck.parsing.groovy.antlr

import groovyjarjarantlr4.v4.runtime.CharStreams
import groovyjarjarantlr4.v4.runtime.CommonTokenStream
import groovyjarjarantlr4.v4.runtime.tree.RuleNode
import modulecheck.parsing.MavenCoordinates
import org.apache.groovy.parser.antlr4.GroovyLangLexer
import org.apache.groovy.parser.antlr4.GroovyLangParser
import org.apache.groovy.parser.antlr4.GroovyParser.BlockStatementContext
import org.apache.groovy.parser.antlr4.GroovyParser.PostfixExpressionContext
import org.apache.groovy.parser.antlr4.GroovyParser.ScriptStatementContext
import org.apache.groovy.parser.antlr4.GroovyParser.StringLiteralContext
import org.apache.groovy.parser.antlr4.GroovyParserBaseVisitor

class GroovyDependencyBlockParser {

fun parse(file: String): List<GroovyDependenciesBlock> {
val dependenciesBlocks = mutableListOf<GroovyDependenciesBlock>()

val flattened = file.collapseBlockComments()
.trimEachLineStart()

val lexer = GroovyLangLexer(CharStreams.fromString(flattened))
val tokens = CommonTokenStream(lexer)

val rawModuleNameVisitor = object : GroovyParserBaseVisitor<String?>() {

override fun shouldVisitNextChild(
node: RuleNode?,
currentResult: String?
): Boolean = currentResult == null

override fun visitStringLiteral(ctx: StringLiteralContext?): String? {
return ctx?.text?.replace("""["']""".toRegex(), "")
}
}

val projectDepVisitor = object : GroovyParserBaseVisitor<String?>() {

override fun shouldVisitNextChild(
node: RuleNode?,
currentResult: String?
): Boolean = currentResult == null

override fun visitPostfixExpression(ctx: PostfixExpressionContext?): String? {
return visitChildren(ctx) ?: when (ctx?.start?.text) {
"projects" -> {
ctx.text.removePrefix("projects.")
}
"project" -> {
ctx.accept(rawModuleNameVisitor)
}
else -> {
null
}
}
}
}

val visitor = object : GroovyParserBaseVisitor<Unit>() {

override fun visitScriptStatement(ctx: ScriptStatementContext?) {
super.visitScriptStatement(ctx)

val statement = ctx?.statement()

if (statement?.start?.text == "dependencies") {
val blockBodyReg = """dependencies\s*\{([\s\S]*)\}""".toRegex()

val blockBody = blockBodyReg.find(file)
?.groupValues
?.get(1)
?.removePrefix("\n")
?: return

val dependenciesBlock = GroovyDependenciesBlock(blockBody)

val blockStatementVisitor = object : GroovyParserBaseVisitor<Unit>() {

override fun visitBlockStatement(ctx: BlockStatementContext) {
super.visitBlockStatement(ctx)

val config = ctx.start.text

val moduleRef = projectDepVisitor.visit(ctx)

if (moduleRef != null) {
dependenciesBlock.addModuleStatement(
moduleRef = moduleRef,
configName = config,
parsedString = ctx.text
)
return
}

val mavenCoordinates = rawModuleNameVisitor.visit(ctx)
?.let { MavenCoordinates.parseOrNull(it) }

if (mavenCoordinates != null) {
dependenciesBlock.addNonModuleStatement(
configName = config,
parsedString = ctx.text,
coordinates = mavenCoordinates
)
return
}

dependenciesBlock.addUnknownStatement(config, ctx.text)
}
}

blockStatementVisitor.visit(ctx)

dependenciesBlocks.add(dependenciesBlock)
}
}
}

GroovyLangParser(tokens)
.compilationUnit()
.accept(visitor)

return dependenciesBlocks
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2021 Rick Busarow
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package modulecheck.parsing.groovy.antlr

import modulecheck.parsing.PluginsBlock

class GroovyPluginsBlock(
contentString: String
) : PluginsBlock(contentString) {

override fun findOriginalStringIndex(parsedString: String) = originalLines
.indexOfFirst { originalLine ->
originalLine.collapseBlockComments()
.trimEachLineStart()
.trimLinesLikeAntlr()
.lines()
.any { it.startsWith(parsedString) }
}

override fun toString(): String {
return "GroovyPluginsBlock(\n" +
"\tallDeclarations=${
allDeclarations.toList()
.joinToString(",\n\t\t", "\n\t\t")
},\n" +
")"
}
}
Loading