-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #420 from rpalcolea/minimize-project-excludes-tests
Minimize - Excluding a project also excludes its dependencies
- Loading branch information
Showing
5 changed files
with
314 additions
and
109 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
...roovy/com/github/jengelman/gradle/plugins/shadow/internal/AbstractDependencyFilter.groovy
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,120 @@ | ||
package com.github.jengelman.gradle.plugins.shadow.internal | ||
|
||
import groovy.util.logging.Slf4j | ||
import org.gradle.api.Project | ||
import org.gradle.api.artifacts.Configuration | ||
import org.gradle.api.artifacts.Dependency | ||
import org.gradle.api.artifacts.ResolvedDependency | ||
import org.gradle.api.file.FileCollection | ||
import org.gradle.api.specs.Spec | ||
import org.gradle.api.specs.Specs | ||
|
||
@Slf4j | ||
abstract class AbstractDependencyFilter implements DependencyFilter { | ||
private final Project project | ||
|
||
protected final List<Spec<? super ResolvedDependency>> includeSpecs = [] | ||
protected final List<Spec<? super ResolvedDependency>> excludeSpecs = [] | ||
|
||
AbstractDependencyFilter(Project project) { | ||
assert project | ||
this.project = project | ||
} | ||
|
||
abstract protected void resolve(Set<ResolvedDependency> dependencies, | ||
Set<ResolvedDependency> includedDependencies, | ||
Set<ResolvedDependency> excludedDependencies) | ||
|
||
FileCollection resolve(Configuration configuration) { | ||
Set<ResolvedDependency> includedDeps = [] | ||
Set<ResolvedDependency> excludedDeps = [] | ||
resolve(configuration.resolvedConfiguration.firstLevelModuleDependencies, includedDeps, excludedDeps) | ||
return project.files(configuration.files) - project.files(excludedDeps.collect { | ||
it.moduleArtifacts*.file | ||
}.flatten()) | ||
} | ||
|
||
FileCollection resolve(Collection<Configuration> configurations) { | ||
configurations.collect { | ||
resolve(it) | ||
}.sum() as FileCollection ?: project.files() | ||
} | ||
|
||
/** | ||
* Exclude dependencies that match the provided spec. | ||
* | ||
* @param spec | ||
* @return | ||
*/ | ||
DependencyFilter exclude(Spec<? super ResolvedDependency> spec) { | ||
excludeSpecs << spec | ||
return this | ||
} | ||
|
||
/** | ||
* Include dependencies that match the provided spec. | ||
* | ||
* @param spec | ||
* @return | ||
*/ | ||
DependencyFilter include(Spec<? super ResolvedDependency> spec) { | ||
includeSpecs << spec | ||
return this | ||
} | ||
|
||
/** | ||
* Create a spec that matches the provided project notation on group, name, and version | ||
* @param notation | ||
* @return | ||
*/ | ||
Spec<? super ResolvedDependency> project(Map<String, ?> notation) { | ||
dependency(project.dependencies.project(notation)) | ||
} | ||
|
||
/** | ||
* Create a spec that matches the default configuration for the provided project path on group, name, and version | ||
* | ||
* @param notation | ||
* @return | ||
*/ | ||
Spec<? super ResolvedDependency> project(String notation) { | ||
dependency(project.dependencies.project(path: notation, configuration: 'default')) | ||
} | ||
|
||
/** | ||
* Create a spec that matches dependencies using the provided notation on group, name, and version | ||
* @param notation | ||
* @return | ||
*/ | ||
Spec<? super ResolvedDependency> dependency(Object notation) { | ||
dependency(project.dependencies.create(notation)) | ||
} | ||
|
||
/** | ||
* Create a spec that matches the provided dependency on group, name, and version | ||
* @param dependency | ||
* @return | ||
*/ | ||
Spec<? super ResolvedDependency> dependency(Dependency dependency) { | ||
this.dependency({ ResolvedDependency it -> | ||
(!dependency.group || it.moduleGroup.matches(dependency.group)) && | ||
(!dependency.name || it.moduleName.matches(dependency.name)) && | ||
(!dependency.version || it.moduleVersion.matches(dependency.version)) | ||
}) | ||
} | ||
|
||
/** | ||
* Create a spec that matches the provided closure | ||
* @param spec | ||
* @return | ||
*/ | ||
Spec<? super ResolvedDependency> dependency(Closure spec) { | ||
return Specs.<ResolvedDependency>convertClosureToSpec(spec) | ||
} | ||
|
||
protected boolean isIncluded(ResolvedDependency dependency) { | ||
boolean include = includeSpecs.empty || includeSpecs.any { it.isSatisfiedBy(dependency) } | ||
boolean exclude = !excludeSpecs.empty && excludeSpecs.any { it.isSatisfiedBy(dependency) } | ||
return include && !exclude | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
...roovy/com/github/jengelman/gradle/plugins/shadow/internal/MinimizeDependencyFilter.groovy
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,29 @@ | ||
package com.github.jengelman.gradle.plugins.shadow.internal | ||
|
||
import groovy.util.logging.Slf4j | ||
import org.gradle.api.Project | ||
import org.gradle.api.artifacts.ResolvedDependency | ||
|
||
@Slf4j | ||
class MinimizeDependencyFilter extends AbstractDependencyFilter { | ||
|
||
MinimizeDependencyFilter(Project project) { | ||
super(project) | ||
} | ||
|
||
@Override | ||
protected void resolve(Set<ResolvedDependency> dependencies, | ||
Set<ResolvedDependency> includedDependencies, | ||
Set<ResolvedDependency> excludedDependencies) { | ||
|
||
dependencies.each { | ||
if (isIncluded(it) && !isParentExcluded(excludedDependencies, it) ? includedDependencies.add(it) : excludedDependencies.add(it)) { | ||
resolve(it.children, includedDependencies, excludedDependencies) | ||
} | ||
} | ||
} | ||
|
||
private boolean isParentExcluded(Set<ResolvedDependency> excludedDependencies, ResolvedDependency dependency) { | ||
excludedDependencies.any { dependency.parents.contains(it) } | ||
} | ||
} |
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
Oops, something went wrong.