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

org.gradle.internal.component.model.VariantFilesRules Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2019 the original author or authors.
 *
 * 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
 *
 *      http://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.
 */
package org.gradle.internal.component.model;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.gradle.api.artifacts.MutableVariantFilesMetadata;
import org.gradle.api.artifacts.VariantFileMetadata;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.internal.artifacts.repositories.resolver.DefaultMutableVariantFilesMetadata;
import org.gradle.internal.Cast;
import org.gradle.internal.component.external.model.AbstractMutableModuleComponentResolveMetadata;
import org.gradle.internal.component.external.model.ComponentVariant;
import org.gradle.internal.component.external.model.DefaultModuleComponentArtifactMetadata;
import org.gradle.internal.component.external.model.UrlBackedArtifactMetadata;
import org.gradle.internal.component.external.model.VariantMetadataRules;

import java.util.List;

public class VariantFilesRules {
    private final List> actions = Lists.newLinkedList();

    public void addFilesAction(VariantMetadataRules.VariantAction action) {
        actions.add(action);
    }

    public  ImmutableList executeForFiles(VariantResolveMetadata variant, ImmutableList declaredFiles, ModuleComponentIdentifier componentIdentifier) {
        DefaultMutableVariantFilesMetadata filesMetadata = execute(variant);
        if (filesMetadata.getFiles().isEmpty()) {
            return declaredFiles;
        }
        ImmutableList.Builder builder = new ImmutableList.Builder<>();
        if (!filesMetadata.isClearExistingFiles()) {
            builder.addAll(declaredFiles);
        }
        for (VariantFileMetadata file : filesMetadata.getFiles()) {
            builder.add(Cast.uncheckedNonnullCast(new AbstractMutableModuleComponentResolveMetadata.FileImpl(file.getName(), file.getUrl())));
        }
        return builder.build();
    }

    public  ImmutableList executeForArtifacts(VariantResolveMetadata variant, ImmutableList artifacts, ModuleComponentIdentifier componentIdentifier) {
        DefaultMutableVariantFilesMetadata filesMetadata = execute(variant);
        if (filesMetadata.getFiles().isEmpty()) {
            return artifacts;
        }
        ImmutableList.Builder builder = new ImmutableList.Builder<>();
        if (!filesMetadata.isClearExistingFiles()) {
            for (T existingArtifact : artifacts) {
                if (isFilePathUnambiguous(existingArtifact)) {
                    builder.add(existingArtifact);
                }
            }
        }
        for (VariantFileMetadata file : filesMetadata.getFiles()) {
            builder.add(Cast.uncheckedNonnullCast(new UrlBackedArtifactMetadata(componentIdentifier, file.getName(), file.getUrl())));
        }
        return builder.build();
    }

    private DefaultMutableVariantFilesMetadata execute(VariantResolveMetadata variant) {
        DefaultMutableVariantFilesMetadata filesMetadata = new DefaultMutableVariantFilesMetadata();
        for (VariantMetadataRules.VariantAction action : actions) {
            action.maybeExecute(variant, filesMetadata);
        }
        return filesMetadata;
    }

    /**
     * If the artifact was sourced from pom metadata using the 'packaging' attribute in the pom,
     * we don't know if the extension of the artifact is the one indicated by the packaging or 'jar'.
     * So we remove the artifact such that the user can explicitly add it in the rule.
     */
    private  boolean isFilePathUnambiguous(T existingArtifact) {
        return !(existingArtifact instanceof DefaultModuleComponentArtifactMetadata) || "jar".equals(existingArtifact.getName().getExtension());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy