Skip to content

Commit

Permalink
Infer sources using ResolvedArtifactResult instead of doing filesyste…
Browse files Browse the repository at this point in the history
…m check (#805)

* Infer sources using ResolvedArtifactResult instead of doing filesystem
check.

* Remove unwanted changes
  • Loading branch information
raviagarwal7 authored and kageiit committed Jan 26, 2019
1 parent 76c886e commit 0cd125d
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public static List<Rule> compose(Collection<ExternalDependency> dependencies) {
throw new IllegalStateException("Dependency not a valid prebuilt: " + dependency);
}

String source = dependency.hasSourceFile() ? dependency.getSourceFileName() : null;
String source;
if (dependency.getRealSourceFile().isPresent()) {
source = dependency.getSourceFileName();
} else {
source = null;
}

ImmutableList.Builder<Rule> rulesBuilder = ImmutableList.builder();
rulesBuilder.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public static List<Rule> compose(Collection<ExternalDependency> dependencies) {
.enableJetifier(dependency.enableJetifier())
.sha256(DependencyUtils.shaSum256(dependency.getRealDependencyFile()));

if (dependency.hasSourceFile()) {
rule.sourcesSha256(DependencyUtils.shaSum256(dependency.getRealSourceFile()));
}
dependency
.getRealSourceFile()
.ifPresent(file -> rule.sourcesSha256(DependencyUtils.shaSum256(file)));

rule.name(dependency.getTargetName());
rule.ruleType(RuleType.PREBUILT.getBuckName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ public abstract class BaseExternalDependency {
public static final String JAR = "jar";

private static final String NAME_DELIMITER = "-";
private static final String SOURCES = "sources";

public abstract VersionlessDependency versionless();

public abstract String version();

public abstract File realDependencyFile();

public abstract Optional<File> realDependencySourceFile();

abstract boolean isVersioned();

public static Builder builder() {
Expand All @@ -43,6 +44,8 @@ public abstract static class Builder {

public abstract Builder setRealDependencyFile(File value);

public abstract Builder setRealDependencySourceFile(Optional<File> value);

public abstract BaseExternalDependency build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class DependencyCache {
private static final Logger LOG = LoggerFactory.getLogger(DependencyCache.class);
private final Project rootProject;
private final DependencyManager dependencyManager;
private final boolean fetchSources;
private final boolean skipPrebuilt;
private final Map<VersionlessDependency, ExternalDependency> forcedDeps = new HashMap<>();

Expand All @@ -42,7 +41,6 @@ public DependencyCache(
@Nullable String forcedConfiguration) {
this.rootProject = project.getRootProject();
this.dependencyManager = dependencyManager;
this.fetchSources = ProjectUtil.getOkBuckExtension(project).getIntellijExtension().sources;
this.skipPrebuilt = skipPrebuilt;

if (forcedConfiguration != null) {
Expand Down Expand Up @@ -72,26 +70,17 @@ public DependencyCache(
this(project, dependencyManager, skipPrebuilt, null);
}

public ExternalDependency get(ExternalDependency externalDependency, boolean resolveOnly) {
public final ExternalDependency get(ExternalDependency externalDependency) {
LOG.info("Requested dependency {}", externalDependency);
ExternalDependency dependency =
forcedDeps.getOrDefault(externalDependency.getVersionless(), externalDependency);
LOG.info("Picked dependency {}", dependency);

dependencyManager.addDependency(dependency, skipPrebuilt);

if (!resolveOnly && fetchSources) {
LOG.info("Fetching sources for {}", dependency);
dependency.computeSourceFilePath(rootProject);
}

return dependency;
}

public final ExternalDependency get(ExternalDependency externalDependency) {
return get(externalDependency, false);
}

/**
* Get the list of annotation processor classes provided by a dependency.
*
Expand Down Expand Up @@ -182,17 +171,9 @@ private Set<ExternalDependency> build(Set<Configuration> configurations) {
.map(
configuration ->
DependencyUtils.resolveExternal(
configuration, externalDependenciesExtension, jetifierExtension))
rootProject, configuration, externalDependenciesExtension, jetifierExtension))
.flatMap(Collection::stream)
.map(dependency -> get(dependency, true))
.map(this::get)
.collect(Collectors.toSet());
}

private static IllegalStateException artifactResolveException(Exception e) {
return new IllegalStateException(
"Failed to resolve an artifact. Make sure you have a repositories block defined. "
+ "See https://github.com/uber/okbuck/wiki/Known-caveats#could-not-resolve-all-"
+ "dependencies-for-configuration for more information.",
e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.uber.okbuck.extension.ExternalDependenciesExtension;
import com.uber.okbuck.extension.JetifierExtension;
import java.io.File;
import javax.annotation.Nullable;
import org.apache.commons.io.FilenameUtils;

public final class DependencyFactory {
Expand All @@ -28,6 +29,7 @@ public static ExternalDependency from(
String name,
String version,
File dependencyFile,
@Nullable File dependencySourceFile,
ExternalDependenciesExtension externalDependenciesExtension,
JetifierExtension jetifierExtension) {
String classifier = DependencyUtils.getModuleClassifier(dependencyFile.getName(), version);
Expand All @@ -39,6 +41,7 @@ public static ExternalDependency from(
version,
classifier,
dependencyFile,
dependencySourceFile,
externalDependenciesExtension,
jetifierExtension);
}
Expand All @@ -49,29 +52,32 @@ public static ExternalDependency from(
version,
classifier,
dependencyFile,
dependencySourceFile,
externalDependenciesExtension,
jetifierExtension);
}

/**
* Create an External Dependency from a local dependency
*
* @param localDep local dependency file
* @param localDependency local dependency file
* @param externalDependenciesExtension External Dependency Extension
* @param jetifierExtension Jetifier Extension
* @return External Dependency
*/
public static LocalExternalDependency fromLocal(
File localDep,
File localDependency,
@Nullable File localSourceDependency,
ExternalDependenciesExtension externalDependenciesExtension,
JetifierExtension jetifierExtension) {

return new LocalExternalDependency(
LOCAL_GROUP,
FilenameUtils.getBaseName(localDep.getName()),
FilenameUtils.getBaseName(localDependency.getName()),
LOCAL_DEP_VERSION,
null,
localDep,
localDependency,
localSourceDependency,
externalDependenciesExtension,
jetifierExtension);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.uber.okbuck.core.dependency;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.hash.Hashing;
import com.google.common.io.Files;
import com.uber.okbuck.core.util.FileUtil;
import com.uber.okbuck.core.util.ProjectUtil;
import com.uber.okbuck.extension.ExternalDependenciesExtension;
import com.uber.okbuck.extension.JetifierExtension;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Objects;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
Expand All @@ -25,7 +22,7 @@
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.artifacts.component.ProjectComponentIdentifier;
import org.gradle.api.file.FileTree;
import org.gradle.api.artifacts.result.ResolvedArtifactResult;
import org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration;

public final class DependencyUtils {
Expand Down Expand Up @@ -109,22 +106,6 @@ static String getModuleClassifier(String fileNameString, @Nullable String versio
}
}

@Nullable
static Path getSingleZipFilePath(Project project, File baseDir, String zipToFind) {
FileTree zipFiles =
project.fileTree(
ImmutableMap.of(
"dir", baseDir.getAbsolutePath(), "includes", ImmutableList.of("**/" + zipToFind)));

return zipFiles
.getFiles()
.stream()
.filter(FileUtil::isZipFile)
.min(Comparator.comparing(DependencyUtils::jarComparisonKeyFunction))
.map(File::toPath)
.orElse(null);
}

private static long jarComparisonKeyFunction(File file) {
try {
return new JarFile(file).entries().nextElement().getTime();
Expand All @@ -134,22 +115,35 @@ private static long jarComparisonKeyFunction(File file) {
}

public static Set<ExternalDependency> resolveExternal(
Project project,
Configuration configuration,
ExternalDependenciesExtension externalDependenciesExtension,
JetifierExtension jetifierExtension) {
try {
return configuration
.getIncoming()
.getArtifacts()
.getArtifacts()
Set<ResolvedArtifactResult> consumableArtifacts =
configuration
.getIncoming()
.getArtifacts()
.getArtifacts()
.stream()
.filter(
artifact ->
!(artifact.getId().getComponentIdentifier()
instanceof ProjectComponentIdentifier))
.filter(artifact -> DependencyUtils.isConsumable(artifact.getFile()))
.collect(Collectors.toSet());

Map<ComponentIdentifier, ResolvedArtifactResult> componentIdToSourcesArtifactMap =
new HashMap<>(ProjectUtil.downloadSources(project, consumableArtifacts));

return consumableArtifacts
.stream()
.map(
artifact -> {
ComponentIdentifier identifier = artifact.getId().getComponentIdentifier();
if (identifier instanceof ProjectComponentIdentifier
|| !isConsumable(artifact.getFile())) {
return null;
}
ResolvedArtifactResult sourcesArtifact =
componentIdToSourcesArtifactMap.get(identifier);

if (identifier instanceof ModuleComponentIdentifier
&& ((ModuleComponentIdentifier) identifier).getVersion().length() > 0) {
ModuleComponentIdentifier moduleIdentifier =
Expand All @@ -159,14 +153,17 @@ public static Set<ExternalDependency> resolveExternal(
moduleIdentifier.getModule(),
moduleIdentifier.getVersion(),
artifact.getFile(),
sourcesArtifact != null ? sourcesArtifact.getFile() : null,
externalDependenciesExtension,
jetifierExtension);
} else {
return DependencyFactory.fromLocal(
artifact.getFile(), externalDependenciesExtension, jetifierExtension);
artifact.getFile(),
sourcesArtifact != null ? sourcesArtifact.getFile() : null,
externalDependenciesExtension,
jetifierExtension);
}
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());
} catch (DefaultLenientConfiguration.ArtifactResolveException e) {
throw artifactResolveException(e);
Expand Down
Loading

0 comments on commit 0cd125d

Please sign in to comment.