Skip to content

Commit

Permalink
More cleanup and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautam Korlam committed Jun 5, 2017
1 parent 6e4e35e commit 226aaa8
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .buckversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b46883515372ee084fbe8f77a403169bbc1d4557
800d5a9669417d5a8b4bc455e34a629422ed1ff1
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,40 @@ class DependencyCache {
}
}

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

private File fetchSourcesFor(ExternalDependency dependency) {
// We do not have sources for these dependencies
if (isWhiteListed(dependency.depFile)) {
return null
}

File cachedCopy = new File(cacheDir, dependency.getSourceCacheName(useFullDepName))

if (!cachedCopy.exists()) {
String sourcesJarName = dependency.getSourceCacheName(false)
File sourcesJar = new File(dependency.depFile.parentFile, sourcesJarName)

if (!sourcesJar.exists()) {
def sourceJars = rootProject.fileTree(
dir: dependency.depFile.parentFile.parentFile.absolutePath,
includes: ["**/${sourcesJarName}"]) as List
if (sourceJars.size() == 1) {
sourcesJar = sourceJars[0]
} else if (sourceJars.size() > 1) {
throw new IllegalStateException("Found multiple source jars: ${sourceJars} for ${dependency}")
}
}
if (sourcesJar.exists()) {
Files.createSymbolicLink(cachedCopy.toPath(), sourcesJar.toPath())
}
}

return cachedCopy.exists() ? cachedCopy : null
}

private void build(boolean cleanup) {
Set<File> resolvedFiles = [] as Set

Expand Down Expand Up @@ -178,6 +212,41 @@ class DependencyCache {
return WHITELIST_LOCAL_PATTERNS.find { depFile.absolutePath.contains(it) } != null
}

static File getPackagedLintJarFrom(File aar) {
File lintJar = new File(aar.parentFile, aar.name.replaceFirst(/\.aar$/, '-lint.jar'))
if (lintJar.exists()) {
return lintJar
}
FileSystem zipFile = FileSystems.newFileSystem(aar.toPath(), null)
Path packagedLintJar = zipFile.getPath("lint.jar")
if (Files.exists(packagedLintJar)) {
Files.copy(packagedLintJar, lintJar.toPath(), StandardCopyOption.REPLACE_EXISTING)
return lintJar
} else {
return null
}
}

static File getAnnotationProcessorsFile(File jar) {
File processors = new File(jar.parentFile, jar.name.replaceFirst(/\.jar$/, '.processors'))
if (processors.exists()) {
return processors
}

JarFile jarFile = new JarFile(jar)
JarEntry jarEntry = (JarEntry) jarFile.getEntry("META-INF/services/javax.annotation.processing.Processor")
if (jarEntry) {
List<String> processorClasses = IOUtils.toString(jarFile.getInputStream(jarEntry))
.trim().split("\\n").findAll { String entry ->
!entry.startsWith('#') && !entry.trim().empty // filter out comments and empty lines
}
processors.text = processorClasses.join('\n')
} else {
processors.createNewFile()
}
return processors
}

private static Configuration createSuperConfiguration(Project project,
String superConfigName,
Set<Configuration> configurations) {
Expand Down Expand Up @@ -214,73 +283,4 @@ class DependencyCache {
println "\n${message}\n"
}
}

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

private File fetchSourcesFor(ExternalDependency dependency) {
// We do not have sources for these dependencies
if (isWhiteListed(dependency.depFile)) {
return null
}

File cachedCopy = new File(cacheDir, dependency.getSourceCacheName(useFullDepName))

if (!cachedCopy.exists()) {
String sourcesJarName = dependency.getSourceCacheName(false)
File sourcesJar = new File(dependency.depFile.parentFile, sourcesJarName)

if (!sourcesJar.exists()) {
def sourceJars = rootProject.fileTree(
dir: dependency.depFile.parentFile.parentFile.absolutePath,
includes: ["**/${sourcesJarName}"]) as List
if (sourceJars.size() == 1) {
sourcesJar = sourceJars[0]
} else if (sourceJars.size() > 1) {
throw new IllegalStateException("Found multiple source jars: ${sourceJars} for ${dependency}")
}
}
if (sourcesJar.exists()) {
Files.createSymbolicLink(cachedCopy.toPath(), sourcesJar.toPath())
}
}

return cachedCopy.exists() ? cachedCopy : null
}

static File getPackagedLintJarFrom(File aar) {
File lintJar = new File(aar.parentFile, aar.name.replaceFirst(/\.aar$/, '-lint.jar'))
if (lintJar.exists()) {
return lintJar
}
FileSystem zipFile = FileSystems.newFileSystem(aar.toPath(), null)
Path packagedLintJar = zipFile.getPath("lint.jar")
if (Files.exists(packagedLintJar)) {
Files.copy(packagedLintJar, lintJar.toPath(), StandardCopyOption.REPLACE_EXISTING)
return lintJar
} else {
return null
}
}

static File getAnnotationProcessorsFile(File jar) {
File processors = new File(jar.parentFile, jar.name.replaceFirst(/\.jar$/, '.processors'))
if (processors.exists()) {
return processors
}

JarFile jarFile = new JarFile(jar)
JarEntry jarEntry = (JarEntry) jarFile.getEntry("META-INF/services/javax.annotation.processing.Processor")
if (jarEntry) {
List<String> processorClasses = IOUtils.toString(jarFile.getInputStream(jarEntry))
.trim().split("\\n").findAll { String entry ->
!entry.startsWith('#') && !entry.trim().empty // filter out comments and empty lines
}
processors.text = processorClasses.join('\n')
} else {
processors.createNewFile()
}
return processors
}
}
52 changes: 30 additions & 22 deletions buildSrc/src/main/java/com/uber/okbuck/core/util/KotlinUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,31 @@

