Skip to content

Commit

Permalink
Restructure package layout
Browse files Browse the repository at this point in the history
(Mostly) detangle GitHub-specific code from generic dependency graph implementation.
  • Loading branch information
bigdaz committed Aug 30, 2023
1 parent d40470b commit 314a0f7
Show file tree
Hide file tree
Showing 30 changed files with 147 additions and 135 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
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

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>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.gradle.github.dependencygraph.internal
package org.gradle.dependencygraph

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 org.gradle.dependencygraph.model.BuildLayout
import org.gradle.dependencygraph.model.ResolvedConfiguration
import org.gradle.dependencygraph.util.PluginParameters
import java.io.File

interface DependencyGraphRenderer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
package org.gradle.github.dependencygraph.internal
package org.gradle.dependencygraph.extractor

import org.gradle.api.GradleException
import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.artifacts.result.ResolvedDependencyResult
import org.gradle.api.internal.artifacts.DefaultProjectComponentIdentifier
import org.gradle.api.internal.artifacts.configurations.ResolveConfigurationDependenciesBuildOperationType
import org.gradle.api.logging.Logging
import org.gradle.dependencygraph.DependencyGraphRenderer
import org.gradle.dependencygraph.model.*
import org.gradle.dependencygraph.util.*
import org.gradle.github.GitHubDependencyGraphPlugin
import org.gradle.github.dependencygraph.internal.model.*
import org.gradle.github.dependencygraph.internal.util.*
import org.gradle.initialization.EvaluateSettingsBuildOperationType
import org.gradle.initialization.LoadProjectsBuildOperationType
import org.gradle.internal.exceptions.DefaultMultiCauseException
import org.gradle.internal.operations.*
import java.io.File
import java.net.URI
import java.nio.file.Paths
import java.util.*
import org.gradle.api.internal.artifacts.configurations.ResolveConfigurationDependenciesBuildOperationType as ResolveConfigurationDependenciesBOT
import org.gradle.initialization.LoadProjectsBuildOperationType as LoadProjectsBOT

const val PARAM_INCLUDE_PROJECTS = "DEPENDENCY_GRAPH_INCLUDE_PROJECTS"
const val PARAM_INCLUDE_CONFIGURATIONS = "DEPENDENCY_GRAPH_INCLUDE_CONFIGURATIONS"

const val PARAM_REPORT_DIR = "DEPENDENCY_GRAPH_REPORT_DIR"

// TODO: Move this to the GitHub-specific code
/**
* Environment variable should be set to the workspace directory that the Git repository is checked out in.
* This is used to determine relative path to build files referenced in the dependency graph.
*/
const val PARAM_GITHUB_WORKSPACE = "GITHUB_WORKSPACE"

abstract class DependencyExtractor :
BuildOperationListener,
Expand Down Expand Up @@ -63,23 +75,23 @@ abstract class DependencyExtractor :

override fun finished(buildOperation: BuildOperationDescriptor, finishEvent: OperationFinishEvent) {
handleBuildOperationType<
ResolveConfigurationDependenciesBOT.Details,
ResolveConfigurationDependenciesBOT.Result
ResolveConfigurationDependenciesBuildOperationType.Details,
ResolveConfigurationDependenciesBuildOperationType.Result
>(buildOperation, finishEvent) { details, result -> extractConfigurationDependencies(details, result) }

handleBuildOperationType<
LoadProjectsBOT.Details,
LoadProjectsBOT.Result>(buildOperation, finishEvent) { _, result -> extractProjects(result) }
LoadProjectsBuildOperationType.Details,
LoadProjectsBuildOperationType.Result>(buildOperation, finishEvent) { _, result -> extractProjects(result) }

handleBuildOperationType<
EvaluateSettingsBuildOperationType.Details,
EvaluateSettingsBuildOperationType.Result>(buildOperation, finishEvent) { details, _ -> extractSettings(details) }
EvaluateSettingsBuildOperationType.Details,
EvaluateSettingsBuildOperationType.Result>(buildOperation, finishEvent) { details, _ -> extractSettings(details) }
}

private inline fun <reified D, reified R> handleBuildOperationType(
buildOperation: BuildOperationDescriptor,
finishEvent: OperationFinishEvent,
handler: (details: D, result: R) -> Unit
buildOperation: BuildOperationDescriptor,
finishEvent: OperationFinishEvent,
handler: (details: D, result: R) -> Unit
) {
try {
handleBuildOperationTypeRaw<D, R>(buildOperation, finishEvent, handler)
Expand All @@ -89,6 +101,25 @@ abstract class DependencyExtractor :
}
}

private inline fun <reified D, reified R> handleBuildOperationTypeRaw(
buildOperation: BuildOperationDescriptor,
finishEvent: OperationFinishEvent,
handler: (details: D, result: R) -> Unit
) {
val details: D? = buildOperation.details.let {
if (it is D) it else null
}
val result: R? = finishEvent.result.let {
if (it is R) it else null
}
if (details == null && result == null) {
return
} else if (details == null || result == null) {
throw IllegalStateException("buildOperation.details & finishedEvent.result were unexpected types")
}
handler(details, result)
}

open fun extractSettings(
details: EvaluateSettingsBuildOperationType.Details
) {
Expand All @@ -99,9 +130,9 @@ abstract class DependencyExtractor :
}

private fun extractProjects(
result: LoadProjectsBOT.Result
result: LoadProjectsBuildOperationType.Result
) {
tailrec fun recursivelyExtractProjects(projects: Set<LoadProjectsBOT.Result.Project>) {
tailrec fun recursivelyExtractProjects(projects: Set<LoadProjectsBuildOperationType.Result.Project>) {
if (projects.isEmpty()) return
projects.forEach { project ->
buildLayout.addProject(project.identityPath, project.buildFile)
Expand Down Expand Up @@ -192,7 +223,14 @@ abstract class DependencyExtractor :
private fun createComponentNode(componentId: String, source: DependencySource, direct: Boolean, component: ResolvedComponentResult, repositoryLookup: RepositoryUrlLookup): ResolvedDependency {
val componentDependencies = component.dependencies.filterIsInstance<ResolvedDependencyResult>().map { componentId(it.selected) }
val repositoryUrl = repositoryLookup.doLookup(component)
return ResolvedDependency(componentId, source, direct, coordinates(component), repositoryUrl, componentDependencies)
return ResolvedDependency(
componentId,
source,
direct,
coordinates(component),
repositoryUrl,
componentDependencies
)
}

private fun componentId(component: ResolvedComponentResult): String {
Expand All @@ -202,12 +240,16 @@ abstract class DependencyExtractor :
private fun coordinates(component: ResolvedComponentResult): DependencyCoordinates {
// TODO: Consider and handle null moduleVersion
val moduleVersionIdentifier = component.moduleVersion!!
return DependencyCoordinates(moduleVersionIdentifier.group, moduleVersionIdentifier.name, moduleVersionIdentifier.version)
return DependencyCoordinates(
moduleVersionIdentifier.group,
moduleVersionIdentifier.name,
moduleVersionIdentifier.version
)
}

private class RepositoryUrlLookup(
private val details: ResolveConfigurationDependenciesBOT.Details,
private val result: ResolveConfigurationDependenciesBOT.Result
private val details: ResolveConfigurationDependenciesBuildOperationType.Details,
private val result: ResolveConfigurationDependenciesBuildOperationType.Result
) {

private fun getRepositoryUrlForId(id: String): String? {
Expand Down Expand Up @@ -256,42 +298,23 @@ abstract class DependencyExtractor :
override fun close() {
if (thrownExceptions.isNotEmpty()) {
throw DefaultMultiCauseException(
"The ${GitHubDependencyGraphPlugin::class.simpleName} plugin encountered errors while extracting dependencies. " +
"Please report this issue at: https://github.com/gradle/github-dependency-graph-gradle-plugin/issues",
thrownExceptions
"The ${GitHubDependencyGraphPlugin::class.simpleName} plugin encountered errors while extracting dependencies. " +
"Please report this issue at: https://github.com/gradle/github-dependency-graph-gradle-plugin/issues",
thrownExceptions
)
}
try {
writeDependencyGraph()
} catch (e: RuntimeException) {
throw GradleException(
"The ${GitHubDependencyGraphPlugin::class.simpleName} plugin encountered errors while writing the dependency snapshot json file. " +
"Please report this issue at: https://github.com/gradle/github-dependency-graph-gradle-plugin/issues",
e
"The ${GitHubDependencyGraphPlugin::class.simpleName} plugin encountered errors while writing the dependency snapshot json file. " +
"Please report this issue at: https://github.com/gradle/github-dependency-graph-gradle-plugin/issues",
e
)
}
}

companion object {
private val LOGGER = Logging.getLogger(DependencyExtractor::class.java)
}
}

private inline fun <reified D, reified R> handleBuildOperationTypeRaw(
buildOperation: BuildOperationDescriptor,
finishEvent: OperationFinishEvent,
handler: (details: D, result: R) -> Unit
) {
val details: D? = buildOperation.details.let {
if (it is D) it else null
}
val result: R? = finishEvent.result.let {
if (it is R) it else null
}
if (details == null && result == null) {
return
} else if (details == null || result == null) {
throw IllegalStateException("buildOperation.details & finishedEvent.result were unexpected types")
}
handler(details, result)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gradle.github.dependencygraph.internal
package org.gradle.dependencygraph.extractor

import org.gradle.api.provider.Property
import org.gradle.api.services.BuildService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gradle.github.dependencygraph.internal
package org.gradle.dependencygraph.extractor

import org.gradle.initialization.EvaluateSettingsBuildOperationType

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gradle.github.dependencygraph.internal
package org.gradle.dependencygraph.extractor

class ResolvedConfigurationFilter(projectFilter: String?, configurationFilter: String?) {
val projectRegex = projectFilter?.toRegex()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gradle.github.dependencygraph.internal.model
package org.gradle.dependencygraph.model

import java.io.File
import java.nio.file.Path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package org.gradle.github.dependencygraph.internal.model
package org.gradle.dependencygraph.model

data class DependencyCoordinates(val group: String, val module: String, val version: String)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gradle.github.dependencygraph.internal.model
package org.gradle.dependencygraph.model

/**
* The source of a dependency declaration, representing where the direct dependency is declared,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gradle.github.dependencygraph.internal.model
package org.gradle.dependencygraph.model

data class ResolvedConfiguration(
val rootSource: DependencySource,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gradle.github.dependencygraph.internal.model
package org.gradle.dependencygraph.model

import com.github.packageurl.PackageURLBuilder

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gradle.github.dependencygraph.internal.util
package org.gradle.dependencygraph.util

import org.gradle.api.internal.GradleInternal
import org.gradle.api.invocation.Gradle
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.gradle.dependencygraph.util


class PluginParameters {
fun load(envName: String, default: String? = null): String {
return System.getProperty(envName)
?: System.getenv()[envName]
?: default
?: throwEnvironmentVariableMissingException(envName)
}

fun loadOptional(envName: String): String? {
return System.getProperty(envName)
?: System.getenv()[envName]
}

private fun throwEnvironmentVariableMissingException(variable: String): Nothing {
throw IllegalStateException("The configuration parameter '$variable' must be set: " +
"set an environment variable, or use '-D${variable}=value' on the command-line.")
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gradle.github.forceresolve
package org.gradle.forceresolve

import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -7,6 +7,7 @@ import org.gradle.api.invocation.Gradle
import org.gradle.api.tasks.TaskProvider
import org.gradle.util.GradleVersion

// TODO: Rename these
private const val RESOLVE_TASK = "GitHubDependencyGraphPlugin_resolveProjectDependencies"
private const val GENERATE_TASK = "GitHubDependencyGraphPlugin_generateDependencyGraph"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gradle.github.forceresolve
package org.gradle.forceresolve

import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gradle.github.forceresolve
package org.gradle.forceresolve

import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gradle.github.dependencygraph
package org.gradle.github

import org.gradle.github.dependencygraph.internal.github.GitHubDependencyGraphRenderer
import org.gradle.dependencygraph.AbstractDependencyExtractorPlugin
import org.gradle.github.dependencygraph.GitHubDependencyGraphRenderer

/**
* A plugin that collects all resolved dependencies in a Gradle build and exports it using the GitHub API format.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ package org.gradle.github

import org.gradle.api.Plugin
import org.gradle.api.invocation.Gradle
import org.gradle.api.logging.Logging
import org.gradle.github.forceresolve.ForceDependencyResolutionPlugin
import org.gradle.github.dependencygraph.GitHubDependencyExtractorPlugin
import org.gradle.github.dependencygraph.internal.util.GradleExtensions
import org.gradle.forceresolve.ForceDependencyResolutionPlugin

/**
* A plugin that collects all resolved dependencies in a Gradle build for submission to the
* GitHub Dependency Submission API.
*/
@Suppress("unused")
class GitHubDependencyGraphPlugin : Plugin<Gradle> {
private companion object : GradleExtensions() {
private val LOGGER = Logging.getLogger(GitHubDependencyGraphPlugin::class.java)
}

override fun apply(gradle: Gradle) {
// Only apply the dependency extractor to the root build
if (gradle.parent == null) {
Expand Down
Loading

0 comments on commit 314a0f7

Please sign in to comment.