Skip to content

Commit

Permalink
Refactor: Make it possible to support non-GitHub output formats
Browse files Browse the repository at this point in the history
Detangles the GitHub-specific output rendering from the rest of the dependency-extraction infrastructure,
making it possible to render the dependency-graph output in different formats.
At this stage, this mechanism is only designed to support renderers that are developed within this project.
In particular, the `DependencyGraphRenderer` interface is subject to change.
  • Loading branch information
bigdaz authored Aug 30, 2023
2 parents 964e9ab + 2e26236 commit cce15ef
Show file tree
Hide file tree
Showing 41 changed files with 397 additions and 264 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
uses: gradle/gradle-build-action@v2

- name: Self Test :plugin
run: ./plugin-self-test GitHubDependencyGraphPlugin_generateDependencyGraph
run: ./plugin-self-test ForceDependencyResolutionPlugin_resolveAllDependencies
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_JOB_ID: ${{ github.run_id }}
Expand All @@ -79,6 +79,6 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: plugin-json
path: build/reports/github-dependency-graph-snapshots/plugin-self-test.json
path: build/reports/dependency-graph-snapshots/plugin-self-test.json
if-no-files-found: error

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ apply plugin: GitHubDependencyGraphPlugin

This causes 2 separate plugins to be applied, that can be used independently:
- `GitHubDependencyExtractorPlugin` collects all dependencies that are resolved during a build execution and writes these to a file. The output file can be found at `<root>/build/reports/github-depenency-graph-snapshots/<job-correlator>.json`.
- `ForceDependencyResolutionPlugin` creates a `GitHubDependencyGraphPlugin_generateDependencyGraph` task that will attempt to resolve all dependencies for a Gradle build, by simply invoking `dependencies` on all projects.
- `ForceDependencyResolutionPlugin` creates a `ForceDependencyResolutionPlugin_resolveAllDependencies` task that will attempt to resolve all dependencies for a Gradle build, by simply invoking `dependencies` on all projects.

### Required environment variables

Expand Down
4 changes: 2 additions & 2 deletions plugin-self-test
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ else
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/gradle/github-dependency-graph-gradle-plugin/dependency-graph/snapshots \
-d @build/reports/github-dependency-graph-snapshots/plugin-self-test.json
https://api.github.com/repos/gradle/dependency-graph-gradle-plugin/dependency-graph/snapshots \
-d @build/reports/dependency-graph-snapshots/plugin-self-test.json
fi
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ abstract class BaseExtractorTest extends Specification {
}

protected BuildResult run() {
return run("GitHubDependencyGraphPlugin_generateDependencyGraph")
return run("ForceDependencyResolutionPlugin_resolveAllDependencies")
}

protected BuildResult run(String... names) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ class MultiProjectDependencyExtractorTest extends BaseExtractorTest {
])

where:
task | description
"GitHubDependencyGraphPlugin_generateDependencyGraph" | "All dependencies resolved"
":c:dependencies" | "One project resolved"
task | description
"ForceDependencyResolutionPlugin_resolveAllDependencies" | "All dependencies resolved"
":c:dependencies" | "One project resolved"
}

def "extracts direct dependency for transitive dependency updated by constraint"() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package org.gradle.github.dependencygraph
package org.gradle.dependencygraph

import org.gradle.api.Plugin
import org.gradle.api.invocation.Gradle
import org.gradle.api.provider.Provider
import org.gradle.github.dependencygraph.internal.DependencyExtractor
import org.gradle.github.dependencygraph.internal.DependencyExtractorBuildService
import org.gradle.github.dependencygraph.internal.LegacyDependencyExtractor
import org.gradle.github.dependencygraph.internal.util.GradleExtensions
import org.gradle.github.dependencygraph.internal.util.service
import org.gradle.dependencygraph.extractor.DependencyExtractor
import org.gradle.dependencygraph.extractor.DependencyExtractorBuildService
import org.gradle.dependencygraph.extractor.LegacyDependencyExtractor
import org.gradle.dependencygraph.util.GradleExtensions
import org.gradle.dependencygraph.util.service
import org.gradle.internal.build.event.BuildEventListenerRegistryInternal
import org.gradle.util.GradleVersion

/**
* A plugin that collects all resolved dependencies in a Gradle build and exports it using the GitHub API format.
*/
class GitHubDependencyExtractorPlugin : Plugin<Gradle> {
abstract class AbstractDependencyExtractorPlugin : Plugin<Gradle> {
// Register extension functions on `Gradle` type
private companion object : GradleExtensions()

/**
* The name of an accessible class that implements `org.gradle.dependencygraph.DependencyGraphRenderer`.
*/
abstract fun getRendererClassName(): String

internal lateinit var dependencyExtractorProvider: Provider<out DependencyExtractor>

override fun apply(gradle: Gradle) {
Expand All @@ -29,7 +31,7 @@ class GitHubDependencyExtractorPlugin : Plugin<Gradle> {
}

// Create the service
dependencyExtractorProvider = applicatorStrategy.createExtractorService(gradle)
dependencyExtractorProvider = applicatorStrategy.createExtractorService(gradle, getRendererClassName())

gradle.rootProject { project ->
dependencyExtractorProvider
Expand All @@ -50,7 +52,8 @@ class GitHubDependencyExtractorPlugin : Plugin<Gradle> {
private interface PluginApplicatorStrategy {

fun createExtractorService(
gradle: Gradle
gradle: Gradle,
rendererClassName: String
): Provider<out DependencyExtractor>

fun registerExtractorListener(
Expand All @@ -66,9 +69,10 @@ class GitHubDependencyExtractorPlugin : Plugin<Gradle> {
object LegacyPluginApplicatorStrategy : PluginApplicatorStrategy {

override fun createExtractorService(
gradle: Gradle
gradle: Gradle,
rendererClassName: String
): Provider<out DependencyExtractor> {
val dependencyExtractor = LegacyDependencyExtractor()
val dependencyExtractor = LegacyDependencyExtractor(rendererClassName)
return gradle.providerFactory.provider { dependencyExtractor }
}

Expand All @@ -93,15 +97,18 @@ class GitHubDependencyExtractorPlugin : Plugin<Gradle> {
}

object DefaultPluginApplicatorStrategy : PluginApplicatorStrategy {
private const val SERVICE_NAME = "gitHubDependencyExtractorService"
private const val SERVICE_NAME = "dependencyExtractorService"

override fun createExtractorService(
gradle: Gradle
gradle: Gradle,
rendererClassName: String
): Provider<out DependencyExtractor> {
return gradle.sharedServices.registerIfAbsent(
SERVICE_NAME,
DependencyExtractorBuildService::class.java
) {}
) {
it.parameters.rendererClassName.set(rendererClassName)
}
}

override fun registerExtractorListener(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.gradle.dependencygraph

import org.gradle.dependencygraph.model.BuildLayout
import org.gradle.dependencygraph.model.ResolvedConfiguration
import org.gradle.dependencygraph.util.PluginParameters
import java.io.File

interface DependencyGraphRenderer {
fun outputDependencyGraph(
pluginParameters: PluginParameters,
buildLayout: BuildLayout,
resolvedConfigurations: List<ResolvedConfiguration>,
outputDirectory: File
)
}
Loading

0 comments on commit cce15ef

Please sign in to comment.