|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 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.plugins |
| 21 | + |
| 22 | +import com.github.ajalt.clikt.parameters.options.default |
| 23 | +import com.github.ajalt.clikt.parameters.options.option |
| 24 | +import com.github.ajalt.clikt.parameters.options.split |
| 25 | +import com.github.ajalt.mordant.widgets.HorizontalRule |
| 26 | +import com.github.ajalt.mordant.widgets.UnorderedList |
| 27 | + |
| 28 | +import org.ossreviewtoolkit.advisor.AdviceProviderFactory |
| 29 | +import org.ossreviewtoolkit.downloader.VersionControlSystemFactory |
| 30 | +import org.ossreviewtoolkit.plugins.api.OrtPlugin |
| 31 | +import org.ossreviewtoolkit.plugins.api.PluginDescriptor |
| 32 | +import org.ossreviewtoolkit.plugins.commands.api.OrtCommand |
| 33 | +import org.ossreviewtoolkit.plugins.commands.api.OrtCommandFactory |
| 34 | +import org.ossreviewtoolkit.plugins.packageconfigurationproviders.api.PackageConfigurationProviderFactory |
| 35 | +import org.ossreviewtoolkit.plugins.packagecurationproviders.api.PackageCurationProviderFactory |
| 36 | +import org.ossreviewtoolkit.reporter.ReporterFactory |
| 37 | +import org.ossreviewtoolkit.scanner.ScannerWrapperFactory |
| 38 | + |
| 39 | +@OrtPlugin( |
| 40 | + id = "plugins", |
| 41 | + displayName = "plugins command", |
| 42 | + description = "Print information about the installed ORT plugins.", |
| 43 | + factory = OrtCommandFactory::class |
| 44 | +) |
| 45 | +class PluginsCommand(descriptor: PluginDescriptor = PluginsCommandFactory.descriptor) : OrtCommand(descriptor) { |
| 46 | + private val types by option( |
| 47 | + "--types", |
| 48 | + help = "A comma-separated list of plugin types to show." |
| 49 | + ).split(",").default(PluginType.entries.map { it.optionName }) |
| 50 | + |
| 51 | + override fun run() { |
| 52 | + types.forEach { type -> |
| 53 | + PluginType.entries.find { it.optionName == type }?.let { pluginType -> |
| 54 | + renderPlugins(pluginType.title, pluginType.descriptors.value) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private fun renderPlugins(title: String, plugins: List<PluginDescriptor>) { |
| 60 | + echo(HorizontalRule(title, "=")) |
| 61 | + echo() |
| 62 | + |
| 63 | + plugins.forEach { plugin -> |
| 64 | + echo( |
| 65 | + HorizontalRule( |
| 66 | + buildString { |
| 67 | + append(plugin.displayName) |
| 68 | + if (plugin.id != plugin.displayName) append(" (id: ${plugin.id})") |
| 69 | + }, |
| 70 | + "-" |
| 71 | + ) |
| 72 | + ) |
| 73 | + echo() |
| 74 | + echo(plugin.description) |
| 75 | + echo() |
| 76 | + |
| 77 | + if (plugin.options.isNotEmpty()) { |
| 78 | + echo("Configuration options:") |
| 79 | + |
| 80 | + echo( |
| 81 | + UnorderedList( |
| 82 | + listEntries = plugin.options.map { option -> |
| 83 | + buildString { |
| 84 | + append("${option.name}: ${option.type.name}") |
| 85 | + option.defaultValue?.also { append(" (Default: $it)") } |
| 86 | + if (option.isRequired) append(" (Required)") |
| 87 | + appendLine() |
| 88 | + append(option.description) |
| 89 | + } |
| 90 | + }.toTypedArray(), |
| 91 | + bulletText = "*" |
| 92 | + ) |
| 93 | + ) |
| 94 | + |
| 95 | + echo() |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +private enum class PluginType( |
| 102 | + val optionName: String, |
| 103 | + val title: String, |
| 104 | + val descriptors: Lazy<List<PluginDescriptor>> |
| 105 | +) { |
| 106 | + ADVICE_PROVIDERS( |
| 107 | + "advice-providers", |
| 108 | + "Advice Providers", |
| 109 | + lazy { AdviceProviderFactory.ALL.map { it.value.descriptor } } |
| 110 | + ), |
| 111 | + COMMANDS( |
| 112 | + "commands", |
| 113 | + "CLI Commands", |
| 114 | + lazy { OrtCommandFactory.ALL.map { it.value.descriptor } } |
| 115 | + ), |
| 116 | + PACKAGE_CONFIGURATION_PROVIDERS( |
| 117 | + "package-configuration-providers", |
| 118 | + "Package Configuration Providers", |
| 119 | + lazy { PackageConfigurationProviderFactory.ALL.map { it.value.descriptor } } |
| 120 | + ), |
| 121 | + PACKAGE_CURATION_PROVIDERS( |
| 122 | + "package-curation-providers", |
| 123 | + "Package Curation Providers", |
| 124 | + lazy { PackageCurationProviderFactory.ALL.map { it.value.descriptor } } |
| 125 | + ), |
| 126 | + PACKAGE_MANAGERS( |
| 127 | + "package-managers", |
| 128 | + "Package Managers (TO BE IMPLEMENTED)", |
| 129 | + lazy { emptyList() } |
| 130 | + ), |
| 131 | + REPORTERS( |
| 132 | + "reporters", |
| 133 | + "Reporters", |
| 134 | + lazy { ReporterFactory.ALL.map { it.value.descriptor } } |
| 135 | + ), |
| 136 | + SCANNERS( |
| 137 | + "scanners", |
| 138 | + "Scanners", |
| 139 | + lazy { ScannerWrapperFactory.ALL.map { it.value.descriptor } } |
| 140 | + ), |
| 141 | + VERSION_CONTROL_SYSTEMS( |
| 142 | + "version-control-systems", |
| 143 | + "Version Control Systems", |
| 144 | + lazy { VersionControlSystemFactory.ALL.map { it.value.descriptor } } |
| 145 | + ) |
| 146 | +} |
0 commit comments