diff --git a/README-zh.md b/README-zh.md index 36ba3f67..f6e6a484 100644 --- a/README-zh.md +++ b/README-zh.md @@ -9,7 +9,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.uber:okbuck:0.53.2' + classpath 'com.uber:okbuck:0.53.3' } } diff --git a/README.md b/README.md index b2fab055..8ee1f176 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.uber:okbuck:0.53.2' + classpath 'com.uber:okbuck:0.53.3' } } diff --git a/Usage.md b/Usage.md index 9b93a8db..d75b18db 100644 --- a/Usage.md +++ b/Usage.md @@ -62,6 +62,11 @@ okbuck { experimental { transform = true } + + externalDependencies { + cache = "3rdparty/jvm" + cleanCacheDir = true + } } dependencies { diff --git a/buildSrc/src/main/java/com/uber/okbuck/core/manager/DependencyManager.java b/buildSrc/src/main/java/com/uber/okbuck/core/manager/DependencyManager.java index d6fb7d85..efb84c15 100644 --- a/buildSrc/src/main/java/com/uber/okbuck/core/manager/DependencyManager.java +++ b/buildSrc/src/main/java/com/uber/okbuck/core/manager/DependencyManager.java @@ -1,5 +1,8 @@ package com.uber.okbuck.core.manager; +import static com.uber.okbuck.core.dependency.OResolvedDependency.AAR; +import static com.uber.okbuck.core.dependency.OResolvedDependency.JAR; + import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; @@ -28,14 +31,6 @@ import com.uber.okbuck.extension.OkBuckExtension; import com.uber.okbuck.template.common.BazelFunctionRule; import com.uber.okbuck.template.core.Rule; -import org.apache.commons.io.FileUtils; -import org.gradle.api.Project; -import org.gradle.api.artifacts.Configuration; -import org.gradle.api.artifacts.Dependency; -import org.gradle.api.artifacts.ExternalDependency; -import org.gradle.api.artifacts.ResolvedConfiguration; -import org.gradle.api.artifacts.ResolvedDependency; - import java.io.File; import java.io.IOException; import java.nio.file.Path; @@ -49,9 +44,12 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.IntStream; - -import static com.uber.okbuck.core.dependency.OResolvedDependency.AAR; -import static com.uber.okbuck.core.dependency.OResolvedDependency.JAR; +import org.gradle.api.Project; +import org.gradle.api.artifacts.Configuration; +import org.gradle.api.artifacts.Dependency; +import org.gradle.api.artifacts.ExternalDependency; +import org.gradle.api.artifacts.ResolvedConfiguration; +import org.gradle.api.artifacts.ResolvedDependency; public class DependencyManager { @@ -72,9 +70,10 @@ public class DependencyManager { private final DependencyExporter dependencyExporter; public DependencyManager( - Project rootProject, OkBuckExtension okBuckExtension, BuckFileManager buckFileManager, + Project rootProject, + OkBuckExtension okBuckExtension, + BuckFileManager buckFileManager, DependencyExporter dependencyExporter) { - this.project = rootProject; this.externalDependenciesExtension = okBuckExtension.getExternalDependenciesExtension(); this.jetifierExtension = okBuckExtension.getJetifierExtension(); @@ -390,17 +389,10 @@ private void processDependencies( OkBuckExtension okBuckExtension) { Path rootPath = project.getRootDir().toPath(); File cacheDir = rootPath.resolve(externalDependenciesExtension.getCache()).toFile(); - if (cacheDir.exists()) { - try { - FileUtils.deleteDirectory(cacheDir); - } catch (IOException e) { - throw new IllegalStateException("Could not delete dependency directory: " + cacheDir, e); - } - } - - if (!cacheDir.mkdirs()) { - throw new IllegalStateException("Couldn't create dependency directory: " + cacheDir); - } + FileUtil.deleteQuitelyAndCreate( + cacheDir, + externalDependenciesExtension.shouldCleanCacheDir(), + okBuckExtension.buildFileName); Map> groupToDependencyMap = dependencyMap diff --git a/buildSrc/src/main/java/com/uber/okbuck/core/util/FileUtil.java b/buildSrc/src/main/java/com/uber/okbuck/core/util/FileUtil.java index 2d6dbad1..6595981a 100644 --- a/buildSrc/src/main/java/com/uber/okbuck/core/util/FileUtil.java +++ b/buildSrc/src/main/java/com/uber/okbuck/core/util/FileUtil.java @@ -22,6 +22,8 @@ import java.util.HashMap; import java.util.stream.Stream; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.filefilter.NameFileFilter; +import org.apache.commons.io.filefilter.TrueFileFilter; import org.gradle.api.Project; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -156,4 +158,26 @@ public static void persistMapToJsonFile(HashMap map, File file) writer.flush(); writer.close(); } + + public static void deleteQuitelyAndCreate(File dir, boolean deleteDir, String fileName) { + + // Clean the dir, if it exists, based on deleteDir config + if (dir.exists()) { + if (deleteDir) { + try { + FileUtils.deleteDirectory(dir); + } catch (IOException e) { + throw new IllegalStateException("Could not delete dependency directory: " + dir, e); + } + } else { + FileUtils.listFiles(dir, new NameFileFilter(fileName), TrueFileFilter.INSTANCE) + .parallelStream() + .forEach(FileUtils::deleteQuietly); + } + } + + if (!dir.exists() && !dir.mkdirs()) { + throw new IllegalStateException("Couldn't create dependency directory: " + dir); + } + } } diff --git a/buildSrc/src/main/java/com/uber/okbuck/extension/ExternalDependenciesExtension.java b/buildSrc/src/main/java/com/uber/okbuck/extension/ExternalDependenciesExtension.java index 690bdd03..b13a8c8a 100644 --- a/buildSrc/src/main/java/com/uber/okbuck/extension/ExternalDependenciesExtension.java +++ b/buildSrc/src/main/java/com/uber/okbuck/extension/ExternalDependenciesExtension.java @@ -16,6 +16,12 @@ public class ExternalDependenciesExtension { /** Specifies the folder where all external dependency rules gets generated. */ @Input private String cache = ".okbuck/ext"; + /** + * Specifies to delete the cache dir, before generating dependency rules If set to false, only + * deletes the existing dependency rules files. + */ + @Input private boolean cleanCacheDir = true; + /** Specifies whether the external dependencies should be downloaded by buck or not. */ @Input private boolean downloadInBuck = true; @@ -46,7 +52,8 @@ public class ExternalDependenciesExtension { * dependencies like robolectric runtime deps. */ @Input - private List allowAllVersions = Collections.singletonList("org.robolectric:android-all-instrumented"); + private List allowAllVersions = + Collections.singletonList("org.robolectric:android-all-instrumented"); /** * Stores the dependency versions to be used for dynamic notations that have , or + in their @@ -172,4 +179,8 @@ public boolean bazelDepsEnabled() { public String getSha256Cache() { return sha256Cache; } + + public boolean shouldCleanCacheDir() { + return cleanCacheDir; + } }