Skip to content

Commit 2e60641

Browse files
committed
refactor(commands)!: Migrate command plugins to new plugin API
Migrate the command plugins to the new plugin API. Relates to #9403. Signed-off-by: Martin Nonnenmacher <[email protected]>
1 parent ef538ee commit 2e60641

File tree

32 files changed

+234
-86
lines changed

32 files changed

+234
-86
lines changed

Diff for: cli/src/main/kotlin/OrtMain.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ import kotlin.system.exitProcess
4848

4949
import org.ossreviewtoolkit.model.config.LicenseFilePatterns
5050
import org.ossreviewtoolkit.model.config.OrtConfiguration
51-
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
51+
import org.ossreviewtoolkit.plugins.api.PluginConfig
52+
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
5253
import org.ossreviewtoolkit.utils.common.EnvironmentVariableFilter
5354
import org.ossreviewtoolkit.utils.common.MaskedString
5455
import org.ossreviewtoolkit.utils.common.Os
@@ -123,7 +124,8 @@ class OrtMain : CliktCommand(ORT_NAME) {
123124
helpFormatter = { MordantHelpFormatter(context = it, REQUIRED_OPTION_MARKER, showDefaultValues = true) }
124125
}
125126

126-
subcommands(OrtCommand.ALL.values)
127+
// Pass an empty PluginConfig here as commands are not configurable.
128+
subcommands(OrtCommandFactory.ALL.map { (_, factory) -> factory.create(PluginConfig()) })
127129

