Search in sources :

Example 1 with PluginRequestInternal

use of org.gradle.plugin.management.internal.PluginRequestInternal 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)

Example 2 with PluginRequestInternal

use of org.gradle.plugin.management.internal.PluginRequestInternal in project gradle by gradle.

the class PluginRequestCollector method listPluginRequests.

public List<PluginRequestInternal> listPluginRequests() {
    List<PluginRequestInternal> pluginRequests = collect(specs, new Transformer<PluginRequestInternal, DependencySpecImpl>() {

        public PluginRequestInternal transform(DependencySpecImpl original) {
            return new DefaultPluginRequest(original.id, original.version, original.apply, original.lineNumber, scriptSource);
        }
    });
    ListMultimap<PluginId, PluginRequestInternal> groupedById = CollectionUtils.groupBy(pluginRequests, new Transformer<PluginId, PluginRequestInternal>() {

        public PluginId transform(PluginRequestInternal pluginRequest) {
            return pluginRequest.getId();
        }
    });
    // Check for duplicates
    for (PluginId key : groupedById.keySet()) {
        List<PluginRequestInternal> pluginRequestsForId = groupedById.get(key);
        if (pluginRequestsForId.size() > 1) {
            PluginRequestInternal first = pluginRequests.get(0);
            PluginRequestInternal second = pluginRequests.get(1);
            InvalidPluginRequestException exception = new InvalidPluginRequestException(second, "Plugin with id '" + key + "' was already requested at line " + first.getLineNumber());
            throw new LocationAwareException(exception, second.getScriptDisplayName(), second.getLineNumber());
        }
    }
    return pluginRequests;
}
Also used : InvalidPluginRequestException(org.gradle.plugin.management.internal.InvalidPluginRequestException) LocationAwareException(org.gradle.internal.exceptions.LocationAwareException) DefaultPluginRequest(org.gradle.plugin.management.internal.DefaultPluginRequest) PluginRequestInternal(org.gradle.plugin.management.internal.PluginRequestInternal) PluginId(org.gradle.plugin.use.PluginId)

Aggregations

PluginRequestInternal (org.gradle.plugin.management.internal.PluginRequestInternal)2 PluginId (org.gradle.plugin.use.PluginId)2 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 LocationAwareException (org.gradle.internal.exceptions.LocationAwareException)1 DefaultPluginRequest (org.gradle.plugin.management.internal.DefaultPluginRequest)1 InvalidPluginRequestException (org.gradle.plugin.management.internal.InvalidPluginRequestException)1 PluginRepository (org.gradle.plugin.repository.PluginRepository)1 BackedByArtifactRepositories (org.gradle.plugin.repository.internal.BackedByArtifactRepositories)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