-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to buildsystem plugin to render dependencies in CG Manifest format #107
Open
shahzaibj
wants to merge
12
commits into
master
Choose a base branch
from
shahzaibj/print-deps-plugin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4d984b7
Add gradle tasks generation to build plugin to create CG Manifest for…
shahzaibj 8acd34a
Add licenses
shahzaibj 989efe7
Add license
shahzaibj 339eb03
Pretty print dependencies JSON
shahzaibj e2594d9
Print transitive dependencies with print deps task
shahzaibj 1a21e51
Add missing licenses
shahzaibj f3fd3d5
Some updates to dep rendering
shahzaibj 4a0b3f2
Add build extension properties for dependency rendering
shahzaibj 1bdaa56
Fix writing cg manifest report to provided directory
shahzaibj 407e6d0
Add javadoc and comments to build plugin dep rendering work
shahzaibj 27f3c58
Merge branch 'master' into shahzaibj/print-deps-plugin
shahzaibj 40a5e3f
Fix issue caused by merge
shahzaibj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
273 changes: 273 additions & 0 deletions
273
...n/java/com/microsoft/identity/buildsystem/rendering/AbstractGradleDependencyRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// All rights reserved. | ||
// | ||
// This code is licensed under the MIT License. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files(the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions : | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
package com.microsoft.identity.buildsystem.rendering; | ||
|
||
import com.microsoft.identity.buildsystem.rendering.settings.GradleDependencyRendererSettings; | ||
|
||
import org.gradle.api.Project; | ||
import org.gradle.api.artifacts.Configuration; | ||
import org.gradle.api.artifacts.Dependency; | ||
import org.gradle.api.artifacts.DependencySet; | ||
import org.gradle.api.artifacts.ProjectDependency; | ||
import org.gradle.api.artifacts.component.ProjectComponentIdentifier; | ||
import org.gradle.api.artifacts.result.DependencyResult; | ||
import org.gradle.api.artifacts.result.ResolutionResult; | ||
import org.gradle.api.artifacts.result.ResolvedComponentResult; | ||
import org.gradle.api.tasks.diagnostics.internal.DependencyReportRenderer; | ||
import org.gradle.api.tasks.diagnostics.internal.TextReportRenderer; | ||
import org.gradle.internal.deprecation.DeprecatableConfiguration; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.NonNull; | ||
import lombok.experimental.Accessors; | ||
|
||
/** | ||
* An abstract implementation of {@link DependencyReportRenderer} that uses the settings provided by | ||
* the {@link GradleDependencyRendererSettings} and renders the dependencies as specified by the | ||
* implementation of the {@link AbstractGradleDependencyRenderer#render(GradleDependency)} and | ||
* {@link AbstractGradleDependencyRenderer#complete(Project, Collection)} methods. | ||
*/ | ||
@AllArgsConstructor | ||
@Accessors(prefix = "m") | ||
public abstract class AbstractGradleDependencyRenderer extends TextReportRenderer implements DependencyReportRenderer { | ||
|
||
/** | ||
* Settings that govern some of the behavior while rendering the dependencies. | ||
* <p> | ||
* We are making this field available to children of this class so they can also take action on | ||
* these settings as applicable. | ||
*/ | ||
protected final GradleDependencyRendererSettings mGradleDependencyRendererSettings; | ||
|
||
private static final IMavenDependencyAdapter sMavenDependencyAdapter = new MavenDependencyAdapter(); | ||
|
||
private static final IDependencyTypeAdapter sConfigurationAdapter = new DependencyTypeAdapter(); | ||
|
||
/** | ||
* Renders a {@link GradleDependency} somewhere as indicated by the implementation of this | ||
* method. | ||
* | ||
* @param gradleDependency the {@link GradleDependency} to render | ||
*/ | ||
public abstract void render(@NonNull final GradleDependency gradleDependency); | ||
|
||
/** | ||
* Complete the rendering of all the {@link GradleDependency} dependencies in a {@link Project}. | ||
* | ||
* @param project the {@link Project} for which to complete the rendering | ||
* @param gradleDependencies the list of {@link GradleDependency} in this project | ||
*/ | ||
public abstract void complete(@NonNull final Project project, | ||
@NonNull final Collection<GradleDependency> gradleDependencies); | ||
|
||
/** | ||
* Keeps track of which dependencies have already been rendered. | ||
*/ | ||
private final Map<String, GradleDependency> mRenderedDepMap = new HashMap<>(); | ||
|
||
@Override | ||
public void startConfiguration(Configuration configuration) { | ||
// We don't need to do anything here by default | ||
// but let's just print configuration name | ||
System.out.println("Starting configuration: " + configuration.getName()); | ||
} | ||
|
||
@Override | ||
public void completeProject(Project project) { | ||
// let's just call our own complete method here and delegate the actual work to that | ||
// method's implementation | ||
complete(project, mRenderedDepMap.values()); | ||
} | ||
|
||
|
||
@Override | ||
public void render(Configuration configuration) { | ||
// If we want to render transitive dependencies and we can resolve them then let's render | ||
// them | ||
if (mGradleDependencyRendererSettings.isRenderTransitiveDependencies() && canBeResolved(configuration)) { | ||
// First resolve the configuration | ||
final ResolutionResult result = configuration.getIncoming().getResolutionResult(); | ||
// now render all dependencies in this configuration | ||
result.allDependencies(dependencyResult -> renderDependencyResult(configuration, dependencyResult)); | ||
} else { | ||
// The configuration couldn't be resolved..let's just render the direct dependencies | ||
System.out.println("Unable to resolve configuration: " + configuration.getName()); | ||
final DependencySet dependencies = configuration.getDependencies(); | ||
renderDependencySet(configuration, dependencies); | ||
} | ||
} | ||
|
||
@Override | ||
public void completeConfiguration(Configuration configuration) { | ||
// We don't need to do anything here by default | ||
} | ||
|
||
/** | ||
* Render each dependency in a Dependency Set. | ||
* | ||
* @param configuration the {@link Configuration} for which we are rendering dependencies | ||
* @param dependencies the {@link DependencySet} that contains a set of dependencies | ||
*/ | ||
private void renderDependencySet(@NonNull final Configuration configuration, | ||
@NonNull final DependencySet dependencies) { | ||
dependencies.iterator().forEachRemaining( | ||
dependency -> renderDependency(configuration, dependency) | ||
); | ||
} | ||
|
||
/** | ||
* Render the dependency represented by a {@link DependencyResult} object. | ||
* | ||
* @param configuration the {@link Configuration} for which we are rendering the dependency | ||
* @param dependencyResult the {@link DependencyResult} that needs to be rendered | ||
*/ | ||
private void renderDependencyResult(@NonNull final Configuration configuration, | ||
@NonNull final DependencyResult dependencyResult) { | ||
// first convert the internal dependency result into our custom IMavenDependency object | ||
final IMavenDependency depToRender = sMavenDependencyAdapter.adapt(dependencyResult); | ||
|
||
// Get the root dependency that brought in this dependency | ||
final ResolvedComponentResult rootResult = dependencyResult.getFrom(); | ||
|
||
// If the dependency is not null then we can render it | ||
if (depToRender != null) { | ||
IMavenDependency depRoot; | ||
|
||
// if the root is a Project and we've decided to NOT to render Projects, then let's skip | ||
if (rootResult.getId() instanceof ProjectComponentIdentifier && !mGradleDependencyRendererSettings.isRenderProjectDependency()) { | ||
depRoot = null; | ||
} else { | ||
// the root was NOT a project so we should proceed to render it anyway | ||
// take the root and convert that into our own custom IMavenDependency object | ||
depRoot = sMavenDependencyAdapter.adapt( | ||
rootResult.getModuleVersion() | ||
); | ||
} | ||
|
||
// Now render the dependency that we received | ||
renderInternal(configuration, depToRender, depRoot); | ||
} else { | ||
System.out.println("Weird. The dependency was null for " + dependencyResult.toString()); | ||
} | ||
} | ||
|
||
/** | ||
* Render the dependency represented by a {@link Dependency} object. | ||
* | ||
* @param configuration the {@link Configuration} for which we are rendering the dependency | ||
* @param dependency the {@link Dependency} that needs to be rendered | ||
*/ | ||
private void renderDependency(@NonNull final Configuration configuration, | ||
@NonNull final Dependency dependency) { | ||
if (shouldRender(dependency)) { | ||
renderInternal(configuration, sMavenDependencyAdapter.adapt(dependency), null); | ||
} | ||
} | ||
|
||
/** | ||
* Determines if we should render a dependency represented by the {@link Dependency} object. | ||
* | ||
* @param dependency the {@link Dependency} for which we need to decide if we want to render it | ||
* @return a boolean that indicates whether the dependency should be rendered | ||
*/ | ||
private boolean shouldRender(@NonNull final Dependency dependency) { | ||
return !(dependency instanceof ProjectDependency) || | ||
mGradleDependencyRendererSettings.isRenderProjectDependency(); | ||
} | ||
|
||
/** | ||
* Determines whether a gradle dependency configuration can be resolved. | ||
* | ||
* @param configuration the {@link Configuration} for which we need to decide if it can be | ||
* resolved | ||
* @return a boolean that indicates whether the configuration can be resolved | ||
*/ | ||
private boolean canBeResolved(Configuration configuration) { | ||
boolean isDeprecatedForResolving = ((DeprecatableConfiguration) configuration).getResolutionAlternatives() != null; | ||
return configuration.isCanBeResolved() && !isDeprecatedForResolving; | ||
} | ||
|
||
/** | ||
* An internal method that will take the dependency information supplied to it, convert it into | ||
* a {@link GradleDependency} representation and delegate the actual rendering to the | ||
* implementation of {@link AbstractGradleDependencyRenderer#render(GradleDependency)} method. | ||
* | ||
* @param configuration the {@link Configuration} for which we are rendering the dependency | ||
* @param mavenDependency the {@link IMavenDependency} that needs to be rendered | ||
* @param depRoot the {@link IMavenDependency} root of the dependency that we need to render | ||
*/ | ||
private void renderInternal(@NonNull final Configuration configuration, | ||
@NonNull final IMavenDependency mavenDependency, | ||
@Nullable final IMavenDependency depRoot) { | ||
// Take the configuration and adapt that into an internal dependency type | ||
final DependencyType incomingDependencyType = sConfigurationAdapter.adapt(configuration); | ||
|
||
// check if we already received (rendered) this dependency | ||
GradleDependency gradleDependency = mRenderedDepMap.get(mavenDependency.toString()); | ||
|
||
// we have not seen this dependency yet | ||
if (gradleDependency == null) { | ||
final Set<IMavenDependency> depRoots = new HashSet<>(); | ||
|
||
// we also have root dependency so let's add that to roots | ||
if (depRoot != null) { | ||
depRoots.add(depRoot); | ||
} | ||
|
||
// Create a GradleDependency object from the information we have | ||
gradleDependency = new GradleDependency( | ||
incomingDependencyType, | ||
mavenDependency, | ||
depRoots | ||
); | ||
} else { | ||
// we have already seen this dependency | ||
// so now we just need to update the roots because the root we received this time | ||
// might not have been recorded yet | ||
if (depRoot != null) { | ||
gradleDependency.addRootDependency(depRoot); | ||
} | ||
|
||
// We would also update dependency type. | ||
// It is possible that when we saw this dep earlier we got in a compile only classpath | ||
// so if we got runtime now then we would overwrite the dependency type | ||
if (incomingDependencyType == DependencyType.RUNTIME) { | ||
gradleDependency.setDependencyType(incomingDependencyType); | ||
} | ||
} | ||
|
||
// Now actually render this dependency | ||
// or do whatever the render method does ;) | ||
render(gradleDependency); | ||
|
||
// we rendered it...save it into the Map | ||
mRenderedDepMap.put(mavenDependency.toString(), gradleDependency); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somewhere there's a graph traversal... right? Looks like gradle handles that for you and just flattens the transitive list.