@@ -144,110 +144,6 @@ class LibraryDependencyFinder {
144144 return plugins
145145 }
146146
147- fun findImportedLibraries (sourceDir : File , libraries : List <LibraryInfo >): List <String > {
148- if (! sourceDir.exists() || ! sourceDir.isDirectory) {
149- println (" LibraryDependencyFinder: Source directory ${sourceDir.absolutePath} does not exist or is not a directory" )
150- return emptyList()
151- }
152-
153- val usedLibraries = mutableSetOf<String >()
154- val importRegex = """ import\s+([\w.]+\*?)\s*""" .toRegex()
155-
156- println (" LibraryDependencyFinder: Scanning for imports in ${sourceDir.absolutePath} " )
157-
158- val sourceFiles = sourceDir.walkTopDown()
159- .filter { it.isFile && (it.extension == " kt" || it.extension == " java" ) }
160- .toList()
161-
162- if (sourceFiles.isEmpty()) {
163- println (" LibraryDependencyFinder: No source files found" )
164- return emptyList()
165- }
166-
167- println (" LibraryDependencyFinder: Found ${sourceFiles.size} source files" )
168-
169- val allImports = mutableSetOf<String >()
170- sourceFiles.forEach { file ->
171- val content = file.readText()
172- importRegex.findAll(content).forEach { matchResult ->
173- allImports.add(matchResult.groupValues[1 ])
174- }
175- }
176-
177- val fileContent = sourceFiles.joinToString(" \n " ) { it.readText() }
178-
179- libraries.forEach { library ->
180- val group = library.group
181- val artifact = library.artifact.replace(" -" , Constants .EMPTY )
182-
183- val usageScore = calculateLibraryUsageScore(group, artifact, allImports, fileContent)
184-
185- if (usageScore >= LIBRARY_USAGE_THRESHOLD ) {
186- usedLibraries.add(library.alias)
187- println (" LibraryDependencyFinder: Detected usage of library ${library.alias} (${library.group} :${library.artifact} ) with score $usageScore " )
188- } else {
189- println (" LibraryDependencyFinder: Library ${library.alias} (${library.group} :${library.artifact} ) not detected (score: $usageScore )" )
190- }
191- }
192-
193- println (" LibraryDependencyFinder: Found ${usedLibraries.size} used libraries out of ${libraries.size} available" )
194- return usedLibraries.toList()
195- }
196-
197- private fun calculateLibraryUsageScore (
198- group : String ,
199- artifact : String ,
200- imports : Set <String >,
201- fileContent : String ,
202- ): Int {
203- val normalizedArtifact = artifact.replace(" -" , Constants .EMPTY )
204- var score = 0
205-
206- val groupParts = group.split(" ." )
207- val lastGroupPart = groupParts.lastOrNull()?.replace(" -" , Constants .EMPTY ).orEmpty()
208-
209- if (imports.any { it == " $group .$artifact " || it == " $group .$normalizedArtifact " }) {
210- println (" - Found exact import match for $group .$artifact " )
211- score + = 3
212- }
213-
214- if (imports.any { it == " $group .*" }) {
215- println (" - Found wildcard import for $group .*" )
216- score + = 1
217- }
218-
219- if (imports.any { it.startsWith(" $group .$artifact ." ) || it.startsWith(" $group .$normalizedArtifact ." ) }) {
220- println (" - Found subpackage imports for $group .$artifact " )
221- score + = 2
222- }
223-
224- val hasGroupImports = imports.any { it.startsWith(" $group ." ) }
225- if (hasGroupImports) {
226- println (" - Found imports starting with $group " )
227- score + = 1
228- }
229-
230- if (lastGroupPart.isNotEmpty() && artifact.isNotEmpty() &&
231- imports.any { it.contains(lastGroupPart) && it.contains(normalizedArtifact) }
232- ) {
233- println (" - Found imports containing both $lastGroupPart and $normalizedArtifact " )
234- score + = 2
235- }
236-
237- val capitalizedArtifact = normalizedArtifact.replaceFirstChar { it.uppercase() }
238- if (capitalizedArtifact.length > 3 && fileContent.contains(capitalizedArtifact)) {
239- println (" - Found class usage: $capitalizedArtifact " )
240- score + = 1
241- }
242-
243- if (lastGroupPart.length > 3 && imports.any { it.contains(lastGroupPart) }) {
244- println (" - Found imports containing $lastGroupPart " )
245- score + = 1
246- }
247-
248- return score
249- }
250-
251147 fun formatLibraryDependencies (libraryAliases : List <String >): String {
252148 if (libraryAliases.isEmpty()) return Constants .EMPTY
253149
@@ -270,8 +166,4 @@ class LibraryDependencyFinder {
270166 }
271167 }.toString()
272168 }
273-
274- companion object {
275- private const val LIBRARY_USAGE_THRESHOLD = 2
276- }
277169}
0 commit comments