Skip to content

Commit

Permalink
refactor(commands)!: Migrate command plugins to new plugin API
Browse files Browse the repository at this point in the history
WIP: This requires support for custom constructor args because the
`CliktCommand` class takes the `name` as a constructor argument.

Relates to #9403.

Signed-off-by: Martin Nonnenmacher <[email protected]>
  • Loading branch information
mnonnenmacher committed Nov 26, 2024
1 parent 99e611b commit 6877fea
Show file tree
Hide file tree
Showing 32 changed files with 226 additions and 85 deletions.
5 changes: 4 additions & 1 deletion cli/src/main/kotlin/OrtMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ import kotlin.system.exitProcess

import org.ossreviewtoolkit.model.config.LicenseFilePatterns
import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.plugins.api.PluginConfig
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand

Check warning on line 52 in cli/src/main/kotlin/OrtMain.kt

View workflow job for this annotation

GitHub Actions / qodana-scan

Unused import directive

Unused import directive
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
import org.ossreviewtoolkit.utils.common.EnvironmentVariableFilter
import org.ossreviewtoolkit.utils.common.MaskedString
import org.ossreviewtoolkit.utils.common.Os
Expand Down Expand Up @@ -123,7 +125,8 @@ class OrtMain : CliktCommand(ORT_NAME) {
helpFormatter = { MordantHelpFormatter(context = it, REQUIRED_OPTION_MARKER, showDefaultValues = true) }
}

subcommands(OrtCommand.ALL.values)
// Commands are not configurable so we can pass an empty PluginConfig here.
subcommands(OrtCommandFactory.ALL.map { (_, factory) -> factory.create(PluginConfig()) })

versionOption(
version = env.ortVersion,
Expand Down
4 changes: 3 additions & 1 deletion plugins/commands/advisor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
id("ort-plugin-conventions")
}