128130
versionOption(
129131
version = env.ortVersion,

Diff for: plugins/commands/advisor/build.gradle.kts

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
plugins {
2121
// Apply precompiled plugins.
22-
id("ort-library-conventions")
22+
id("ort-plugin-conventions")
2323
}
2424

2525
dependencies {
2626
api(projects.plugins.commands.commandApi)
2727

28+
ksp(projects.plugins.commands.commandApi)
29+
2830
implementation(projects.advisor)
2931
implementation(projects.model)
3032
implementation(projects.utils.commonUtils)

Diff for: plugins/commands/advisor/src/main/kotlin/AdvisorCommand.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ import org.ossreviewtoolkit.advisor.Advisor
4545
import org.ossreviewtoolkit.model.FileFormat
4646
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
4747
import org.ossreviewtoolkit.model.utils.mergeLabels
48+
import org.ossreviewtoolkit.plugins.api.OrtPlugin
49+
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
4850
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
51+
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
4952
import org.ossreviewtoolkit.plugins.commands.api.utils.SeverityStatsPrinter
5053
import org.ossreviewtoolkit.plugins.commands.api.utils.configurationGroup
5154
import org.ossreviewtoolkit.plugins.commands.api.utils.outputGroup
@@ -57,10 +60,13 @@ import org.ossreviewtoolkit.utils.ort.ORT_FAILURE_STATUS_CODE
5760
import org.ossreviewtoolkit.utils.ort.ORT_RESOLUTIONS_FILENAME
5861
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory
5962

60-
class AdvisorCommand : OrtCommand(
61-
name = "advise",
62-
help = "Check dependencies for security vulnerabilities."
63-
) {
63+
@OrtPlugin(
64+
id = "advise",
65+
displayName = "advise command",
66+
description = "Check dependencies for security vulnerabilities.",
67+
factory = OrtCommandFactory::class
68+
)
69+
class AdvisorCommand(descriptor: PluginDescriptor = AdvisorCommandFactory.descriptor) : OrtCommand(descriptor) {
6470
private val ortFile by option(
6571
"--ort-file", "-i",
6672
help = "An ORT result file with an analyzer result to use."

Diff for: plugins/commands/analyzer/build.gradle.kts

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
plugins {
2121
// Apply precompiled plugins.
22-
id("ort-library-conventions")
22+
id("ort-plugin-conventions")
2323
}
2424

2525
dependencies {
2626
api(projects.plugins.commands.commandApi)
2727

28+
ksp(projects.plugins.commands.commandApi)
29+
2830
implementation(projects.plugins.packageCurationProviders.packageCurationProviderApi)
2931

3032
implementation(projects.analyzer)

Diff for: plugins/commands/analyzer/src/main/kotlin/AnalyzerCommand.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ import org.ossreviewtoolkit.model.config.RepositoryConfiguration
4747
import org.ossreviewtoolkit.model.readValueOrNull
4848
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
4949
import org.ossreviewtoolkit.model.utils.mergeLabels
50+
import org.ossreviewtoolkit.plugins.api.OrtPlugin
51+
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
5052
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
53+
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
5154
import org.ossreviewtoolkit.plugins.commands.api.utils.SeverityStatsPrinter
5255
import org.ossreviewtoolkit.plugins.commands.api.utils.configurationGroup
5356
import org.ossreviewtoolkit.plugins.commands.api.utils.inputGroup
@@ -62,10 +65,13 @@ import org.ossreviewtoolkit.utils.ort.ORT_REPO_CONFIG_FILENAME
6265
import org.ossreviewtoolkit.utils.ort.ORT_RESOLUTIONS_FILENAME
6366
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory
6467

65-
class AnalyzerCommand : OrtCommand(
66-
name = "analyze",
67-
help = "Determine dependencies of a software project."
68-
) {
68+
@OrtPlugin(
69+
id = "analyze",
70+
displayName = "analyze command",
71+
description = "Determine dependencies of a software project.",
72+
factory = OrtCommandFactory::class
73+
)
74+
class AnalyzerCommand(descriptor: PluginDescriptor = AnalyzerCommandFactory.descriptor) : OrtCommand(descriptor) {
6975
private val inputDir by option(
7076
"--input-dir", "-i",
7177
help = "The project directory to analyze. May point to a definition file if only a single package manager is " +

Diff for: plugins/commands/api/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ plugins {
2424

2525
dependencies {
2626
api(projects.model)
27-
api(projects.utils.commonUtils)
27+
api(projects.plugins.api)
2828

2929
api(libs.clikt)
3030

Diff for: plugins/commands/api/src/main/kotlin/OrtCommand.kt

+5-13
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,15 @@ import com.github.ajalt.clikt.core.requireObject
2727
import java.io.File
2828

2929
import org.ossreviewtoolkit.model.config.OrtConfiguration
30-
import org.ossreviewtoolkit.utils.common.Plugin
30+
import org.ossreviewtoolkit.plugins.api.Plugin
31+
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
3132
import org.ossreviewtoolkit.utils.ort.ORT_CONFIG_FILENAME
3233

3334
/**
34-
* An interface for [CliktCommand]-based ORT commands that come as named plugins.
35+
* An interface for [CliktCommand]-based ORT commands that come as [Plugin]s.
3536
*/
36-
abstract class OrtCommand(name: String, private val help: String) : CliktCommand(name), Plugin {
37-
companion object {
38-
/**
39-
* All [ORT commands][OrtCommand] available in the classpath, associated by their names.
40-
*/
41-
val ALL by lazy { Plugin.getAll<OrtCommand>() }
42-
}
43-
44-
override val type = commandName
45-
46-
override fun help(context: Context) = help
37+
abstract class OrtCommand(override val descriptor: PluginDescriptor) : CliktCommand(descriptor.id), Plugin {
38+
override fun help(context: Context) = descriptor.description
4739

4840
protected val ortConfig by requireObject<OrtConfiguration>()
4941

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.ossreviewtoolkit.plugins.commands.api
21+
22+
import org.ossreviewtoolkit.plugins.api.PluginFactory
23+
24+
/**
25+
* A factory interface for creating [OrtCommand] instances.
26+
*/
27+
interface OrtCommandFactory : PluginFactory<OrtCommand> {
28+
companion object {
29+
/**
30+
* All [ORT command factories][OrtCommandFactory] available in the classpath, associated by their ids.
31+
*/
32+
val ALL by lazy { PluginFactory.getAll<OrtCommandFactory, OrtCommand>() }
33+
}
34+
}

Diff for: plugins/commands/compare/build.gradle.kts

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
plugins {
2121
// Apply precompiled plugins.
22-
id("ort-library-conventions")
22+
id("ort-plugin-conventions")
2323
}
2424

2525
dependencies {
2626
api(projects.plugins.commands.commandApi)
2727

28+
ksp(projects.plugins.commands.commandApi)
29+
2830
implementation(projects.model)
2931
implementation(projects.utils.commonUtils)
3032

Diff for: plugins/commands/compare/src/main/kotlin/CompareCommand.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,21 @@ import java.time.Instant
4343

4444
import org.ossreviewtoolkit.model.OrtResult
4545
import org.ossreviewtoolkit.model.mapper
46+
import org.ossreviewtoolkit.plugins.api.OrtPlugin
47+
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
4648
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
49+
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
4750
import org.ossreviewtoolkit.utils.common.expandTilde
4851
import org.ossreviewtoolkit.utils.common.getCommonParentFile
4952
import org.ossreviewtoolkit.utils.ort.Environment
5053

51-
class CompareCommand : OrtCommand(
52-
name = "compare",
53-
help = "Compare two ORT results with various methods."
54-
) {
54+
@OrtPlugin(
55+
id = "compare",
56+
displayName = "compare command",
57+
description = "Compare two ORT results with various methods.",
58+
factory = OrtCommandFactory::class
59+
)
60+
class CompareCommand(descriptor: PluginDescriptor = CompareCommandFactory.descriptor) : OrtCommand(descriptor) {
5561
private enum class CompareMethod { SEMANTIC_DIFF, TEXT_DIFF }
5662

5763
private val fileA by argument(help = "The first ORT result file to compare.")

Diff for: plugins/commands/config/build.gradle.kts

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
plugins {
2121
// Apply precompiled plugins.
22-
id("ort-library-conventions")
22+
id("ort-plugin-conventions")
2323
}
2424

2525
dependencies {
2626
api(projects.plugins.commands.commandApi)
2727

28+
ksp(projects.plugins.commands.commandApi)
29+
2830
implementation(projects.model)
2931
implementation(projects.utils.commonUtils)
3032

Diff for: plugins/commands/config/src/main/kotlin/ConfigCommand.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,21 @@ import com.github.ajalt.mordant.rendering.Theme
3131

3232
import org.ossreviewtoolkit.model.config.OrtConfiguration
3333
import org.ossreviewtoolkit.model.config.OrtConfigurationWrapper
34+
import org.ossreviewtoolkit.plugins.api.OrtPlugin
35+
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
3436
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
37+
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
3538
import org.ossreviewtoolkit.utils.common.collectMessages
3639
import org.ossreviewtoolkit.utils.common.expandTilde
3740
import org.ossreviewtoolkit.utils.ort.ORT_REFERENCE_CONFIG_FILENAME
3841

39-
class ConfigCommand : OrtCommand(
40-
name = "config",
41-
help = "Show different ORT configurations."
42-
) {
42+
@OrtPlugin(
43+
id = "config",
44+
displayName = "config command",
45+
description = "Show different ORT configurations.",
46+
factory = OrtCommandFactory::class
47+
)
48+
class ConfigCommand(descriptor: PluginDescriptor = ConfigCommandFactory.descriptor) : OrtCommand(descriptor) {
4349
private val showDefault by option(
4450
"--show-default",
4551
help = "Show the default configuration used when no custom configuration is present."

Diff for: plugins/commands/downloader/build.gradle.kts

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
plugins {
2121
// Apply precompiled plugins.
22-
id("ort-library-conventions")
22+
id("ort-plugin-conventions")
2323
}
2424

2525
dependencies {
2626
api(projects.plugins.commands.commandApi)
2727

28+
ksp(projects.plugins.commands.commandApi)
29+
2830
implementation(projects.downloader)
2931
implementation(projects.model)
3032
implementation(projects.utils.commonUtils)

Diff for: plugins/commands/downloader/src/main/kotlin/DownloaderCommand.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ import org.ossreviewtoolkit.model.licenses.LicenseView
8282
import org.ossreviewtoolkit.model.licenses.ResolvedLicenseInfo
8383
import org.ossreviewtoolkit.model.readValue
8484
import org.ossreviewtoolkit.model.utils.createLicenseInfoResolver
85+
import org.ossreviewtoolkit.plugins.api.OrtPlugin
86+
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
8587
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
88+
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
8689
import org.ossreviewtoolkit.plugins.commands.api.utils.GroupTypes.FileType
8790
import org.ossreviewtoolkit.plugins.commands.api.utils.GroupTypes.StringType
8891
import org.ossreviewtoolkit.plugins.commands.api.utils.OPTION_GROUP_INPUT
@@ -102,10 +105,13 @@ import org.ossreviewtoolkit.utils.ort.ortConfigDirectory
102105
import org.ossreviewtoolkit.utils.ort.showStackTrace
103106
import org.ossreviewtoolkit.utils.spdx.SpdxLicenseChoice
104107

105-
class DownloaderCommand : OrtCommand(
106-
name = "download",
107-
help = "Fetch source code from a remote location."
108-
) {
108+
@OrtPlugin(
109+
id = "download",
110+
displayName = "download command",
111+
description = "Fetch source code from a remote location.",
112+
factory = OrtCommandFactory::class
113+
)
114+
class DownloaderCommand(descriptor: PluginDescriptor = DownloaderCommandFactory.descriptor) : OrtCommand(descriptor) {
109115
private val input by mutuallyExclusiveOptions(
110116
option(
111117
"--ort-file", "-i",

Diff for: plugins/commands/evaluator/build.gradle.kts

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
plugins {
2121
// Apply precompiled plugins.
22-
id("ort-library-conventions")
22+
id("ort-plugin-conventions")
2323
}
2424

2525
dependencies {
2626
api(projects.plugins.commands.commandApi)
2727

28+
ksp(projects.plugins.commands.commandApi)
29+
2830
implementation(projects.plugins.packageConfigurationProviders.packageConfigurationProviderApi)
2931
implementation(projects.plugins.packageCurationProviders.packageCurationProviderApi)
3032

Diff for: plugins/commands/evaluator/src/main/kotlin/EvaluatorCommand.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ import org.ossreviewtoolkit.model.readValue
5757
import org.ossreviewtoolkit.model.readValueOrDefault
5858
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
5959
import org.ossreviewtoolkit.model.utils.mergeLabels
60+
import org.ossreviewtoolkit.plugins.api.OrtPlugin
61+
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
6062
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
63+
import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory
6164
import org.ossreviewtoolkit.plugins.commands.api.utils.SeverityStatsPrinter
6265
import org.ossreviewtoolkit.plugins.commands.api.utils.configurationGroup
6366
import org.ossreviewtoolkit.plugins.commands.api.utils.inputGroup
@@ -81,10 +84,13 @@ import org.ossreviewtoolkit.utils.ort.ORT_LICENSE_CLASSIFICATIONS_FILENAME
8184
import org.ossreviewtoolkit.utils.ort.ORT_RESOLUTIONS_FILENAME
8285
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory
8386

84-
class EvaluatorCommand : OrtCommand(
85-
name = "evaluate",
86-
help = "Evaluate ORT result files against policy rules."
87-
) {
87+
@OrtPlugin(
88+
id = "evaluate",
89+
displayName = "evaluate command",
90+
description = "Evaluate ORT result files against policy rules.",
91+
factory = OrtCommandFactory::class
92+
)
93+
class EvaluatorCommand(descriptor: PluginDescriptor = EvaluatorCommandFactory.descriptor) : OrtCommand(descriptor) {
8894
private val ortFile by option(
8995
"--ort-file", "-i",
9096
help = "The ORT result file to read as input."

Diff for: plugins/commands/migrate/build.gradle.kts

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
plugins {
2121
// Apply precompiled plugins.
22-
id("ort-library-conventions")
22+
id("ort-plugin-conventions")
2323
}
2424

2525
dependencies {
2626
api(projects.plugins.commands.commandApi)
2727

28+
ksp(projects.plugins.commands.commandApi)
29+
2830
implementation(projects.plugins.packageCurationProviders.ortConfigPackageCurationProvider)
2931
implementation(projects.plugins.packageManagers.nugetPackageManager)
3032
implementation(projects.utils.commonUtils)

0 commit comments

Comments
 (0)