Skip to content

Commit

Permalink
feat: Add support for exporting raw dependencies (#973)
Browse files Browse the repository at this point in the history
* feat: Add support for exporting raw dependencies

plugin can accept two project parameters
* exportDependencies: to enable exporting of raw dependencies
* exportDependenciesFile: Name with optional relative path from the root of the project where the dependencies will be exported ( default: .okbuck/raw-deps )

Supports exporting raw dependencies in JSON format.

```
./gradlew okbuck -PexportDependencies=true -PexportDependenciesFile=.okbuck/raw-deps.json
```

* Moved configurations to OKBuckExtension and handle excludeRule to use group:module when both are not blank

* * Addressed review comments
* Ran google formatter
* Autoclose BufferedWriter to flush the contents and close
  • Loading branch information
hvvikram authored Feb 17, 2023
1 parent 7faa0c9 commit c0bb2b6
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 52 deletions.
2 changes: 1 addition & 1 deletion buildSrc/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP=com.uber
VERSION_NAME=0.53.0-SNAPSHOT
VERSION_NAME=0.53.1-SNAPSHOT
POM_DESCRIPTION=A Gradle plugin that lets developers utilize the Buck build system on a Gradle project
POM_URL=https://github.com/uber/okbuck/
POM_SCM_URL=https://github.com/uber/okbuck/
Expand Down
36 changes: 18 additions & 18 deletions buildSrc/src/main/java/com/uber/okbuck/OkBuckGradlePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.uber.okbuck.core.annotation.AnnotationProcessorCache;
import com.uber.okbuck.core.dependency.DependencyCache;
import com.uber.okbuck.core.dependency.DependencyFactory;
import com.uber.okbuck.core.dependency.exporter.DependencyExporter;
import com.uber.okbuck.core.dependency.exporter.JsonDependencyExporter;
import com.uber.okbuck.core.manager.BuckFileManager;
import com.uber.okbuck.core.manager.BuckManager;
import com.uber.okbuck.core.manager.D8Manager;
Expand Down Expand Up @@ -32,6 +34,11 @@
import com.uber.okbuck.template.common.ExportFile;
import com.uber.okbuck.template.core.Rule;
import com.uber.okbuck.wrapper.BuckWrapperTask;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -42,10 +49,6 @@
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;

// Dependency Tree
//
Expand Down Expand Up @@ -156,7 +159,9 @@ public void apply(Project rootProject) {
new AnnotationProcessorCache(rootBuckProject, buckFileManager, processorBuildFile);

// Create Dependency manager
dependencyManager = new DependencyManager(rootBuckProject, okbuckExt, buckFileManager);
dependencyManager =
new DependencyManager(
rootBuckProject, okbuckExt, buckFileManager, createDependencyExporter(okbuckExt));

// Create Lint Manager
String lintBuildFile = LINT_BUILD_FOLDER + "/" + okbuckExt.buildFileName;
Expand Down Expand Up @@ -238,10 +243,7 @@ public void apply(Project rootProject) {
wrapper.ignoredDirs);

Map<String, Configuration> extraConfigurations =
okbuckExt
.extraDepCachesMap
.keySet()
.stream()
okbuckExt.extraDepCachesMap.keySet().stream()
.collect(
Collectors.toMap(
Function.identity(),
Expand Down Expand Up @@ -315,9 +317,7 @@ public void apply(Project rootProject) {
rootOkBuckTask.dependsOn(okBuckClean);

// Create okbuck task on each project to generate their buck file
okbuckExt
.buckProjects
.stream()
okbuckExt.buckProjects.stream()
.filter(p -> p.getBuildFile().exists())
.forEach(
bp -> {
Expand All @@ -338,9 +338,7 @@ public void apply(Project rootProject) {

private void writeExportedFileRules(Project rootBuckProject, OkBuckExtension okBuckExtension) {
Set<String> currentProjectPaths =
okBuckExtension
.buckProjects
.stream()
okBuckExtension.buckProjects.stream()
.filter(project -> ProjectUtil.getType(project) != ProjectType.UNKNOWN)
.map(
project ->
Expand Down Expand Up @@ -369,14 +367,16 @@ private void writeExportedFileRules(Project rootBuckProject, OkBuckExtension okB
.toFile();
try (OutputStream os =
new FileOutputStream(buckFile, currentProjectPaths.contains(entry.getKey()))) {
entry
.getValue()
.stream()
entry.getValue().stream()
.sorted((rule1, rule2) -> rule1.name().compareToIgnoreCase(rule2.name()))
.forEach(rule -> rule.render(os));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}

private static DependencyExporter createDependencyExporter(OkBuckExtension okbuckExt) {
return new JsonDependencyExporter(okbuckExt.getExportDependenciesExtension());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.uber.okbuck.core.dependency.exporter;

import org.gradle.api.artifacts.ExternalDependency;

import java.util.Set;

public interface DependencyExporter {

void export(Set<ExternalDependency> dependencies);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.uber.okbuck.core.dependency.exporter;

import org.codehaus.plexus.util.StringUtils;
import org.gradle.api.artifacts.ExternalDependency;

import javax.annotation.Nullable;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class DependencyExporterModel {

@Nullable private final String name;
@Nullable private final String version;
@Nullable private final String group;
private final boolean force;
@Nullable private Set<String> excludeRules = new TreeSet<>();

public DependencyExporterModel(ExternalDependency externalDependency) {
name = externalDependency.getName();
version = externalDependency.getVersion();
group = externalDependency.getGroup();
force = externalDependency.isForce();

if (externalDependency.getExcludeRules() != null) {
excludeRules =
externalDependency.getExcludeRules()
.stream()
.map(
e -> {
if (StringUtils.isNotBlank(e.getGroup())
&& StringUtils.isNotBlank(e.getModule())) {
return String.format("%s:%s", e.getGroup(), e.getModule());
}

if (StringUtils.isNotBlank(e.getGroup())) {
return e.getGroup();
}

if (StringUtils.isNotBlank(e.getModule())) {
return e.getModule();
}
return null;
})
.collect(Collectors.toSet());
}
}

@Nullable
public String getName() {
return name;
}

@Nullable
public String getVersion() {
return version;
}

@Nullable
public String getGroup() {
return group;
}

public boolean isForce() {
return force;
}

@Nullable
public Set<String> getExcludeRules() {
return excludeRules;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.uber.okbuck.core.dependency.exporter;

public class ExporterException extends RuntimeException {
public ExporterException(Exception e) {
super(e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.uber.okbuck.core.dependency.exporter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.uber.okbuck.extension.ExportDependenciesExtension;
import org.gradle.api.artifacts.ExternalDependency;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Set;
import java.util.stream.Collectors;

public class JsonDependencyExporter implements DependencyExporter {

private static final Logger LOG = LoggerFactory.getLogger(JsonDependencyExporter.class);

private final ExportDependenciesExtension exportDependenciesExtension;

public JsonDependencyExporter(ExportDependenciesExtension exportDependenciesExtension) {
this.exportDependenciesExtension = exportDependenciesExtension;
}

@Override
public void export(Set<ExternalDependency> dependencies) {
if (!exportDependenciesExtension.isEnabled()) {
LOG.info("Exporting dependencies is disabled");
return;
}

Set<DependencyExporterModel> dependencyModels =
dependencies.stream()
.map(dependency -> new DependencyExporterModel(dependency))
.collect(Collectors.toSet());

Gson gson = new GsonBuilder().setPrettyPrinting().create();
try (BufferedWriter writer =
Files.newBufferedWriter(
Paths.get(exportDependenciesExtension.getFile()), StandardCharsets.UTF_8)) {
LOG.info("Exporting dependencies to JSON at " + exportDependenciesExtension.getFile());
gson.toJson(dependencyModels, writer);
} catch (IOException e) {
throw new ExporterException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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 All @@ -21,6 +18,7 @@
import com.uber.okbuck.core.dependency.LocalOExternalDependency;
import com.uber.okbuck.core.dependency.OExternalDependency;
import com.uber.okbuck.core.dependency.VersionlessDependency;
import com.uber.okbuck.core.dependency.exporter.DependencyExporter;
import com.uber.okbuck.core.model.base.Scope;
import com.uber.okbuck.core.util.FileUtil;
import com.uber.okbuck.core.util.ProjectCache;
Expand All @@ -30,6 +28,14 @@
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 @@ -43,13 +49,9 @@
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
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 static com.uber.okbuck.core.dependency.OResolvedDependency.AAR;
import static com.uber.okbuck.core.dependency.OResolvedDependency.JAR;

public class DependencyManager {

Expand All @@ -67,13 +69,17 @@ public class DependencyManager {

private final HashMap<String, String> sha256Cache;

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();
this.buckFileManager = buckFileManager;
this.dependencyExporter = dependencyExporter;
this.sha256Cache = initSha256Cache(rootProject, externalDependenciesExtension);
}

Expand Down Expand Up @@ -145,6 +151,8 @@ private static void resolveDepsWithProject(
}

public void finalizeDependencies(OkBuckExtension okBuckExtension) {
dependencyExporter.export(rawDependencies);

Map<VersionlessDependency, Collection<OExternalDependency>> filteredDependencyMap =
filterDependencies();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.uber.okbuck.extension;

import org.gradle.api.Project;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;

import java.nio.file.Paths;

public class ExportDependenciesExtension {
@Input private boolean enabled = false;
@Input @Optional private String file = ".okbuck/raw-deps";

private final String projectRoot;

public ExportDependenciesExtension(Project project) {
projectRoot = project.getProjectDir().getAbsolutePath();
}

public boolean isEnabled() {
return enabled;
}

public String getFile() {
return Paths.get(projectRoot, file).toString();
}
}
Loading

0 comments on commit c0bb2b6

Please sign in to comment.