dependencies {
api(projects.plugins.commands.commandApi)

ksp(projects.plugins.commands.commandApi)

implementation(projects.advisor)
implementation(projects.model)
implementation(projects.utils.commonUtils)
Expand Down
14 changes: 10 additions & 4 deletions plugins/commands/advisor/src/main/kotlin/AdvisorCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ import org.ossreviewtoolkit.advisor.Advisor
import org.ossreviewtoolkit.model.FileFormat
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
import org.ossreviewtoolkit.model.utils.mergeLabels
import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
import org.ossreviewtoolkit.plugins.commands.api.utils.SeverityStatsPrinter
import org.ossreviewtoolkit.plugins.commands.api.utils.configurationGroup
import org.ossreviewtoolkit.plugins.commands.api.utils.outputGroup
Expand All @@ -57,10 +60,13 @@ import org.ossreviewtoolkit.utils.ort.ORT_FAILURE_STATUS_CODE
import org.ossreviewtoolkit.utils.ort.ORT_RESOLUTIONS_FILENAME
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory

class AdvisorCommand : OrtCommand(
name = "advise",
help = "Check dependencies for security vulnerabilities."
) {
@OrtPlugin(
id = "advise",
displayName = "advise command",
description= "Check dependencies for security vulnerabilities.",
factory = OrtCommandFactory::class
)
class AdvisorCommand(descriptor: PluginDescriptor) : OrtCommand(descriptor) {
private val ortFile by option(
"--ort-file", "-i",
help = "An ORT result file with an analyzer result to use."
Expand Down
4 changes: 3 additions & 1 deletion plugins/commands/analyzer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
id("ort-plugin-conventions")
}

dependencies {
api(projects.plugins.commands.commandApi)

ksp(projects.plugins.commands.commandApi)

implementation(projects.plugins.packageCurationProviders.packageCurationProviderApi)

implementation(projects.analyzer)
Expand Down
14 changes: 10 additions & 4 deletions plugins/commands/analyzer/src/main/kotlin/AnalyzerCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.readValueOrNull
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
import org.ossreviewtoolkit.model.utils.mergeLabels
import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
import org.ossreviewtoolkit.plugins.commands.api.utils.SeverityStatsPrinter
import org.ossreviewtoolkit.plugins.commands.api.utils.configurationGroup
import org.ossreviewtoolkit.plugins.commands.api.utils.inputGroup
Expand All @@ -62,10 +65,13 @@ import org.ossreviewtoolkit.utils.ort.ORT_REPO_CONFIG_FILENAME
import org.ossreviewtoolkit.utils.ort.ORT_RESOLUTIONS_FILENAME
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory

class AnalyzerCommand : OrtCommand(
name = "analyze",
help = "Determine dependencies of a software project."
) {
@OrtPlugin(
id = "analyze",
displayName = "analyze command",
description = "Determine dependencies of a software project.",
factory = OrtCommandFactory::class
)
class AnalyzerCommand(descriptor: PluginDescriptor) : OrtCommand(descriptor) {
private val inputDir by option(
"--input-dir", "-i",
help = "The project directory to analyze. May point to a definition file if only a single package manager is " +
Expand Down
2 changes: 1 addition & 1 deletion plugins/commands/api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ plugins {

dependencies {
api(projects.model)
api(projects.utils.commonUtils)
api(projects.plugins.api)

api(libs.clikt)

Expand Down
18 changes: 5 additions & 13 deletions plugins/commands/api/src/main/kotlin/OrtCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,15 @@ import com.github.ajalt.clikt.core.requireObject
import java.io.File

import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.utils.common.Plugin
import org.ossreviewtoolkit.plugins.api.Plugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.utils.ort.ORT_CONFIG_FILENAME

/**
* An interface for [CliktCommand]-based ORT commands that come as named plugins.
* An interface for [CliktCommand]-based ORT commands that come as [Plugin]s.
*/
abstract class OrtCommand(name: String, private val help: String) : CliktCommand(name), Plugin {
companion object {
/**
* All [ORT commands][OrtCommand] available in the classpath, associated by their names.
*/
val ALL by lazy { Plugin.getAll<OrtCommand>() }
}

override val type = commandName

override fun help(context: Context) = help
abstract class OrtCommand(override val descriptor: PluginDescriptor) : CliktCommand(descriptor.id), Plugin {
override fun help(context: Context) = descriptor.description

protected val ortConfig by requireObject<OrtConfiguration>()

Expand Down
34 changes: 34 additions & 0 deletions plugins/commands/api/src/main/kotlin/OrtCommandFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.plugins.commands.api

import org.ossreviewtoolkit.plugins.api.PluginFactory

/**
* A factory interface for creating [OrtCommand] instances.
*/
interface OrtCommandFactory : PluginFactory<OrtCommand> {
companion object {
/**
* All [ORT command factories][OrtCommandFactory] available in the classpath, associated by their ids.
*/
val ALL by lazy { PluginFactory.getAll<OrtCommandFactory, OrtCommand>() }
}
}
4 changes: 3 additions & 1 deletion plugins/commands/compare/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
id("ort-plugin-conventions")
}

dependencies {
api(projects.plugins.commands.commandApi)

ksp(projects.plugins.commands.commandApi)

implementation(projects.model)
implementation(projects.utils.commonUtils)

Expand Down
14 changes: 10 additions & 4 deletions plugins/commands/compare/src/main/kotlin/CompareCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ import java.time.Instant

import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.mapper
import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
import org.ossreviewtoolkit.utils.common.expandTilde
import org.ossreviewtoolkit.utils.common.getCommonParentFile
import org.ossreviewtoolkit.utils.ort.Environment

class CompareCommand : OrtCommand(
name = "compare",
help = "Compare two ORT results with various methods."
) {
@OrtPlugin(
id = "compare",
displayName = "compare command",
description = "Compare two ORT results with various methods.",
factory = OrtCommandFactory::class
)
class CompareCommand(descriptor: PluginDescriptor) : OrtCommand(descriptor) {
private enum class CompareMethod { SEMANTIC_DIFF, TEXT_DIFF }

private val fileA by argument(help = "The first ORT result file to compare.")
Expand Down
4 changes: 3 additions & 1 deletion plugins/commands/config/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
id("ort-plugin-conventions")
}

dependencies {
api(projects.plugins.commands.commandApi)

ksp(projects.plugins.commands.commandApi)

implementation(projects.model)
implementation(projects.utils.commonUtils)

Expand Down
14 changes: 10 additions & 4 deletions plugins/commands/config/src/main/kotlin/ConfigCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@ import com.github.ajalt.mordant.rendering.Theme

import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.model.config.OrtConfigurationWrapper
import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
import org.ossreviewtoolkit.utils.common.collectMessages
import org.ossreviewtoolkit.utils.common.expandTilde
import org.ossreviewtoolkit.utils.ort.ORT_REFERENCE_CONFIG_FILENAME

class ConfigCommand : OrtCommand(
name = "config",
help = "Show different ORT configurations."
) {
@OrtPlugin(
id = "config",
displayName = "config command",
description = "Show different ORT configurations.",
factory = OrtCommandFactory::class
)
class ConfigCommand(descriptor: PluginDescriptor) : OrtCommand(descriptor) {
private val showDefault by option(
"--show-default",
help = "Show the default configuration used when no custom configuration is present."
Expand Down
4 changes: 3 additions & 1 deletion plugins/commands/downloader/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
id("ort-plugin-conventions")
}

dependencies {
api(projects.plugins.commands.commandApi)

ksp(projects.plugins.commands.commandApi)

implementation(projects.downloader)
implementation(projects.model)
implementation(projects.utils.commonUtils)
Expand Down
14 changes: 10 additions & 4 deletions plugins/commands/downloader/src/main/kotlin/DownloaderCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ import org.ossreviewtoolkit.model.licenses.LicenseView
import org.ossreviewtoolkit.model.licenses.ResolvedLicenseInfo
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.utils.createLicenseInfoResolver
import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
import org.ossreviewtoolkit.plugins.commands.api.utils.GroupTypes.FileType
import org.ossreviewtoolkit.plugins.commands.api.utils.GroupTypes.StringType
import org.ossreviewtoolkit.plugins.commands.api.utils.OPTION_GROUP_INPUT
Expand All @@ -102,10 +105,13 @@ import org.ossreviewtoolkit.utils.ort.ortConfigDirectory
import org.ossreviewtoolkit.utils.ort.showStackTrace
import org.ossreviewtoolkit.utils.spdx.SpdxLicenseChoice

class DownloaderCommand : OrtCommand(
name = "download",
help = "Fetch source code from a remote location."
) {
@OrtPlugin(
id = "download",
displayName = "download command",
description = "Fetch source code from a remote location.",
factory = OrtCommandFactory::class
)
class DownloaderCommand(descriptor: PluginDescriptor) : OrtCommand(descriptor) {
private val input by mutuallyExclusiveOptions(
option(
"--ort-file", "-i",
Expand Down
4 changes: 3 additions & 1 deletion plugins/commands/evaluator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
id("ort-plugin-conventions")
}

dependencies {
api(projects.plugins.commands.commandApi)

ksp(projects.plugins.commands.commandApi)

implementation(projects.plugins.packageConfigurationProviders.packageConfigurationProviderApi)
implementation(projects.plugins.packageCurationProviders.packageCurationProviderApi)

Expand Down
14 changes: 10 additions & 4 deletions plugins/commands/evaluator/src/main/kotlin/EvaluatorCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.readValueOrDefault
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
import org.ossreviewtoolkit.model.utils.mergeLabels
import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
import org.ossreviewtoolkit.plugins.commands.api.utils.SeverityStatsPrinter
import org.ossreviewtoolkit.plugins.commands.api.utils.configurationGroup
import org.ossreviewtoolkit.plugins.commands.api.utils.inputGroup
Expand All @@ -81,10 +84,13 @@ import org.ossreviewtoolkit.utils.ort.ORT_LICENSE_CLASSIFICATIONS_FILENAME
import org.ossreviewtoolkit.utils.ort.ORT_RESOLUTIONS_FILENAME
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory

class EvaluatorCommand : OrtCommand(
name = "evaluate",
help = "Evaluate ORT result files against policy rules."
) {
@OrtPlugin(
id = "evaluate",
displayName = "evaluate command",
description = "Evaluate ORT result files against policy rules.",
factory = OrtCommandFactory::class
)
class EvaluatorCommand(descriptor: PluginDescriptor) : OrtCommand(descriptor) {
private val ortFile by option(
"--ort-file", "-i",
help = "The ORT result file to read as input."
Expand Down
4 changes: 3 additions & 1 deletion plugins/commands/migrate/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
id("ort-plugin-conventions")
}

dependencies {
api(projects.plugins.commands.commandApi)

ksp(projects.plugins.commands.commandApi)

implementation(projects.plugins.packageCurationProviders.ortConfigPackageCurationProvider)
implementation(projects.plugins.packageManagers.nugetPackageManager)
implementation(projects.utils.commonUtils)
Expand Down
Loading

0 comments on commit 6877fea

Please sign in to comment.