Skip to content

Commit

Permalink
Add config cleanCacheDir (#977)
Browse files Browse the repository at this point in the history
* Add config cleanCacheDir which controls whether to delete the entire cache dir or only the existing dependency rules files, before generating the dependency rule files

* Switched to parallel delete

* Use TrueFileFilter for directory filter instead of null
  • Loading branch information
hvvikram authored Mar 17, 2023
1 parent b52e4a1 commit f1c64f0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.uber:okbuck:0.53.2'
classpath 'com.uber:okbuck:0.53.3'
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.uber:okbuck:0.53.2'
classpath 'com.uber:okbuck:0.53.3'
}
}
Expand Down
5 changes: 5 additions & 0 deletions Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ okbuck {
experimental {
transform = true
}
externalDependencies {
cache = "3rdparty/jvm"
cleanCacheDir = true
}
}
dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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();
Expand Down Expand Up @@ -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<Path, List<OExternalDependency>> groupToDependencyMap =
dependencyMap
Expand Down
24 changes: 24 additions & 0 deletions buildSrc/src/main/java/com/uber/okbuck/core/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -156,4 +158,26 @@ public static void persistMapToJsonFile(HashMap<String, String> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -46,7 +52,8 @@ public class ExternalDependenciesExtension {
* dependencies like robolectric runtime deps.
*/
@Input
private List<String> allowAllVersions = Collections.singletonList("org.robolectric:android-all-instrumented");
private List<String> allowAllVersions =
Collections.singletonList("org.robolectric:android-all-instrumented");

/**
* Stores the dependency versions to be used for dynamic notations that have , or + in their
Expand Down Expand Up @@ -172,4 +179,8 @@ public boolean bazelDepsEnabled() {
public String getSha256Cache() {
return sha256Cache;
}

public boolean shouldCleanCacheDir() {
return cleanCacheDir;
}
}

0 comments on commit f1c64f0

Please sign in to comment.