Skip to content

Commit

Permalink
refactor(stack): Factor out getProject()
Browse files Browse the repository at this point in the history
Reduce the size of the large `resolveDependencies()` to improve
readability.

Signed-off-by: Frank Viernau <frank_viernau@epam.com>
  • Loading branch information
fviernau committed Jul 3, 2024
1 parent cb1a182 commit 5a9700f
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions plugins/package-managers/stack/src/main/kotlin/Stack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,6 @@ class Stack(
override fun resolveDependencies(definitionFile: File, labels: Map<String, String>): List<ProjectAnalyzerResult> {
val workingDir = definitionFile.parentFile

// Parse project information from the *.cabal file.
val cabalFiles = workingDir.walk().filter {
it.isFile && it.extension == "cabal"
}.toList()

val cabalFile = when (cabalFiles.size) {
0 -> throw IOException("No *.cabal file found in '$workingDir'.")
1 -> cabalFiles.first()
else -> throw IOException("Multiple *.cabal files found in '$cabalFiles'.")
}

val projectPackage = parseCabalFile(cabalFile.readText(), managerName)

val externalDependencies = listDependencies(workingDir, EXTERNAL_SCOPE_NAME)
val testDependencies = listDependencies(workingDir, TEST_SCOPE_NAME)
val benchDependencies = listDependencies(workingDir, BENCH_SCOPE_NAME)
Expand Down Expand Up @@ -132,17 +119,7 @@ class Stack(

val referencedPackageIds = scopes.flatMapTo(mutableSetOf()) { it.collectDependencies() }
val packages = dependencyPackageMap.values.filterTo(mutableSetOf()) { it.id in referencedPackageIds }

val project = Project(
id = projectPackage.id,
definitionFilePath = VersionControlSystem.getPathInfo(definitionFile).path,
authors = projectPackage.authors,
declaredLicenses = projectPackage.declaredLicenses,
vcs = projectPackage.vcs,
vcsProcessed = processProjectVcs(workingDir, projectPackage.vcs, projectPackage.homepageUrl),
homepageUrl = projectPackage.homepageUrl,
scopeDependencies = scopes
)
val project = getProject(definitionFile, scopes)

return listOf(ProjectAnalyzerResult(project, packages))
}
Expand All @@ -169,6 +146,34 @@ class Stack(
return dependenciesJson.parseDependencies()
}

private fun getProject(definitionFile: File, scopes: Set<Scope>): Project {
val workingDir = definitionFile.parentFile

// Parse project information from the *.cabal file.
val cabalFiles = workingDir.walk().filter {
it.isFile && it.extension == "cabal"
}.toList()

val cabalFile = when (cabalFiles.size) {
0 -> throw IOException("No *.cabal file found in '$workingDir'.")
1 -> cabalFiles.first()
else -> throw IOException("Multiple *.cabal files found in '$cabalFiles'.")
}

val projectPackage = parseCabalFile(cabalFile.readText(), managerName)

return Project(
id = projectPackage.id,
definitionFilePath = VersionControlSystem.getPathInfo(definitionFile).path,
authors = projectPackage.authors,
declaredLicenses = projectPackage.declaredLicenses,
vcs = projectPackage.vcs,
vcsProcessed = processProjectVcs(workingDir, projectPackage.vcs, projectPackage.homepageUrl),
homepageUrl = projectPackage.homepageUrl,
scopeDependencies = scopes
)
}

private fun Dependency.toPackage(): Package {
val id = Identifier(
type = "Hackage",
Expand Down

0 comments on commit 5a9700f

Please sign in to comment.