-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Detangle GitHub rendering from extraction
- Extracted `DependencyGraphRenderer` interface with single GitHub implementation - Abstract plugin allows renderer classname to be specified This should make it possible to implement support for other output formats.
- Loading branch information
Showing
9 changed files
with
202 additions
and
143 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
...in/src/main/kotlin/org/gradle/github/dependencygraph/AbstractDependencyExtractorPlugin.kt
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,127 @@ | ||
package org.gradle.github.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.internal.build.event.BuildEventListenerRegistryInternal | ||
import org.gradle.util.GradleVersion | ||
|
||
abstract class AbstractDependencyExtractorPlugin : Plugin<Gradle> { | ||
// Register extension functions on `Gradle` type | ||
private companion object : GradleExtensions() | ||
|
||
abstract fun getRendererClassName(): String | ||
|
||
internal lateinit var dependencyExtractorProvider: Provider<out DependencyExtractor> | ||
|
||
override fun apply(gradle: Gradle) { | ||
val gradleVersion = GradleVersion.current() | ||
// Create the adapter based upon the version of Gradle | ||
val applicatorStrategy = when { | ||
gradleVersion < GradleVersion.version("8.0") -> PluginApplicatorStrategy.LegacyPluginApplicatorStrategy | ||
else -> PluginApplicatorStrategy.DefaultPluginApplicatorStrategy | ||
} | ||
|
||
// Create the service | ||
dependencyExtractorProvider = applicatorStrategy.createExtractorService(gradle, getRendererClassName()) | ||
|
||
gradle.rootProject { project -> | ||
dependencyExtractorProvider | ||
.get() | ||
.rootProjectBuildDirectory = project.buildDir | ||
} | ||
|
||
// Register the service to listen for Build Events | ||
applicatorStrategy.registerExtractorListener(gradle, dependencyExtractorProvider) | ||
|
||
// Register the shutdown hook that should execute at the completion of the Gradle build. | ||
applicatorStrategy.registerExtractorServiceShutdown(gradle, dependencyExtractorProvider) | ||
} | ||
|
||
/** | ||
* Adapters for creating the [DependencyExtractor] and installing it into [Gradle] based upon the Gradle version. | ||
*/ | ||
private interface PluginApplicatorStrategy { | ||
|
||
fun createExtractorService( | ||
gradle: Gradle, | ||
rendererClassName: String | ||
): Provider<out DependencyExtractor> | ||
|
||
fun registerExtractorListener( | ||
gradle: Gradle, | ||
extractorServiceProvider: Provider<out DependencyExtractor> | ||
) | ||
|
||
fun registerExtractorServiceShutdown( | ||
gradle: Gradle, | ||
extractorServiceProvider: Provider<out DependencyExtractor> | ||
) | ||
|
||
object LegacyPluginApplicatorStrategy : PluginApplicatorStrategy { | ||
|
||
override fun createExtractorService( | ||
gradle: Gradle, | ||
rendererClassName: String | ||
): Provider<out DependencyExtractor> { | ||
val dependencyExtractor = LegacyDependencyExtractor(rendererClassName) | ||
return gradle.providerFactory.provider { dependencyExtractor } | ||
} | ||
|
||
override fun registerExtractorListener( | ||
gradle: Gradle, | ||
extractorServiceProvider: Provider<out DependencyExtractor> | ||
) { | ||
gradle.buildOperationListenerManager | ||
.addListener(extractorServiceProvider.get()) | ||
} | ||
|
||
override fun registerExtractorServiceShutdown( | ||
gradle: Gradle, | ||
extractorServiceProvider: Provider<out DependencyExtractor> | ||
) { | ||
gradle.buildFinished { | ||
extractorServiceProvider.get().close() | ||
gradle.buildOperationListenerManager | ||
.removeListener(extractorServiceProvider.get()) | ||
} | ||
} | ||
} | ||
|
||
object DefaultPluginApplicatorStrategy : PluginApplicatorStrategy { | ||
private const val SERVICE_NAME = "gitHubDependencyExtractorService" | ||
|
||
override fun createExtractorService( | ||
gradle: Gradle, | ||
rendererClassName: String | ||
): Provider<out DependencyExtractor> { | ||
return gradle.sharedServices.registerIfAbsent( | ||
SERVICE_NAME, | ||
DependencyExtractorBuildService::class.java | ||
) { | ||
it.parameters.rendererClassName.set(rendererClassName) | ||
} | ||
} | ||
|
||
override fun registerExtractorListener( | ||
gradle: Gradle, | ||
extractorServiceProvider: Provider<out DependencyExtractor> | ||
) { | ||
gradle.service<BuildEventListenerRegistryInternal>() | ||
.onOperationCompletion(extractorServiceProvider) | ||
} | ||
|
||
override fun registerExtractorServiceShutdown( | ||
gradle: Gradle, | ||
extractorServiceProvider: Provider<out DependencyExtractor> | ||
) { | ||
// No-op as DependencyExtractorService is Auto-Closable | ||
} | ||
} | ||
} | ||
} |
119 changes: 4 additions & 115 deletions
119
plugin/src/main/kotlin/org/gradle/github/dependencygraph/GitHubDependencyExtractorPlugin.kt
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 |
---|---|---|
@@ -1,123 +1,12 @@ | ||
package org.gradle.github.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.internal.build.event.BuildEventListenerRegistryInternal | ||
import org.gradle.util.GradleVersion | ||
import org.gradle.github.dependencygraph.internal.github.GitHubDependencyGraphRenderer | ||
|
||
/** | ||
* A plugin that collects all resolved dependencies in a Gradle build and exports it using the GitHub API format. | ||
*/ | ||
class GitHubDependencyExtractorPlugin : Plugin<Gradle> { | ||
// Register extension functions on `Gradle` type | ||
private companion object : GradleExtensions() | ||
|
||
internal lateinit var dependencyExtractorProvider: Provider<out DependencyExtractor> | ||
|
||
override fun apply(gradle: Gradle) { | ||
val gradleVersion = GradleVersion.current() | ||
// Create the adapter based upon the version of Gradle | ||
val applicatorStrategy = when { | ||
gradleVersion < GradleVersion.version("8.0") -> PluginApplicatorStrategy.LegacyPluginApplicatorStrategy | ||
else -> PluginApplicatorStrategy.DefaultPluginApplicatorStrategy | ||
} | ||
|
||
// Create the service | ||
dependencyExtractorProvider = applicatorStrategy.createExtractorService(gradle) | ||
|
||
gradle.rootProject { project -> | ||
dependencyExtractorProvider | ||
.get() | ||
.rootProjectBuildDirectory = project.buildDir | ||
} | ||
|
||
// Register the service to listen for Build Events | ||
applicatorStrategy.registerExtractorListener(gradle, dependencyExtractorProvider) | ||
|
||
// Register the shutdown hook that should execute at the completion of the Gradle build. | ||
applicatorStrategy.registerExtractorServiceShutdown(gradle, dependencyExtractorProvider) | ||
} | ||
|
||
/** | ||
* Adapters for creating the [DependencyExtractor] and installing it into [Gradle] based upon the Gradle version. | ||
*/ | ||
private interface PluginApplicatorStrategy { | ||
|
||
fun createExtractorService( | ||
gradle: Gradle | ||
): Provider<out DependencyExtractor> | ||
|
||
fun registerExtractorListener( | ||
gradle: Gradle, | ||
extractorServiceProvider: Provider<out DependencyExtractor> | ||
) | ||
|
||
fun registerExtractorServiceShutdown( | ||
gradle: Gradle, | ||
extractorServiceProvider: Provider<out DependencyExtractor> | ||
) | ||
|
||
object LegacyPluginApplicatorStrategy : PluginApplicatorStrategy { | ||
|
||
override fun createExtractorService( | ||
gradle: Gradle | ||
): Provider<out DependencyExtractor> { | ||
val dependencyExtractor = LegacyDependencyExtractor() | ||
return gradle.providerFactory.provider { dependencyExtractor } | ||
} | ||
|
||
override fun registerExtractorListener( | ||
gradle: Gradle, | ||
extractorServiceProvider: Provider<out DependencyExtractor> | ||
) { | ||
gradle.buildOperationListenerManager | ||
.addListener(extractorServiceProvider.get()) | ||
} | ||
|
||
override fun registerExtractorServiceShutdown( | ||
gradle: Gradle, | ||
extractorServiceProvider: Provider<out DependencyExtractor> | ||
) { | ||
gradle.buildFinished { | ||
extractorServiceProvider.get().close() | ||
gradle.buildOperationListenerManager | ||
.removeListener(extractorServiceProvider.get()) | ||
} | ||
} | ||
} | ||
|
||
object DefaultPluginApplicatorStrategy : PluginApplicatorStrategy { | ||
private const val SERVICE_NAME = "gitHubDependencyExtractorService" | ||
|
||
override fun createExtractorService( | ||
gradle: Gradle | ||
): Provider<out DependencyExtractor> { | ||
return gradle.sharedServices.registerIfAbsent( | ||
SERVICE_NAME, | ||
DependencyExtractorBuildService::class.java | ||
) {} | ||
} | ||
|
||
override fun registerExtractorListener( | ||
gradle: Gradle, | ||
extractorServiceProvider: Provider<out DependencyExtractor> | ||
) { | ||
gradle.service<BuildEventListenerRegistryInternal>() | ||
.onOperationCompletion(extractorServiceProvider) | ||
} | ||
|
||
override fun registerExtractorServiceShutdown( | ||
gradle: Gradle, | ||
extractorServiceProvider: Provider<out DependencyExtractor> | ||
) { | ||
// No-op as DependencyExtractorService is Auto-Closable | ||
} | ||
} | ||
class GitHubDependencyExtractorPlugin : AbstractDependencyExtractorPlugin() { | ||
override fun getRendererClassName(): String { | ||
return GitHubDependencyGraphRenderer::class.java.name | ||
} | ||
} |
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
13 changes: 12 additions & 1 deletion
13
...main/kotlin/org/gradle/github/dependencygraph/internal/DependencyExtractorBuildService.kt
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 |
---|---|---|
@@ -1,8 +1,19 @@ | ||
package org.gradle.github.dependencygraph.internal | ||
|
||
import org.gradle.api.provider.Property | ||
import org.gradle.api.services.BuildService | ||
import org.gradle.api.services.BuildServiceParameters | ||
|
||
abstract class DependencyExtractorBuildService : | ||
DependencyExtractor(), | ||
BuildService<BuildServiceParameters.None> | ||
BuildService<DependencyExtractorBuildService.Params> | ||
{ | ||
// Some parameters for the web server | ||
internal interface Params : BuildServiceParameters { | ||
val rendererClassName: Property<String> | ||
} | ||
|
||
override fun getRendererClassName(): String { | ||
return parameters.rendererClassName.get() | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
plugin/src/main/kotlin/org/gradle/github/dependencygraph/internal/DependencyGraphRenderer.kt
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,14 @@ | ||
package org.gradle.github.dependencygraph.internal | ||
|
||
import org.gradle.github.dependencygraph.internal.model.BuildLayout | ||
import org.gradle.github.dependencygraph.internal.model.ResolvedConfiguration | ||
import org.gradle.github.dependencygraph.internal.util.PluginParameters | ||
import java.io.File | ||
|
||
interface DependencyGraphRenderer { | ||
fun outputDependencyGraph(pluginParameters: PluginParameters, | ||
buildLayout: BuildLayout, | ||
resolvedConfigurations: MutableList<ResolvedConfiguration>, | ||
outputDirectory: File | ||
) | ||
} |
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
Oops, something went wrong.