import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;

public final class KotlinUtil {

private static final String KOTLIN_DEPS_CONFIG = "okbuck_kotlin_deps";
private static final String KOTLIN_GROUP = "org.jetbrains.kotlin";
private static final String KOTLIN_COMPILER_MODULE = "kotlin-compiler";
private static final String KOTLIN_GRADLE_MODULE = "kotlin-gradle-plugin";
private static final String KOTLIN_STDLIB_MODULE = "kotlin-stdlib";
public static final String KOTLIN_HOME_LOCATION = OkBuckGradlePlugin.DEFAULT_CACHE_PATH + "/kotlin_home";

private KotlinUtil() {}

@Nullable
static String getKotlinVersion(Project project) {
return project.getBuildscript()
.getConfigurations()
.getByName("classpath")
.getResolvedConfiguration()
.getResolvedArtifacts()
.stream()
.filter(resolvedArtifact -> {
ModuleVersionIdentifier identifier = resolvedArtifact.getModuleVersion().getId();
return (KOTLIN_GROUP.equals(identifier.getGroup()) &&
KOTLIN_COMPILER_MODULE.equals(identifier.getName()));
})
.findFirst()
.map(r -> r.getModuleVersion().getId().getVersion())
.orElse(null);
}

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void setupKotlinHome(Project rootProject) {
String kotlinVersion = getKotlinVersion(rootProject);

String kotlinVersion = ProjectUtil.findVersionInClasspath(rootProject, KOTLIN_GROUP, KOTLIN_GRADLE_MODULE);
Configuration kotlinConfig = rootProject.getConfigurations().maybeCreate(KOTLIN_DEPS_CONFIG);
DependencyHandler handler = rootProject.getDependencies();
handler.add(KOTLIN_DEPS_CONFIG, String.format("%s:%s:%s", KOTLIN_GROUP, KOTLIN_COMPILER_MODULE, kotlinVersion));
Expand All @@ -53,5 +40,26 @@ public static void setupKotlinHome(Project rootProject) {
KOTLIN_HOME_LOCATION,
Collections.singleton(kotlinConfig),
null);

removeVersions(Paths.get(KOTLIN_HOME_LOCATION),
KOTLIN_COMPILER_MODULE,
KOTLIN_STDLIB_MODULE);
}

private static void removeVersions(Path dir, String... toRename) {
for (String rename : toRename) {
try {
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String fileName = file.getFileName().toString();
if (fileName.startsWith(rename)) {
Files.move(file, file.getParent().resolve(rename + ".jar"));
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException ignored) {}
}
}
}
16 changes: 1 addition & 15 deletions buildSrc/src/main/java/com/uber/okbuck/core/util/LintUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.apache.commons.io.FileUtils;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.jetbrains.annotations.Nullable;

import java.io.File;
Expand All @@ -31,19 +29,7 @@ private LintUtil() {}

@Nullable
public static String getDefaultLintVersion(Project project) {
return project.getBuildscript()
.getConfigurations()
.getByName("classpath")
.getResolvedConfiguration()
.getResolvedArtifacts()
.stream()
.filter(resolvedArtifact -> {
ModuleVersionIdentifier identifier = resolvedArtifact.getModuleVersion().getId();
return (LINT_GROUP.equals(identifier.getGroup()) && LINT_MODULE.equals(identifier.getName()));
})
.findFirst()
.map(r -> r.getModuleVersion().getId().getVersion())
.orElse(null);
return ProjectUtil.findVersionInClasspath(project, LINT_GROUP, LINT_MODULE);
}

@SuppressWarnings("ResultOfMethodCallIgnored")
Expand Down
25 changes: 2 additions & 23 deletions buildSrc/src/main/java/com/uber/okbuck/core/util/ProguardUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency;
import org.jetbrains.annotations.Nullable;

Expand All @@ -23,9 +21,9 @@ private ProguardUtil() {}

@Nullable
public static String getProguardJarPath(Project project) {
String proguardVersion = ProjectUtil.findVersionInClasspath(project, PROGUARD_GROUP, PROGUARD_MODULE);
Configuration proguardConfiguration = project.getConfigurations().detachedConfiguration(
new DefaultExternalModuleDependency(PROGUARD_GROUP, PROGUARD_MODULE,
getProguardVersion(project)));
new DefaultExternalModuleDependency(PROGUARD_GROUP, PROGUARD_MODULE, proguardVersion));
DependencyCache proguardCache = new DependencyCache("proguard",
project.getRootProject(),
PROGUARD_DEPS_CACHE,
Expand All @@ -36,23 +34,4 @@ public static String getProguardJarPath(Project project) {
} catch (IllegalStateException ignored) {}
return proguardJarPath;
}

@Nullable
private static String getProguardVersion(Project project) {
return project.getBuildscript()
.getConfigurations()
.getByName("classpath")
.getResolvedConfiguration()
.getResolvedArtifacts()
.stream()
.filter(ProguardUtil::findProguard)
.findFirst()
.map(r -> r.getModuleVersion().getId().getVersion())
.orElse(null);
}

private static boolean findProguard(ResolvedArtifact artifact) {
ModuleVersionIdentifier identifier = artifact.getModuleVersion().getId();
return (PROGUARD_GROUP.equals(identifier.getGroup()) && PROGUARD_MODULE.equals(identifier.getName()));
}
}
20 changes: 20 additions & 0 deletions buildSrc/src/main/java/com/uber/okbuck/core/util/ProjectUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.uber.okbuck.core.model.base.TargetCache;

