Skip to content
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

Correctly resolve to the project dependency if both project & it's artifact are present. #285

Merged
merged 1 commit into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class DependencyCache {

private final Configuration superConfiguration
private final Map<VersionlessDependency, String> lintJars = [:]
private final Map<VersionlessDependency, String> externalDeps = [:]
private final Map<VersionlessDependency, Set<String>> annotationProcessors = [:]
private final Map<VersionlessDependency, ExternalDependency> greatestVersions = [:]
private final Set<File> cachedCopies = [] as Set

private final Map<VersionlessDependency, ExternalDependency> externalDeps = [:]
private final Map<VersionlessDependency, TargetDependency> targetDeps = [:]

DependencyCache(
String name,
Expand Down Expand Up @@ -59,17 +59,19 @@ class DependencyCache {
build(cleanup)
}

String get(ExternalDependency dependency) {
String dep = externalDeps.get(dependency)
if (dep == null) {
String get(VersionlessDependency dependency) {
ExternalDependency externalDependency = externalDeps.get(dependency)
if (externalDependency == null) {
throw new IllegalStateException("Could not find dependency path for ${dependency}")
}
return dep

File cachedCopy = new File(cacheDir, externalDependency.getCacheName(useFullDepName))
return FileUtil.getRelativePath(rootProject.projectDir, cachedCopy)
}

@Synchronized
Set<String> getAnnotationProcessors(ExternalDependency dependency) {
ExternalDependency greatest = greatestVersions.get(dependency)
Set<String> getAnnotationProcessors(VersionlessDependency dependency) {
ExternalDependency greatest = externalDeps.get(dependency)
if (greatest.depFile.name.endsWith(".jar")) {
Set<String> processors = annotationProcessors.get(greatest)
if (!processors) {
Expand All @@ -85,24 +87,30 @@ class DependencyCache {

private void build(boolean cleanup) {
Set<File> resolvedFiles = [] as Set
Set<ExternalDependency> allExtDeps = [] as Set
superConfiguration.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact artifact ->
String identifier = artifact.id.componentIdentifier.displayName
File dep = artifact.file
resolvedFiles.add(dep)

if (!identifier.contains(" ")) {
ExternalDependency dependency = new ExternalDependency(artifact.moduleVersion.id, dep)
allExtDeps.add(dependency)
superConfiguration.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact artifact ->
String projectIdentifier = artifact.id.componentIdentifier.displayName

if (projectIdentifier.contains(" ")) {
TargetDependency dependency = new TargetDependency(artifact.moduleVersion.id, projectIdentifier)
targetDeps.put(dependency, dependency)
} else {
ExternalDependency dependency = new ExternalDependency(artifact.moduleVersion.id, artifact.file)
externalDeps.put(dependency, dependency)
}

resolvedFiles.add(artifact.file)
}

superConfiguration.files.findAll { File resolved ->
!resolvedFiles.contains(resolved)
}.each { File localDep ->
allExtDeps.add(ExternalDependency.fromLocal(localDep))
ExternalDependency localDependency = ExternalDependency.fromLocal(localDep)
externalDeps.put(localDependency, localDependency)
}

resolvedFiles = null

// Download sources if enabled
if (fetchSources) {
new IdeDependenciesExtractor().extractRepoFileDependencies(
Expand All @@ -113,8 +121,9 @@ class DependencyCache {
false)
}

allExtDeps.each { ExternalDependency e ->
greatestVersions.put(e, e)
Set<File> cachedCopies = [] as Set

externalDeps.each { _, ExternalDependency e ->
File cachedCopy = new File(cacheDir, e.getCacheName(useFullDepName))

// Copy the file into the cache
Expand All @@ -123,12 +132,9 @@ class DependencyCache {
}
cachedCopies.add(cachedCopy)

String path = FileUtil.getRelativePath(rootProject.projectDir, cachedCopy)
externalDeps.put(e, path)

// Extract Lint Jars
if (extractLintJars && cachedCopy.name.endsWith(".aar")) {
File lintJar = getPackagedLintJar(cachedCopy)
File lintJar = getPackagedLintJarFrom(cachedCopy)
if (lintJar != null) {
String lintJarPath = FileUtil.getRelativePath(rootProject.projectDir, lintJar)
lintJars.put(e, lintJarPath)
Expand All @@ -138,7 +144,7 @@ class DependencyCache {

// Fetch Sources
if (fetchSources) {
fetchSourcesFor(e)
cachedCopies.add(fetchSourcesFor(e))
}
}

Expand All @@ -156,7 +162,17 @@ class DependencyCache {
}
}

private static Configuration createSuperConfiguration(Project project, String superConfigName,
String getTargetIdentifier(VersionlessDependency dependency) {
TargetDependency targetDependency = targetDeps.get(dependency)
if (targetDependency) {
return targetDependency.projectIdentifier
} else {
return null
}
}

private static Configuration createSuperConfiguration(Project project,
String superConfigName,
Set<Configuration> configurations) {
Configuration superConfiguration = project.configurations.maybeCreate(superConfigName)
configurations.each {
Expand All @@ -165,12 +181,12 @@ class DependencyCache {
return superConfiguration
}

String getLintJar(ExternalDependency dependency) {
String getLintJar(VersionlessDependency dependency) {
return lintJars.get(dependency)
}

private void fetchSourcesFor(ExternalDependency dependency) {
String sourcesJarName = dependency.depFile.name.replaceFirst(/\.(jar|aar)$/, ExternalDependency.SOURCES_JAR)
private File fetchSourcesFor(ExternalDependency dependency) {
String sourcesJarName = dependency.getSourceCacheName(false)

File sourcesJar = null
if (FileUtils.directoryContains(rootProject.projectDir, dependency.depFile)) {
Expand All @@ -188,10 +204,10 @@ class DependencyCache {
if (sourcesJar != null && sourcesJar.exists() && !cachedCopy.exists()) {
FileUtils.copyFile(sourcesJar, cachedCopy)
}
cachedCopies.add(cachedCopy)
return cachedCopy
}

static File getPackagedLintJar(File aar) {
static File getPackagedLintJarFrom(File aar) {
File lintJar = new File(aar.parentFile, aar.name.replaceFirst(/\.aar$/, '-lint.jar'))
if (lintJar.exists()) {
return lintJar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ExternalDependency extends VersionlessDependency {

@Override
String toString() {
return "${this.version} : ${this.depFile.toString()}"
return "${this.group}:${this.name}:${this.version} -> ${this.depFile.toString()}"
}

String getCacheName(boolean useFullDepName = false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.uber.okbuck.core.dependency

import org.gradle.api.artifacts.ModuleVersionIdentifier


class TargetDependency extends VersionlessDependency {

final String projectIdentifier

TargetDependency(ModuleVersionIdentifier identifier, String projectIdentifier) {
super(identifier)
this.projectIdentifier = projectIdentifier
}

@Override
String toString() {
return "${this.group}:${this.name} -> ${this.projectIdentifier}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ class VersionlessDependency {
group = identifier.group
name = identifier.name
}

@Override
String toString() {
return "${this.group}:${this.name}"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.uber.okbuck.core.model.android

import com.uber.okbuck.core.dependency.ExternalDependency
import com.uber.okbuck.core.dependency.VersionlessDependency
import com.uber.okbuck.core.model.base.Scope
import com.uber.okbuck.core.model.base.Target
import com.uber.okbuck.core.util.FileUtil
Expand Down Expand Up @@ -57,12 +58,12 @@ class ExoPackageScope extends Scope {
first = last = exoPackageDep
}

ExternalDependency externalDep = base.external.find { ExternalDependency externalDependency ->
VersionlessDependency externalDep = base.external.find { VersionlessDependency dependency ->
boolean match = true
if (fullyQualified) {
match &= externalDependency.group == first
match &= dependency.group == first
}
match &= externalDependency.name == last
match &= dependency.name == last
return match
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.android.build.gradle.api.BaseVariantOutput
import com.uber.okbuck.OkBuckGradlePlugin
import com.uber.okbuck.core.dependency.DependencyCache
import com.uber.okbuck.core.dependency.ExternalDependency
import com.uber.okbuck.core.dependency.VersionlessDependency
import com.uber.okbuck.core.model.android.AndroidLibTarget
import com.uber.okbuck.core.model.groovy.GroovyLibTarget
import com.uber.okbuck.core.model.java.JavaLibTarget
Expand All @@ -28,8 +29,8 @@ class Scope {
DependencyCache depCache

protected final Project project
protected final Set<ExternalDependency> external = [] as Set
protected final Set<ExternalDependency> firstLevel = [] as Set
protected final Set<VersionlessDependency> external = [] as Set
protected final Set<VersionlessDependency> firstLevel = [] as Set

Scope(Project project,
Collection<String> configurations,
Expand All @@ -48,21 +49,21 @@ class Scope {
}

Set<String> getExternalDeps() {
external.collect { ExternalDependency dependency ->
return external.collect { VersionlessDependency dependency ->
depCache.get(dependency)
}
}

Set<String> getPackagedLintJars() {
external.findAll { ExternalDependency dependency ->
depCache.getLintJar(dependency) != null
}.collect { ExternalDependency dependency ->
return external.collect { VersionlessDependency dependency ->
depCache.getLintJar(dependency)
}.findAll { String lintJar ->
lintJar != null
}
}

Set<String> getAnnotationProcessors() {
((firstLevel.collect {
return ((firstLevel.collect {
depCache.getAnnotationProcessors(it)
} + targetDeps.collect { Target target ->
(List<String>) target.getProp(project.rootProject.okbuck.annotationProcessors, null)
Expand All @@ -79,47 +80,51 @@ class Scope {
}
}

Set<ResolvedArtifact> artifacts = validConfigurations.collect {
it.resolvedConfiguration.resolvedArtifacts
}.flatten() as Set<ResolvedArtifact>

// get all first level external dependencies
validConfigurations.collect {
it.resolvedConfiguration.firstLevelModuleDependencies.each { ResolvedDependency resolvedDependency ->
ResolvedArtifact artifact = resolvedDependency.moduleArtifacts[0]
if (!artifact.id.componentIdentifier.displayName.contains(" ")) {
firstLevel.add(new ExternalDependency(artifact.moduleVersion.id, artifact.file))
VersionlessDependency dependency = new VersionlessDependency(artifact.moduleVersion.id)

if (!depCache.getTargetIdentifier(dependency)) {
firstLevel.add(dependency)
}
}
}

// get all resolved artifacts including transitives
Set<ResolvedArtifact> artifacts = validConfigurations.collect {
it.resolvedConfiguration.resolvedArtifacts
}.flatten() as Set<ResolvedArtifact>

Set<File> files = validConfigurations.collect {
it.files
}.flatten() as Set<File>

Set<File> resolvedFiles = [] as Set
artifacts.each { ResolvedArtifact artifact ->
String identifier = artifact.id.componentIdentifier.displayName
File dep = artifact.file
VersionlessDependency dependency = new VersionlessDependency(artifact.moduleVersion.id)

resolvedFiles.add(dep)

if (identifier.contains(" ")) {
Project targetProject = project.project(identifier.replaceFirst("project ", ""))
Target target = getTargetForOutput(targetProject, dep)
String targetIdentifier = depCache.getTargetIdentifier(dependency)
if (targetIdentifier) {
Project targetProject = project.project(targetIdentifier.replaceFirst("project ", ""))
Target target = getTargetForOutput(targetProject, artifact.file)
if (target) {
targetDeps.add(target)
}
} else {
external.add(new ExternalDependency(artifact.moduleVersion.id, dep))
external.add(dependency)
}

resolvedFiles.add(artifact.file)
}

// add remaining local jar/aar files to external dependency
files.findAll { File resolved ->
!resolvedFiles.contains(resolved)
}.each { File localDep ->
external.add(ExternalDependency.fromLocal(localDep))
}

}

@SuppressWarnings("GrReassignedInClosureLocalVar")
Expand Down