-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for exporting raw dependencies (#973)
* 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
Showing
9 changed files
with
236 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/DependencyExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
72 changes: 72 additions & 0 deletions
72
buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/DependencyExporterModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/ExporterException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/JsonDependencyExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
buildSrc/src/main/java/com/uber/okbuck/extension/ExportDependenciesExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.