001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl.scope;
020
021import java.util.Comparator;
022import java.util.stream.Collectors;
023
024import org.eclipse.aether.impl.scope.BuildScope;
025import org.eclipse.aether.impl.scope.ScopeManagerConfiguration;
026import org.eclipse.aether.scope.ResolutionScope;
027
028import static org.eclipse.aether.impl.scope.BuildScopeQuery.all;
029
030/**
031 * This is a diagnostic tool to dump out scope manager states.
032 */
033public final class ScopeManagerDump {
034
035    /**
036     * Invoke this method with configuration and this class will dump its interpretation.
037     */
038    public static void dump(ScopeManagerConfiguration configuration) {
039        new ScopeManagerDump(configuration);
040    }
041
042    private ScopeManagerDump(ScopeManagerConfiguration configuration) {
043        ScopeManagerImpl scopeManager = new ScopeManagerImpl(configuration);
044        System.out.println();
045        dumpBuildScopes(scopeManager);
046        System.out.println();
047        dumpDependencyScopes(scopeManager);
048        System.out.println();
049        dumpDependencyScopeDerives(scopeManager);
050        System.out.println();
051        dumpResolutionScopes(scopeManager);
052    }
053
054    private void dumpBuildScopes(ScopeManagerImpl scopeManager) {
055        System.out.println(scopeManager.getId() + " defined build scopes:");
056        scopeManager.getBuildScopeSource().query(all()).stream()
057                .sorted(Comparator.comparing(BuildScope::order))
058                .forEach(s -> System.out.println(s.getId() + " (order=" + s.order() + ")"));
059    }
060
061    private void dumpDependencyScopes(ScopeManagerImpl scopeManager) {
062        System.out.println(scopeManager.getId() + " defined dependency scopes:");
063        scopeManager.getDependencyScopeUniverse().stream()
064                .sorted(Comparator.comparing(scopeManager::getDependencyScopeWidth)
065                        .reversed())
066                .forEach(s -> {
067                    System.out.println(s + " (width=" + scopeManager.getDependencyScopeWidth(s) + ")");
068                    System.out.println("  Query : " + scopeManager.getPresence(s));
069                    System.out.println("  Presence: "
070                            + scopeManager.getBuildScopeSource().query(scopeManager.getPresence(s)).stream()
071                                    .map(BuildScope::getId)
072                                    .collect(Collectors.toSet()));
073                    System.out.println("  Main project scope: "
074                            + scopeManager
075                                    .getDependencyScopeMainProjectBuildScope(s)
076                                    .map(BuildScope::getId)
077                                    .orElse("null"));
078                });
079    }
080
081    private void dumpDependencyScopeDerives(ScopeManagerImpl scopeManager) {
082        System.out.println(scopeManager.getId() + " defined dependency derive matrix:");
083        ManagedScopeDeriver deriver = new ManagedScopeDeriver(scopeManager);
084        scopeManager.getDependencyScopeUniverse().stream()
085                .sorted(Comparator.comparing(scopeManager::getDependencyScopeWidth)
086                        .reversed())
087                .forEach(parent -> scopeManager.getDependencyScopeUniverse().stream()
088                        .sorted(Comparator.comparing(scopeManager::getDependencyScopeWidth)
089                                .reversed())
090                        .forEach(child -> System.out.println(parent.getId() + " w/ child " + child.getId() + " -> "
091                                + deriver.getDerivedScope(parent.getId(), child.getId()))));
092    }
093
094    private void dumpResolutionScopes(ScopeManagerImpl scopeManager) {
095        System.out.println(scopeManager.getId() + " defined resolution scopes:");
096        scopeManager.getResolutionScopeUniverse().stream()
097                .sorted(Comparator.comparing(ResolutionScope::getId))
098                .forEach(s -> {
099                    System.out.println("* " + s.getId());
100                    System.out.println("     Directly included: " + scopeManager.getDirectlyIncludedLabels(s));
101                    System.out.println("     Directly excluded: " + scopeManager.getDirectlyExcludedLabels(s));
102                    System.out.println(" Transitively excluded: " + scopeManager.getTransitivelyExcludedLabels(s));
103                });
104    }
105}