import org.gradle.api.Project;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.plugins.ApplicationPlugin;
import org.gradle.api.plugins.GroovyPlugin;
import org.gradle.api.plugins.JavaPlugin;
Expand Down Expand Up @@ -60,4 +61,23 @@ static OkBuckGradlePlugin getPlugin(Project project) {
private static TargetCache getTargetCache(Project project) {
return getPlugin(project).targetCache;
}

static String findVersionInClasspath(Project project, String group, String module) {
return project.getBuildscript()
.getConfigurations()
.getByName("classpath")
.getIncoming()
.getArtifacts()
.getArtifacts()
.stream()
.filter(resolvedArtifactResult -> {
ModuleComponentIdentifier identifier =
(ModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier();
return (group.equals(identifier.getGroup()) &&
module.equals(identifier.getModule()));
})
.findFirst()
.map(r -> ((ModuleComponentIdentifier) r.getId().getComponentIdentifier()).getVersion())
.orElse(null);
}
}
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def versions = [
butterKnifeVersion : '8.4.0',
daggerVersion : '2.8',
kotlinVersion : '1.0.6',
kotlinVersion : '1.1.2',
leakCanaryVersion : '1.5',
supportVersion : '25.0.1',
androidPluginVersion : '2.3.2',
Expand Down

0 comments on commit 226aaa8

Please sign in to comment.