diff --git a/buildSrc/gradle.properties b/buildSrc/gradle.properties index eede6ac53..25c74e923 100644 --- a/buildSrc/gradle.properties +++ b/buildSrc/gradle.properties @@ -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/ diff --git a/buildSrc/src/main/java/com/uber/okbuck/OkBuckGradlePlugin.java b/buildSrc/src/main/java/com/uber/okbuck/OkBuckGradlePlugin.java index 2e8afa870..11f586a96 100644 --- a/buildSrc/src/main/java/com/uber/okbuck/OkBuckGradlePlugin.java +++ b/buildSrc/src/main/java/com/uber/okbuck/OkBuckGradlePlugin.java @@ -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; @@ -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; @@ -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 // @@ -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; @@ -238,10 +243,7 @@ public void apply(Project rootProject) { wrapper.ignoredDirs); Map extraConfigurations = - okbuckExt - .extraDepCachesMap - .keySet() - .stream() + okbuckExt.extraDepCachesMap.keySet().stream() .collect( Collectors.toMap( Function.identity(), @@ -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 -> { @@ -338,9 +338,7 @@ public void apply(Project rootProject) { private void writeExportedFileRules(Project rootBuckProject, OkBuckExtension okBuckExtension) { Set currentProjectPaths = - okBuckExtension - .buckProjects - .stream() + okBuckExtension.buckProjects.stream() .filter(project -> ProjectUtil.getType(project) != ProjectType.UNKNOWN) .map( project -> @@ -369,9 +367,7 @@ 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) { @@ -379,4 +375,8 @@ private void writeExportedFileRules(Project rootBuckProject, OkBuckExtension okB } } } + + private static DependencyExporter createDependencyExporter(OkBuckExtension okbuckExt) { + return new JsonDependencyExporter(okbuckExt.getExportDependenciesExtension()); + } } diff --git a/buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/DependencyExporter.java b/buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/DependencyExporter.java new file mode 100644 index 000000000..a7335d7d6 --- /dev/null +++ b/buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/DependencyExporter.java @@ -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 dependencies); +} diff --git a/buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/DependencyExporterModel.java b/buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/DependencyExporterModel.java new file mode 100644 index 000000000..b4a0fdfec --- /dev/null +++ b/buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/DependencyExporterModel.java @@ -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 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 getExcludeRules() { + return excludeRules; + } +} diff --git a/buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/ExporterException.java b/buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/ExporterException.java new file mode 100644 index 000000000..b2342c224 --- /dev/null +++ b/buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/ExporterException.java @@ -0,0 +1,7 @@ +package com.uber.okbuck.core.dependency.exporter; + +public class ExporterException extends RuntimeException { + public ExporterException(Exception e) { + super(e); + } +} diff --git a/buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/JsonDependencyExporter.java b/buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/JsonDependencyExporter.java new file mode 100644 index 000000000..b9867cdf0 --- /dev/null +++ b/buildSrc/src/main/java/com/uber/okbuck/core/dependency/exporter/JsonDependencyExporter.java @@ -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 dependencies) { + if (!exportDependenciesExtension.isEnabled()) { + LOG.info("Exporting dependencies is disabled"); + return; + } + + Set 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); + } + } +} 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 b8892a64d..d6fb7d859 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,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; @@ -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; @@ -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; @@ -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 { @@ -67,13 +69,17 @@ public class DependencyManager { private final HashMap 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); } @@ -145,6 +151,8 @@ private static void resolveDepsWithProject( } public void finalizeDependencies(OkBuckExtension okBuckExtension) { + dependencyExporter.export(rawDependencies); + Map> filteredDependencyMap = filterDependencies(); diff --git a/buildSrc/src/main/java/com/uber/okbuck/extension/ExportDependenciesExtension.java b/buildSrc/src/main/java/com/uber/okbuck/extension/ExportDependenciesExtension.java new file mode 100644 index 000000000..2f0a38d7b --- /dev/null +++ b/buildSrc/src/main/java/com/uber/okbuck/extension/ExportDependenciesExtension.java @@ -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(); + } +} diff --git a/buildSrc/src/main/java/com/uber/okbuck/extension/OkBuckExtension.java b/buildSrc/src/main/java/com/uber/okbuck/extension/OkBuckExtension.java index 5b863f14b..b37a44660 100644 --- a/buildSrc/src/main/java/com/uber/okbuck/extension/OkBuckExtension.java +++ b/buildSrc/src/main/java/com/uber/okbuck/extension/OkBuckExtension.java @@ -1,5 +1,12 @@ package com.uber.okbuck.extension; +import org.gradle.api.Action; +import org.gradle.api.Project; +import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.Internal; +import org.gradle.api.tasks.Optional; + +import javax.annotation.Nullable; import java.io.File; import java.util.Collection; import java.util.HashMap; @@ -7,13 +14,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import javax.annotation.Nullable; -import org.gradle.api.Action; -import org.gradle.api.Project; -import org.gradle.api.tasks.Input; -import org.gradle.api.tasks.Internal; -import org.gradle.api.tasks.Nested; -import org.gradle.api.tasks.Optional; @SuppressWarnings("unused") public class OkBuckExtension { @@ -95,8 +95,7 @@ public class OkBuckExtension { @Input public boolean legacyAnnotationProcessorSupport = true; /** The prebuilt buck binary to use */ - @Input - @Optional + @Input @Optional public String buckBinary = DEFAULT_BUCK_BINARY_REPO + ":" + DEFAULT_BUCK_BINARY_SHA + "@pex"; /** The prebuilt buck binary to use with java 11 */ @@ -104,21 +103,24 @@ public class OkBuckExtension { public String buckBinaryJava11 = DEFAULT_BUCK_BINARY_REPO + ":" + DEFAULT_BUCK_BINARY_SHA + ":java11@pex"; - @Internal private WrapperExtension wrapperExtension = new WrapperExtension(); - @Internal private KotlinExtension kotlinExtension; - @Internal private ScalaExtension scalaExtension = new ScalaExtension(); - @Internal private IntellijExtension intellijExtension = new IntellijExtension(); - @Internal - private ExperimentalExtension experimentalExtension = new ExperimentalExtension(); - @Internal private TestExtension testExtension = new TestExtension(); - @Internal private TransformExtension transformExtension = new TransformExtension(); - @Internal private LintExtension lintExtension; - @Internal private JetifierExtension jetifierExtension; + @Internal private final WrapperExtension wrapperExtension = new WrapperExtension(); + @Internal private final KotlinExtension kotlinExtension; + @Internal private final ScalaExtension scalaExtension = new ScalaExtension(); + @Internal private final IntellijExtension intellijExtension = new IntellijExtension(); + @Internal private final ExperimentalExtension experimentalExtension = new ExperimentalExtension(); + @Internal private final TestExtension testExtension = new TestExtension(); + @Internal private final TransformExtension transformExtension = new TransformExtension(); + @Internal private final LintExtension lintExtension; + @Internal private final JetifierExtension jetifierExtension; + @Internal - private ExternalDependenciesExtension externalDependenciesExtension = + private final ExternalDependenciesExtension externalDependenciesExtension = new ExternalDependenciesExtension(); - @Internal private VisibilityExtension visibilityExtension = new VisibilityExtension(); - @Internal private RuleOverridesExtension ruleOverridesExtension; + + @Internal private final VisibilityExtension visibilityExtension = new VisibilityExtension(); + @Internal private final RuleOverridesExtension ruleOverridesExtension; + + @Internal private final ExportDependenciesExtension exportDependenciesExtension; public OkBuckExtension(Project project) { buckProjects = project.getSubprojects(); @@ -126,6 +128,7 @@ public OkBuckExtension(Project project) { lintExtension = new LintExtension(project); jetifierExtension = new JetifierExtension(project); ruleOverridesExtension = new RuleOverridesExtension(project); + exportDependenciesExtension = new ExportDependenciesExtension(project); } public void wrapper(Action container) { @@ -320,4 +323,12 @@ public boolean isFailOnChangingDependencies() { public boolean isLegacyAnnotationProcessorSupport() { return legacyAnnotationProcessorSupport; } + + public void exportDependencies(Action container) { + container.execute(exportDependenciesExtension); + } + + public ExportDependenciesExtension getExportDependenciesExtension() { + return exportDependenciesExtension; + } }