Skip to content

Commit

Permalink
refactor(stack): Move two functions to the class level
Browse files Browse the repository at this point in the history
Reduce the level of nesting to improve readability.

Signed-off-by: Frank Viernau <frank_viernau@epam.com>
  • Loading branch information
fviernau committed Jul 1, 2024
1 parent 32f8d45 commit be27bed
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions plugins/package-managers/stack/src/main/kotlin/Stack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,33 +104,10 @@ class Stack(
val projectPackage = parseCabalFile(cabalFile.readText())
val projectId = projectPackage.id.copy(type = managerName)

fun runStack(vararg command: String): ProcessCapture {
// Delete any left-overs from interrupted stack runs.
workingDir.resolve(".stack-work").safeDeleteRecursively()

return run(workingDir, *command)
}

fun listDependencies(scope: String): List<Dependency> {
val scopeOptions = listOfNotNull(
"--$scope",
// Disable the default inclusion of external dependencies if another scope than "external" is specified.
"--no-$EXTERNAL_SCOPE_NAME".takeIf { scope != EXTERNAL_SCOPE_NAME }
)

val dependenciesJson = runStack(
// Use a hints file for global packages to not require installing the Glasgow Haskell Compiler (GHC).
"ls", "dependencies", "json", "--global-hints", *scopeOptions.toTypedArray()
).stdout

return dependenciesJson.parseDependencies()
}

val allDependencies = mutableSetOf<Dependency>()

val externalDependencyList = listDependencies(EXTERNAL_SCOPE_NAME).also { allDependencies += it }
val testDependencyList = listDependencies(TEST_SCOPE_NAME).also { allDependencies += it }
val benchDependencyList = listDependencies(BENCH_SCOPE_NAME).also { allDependencies += it }
val externalDependencyList = listDependencies(workingDir, EXTERNAL_SCOPE_NAME).also { allDependencies += it }
val testDependencyList = listDependencies(workingDir, TEST_SCOPE_NAME).also { allDependencies += it }
val benchDependencyList = listDependencies(workingDir, BENCH_SCOPE_NAME).also { allDependencies += it }

val dependencyPackageMap = mutableMapOf<Dependency, Package>()

Expand Down Expand Up @@ -200,6 +177,28 @@ class Stack(
return listOf(ProjectAnalyzerResult(project, dependencyPackageMap.values.toSet()))
}

private fun runStack(workingDir: File, vararg command: String): ProcessCapture {
// Delete any left-overs from interrupted stack runs.
workingDir.resolve(".stack-work").safeDeleteRecursively()

return run(workingDir, *command)
}

private fun listDependencies(workingDir: File, scope: String): List<Dependency> {
val scopeOptions = listOfNotNull(
"--$scope",
// Disable the default inclusion of external dependencies if another scope than "external" is specified.
"--no-$EXTERNAL_SCOPE_NAME".takeIf { scope != EXTERNAL_SCOPE_NAME }
)

val dependenciesJson = runStack(
// Use a hints file for global packages to not require installing the Glasgow Haskell Compiler (GHC).
workingDir, "ls", "dependencies", "json", "--global-hints", *scopeOptions.toTypedArray()
).stdout

return dependenciesJson.parseDependencies()
}

private fun getPackageUrl(name: String, version: String) = "https://hackage.haskell.org/package/$name-$version"

private fun downloadCabalFile(pkgId: Identifier): String? {
Expand Down

0 comments on commit be27bed

Please sign in to comment.