All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.PackageManagerFactory.kt Maven / Gradle / Ivy

/*
 * Copyright (C) 2017 The ORT Project Authors (see )
 *
 * 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.analyzer

import java.io.File
import java.nio.file.FileSystems
import java.nio.file.PathMatcher
import java.util.ServiceLoader

import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.utils.common.Plugin

/**
 * A common interface for use with [ServiceLoader] that all [AbstractPackageManagerFactory] classes need to implement.
 */
interface PackageManagerFactory : Plugin {
    companion object {
        /**
         * All [package manager factories][PackageManagerFactory] available in the classpath, associated by their names.
         */
        val ALL by lazy { Plugin.getAll() }

        /**
         * The available [package manager factories][PackageManagerFactory] that are enabled by default.
         */
        val ENABLED_BY_DEFAULT by lazy { ALL.values.filter { it.isEnabledByDefault } }
    }

    /**
     * The glob matchers for all definition files.
     */
    val matchersForDefinitionFiles: List

    /**
     * Create a [PackageManager] for analyzing the [analysisRoot] directory using the specified [analyzerConfig] and
     * [repoConfig].
     */
    fun create(
        analysisRoot: File,
        analyzerConfig: AnalyzerConfiguration,
        repoConfig: RepositoryConfiguration
    ): PackageManager
}

/**
 * A generic factory class for a [PackageManager].
 */
abstract class AbstractPackageManagerFactory(
    override val type: String,
    override val isEnabledByDefault: Boolean = true
) : PackageManagerFactory {
    /**
     * The prioritized list of glob patterns of definition files supported by this package manager. Only all matches of
     * the first glob having any matches are considered.
     */
    abstract val globsForDefinitionFiles: List

    override val matchersForDefinitionFiles by lazy {
        globsForDefinitionFiles.map {
            FileSystems.getDefault().getPathMatcher("glob:**/$it")
        }
    }

    abstract override fun create(
        analysisRoot: File,
        analyzerConfig: AnalyzerConfiguration,
        repoConfig: RepositoryConfiguration
    ): T

    /**
     * Return the package manager's name here to allow Clikt to display something meaningful when listing the
     * package managers which are enabled by default via their factories.
     */
    override fun toString() = type
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy