Skip to content

refactor(commands)!: Migrate command plugins to new plugin API #9507

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

Merged
merged 1 commit into from
Dec 8, 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
6 changes: 4 additions & 2 deletions cli/src/main/kotlin/OrtMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ import kotlin.system.exitProcess

import org.ossreviewtoolkit.model.config.LicenseFilePatterns
import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
import org.ossreviewtoolkit.plugins.api.PluginConfig
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 +124,8 @@ class OrtMain : CliktCommand(ORT_NAME) {
helpFormatter = { MordantHelpFormatter(context = it, REQUIRED_OPTION_MARKER, showDefaultValues = true) }
}

subcommands(OrtCommand.ALL.values)
// Pass an empty PluginConfig here as commands are not configurable.
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 = AdvisorCommandFactory.descriptor) : 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 = AnalyzerCommandFactory.descriptor) : 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 = CompareCommandFactory.descriptor) : 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 = ConfigCommandFactory.descriptor) : 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
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 = DownloaderCommandFactory.descriptor) : 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 = EvaluatorCommandFactory.descriptor) : 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
Loading