Search in sources :

Example 1 with PluginResolver

use of org.gradle.plugin.use.resolve.internal.PluginResolver in project gradle by gradle.

the class DefaultPluginRequestApplicator method applyPlugins.

public void applyPlugins(final PluginRequests requests, final ScriptHandlerInternal scriptHandler, @Nullable final PluginManagerInternal target, ClassLoaderScope classLoaderScope) {
    if (requests.isEmpty()) {
        defineScriptHandlerClassScope(scriptHandler, classLoaderScope, Collections.<PluginImplementation<?>>emptyList());
        return;
    }
    if (target == null) {
        throw new IllegalStateException("Plugin target is 'null' and there are plugin requests");
    }
    final PluginResolver effectivePluginResolver = wrapInNotInClasspathCheck(classLoaderScope);
    List<Result> results = collect(requests, new Transformer<Result, PluginRequestInternal>() {

        public Result transform(PluginRequestInternal request) {
            PluginRequestInternal configuredRequest = pluginResolutionStrategy.applyTo(request);
            return resolveToFoundResult(effectivePluginResolver, configuredRequest);
        }
    });
    // Could be different to ids in the requests as they may be unqualified
    final Map<Result, PluginId> legacyActualPluginIds = Maps.newLinkedHashMap();
    final Map<Result, PluginImplementation<?>> pluginImpls = Maps.newLinkedHashMap();
    final Map<Result, PluginImplementation<?>> pluginImplsFromOtherLoaders = Maps.newLinkedHashMap();
    if (!results.isEmpty()) {
        final RepositoryHandler repositories = scriptHandler.getRepositories();
        for (PluginRepository pluginRepository : pluginRepositoryRegistry.getPluginRepositories()) {
            if (pluginRepository instanceof BackedByArtifactRepositories) {
                ((BackedByArtifactRepositories) pluginRepository).createArtifactRepositories(repositories);
            }
        }
        final List<MavenArtifactRepository> mavenRepos = repositories.withType(MavenArtifactRepository.class);
        final Set<String> repoUrls = Sets.newLinkedHashSet();
        for (final Result result : results) {
            applyPlugin(result.request, result.found.getPluginId(), new Runnable() {

                @Override
                public void run() {
                    result.found.execute(new PluginResolveContext() {

                        public void addLegacy(PluginId pluginId, final String m2RepoUrl, Object dependencyNotation) {
                            repoUrls.add(m2RepoUrl);
                            addLegacy(pluginId, dependencyNotation);
                        }

                        @Override
                        public void addLegacy(PluginId pluginId, Object dependencyNotation) {
                            legacyActualPluginIds.put(result, pluginId);
                            scriptHandler.addScriptClassPathDependency(dependencyNotation);
                        }

                        @Override
                        public void add(PluginImplementation<?> plugin) {
                            pluginImpls.put(result, plugin);
                        }

                        @Override
                        public void addFromDifferentLoader(PluginImplementation<?> plugin) {
                            pluginImplsFromOtherLoaders.put(result, plugin);
                        }
                    });
                }
            });
        }
        for (final String m2RepoUrl : repoUrls) {
            boolean repoExists = any(mavenRepos, new Spec<MavenArtifactRepository>() {

                public boolean isSatisfiedBy(MavenArtifactRepository element) {
                    return element.getUrl().toString().equals(m2RepoUrl);
                }
            });
            if (!repoExists) {
                repositories.maven(new Action<MavenArtifactRepository>() {

                    public void execute(MavenArtifactRepository mavenArtifactRepository) {
                        mavenArtifactRepository.setUrl(m2RepoUrl);
                    }
                });
            }
        }
    }
    defineScriptHandlerClassScope(scriptHandler, classLoaderScope, pluginImplsFromOtherLoaders.values());
    // It won't for arbitrary scripts though.
    for (final Map.Entry<Result, PluginId> entry : legacyActualPluginIds.entrySet()) {
        final PluginRequestInternal request = entry.getKey().request;
        final PluginId id = entry.getValue();
        applyPlugin(request, id, new Runnable() {

            public void run() {
                if (request.isApply()) {
                    target.apply(id.toString());
                }
            }
        });
    }
    for (final Map.Entry<Result, PluginImplementation<?>> entry : Iterables.concat(pluginImpls.entrySet(), pluginImplsFromOtherLoaders.entrySet())) {
        final Result result = entry.getKey();
        applyPlugin(result.request, result.found.getPluginId(), new Runnable() {

            public void run() {
                if (result.request.isApply()) {
                    target.apply(entry.getValue());
                }
            }
        });
    }
}
Also used : PluginResolveContext(org.gradle.plugin.use.resolve.internal.PluginResolveContext) PluginRepository(org.gradle.plugin.repository.PluginRepository) PluginId(org.gradle.plugin.use.PluginId) PluginResolutionResult(org.gradle.plugin.use.resolve.internal.PluginResolutionResult) BackedByArtifactRepositories(org.gradle.plugin.repository.internal.BackedByArtifactRepositories) PluginImplementation(org.gradle.api.internal.plugins.PluginImplementation) MavenArtifactRepository(org.gradle.api.artifacts.repositories.MavenArtifactRepository) PluginResolver(org.gradle.plugin.use.resolve.internal.PluginResolver) NotNonCorePluginOnClasspathCheckPluginResolver(org.gradle.plugin.use.resolve.internal.NotNonCorePluginOnClasspathCheckPluginResolver) PluginRequestInternal(org.gradle.plugin.management.internal.PluginRequestInternal) RepositoryHandler(org.gradle.api.artifacts.dsl.RepositoryHandler) Map(java.util.Map)

Aggregations

Map (java.util.Map)1 RepositoryHandler (org.gradle.api.artifacts.dsl.RepositoryHandler)1 MavenArtifactRepository (org.gradle.api.artifacts.repositories.MavenArtifactRepository)1 PluginImplementation (org.gradle.api.internal.plugins.PluginImplementation)1 PluginRequestInternal (org.gradle.plugin.management.internal.PluginRequestInternal)1 PluginRepository (org.gradle.plugin.repository.PluginRepository)1 BackedByArtifactRepositories (org.gradle.plugin.repository.internal.BackedByArtifactRepositories)1 PluginId (org.gradle.plugin.use.PluginId)1 NotNonCorePluginOnClasspathCheckPluginResolver (org.gradle.plugin.use.resolve.internal.NotNonCorePluginOnClasspathCheckPluginResolver)1 PluginResolutionResult (org.gradle.plugin.use.resolve.internal.PluginResolutionResult)1 PluginResolveContext (org.gradle.plugin.use.resolve.internal.PluginResolveContext)1 PluginResolver (org.gradle.plugin.use.resolve.internal.PluginResolver)1