Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small improvements to the GenerateScopeExcludesCommand #8523

Merged
merged 4 commits into from
Apr 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.file

import org.apache.logging.log4j.kotlin.logger

import org.ossreviewtoolkit.helper.utils.minimize
import org.ossreviewtoolkit.helper.utils.readOrtResult
import org.ossreviewtoolkit.helper.utils.replaceScopeExcludes
Expand All @@ -45,35 +47,42 @@
"--ort-file", "-i",
help = "The ORT file to generate scope excludes for."
).convert { it.expandTilde() }
.file(mustExist = true, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = false)
.file(mustExist = true, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = true)
.convert { it.absoluteFile.normalize() }
.required()

private val repositoryConfigurationFile by option(
"--repository-configuration-file",
help = "The repository configuration file to write the generated scope excludes to."
).convert { it.expandTilde() }
.file(mustExist = true, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = false)
.file(mustExist = false, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = false)
.convert { it.absoluteFile.normalize() }
.required()

override fun run() {
val ortResult = readOrtResult(ortFile)
val scopeExcludes = ortResult.generateScopeExcludes()

repositoryConfigurationFile
.readValue<RepositoryConfiguration>()
val repositoryConfiguration = if (repositoryConfigurationFile.isFile) {
repositoryConfigurationFile.readValue<RepositoryConfiguration>()

Check warning on line 67 in helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt#L67

Added line #L67 was not covered by tests
} else {
RepositoryConfiguration()

Check warning on line 69 in helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt#L69

Added line #L69 was not covered by tests
}

repositoryConfiguration

Check warning on line 72 in helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt#L72

Added line #L72 was not covered by tests
.replaceScopeExcludes(scopeExcludes)
.sortScopeExcludes()
.write(repositoryConfigurationFile)
}
}

private fun OrtResult.generateScopeExcludes(): List<ScopeExclude> {
val projectScopes = getProjects().flatMap { project ->
val projectScopes = getProjects().flatMapTo(mutableSetOf()) { project ->

Check warning on line 80 in helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt#L80

Added line #L80 was not covered by tests
dependencyNavigator.scopeNames(project)
}

logger.info { "Found the following scopes: ${projectScopes.joinToString()}" }

Check warning on line 84 in helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt#L84

Added line #L84 was not covered by tests

return getProjects().flatMap { project ->
getScopeExcludesForPackageManager(project.id.type)
}.minimize(projectScopes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@

val scopeExcludes = ortResult
.getProjects()
.flatMap { project -> project.scopes.map { scope -> scope.name } }
.flatMapTo(mutableSetOf()) { project -> project.scopes.map { scope -> scope.name } }

Check warning on line 97 in helper-cli/src/main/kotlin/commands/repoconfig/RemoveEntriesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/repoconfig/RemoveEntriesCommand.kt#L97

Added line #L97 was not covered by tests
.let { projectScopes -> ortResult.getExcludes().scopes.minimize(projectScopes) }

val licenseFindings = ortResult.getProjectLicenseFindings()
Expand Down
2 changes: 1 addition & 1 deletion helper-cli/src/main/kotlin/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ import org.ossreviewtoolkit.utils.spdx.SpdxSingleLicenseExpression
* Return an approximated minimal sublist of [this] so that the result still matches the exact same entries of the given
* [projectScopes].
*/
internal fun List<ScopeExclude>.minimize(projectScopes: List<String>): List<ScopeExclude> {
internal fun List<ScopeExclude>.minimize(projectScopes: Set<String>): List<ScopeExclude> {
val scopeExcludes = associateWith { scopeExclude ->
projectScopes.filter { scopeExclude.matches(it) }.toSet()
}
Expand Down
Loading