Skip to content

Commit 7b2d392

Browse files
committed
Only add dependency scope if configured
To avoid assigned all dependencies to either 'runtime' or 'development' scope, we leave the current behaviour of assigning no scopes unless the relevant input parameters are configured.
1 parent b5fdd60 commit 7b2d392

File tree

7 files changed

+47
-12
lines changed

7 files changed

+47
-12
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ You can provide this value via the `DEPENDENCY_GRAPH_INCLUDE_PROJECTS` environme
5151
To restrict which Gradle configurations contribute to the report, you can filter configurations by name using a regular expression.
5252
You can provide this value via the `DEPENDENCY_GRAPH_INCLUDE_CONFIGURATIONS` environment variable or system property.
5353

54+
### Controlling the scope of dependencies in the dependency graph
55+
56+
The GitHub dependency graph allows a scope to be assigned to each reported dependency.
57+
The only permissible values for scope are 'runtime' and 'development'.
58+
59+
By default, no scope is assigned to dependencies in the graph. To enable scopes in the generated dependency graph,
60+
at least one of `DEPENDENCY_GRAPH_RUNTIME_PROJECTS` or `DEPENDENCY_GRAPH_RUNTIME_CONFIGURATIONS` must be configured.
61+
62+
To restrict which Gradle subprojects contribute 'runtime' dependencies to the report, specify which projects to include via a regular expression.
63+
You can provide this value via the `DEPENDENCY_GRAPH_RUNTIME_PROJECTS` environment variable or system property.
64+
For a project not matching this filter, all dependencies will be scoped 'development'.
65+
66+
To restrict which Gradle configurations contribute 'runtime' dependencies to the report, you can filter configurations by name using a regular expression.
67+
You can provide this value via the `DEPENDENCY_GRAPH_RUNTIME_CONFIGURATIONS` environment variable or system property.
68+
Dependencies resolved by a matching configuration will be scoped 'runtime': all other dependencies will be scoped 'development'.
69+
70+
For dependencies that are resolved in multiple projects and/or multiple configurations, only a single 'runtime' scoped resolution
71+
is required for that dependency to be scoped 'runtime'.
72+
5473
### Gradle compatibility
5574

5675
The plugin should be compatible with most versions of Gradle >= 5.2, and has been tested against

plugin-test/src/test/groovy/org/gradle/github/dependencygraph/BaseExtractorTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ abstract class BaseExtractorTest extends Specification {
208208
}
209209
assert actual.relationship == (expected.relationship ?: "direct")
210210
assert actual.dependencies == (expected.dependencies ?: [])
211-
assert actual.scope == (expected.scope ?: "runtime")
211+
assert actual.scope == expected.scope
212212
}
213213

214214
return true

plugin/src/main/kotlin/org/gradle/dependencygraph/extractor/DependencyExtractor.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import org.gradle.api.internal.artifacts.configurations.ResolveConfigurationDepe
99
import org.gradle.api.logging.Logging
1010
import org.gradle.dependencygraph.DependencyGraphRenderer
1111
import org.gradle.dependencygraph.model.*
12-
import org.gradle.dependencygraph.model.DependencyScope.RUNTIME
13-
import org.gradle.dependencygraph.model.DependencyScope.DEVELOPMENT
12+
import org.gradle.dependencygraph.model.DependencyScope.*
1413
import org.gradle.dependencygraph.util.*
1514
import org.gradle.initialization.EvaluateSettingsBuildOperationType
1615
import org.gradle.initialization.LoadProjectsBuildOperationType
@@ -176,7 +175,7 @@ abstract class DependencyExtractor :
176175
return
177176
}
178177

179-
val scope = if (runtimeFilter.include(rootPath, configurationName)) RUNTIME else DEVELOPMENT
178+
val scope = dependencyScope(rootPath, configurationName)
180179

181180
val rootId = if (projectIdentityPath == null) "build $rootPath" else componentId(rootComponent)
182181
val rootOrigin = DependencyOrigin(rootId, rootPath)
@@ -198,6 +197,16 @@ abstract class DependencyExtractor :
198197
resolvedConfigurations.add(resolvedConfiguration)
199198
}
200199

200+
private fun dependencyScope(
201+
rootPath: String,
202+
configurationName: String
203+
): DependencyScope {
204+
if (runtimeFilter.isConfigured()) {
205+
return if (runtimeFilter.include(rootPath, configurationName)) RUNTIME else DEVELOPMENT
206+
}
207+
return UNKNOWN
208+
}
209+
201210
private fun walkComponentDependencies(
202211
component: ResolvedComponentResult,
203212
parentOrigin: DependencyOrigin,

plugin/src/main/kotlin/org/gradle/dependencygraph/extractor/ResolvedConfigurationFilter.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ class ResolvedConfigurationFilter(projectFilter: String?, configurationFilter: S
1313
}
1414
return true
1515
}
16+
17+
fun isConfigured(): Boolean {
18+
return projectFilter != null || configurationFilter != null
19+
}
1620
}

plugin/src/main/kotlin/org/gradle/dependencygraph/model/DependencyScope.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ package org.gradle.dependencygraph.model
66
* Later development may extend this to a richer set of scopes.
77
*/
88
enum class DependencyScope {
9-
DEVELOPMENT, RUNTIME;
9+
UNKNOWN, DEVELOPMENT, RUNTIME;
1010

1111
companion object {
1212
fun getEffectiveScope(scopes: List<DependencyScope>): DependencyScope {
1313
if (scopes.contains(RUNTIME)) return RUNTIME
14-
return DEVELOPMENT
14+
if (scopes.contains(DEVELOPMENT)) return DEVELOPMENT
15+
return UNKNOWN
1516
}
1617
}
1718
}

plugin/src/main/kotlin/org/gradle/github/dependencygraph/GitHubRepositorySnapshotBuilder.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ class GitHubRepositorySnapshotBuilder(
3838
)
3939
}
4040

41-
private fun determineGitHubScope(configuration: ResolvedConfiguration): GitHubDependency.Scope {
41+
private fun determineGitHubScope(configuration: ResolvedConfiguration): GitHubDependency.Scope? {
4242
return when(configuration.scope) {
4343
DependencyScope.DEVELOPMENT -> GitHubDependency.Scope.development
4444
DependencyScope.RUNTIME -> GitHubDependency.Scope.runtime
45+
DependencyScope.UNKNOWN -> null
4546
}
4647
}
4748

@@ -75,7 +76,7 @@ class GitHubRepositorySnapshotBuilder(
7576
/**
7677
* Merge each resolved component with the same ID into a single GitHubDependency.
7778
*/
78-
fun addResolved(component: ResolvedDependency, scope: GitHubDependency.Scope) {
79+
fun addResolved(component: ResolvedDependency, scope: GitHubDependency.Scope?) {
7980
val dep = dependencyBuilders.getOrPut(component.id) {
8081
GitHubDependencyBuilder(component.packageUrl())
8182
}
@@ -98,7 +99,7 @@ class GitHubRepositorySnapshotBuilder(
9899

99100
private class GitHubDependencyBuilder(val package_url: String) {
100101
var relationship: GitHubDependency.Relationship = GitHubDependency.Relationship.indirect
101-
var scope: GitHubDependency.Scope = GitHubDependency.Scope.development
102+
var scope: GitHubDependency.Scope? = null
102103
val dependencies = mutableListOf<String>()
103104

104105
fun addRelationship(newRelationship: GitHubDependency.Relationship) {
@@ -108,8 +109,9 @@ class GitHubRepositorySnapshotBuilder(
108109
}
109110
}
110111

111-
fun addScope(newScope: GitHubDependency.Scope) {
112-
if (scope == GitHubDependency.Scope.development) {
112+
fun addScope(newScope: GitHubDependency.Scope?) {
113+
if (newScope == null) return
114+
if (scope == null || scope == GitHubDependency.Scope.development) {
113115
scope = newScope
114116
}
115117
}

plugin/src/main/kotlin/org/gradle/github/dependencygraph/model/GitHubDependency.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package org.gradle.github.dependencygraph.model
33
data class GitHubDependency(
44
val package_url: String,
55
val relationship: Relationship,
6-
val scope: Scope,
6+
val scope: Scope?,
77
val dependencies: List<String>
88
) {
99
enum class Relationship {

0 commit comments

Comments
 (0)