Skip to content

Commit

Permalink
refactor(GoMod): Re-arrange functions within GoMod
Browse files Browse the repository at this point in the history
Group the functions which obtain data from Go tooling together, and also
the ones which map that data to ORT's model.

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Oct 30, 2023
1 parent e38a6c7 commit 1a7d15f
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions analyzer/src/main/kotlin/managers/GoMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -209,49 +209,6 @@ class GoMod(
return graph.breakCycles()
}

private fun ModuleInfo.toId(): Identifier {
if (replace != null) return replace.toId() // Apply replace directive.

return if (version.isBlank()) {
// If the version is blank, it is a project in ORT speak.
checkNotNull(dir) { "For projects, the directory is expected to not be null." }

val projectDir = File(dir).absoluteFile

require(projectDir.startsWith(analysisRoot)) {
"A replace directive references a module in '$projectDir' outside of analysis root which is not " +
"supported."
}

Identifier(
type = managerName,
namespace = "",
name = getProjectName(projectDir),
version = processProjectVcs(projectDir).revision
)
} else {
// If the version is not blank, it is a package in ORT speak.
Identifier(
type = "Go",
namespace = "",
name = path,
version = normalizeModuleVersion(version)
)
}
}

private fun getProjectName(projectDir: File): String {
projectDir.resolve("go.mod").also { goModFile ->
require(goModFile.isFile) {
"Expected file '$goModFile' which does not exist."
}
}

val list = runGo("list", "-m", "-json", "-buildvcs=false", workingDir = projectDir)

return list.stdout.byteInputStream().use { JSON.decodeToSequence<ModuleInfo>(it) }.single().path
}

/**
* Return the list of all modules available via the `go list` command.
*/
Expand All @@ -261,6 +218,20 @@ class GoMod(
return list.stdout.byteInputStream().use { JSON.decodeToSequence<ModuleInfo>(it) }.toList()
}

/**
* Return the module names of all transitive main module dependencies. This excludes test-only dependencies.
*/
private fun getTransitiveMainModuleDependencies(projectDir: File): Set<String> {
// See https://pkg.go.dev/text/template for the format syntax.
val list = runGo("list", "-deps", "-json=Module", "-buildvcs=false", "./...", workingDir = projectDir)

val depInfos = list.stdout.byteInputStream().use { JSON.decodeToSequence<DepInfo>(it) }

return depInfos.mapNotNullTo(mutableSetOf()) { depInfo ->
depInfo.module?.path
}
}

/**
* Return the subset of the modules in [graph] required for building and testing the main module. So, test
* dependencies of dependencies are filtered out. The [GoModule]s in [Graph] must not have the replace directive
Expand All @@ -281,17 +252,49 @@ class GoMod(
return graph.nodes.filterTo(mutableSetOf()) { it.name in vendorModuleNames }
}

/**
* Return the module names of all transitive main module dependencies. This excludes test-only dependencies.
*/
private fun getTransitiveMainModuleDependencies(projectDir: File): Set<String> {
// See https://pkg.go.dev/text/template for the format syntax.
val list = runGo("list", "-deps", "-json=Module", "-buildvcs=false", "./...", workingDir = projectDir)
private fun runGo(vararg args: CharSequence, workingDir: File? = null) =

Check warning on line 255 in analyzer/src/main/kotlin/managers/GoMod.kt

View check run for this annotation

Codecov / codecov/patch

analyzer/src/main/kotlin/managers/GoMod.kt#L255

Added line #L255 was not covered by tests
run(args = args, workingDir = workingDir, environment = environment)

val depInfos = list.stdout.byteInputStream().use { JSON.decodeToSequence<DepInfo>(it) }
private fun getProjectName(projectDir: File): String {
projectDir.resolve("go.mod").also { goModFile ->
require(goModFile.isFile) {
"Expected file '$goModFile' which does not exist."

Check warning on line 261 in analyzer/src/main/kotlin/managers/GoMod.kt

View check run for this annotation

Codecov / codecov/patch

analyzer/src/main/kotlin/managers/GoMod.kt#L261

Added line #L261 was not covered by tests
}
}

return depInfos.mapNotNullTo(mutableSetOf()) { depInfo ->
depInfo.module?.path
val list = runGo("list", "-m", "-json", "-buildvcs=false", workingDir = projectDir)

return list.stdout.byteInputStream().use { JSON.decodeToSequence<ModuleInfo>(it) }.single().path
}

private fun ModuleInfo.toId(): Identifier {
if (replace != null) return replace.toId() // Apply replace directive.

return if (version.isBlank()) {
// If the version is blank, it is a project in ORT speak.
checkNotNull(dir) { "For projects, the directory is expected to not be null." }

val projectDir = File(dir).absoluteFile

require(projectDir.startsWith(analysisRoot)) {
"A replace directive references a module in '$projectDir' outside of analysis root which is not " +

Check warning on line 280 in analyzer/src/main/kotlin/managers/GoMod.kt

View check run for this annotation

Codecov / codecov/patch

analyzer/src/main/kotlin/managers/GoMod.kt#L280

Added line #L280 was not covered by tests
"supported."
}

Identifier(
type = managerName,
namespace = "",
name = getProjectName(projectDir),
version = processProjectVcs(projectDir).revision
)
} else {
// If the version is not blank, it is a package in ORT speak.
Identifier(
type = "Go",
namespace = "",
name = path,
version = normalizeModuleVersion(version)
)
}
}

Expand Down Expand Up @@ -320,9 +323,6 @@ class GoMod(
)
}

private fun runGo(vararg args: CharSequence, workingDir: File? = null) =
run(args = args, workingDir = workingDir, environment = environment)

/**
* Convert this [Graph] to a set of [PackageReference]s that spawn the dependency trees of the direct dependencies
* of the main module. The graph must not contain any cycles, so [Graph.breakCycles] should be called
Expand Down

0 comments on commit 1a7d15f

Please sign in to comment.